@@ -6,16 +6,19 @@ import { expect } from "@jest/globals";
6
6
const can = await getActor ( false , "evm_utils" ) ;
7
7
8
8
test ( "create_transaction" , async ( ) => {
9
+ let arr = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
10
+ let ten_thousand_as_bytes = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 39 , 16 ] ;
11
+
9
12
let item = {
10
13
Legacy : {
11
14
to : ethers . utils . arrayify ( ethers . utils . hexlify ( "0xe94f1fa4f27d9d288ffea234bb62e1fbc086ca0c" ) ) ,
12
- value : 0 ,
15
+ value : arr ,
13
16
data : [ ] ,
14
17
sign : [ ] ,
15
18
chain_id : 1 ,
16
- nonce : 1 ,
17
- gas_limit : 10_000 ,
18
- gas_price : 10_000
19
+ nonce : arr ,
20
+ gas_limit : ten_thousand_as_bytes ,
21
+ gas_price : ten_thousand_as_bytes
19
22
}
20
23
} ;
21
24
@@ -47,29 +50,82 @@ test("create_transaction", async () => {
47
50
expect ( msgHash ) . toBe ( hash ) ;
48
51
} ) ;
49
52
53
+ test ( "encode_signed_transaction" , async ( ) => {
54
+ let arr = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
55
+ let ten_thousand_as_bytes = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 39 , 16 ] ;
56
+
57
+ let item = {
58
+ Legacy : {
59
+ to : ethers . utils . arrayify ( ethers . utils . hexlify ( "0xe94f1fa4f27d9d288ffea234bb62e1fbc086ca0c" ) ) ,
60
+ value : arr ,
61
+ data : [ ] ,
62
+ sign : [
63
+ {
64
+ r : ethers . utils . arrayify ( ethers . utils . hexlify ( "0xde8ef039a601ad81d0882568e503fd7d865d8e6e03c35f635b301263ce7e00c2" ) ) ,
65
+ s : ethers . utils . arrayify ( ethers . utils . hexlify ( "0x770ceb3615e121d5fa712de1d9b5b27c64dc7ee0610d19307210a6de4b347ad9" ) ) ,
66
+ v : 38 ,
67
+ from : [ ] ,
68
+ hash : arr
69
+ }
70
+ ] ,
71
+ chain_id : 1 ,
72
+ nonce : arr ,
73
+ gas_limit : ten_thousand_as_bytes ,
74
+ gas_price : ten_thousand_as_bytes
75
+ }
76
+ } ;
77
+
78
+ let encoded = await can . encode_signed_transaction ( item ) ;
79
+ let tx = ethers . utils . parseTransaction ( encoded . Ok [ 0 ] ) ; //First value is tx
80
+
81
+ let hash = ethers . utils . hexlify ( encoded . Ok [ 1 ] ) ;
82
+
83
+ let signing_tx = {
84
+ chainId : tx . chainId ,
85
+ data : tx . data ,
86
+ to : tx . to ,
87
+ gasLimit : tx . gasLimit ,
88
+ gasPrice : tx . gasPrice ,
89
+ nonce : tx . nonce ,
90
+ type : tx . type ,
91
+ value : tx . value
92
+ } ;
93
+ let signature_like = {
94
+ r : ethers . utils . hexlify ( item . Legacy . sign [ 0 ] . r ) ,
95
+ s : ethers . utils . hexlify ( item . Legacy . sign [ 0 ] . s ) ,
96
+ v : item . Legacy . sign [ 0 ] . v
97
+ } ;
98
+
99
+ const raw = ethers . utils . serializeTransaction ( signing_tx , signature_like ) ; // returns RLP encoded tx
100
+ const msgHash = ethers . utils . keccak256 ( raw ) ;
101
+
102
+ expect ( tx . to . toLocaleLowerCase ( ) ) . toBe ( "0xe94f1fa4f27d9d288ffea234bb62e1fbc086ca0c" ) ;
103
+ expect ( tx . nonce ) . toStrictEqual ( 1 ) ;
104
+ expect ( tx . gasLimit ) . toStrictEqual ( BigNumber . from ( 10_000 ) ) ;
105
+ expect ( tx . gasPrice ) . toStrictEqual ( BigNumber . from ( 10_000 ) ) ;
106
+ expect ( tx . chainId ) . toStrictEqual ( item . Legacy . chain_id ) ;
107
+
108
+ expect ( msgHash ) . toBe ( hash ) ;
109
+ } ) ;
50
110
51
111
test ( "parse_transaction" , async ( ) => {
52
112
let ser = {
53
- to : "0xe94f1fa4f27d9d288ffea234bb62e1fbc086ca0c" ,
54
- value : 0 ,
55
- data : [ ] ,
56
- chainId : 1 ,
57
- nonce : 1 ,
58
- gasLimit : 10_000 ,
59
- gasPrice : 10_000
113
+ to : "0xe94f1fa4f27d9d288ffea234bb62e1fbc086ca0c" ,
114
+ value : 0 ,
115
+ data : [ ] ,
116
+ chainId : 1 ,
117
+ nonce : 1 ,
118
+ gasPrice : BigNumber . from ( 10_000 ) ,
119
+ gasLimit : 1
60
120
} ;
61
121
62
122
let raw_tx = ethers . utils . serializeTransaction ( ser ) ;
63
123
64
- console . log ( raw_tx ) ;
65
-
66
124
let decoded = await can . parse_transaction ( ethers . utils . arrayify ( raw_tx ) ) ;
67
125
68
126
expect ( decoded . Ok . Legacy ) . not . toBeUndefined ( ) ;
69
- expect ( decoded . Ok . Legacy . gas_limit ) . toBe ( 10_000n ) ;
70
- expect ( decoded . Ok . Legacy . gas_price ) . toBe ( 10_000n ) ;
71
- expect ( decoded . Ok . Legacy . nonce ) . toBe ( 1n ) ;
72
- expect ( decoded . Ok . Legacy . chain_id ) . toBe ( 1n ) ;
127
+ expect ( BigNumber . from ( decoded . Ok . Legacy . gas_limit ) ) . toStrictEqual ( BigNumber . from ( 1 ) ) ;
128
+ expect ( BigNumber . from ( decoded . Ok . Legacy . gas_price ) ) . toStrictEqual ( BigNumber . from ( 10_000 ) ) ;
129
+ expect ( BigNumber . from ( decoded . Ok . Legacy . nonce ) ) . toStrictEqual ( BigNumber . from ( 1 ) ) ;
130
+ expect ( BigNumber . from ( decoded . Ok . Legacy . chain_id ) ) . toStrictEqual ( BigNumber . from ( 1 ) ) ;
73
131
} ) ;
74
-
75
-
0 commit comments