Skip to content

Commit

Permalink
Respect chainId from TransactionArgs when signing (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov authored Jun 10, 2024
1 parent ac93b9a commit 954c23c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 6 additions & 6 deletions web3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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)
result[1].add(cp)
8 changes: 6 additions & 2 deletions web3/transaction_signing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 954c23c

Please sign in to comment.