-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transaction signing is something that happens in a lot of places - this PR introduces primitives for transaction signing in `transaction_utils` such that we can use the same logic across web3/eth1/etc for this simple operation. `transaction_utils` also contains a few more "spec-derived" helpers for working with transactions, such as the computation of a contract address etc that cannot easily be introduced in `transactions` itself without bringing in dependencies like secp and rlp, so they end up in a separate module. Finally, since these modules collect "versions" of these transaction types across different eips, some tests are moved to follow the same structure.
- Loading branch information
1 parent
84664b0
commit 4ea11b9
Showing
10 changed files
with
221 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# eth | ||
# Copyright (c) 2024 Status Research & Development GmbH | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
import ./[keys, transactions, transactions_rlp] | ||
|
||
export keys, transactions | ||
|
||
proc signature*(tx: Transaction): Opt[Signature] = | ||
var bytes {.noinit.}: array[65, byte] | ||
bytes[0 .. 31] = tx.R.toBytesBE() | ||
bytes[32 .. 63] = tx.S.toBytesBE() | ||
|
||
bytes[64] = | ||
if tx.txType != TxLegacy: | ||
tx.V.byte | ||
elif tx.V >= EIP155_CHAIN_ID_OFFSET: | ||
byte(1 - (tx.V and 1)) | ||
elif tx.V == 27 or tx.V == 28: | ||
byte(tx.V - 27) | ||
else: | ||
return Opt.none(Signature) | ||
|
||
Signature.fromRaw(bytes).mapConvertErr(void) | ||
|
||
proc `signature=`*(tx: var Transaction, param: tuple[sig: Signature, eip155: bool]) = | ||
let raw = param.sig.toRaw() | ||
|
||
tx.R = UInt256.fromBytesBE(raw.toOpenArray(0, 31)) | ||
tx.S = UInt256.fromBytesBE(raw.toOpenArray(32, 63)) | ||
|
||
let v = raw[64].uint64 | ||
tx.V = | ||
case tx.txType | ||
of TxLegacy: | ||
if param.eip155: | ||
v + uint64(tx.chainId) * 2 + 35 | ||
else: | ||
v + 27'u64 | ||
else: | ||
v | ||
|
||
proc sign*(tx: Transaction, pk: PrivateKey, eip155: bool): (Signature, bool) = | ||
let hash = tx.rlpHashForSigning(eip155) | ||
|
||
(sign(pk, SkMessage(hash.data)), eip155) | ||
|
||
proc recoverKey*(tx: Transaction): Opt[PublicKey] = | ||
## Recovering key / sender is a costly operation - make sure to reuse the | ||
## outcome! | ||
## | ||
## Returns `none` if the signature is invalid with respect to the rest of | ||
## the transaction data. | ||
let | ||
sig = ?tx.signature() | ||
txHash = tx.rlpHashForSigning(tx.isEip155()) | ||
|
||
recover(sig, SkMessage(txHash.data)).mapConvertErr(void) | ||
|
||
proc recoverSender*(tx: Transaction): Opt[Address] = | ||
## Recovering key / sender is a costly operation - make sure to reuse the | ||
## outcome! | ||
## | ||
## Returns `none` if the signature is invalid with respect to the rest of | ||
## the transaction data. | ||
let key = ?tx.recoverKey() | ||
ok key.to(Address) | ||
|
||
proc creationAddress*(tx: Transaction, sender: Address): Address = | ||
let hash = keccak256(rlp.encodeList(sender, tx.nonce)) | ||
hash.to(Address) | ||
|
||
proc getRecipient*(tx: Transaction, sender: Address): Address = | ||
tx.to.valueOr(tx.creationAddress(sender)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,4 +296,3 @@ suite "EIP-7865 tests": | |
|
||
check decodedBody == body | ||
check decodedBlk == blk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Nimbus | ||
# Copyright (c) 2023-2024 Status Research & Development GmbH | ||
# Licensed under either of | ||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or | ||
# http://www.apache.org/licenses/LICENSE-2.0) | ||
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or | ||
# http://opensource.org/licenses/MIT) | ||
# at your option. This file may not be copied, modified, or distributed except | ||
# according to those terms. | ||
{.used.} | ||
|
||
import | ||
unittest2, | ||
../../eth/common/[receipts_rlp] | ||
|
||
template roundTrip(v: untyped) = | ||
let bytes = rlp.encode(v) | ||
let v2 = rlp.decode(bytes, Receipt) | ||
let bytes2 = rlp.encode(v2) | ||
check bytes == bytes2 | ||
|
||
suite "Receipts": | ||
test "EIP-4844": | ||
let rec = Receipt( | ||
receiptType: Eip4844Receipt, | ||
isHash: false, | ||
status: false, | ||
cumulativeGasUsed: 100.GasInt) | ||
|
||
roundTrip(rec) | ||
|
||
test "EIP-7702": | ||
let rec = Receipt( | ||
receiptType: Eip7702Receipt, | ||
isHash: false, | ||
status: false, | ||
cumulativeGasUsed: 100.GasInt) | ||
|
||
roundTrip(rec) |
Oops, something went wrong.