Skip to content

Commit 1569b26

Browse files
committed
Expose WriteOperation#resovlePolicies #signTransaction methods
Methods can be used for more direct control on how transaction is signed and sent to blockchain.
1 parent 133b891 commit 1569b26

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,59 @@ public class WriteOperation: ReadOperation {
2020
/// - sendRaw: If set to `true` transaction will be signed and sent using `eth_sendRawTransaction`.
2121
/// Otherwise, no signing attempts will take place and the `eth_sendTransaction` RPC will be used instead.
2222
/// Default value is `true`.
23-
public func writeToChain(password: String, policies: Policies = .auto, sendRaw: Bool = true) async throws -> TransactionSendingResult {
24-
try await policyResolver.resolveAll(for: &transaction, with: policies)
23+
public func writeToChain(password: String,
24+
policies: Policies = .auto,
25+
sendRaw: Bool = true) async throws -> TransactionSendingResult {
26+
try await resolvePolicies(policies)
2527

2628
guard sendRaw else {
2729
return try await web3.eth.send(transaction)
2830
}
2931

32+
try signTransaction(password: password)
33+
34+
guard let transactionData = transaction.encode(for: .transaction) else {
35+
throw Web3Error.dataError
36+
}
37+
return try await web3.eth.send(raw: transactionData)
38+
}
39+
40+
/// Resolves all policy-driven transaction attributes: gas limit, gas price, nonce.
41+
/// - Parameters:
42+
/// - policies: Determining the behaviour of how transaction attributes like gas limit and
43+
/// nonce are resolved. Default value is ``Policies/auto``.
44+
/// - Throws: Rethrows any error that occurs during policy resolution.
45+
public func resolvePolicies(_ policies: Policies) async throws {
46+
try await policyResolver.resolveAll(for: &transaction, with: policies)
47+
}
48+
49+
/// Signs the transaction locally using the attached keystore manager.
50+
/// - Parameters:
51+
/// - password: Password for the private key in the keystore manager attached to the provider
52+
/// you set to `web3` passed in the initializer.
53+
/// - Throws:
54+
/// - ``Web3Error/inputError`` if no keystore is attached to the provider,
55+
/// or if signing fails with the provided password.
56+
@discardableResult
57+
public func signTransaction(password: String) throws -> CodableTransaction {
3058
guard let attachedKeystoreManager = web3.provider.attachedKeystoreManager else {
31-
throw Web3Error.inputError(desc: "Failed to locally sign a transaction. Web3 provider doesn't have keystore attached.")
59+
throw Web3Error.inputError(
60+
desc: "Failed to locally sign a transaction. Web3 provider doesn't have keystore attached."
61+
)
3262
}
3363

3464
do {
35-
try Web3Signer.signTX(transaction: &transaction,
36-
keystore: attachedKeystoreManager,
37-
account: transaction.from ?? transaction.sender ?? EthereumAddress.contractDeploymentAddress(),
38-
password: password)
65+
try Web3Signer.signTX(
66+
transaction: &transaction,
67+
keystore: attachedKeystoreManager,
68+
account: transaction.from ?? transaction.sender ?? EthereumAddress.contractDeploymentAddress(),
69+
password: password
70+
)
71+
return transaction
3972
} catch {
40-
throw Web3Error.inputError(desc: "Failed to locally sign a transaction. \(error.localizedDescription)")
73+
throw Web3Error.inputError(
74+
desc: "Failed to locally sign a transaction. \(error.localizedDescription)"
75+
)
4176
}
42-
guard let transactionData = transaction.encode(for: .transaction) else { throw Web3Error.dataError }
43-
return try await web3.eth.send(raw: transactionData)
4477
}
4578
}

0 commit comments

Comments
 (0)