From 954c23cca2489610b5c88aee654ab904447af801 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Mon, 10 Jun 2024 10:14:14 +0200 Subject: [PATCH] Respect chainId from TransactionArgs when signing (#152) --- web3.nim | 12 ++++++------ web3/transaction_signing.nim | 8 ++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/web3.nim b/web3.nim index 0768abb..a450a05 100644 --- a/web3.nim +++ b/web3.nim @@ -304,7 +304,7 @@ proc send*(web3: Web3, c: TransactionArgs): Future[TxHash] {.async.} = else: return await web3.provider.eth_sendTransaction(c) -proc send*(web3: Web3, c: TransactionArgs, chainId: ChainId): Future[TxHash] {.async.} = +proc send*(web3: Web3, c: TransactionArgs, chainId: ChainId): Future[TxHash] {.deprecated: "Provide chainId in TransactionArgs", async.} = doAssert(web3.privateKey.isSome()) var cc = c if cc.nonce.isNone: @@ -325,6 +325,8 @@ proc sendData(web3: Web3, else: none(Quantity) nonce = if web3.privateKey.isSome(): some(await web3.nextNonce()) else: none(Quantity) + chainId = if chainId.isSome(): some(Quantity(chainId.get)) + else: none(Quantity) cc = TransactionArgs( data: some(data), @@ -334,12 +336,10 @@ proc sendData(web3: Web3, value: some(value), nonce: nonce, gasPrice: gasPrice, + chainId: chainId ) - if chainId.isNone: - return await web3.send(cc) - else: - return await web3.send(cc, chainId.get) + return await web3.send(cc) proc send*[T](c: ContractInvocation[T, Web3SenderImpl], value = 0.u256, @@ -518,4 +518,4 @@ macro adjust*(s: AsyncSender, modifications: varargs[untyped]): untyped = let fieldVal = s[1] result[1].add quote do: `cp`.sender.`fieldName` = `fieldVal` - result[1].add(cp) \ No newline at end of file + result[1].add(cp) diff --git a/web3/transaction_signing.nim b/web3/transaction_signing.nim index 2f09635..143f55c 100644 --- a/web3/transaction_signing.nim +++ b/web3/transaction_signing.nim @@ -50,10 +50,14 @@ func encodeTransaction*(s: TransactionArgs, pk: PrivateKey): seq[byte] = tr.value = s.value.get tr.nonce = uint64(s.nonce.get) tr.payload = s.payload - signTransaction(tr, pk) + if s.chainId.isSome(): + tr.chainId = ChainId(s.chainId.get) + signTransactionEip155(tr, pk) + else: + signTransaction(tr, pk) return rlp.encode(tr) -func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, chainId: ChainId): seq[byte] = +func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, chainId: ChainId): seq[byte] {.deprecated: "Provide chainId in TransactionArgs".} = var tr = Transaction(txType: TxLegacy, chainId: chainId) tr.gasLimit = s.gas.get.GasInt tr.gasPrice = s.gasPrice.get.GasInt