implement txHashNoSignature for EIP2817's AccessListTx

This commit is contained in:
jangko 2021-05-14 22:34:32 +07:00
parent 61d5327f55
commit b6b6f3dec7
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 74 additions and 31 deletions

View File

@ -330,7 +330,7 @@ proc append*(rlpWriter: var RlpWriter, value: Stint) =
discard discard
type type
TxTypes = LegacyTx | AccessListTx TxTypes* = LegacyTx | AccessListTx
proc read*(rlp: var Rlp, t: var TxTypes, _: type EthAddress): EthAddress {.inline.} = proc read*(rlp: var Rlp, t: var TxTypes, _: type EthAddress): EthAddress {.inline.} =
if rlp.blobLen != 0: if rlp.blobLen != 0:

View File

@ -3,8 +3,8 @@ import
".."/[common, rlp, keys] ".."/[common, rlp, keys]
proc initLegacyTx*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddress, proc initLegacyTx*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddress,
value: UInt256, payload: Blob, V: uint64, R, S: UInt256, isContractCreation = false): LegacyTx = value: UInt256, payload: Blob, V: int64, R, S: UInt256, isContractCreation = false): LegacyTx =
result.accountNonce = nonce result.nonce = nonce
result.gasPrice = gasPrice result.gasPrice = gasPrice
result.gasLimit = gasLimit result.gasLimit = gasLimit
result.to = to result.to = to
@ -16,58 +16,101 @@ proc initLegacyTx*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddre
result.isContractCreation = isContractCreation result.isContractCreation = isContractCreation
type type
TransHashObj = object LegacyUnsignedTx* = object
accountNonce: AccountNonce nonce*: AccountNonce
gasPrice: GasInt gasPrice*: GasInt
gasLimit: GasInt gasLimit*: GasInt
to {.rlpCustomSerialization.}: EthAddress to* {.rlpCustomSerialization.}: EthAddress
value: UInt256 value*: UInt256
payload: Blob payload*: Blob
mIsContractCreation {.rlpIgnore.}: bool isContractCreation* {.rlpIgnore.}: bool
proc read(rlp: var Rlp, t: var TransHashObj, _: type EthAddress): EthAddress {.inline.} = AccessListUnsignedTx* = object
chainId* {.rlpCustomSerialization.}: ChainId
nonce* : AccountNonce
gasPrice* : GasInt
gasLimit* : GasInt
to* {.rlpCustomSerialization.}: EthAddress
value* : UInt256
payload* : Blob
accessList*: AccessList
isContractCreation* {.rlpIgnore.}: bool
UnsignedTxTypes* = LegacyUnsignedTx | AccessListUnsignedTx
proc read*(rlp: var Rlp, t: var UnsignedTxTypes, _: type EthAddress): EthAddress {.inline.} =
if rlp.blobLen != 0: if rlp.blobLen != 0:
result = rlp.read(EthAddress) result = rlp.read(EthAddress)
else: else:
t.mIsContractCreation = true t.isContractCreation = true
proc append(rlpWriter: var RlpWriter, t: TransHashObj, a: EthAddress) {.inline.} = proc append*(rlpWriter: var RlpWriter, t: UnsignedTxTypes, a: EthAddress) {.inline.} =
if t.mIsContractCreation: if t.isContractCreation:
rlpWriter.append("") rlpWriter.append("")
else: else:
rlpWriter.append(a) rlpWriter.append(a)
const proc read*(rlp: var Rlp, t: var AccessListUnsignedTx, _: type ChainId): ChainId {.inline.} =
EIP155_CHAIN_ID_OFFSET* = 35 rlp.read(uint64).ChainId
func rlpEncode*(transaction: LegacyTx): auto = proc append*(rlpWriter: var RlpWriter, t: AccessListUnsignedTx, a: ChainId) {.inline.} =
rlpWriter.append(a.uint64)
const
EIP155_CHAIN_ID_OFFSET* = 35'i64
func rlpEncode*(tx: LegacyTx): auto =
# Encode transaction without signature # Encode transaction without signature
return rlp.encode(TransHashObj( return rlp.encode(LegacyUnsignedTx(
accountNonce: transaction.accountNonce, nonce: tx.nonce,
gasPrice: transaction.gasPrice, gasPrice: tx.gasPrice,
gasLimit: transaction.gasLimit, gasLimit: tx.gasLimit,
to: transaction.to, to: tx.to,
value: transaction.value, value: tx.value,
payload: transaction.payload, payload: tx.payload,
mIsContractCreation: transaction.isContractCreation isContractCreation: tx.isContractCreation
)) ))
func rlpEncodeEIP155*(tx: LegacyTx): auto = func rlpEncodeEIP155*(tx: LegacyTx): auto =
let V = (tx.V.int - EIP155_CHAIN_ID_OFFSET) div 2 let V = (tx.V - EIP155_CHAIN_ID_OFFSET) div 2
# Encode transaction without signature # Encode transaction without signature
return rlp.encode(Transaction( return rlp.encode(LegacyTx(
accountNonce: tx.accountNonce, nonce: tx.nonce,
gasPrice: tx.gasPrice, gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit, gasLimit: tx.gasLimit,
to: tx.to, to: tx.to,
value: tx.value, value: tx.value,
payload: tx.payload, payload: tx.payload,
isContractCreation: tx.isContractCreation, isContractCreation: tx.isContractCreation,
V: V.uint64, V: V,
R: 0.u256, R: 0.u256,
S: 0.u256 S: 0.u256
)) ))
func txHashNoSignature*(tx: LegacyTx): Hash256 = func txHashNoSignature*(tx: LegacyTx): Hash256 =
# Hash transaction without signature # Hash transaction without signature
return keccak256.digest(if tx.V.int >= EIP155_CHAIN_ID_OFFSET: tx.rlpEncodeEIP155 else: tx.rlpEncode) return keccak256.digest(if tx.V >= EIP155_CHAIN_ID_OFFSET: tx.rlpEncodeEIP155 else: tx.rlpEncode)
func txHashNoSignature*(tx: AccessListTx): Hash256 =
let unsignedTx = AccessListUnsignedTx(
chainId: tx.chainId,
nonce: tx.nonce,
gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit,
to: tx.to,
value: tx.value,
payload: tx.payload,
accessList: tx.accessList,
isContractCreation: tx.isContractCreation
)
var rw = initRlpWriter()
rw.append(1)
rw.append(unsignedTx)
let rlpBytes = rlp.encode(rw.finish())
return keccak256.digest(rlpBytes)
func txHashNoSignature*(tx: Transaction): Hash256 =
if tx.txType == LegacyTxType:
txHashNoSignature(tx.legacyTx)
else:
txHashNoSignature(tx.accessListTx)