ethereum: add support for eip-1559 transactions

Adds a new transaction type and signing method for eip-1559-style
transactions.

Includes a $typed-transactions type which can support any number of
eip-2718-style typed transactions.
This commit is contained in:
Fang 2021-09-18 20:37:48 +02:00
parent 3c5fd9fda6
commit 4692c10fe2
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972
2 changed files with 140 additions and 0 deletions

View File

@ -22,6 +22,15 @@
%- serialize-point
(priv-to-pub prv)
::
++ sign-typed-transaction
|= [tx=typed-transaction:rpc pk=@]
^- @ux
=- (cat 3 - -.tx)
?- -.tx
%0x0 (sign-transaction +.tx pk)
%0x2 (sign-transaction-1559 +.tx pk)
==
::
++ sign-transaction
=, crypto
|= [tx=transaction:rpc pk=@]
@ -43,6 +52,43 @@
%+ encode:rlp %l
tx(to b+20^to.tx, chain-id [v r s ~])
--
::
++ sign-transaction-1559
=, crypto
|= [tx=transaction-1559:rpc pk=@]
|^ ^- @ux
=; hash=@
=+ (ecdsa-raw-sign:secp256k1:secp hash pk)
::NOTE we retrieve y's parity from the v value
(encode-1559 ~ (end 0 v) r s)
:: hash the raw transaction data including leading 0x2
%- keccak-256:keccak
=+ dat=(cat 3 (encode-1559 ~) 0x2)
=+ wid=(met 3 dat)
[wid (rev 3 wid dat)]
::
++ encode-1559
|= sig=(unit [y=@ r=@ s=@])
%+ encode:rlp %l
=, tx
:* chain-id
nonce
max-priority-gas-fee
max-gas-fee
gas
b+20^to
value
data
::
:- %l
%+ turn ~(tap by access-list)
|= [a=address b=(list @ux)]
l+~[b+20^a l+(turn b |=(c=@ux b+32^c))]
::
?~ sig ~
~[y r s]:u.sig
==
--
--
::
:: rlp en/decoding
@ -454,6 +500,11 @@
==
::
:: raw transaction data
+$ typed-transaction
$% [%0x0 transaction]
[%0x2 transaction-1559]
==
::
+$ transaction
$: nonce=@ud
gas-price=@ud
@ -464,6 +515,18 @@
chain-id=@ux
==
::
+$ transaction-1559
$: chain-id=@ux
nonce=@ud
max-priority-gas-fee=@ud
max-gas-fee=@ud
gas=@ud
to=address
value=@ud
data=@ux
access-list=(jar address @ux)
==
::
:: ethereum json rpc api
::
:: supported requests.

View File

@ -73,4 +73,81 @@
231e.c67a.98a0.0d6d.eb23.173b.26eb.62c7.3ad2.
3bc2.afb9.efac.6879.f6b2.8063.f686.586e.31da.
2343
::
++ test-signing-transaction-1559
%+ expect-eq
!> 0x2f8.7303.0b84.3b9a.ca00.8501.2a05.f200.8252.
0894.24f9.63d6.9f36.d51c.c40d.5208.2d49.78d2.
c4c2.7a17.8801.6345.785d.8a00.0080.c080.a05d.
dc02.7d72.48d2.d542.75c7.3f1b.5bac.edd6.5af9.
d136.5829.e637.7473.1ebf.5ca6.86a0.404b.71cc.
a3d0.409f.dd84.9857.fb15.0f7d.f943.9fca.c425.
c141.236a.a963.6528.2bb5
!> %+ sign-typed-transaction:key:ethereum
:- %0x2
:* 0x3
11
1.000.000.000
5.000.000.000
21.000
0x24f9.63d6.9f36.d51c.c40d.5208.2d49.78d2.c4c2.7a17
100.000.000.000.000.000
0x0
~
==
0x7f50.12dc.a92e.4aef.d3e1.a9f7.e761.f459.
c350.74e0.2f0c.93eb.5bb2.dafb.f5ea.8058
::
++ test-signing-transaction-1559-access-list
%+ expect-eq
!> 0x2.f8ac.030c.8477.3594.0085.0254.0be4.0082.
7530.9424.f963.d69f.36d5.1cc4.0d52.082d.4978.
d2c4.c27a.1788.0163.4578.5d8a.0000.80f8.38f7.
9424.f963.d69f.36d5.1cc4.0d52.082d.4978.d2c4.
c27a.17e1.a000.0000.0000.0000.0000.0000.0000.
0000.0000.0000.0000.0000.0000.0000.0000.0000.
0101.a015.5039.4a51.0a1a.601a.54fb.147d.e17f.
a6b9.bd41.9bdb.1654.de06.b7bc.3481.f67e.4ba0.
4e88.0574.d378.6ede.1de2.586a.fe98.e30f.d63c.
ef3b.e31c.76a9.a0bd.66a8.aef7.8191
!> %+ sign-typed-transaction:key:ethereum
:- %0x2
:* 0x3
12
2.000.000.000
10.000.000.000
30.000
0x24f9.63d6.9f36.d51c.c40d.5208.2d49.78d2.c4c2.7a17
100.000.000.000.000.000
0x0
%+ ~(add ja *(jar address:ethereum @ux))
0x24f9.63d6.9f36.d51c.c40d.5208.2d49.78d2.c4c2.7a17
0x1
==
0x7f50.12dc.a92e.4aef.d3e1.a9f7.e761.f459.
c350.74e0.2f0c.93eb.5bb2.dafb.f5ea.8058
::
++ test-signing-transaction-1559-to-zero
%+ expect-eq
!> 0x2f8.7303.0f84.3b9a.ca00.8501.2a05.f200.8252.
0894.0000.0000.0000.0000.0000.0000.0000.0000.
0000.0000.8801.6345.785d.8a00.0080.c080.a0c5.
43ea.1ee9.04d1.5f9f.d23d.e881.672f.06f3.41e2.
69e0.fedc.1ed0.4f0b.20f0.1679.06a0.442e.fe89.
1bb6.48cf.d39b.20aa.5b1f.8a16.7be2.0ee2.d45b.
3d3d.7b0a.bce5.1dc7.1232
!> %+ sign-typed-transaction:key:ethereum
:- %0x2
:* 0x3
15
1.000.000.000
5.000.000.000
21.000
0x0
100.000.000.000.000.000
0x0
~
==
0x7f50.12dc.a92e.4aef.d3e1.a9f7.e761.f459.
c350.74e0.2f0c.93eb.5bb2.dafb.f5ea.8058
--