@@ -20,26 +20,59 @@ public class WriteOperation: ReadOperation {
20
20
/// - sendRaw: If set to `true` transaction will be signed and sent using `eth_sendRawTransaction`.
21
21
/// Otherwise, no signing attempts will take place and the `eth_sendTransaction` RPC will be used instead.
22
22
/// 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)
25
27
26
28
guard sendRaw else {
27
29
return try await web3. eth. send ( transaction)
28
30
}
29
31
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 {
30
58
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
+ )
32
62
}
33
63
34
64
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
39
72
} 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
+ )
41
76
}
42
- guard let transactionData = transaction. encode ( for: . transaction) else { throw Web3Error . dataError }
43
- return try await web3. eth. send ( raw: transactionData)
44
77
}
45
78
}
0 commit comments