github.com/klaytn/klaytn@v1.12.1/blockchain/types/transaction_signing_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/types/transaction_signing_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package types 22 23 import ( 24 "crypto/ecdsa" 25 "math/big" 26 "reflect" 27 "runtime" 28 "strings" 29 "testing" 30 31 "github.com/klaytn/klaytn/blockchain/types/accountkey" 32 "github.com/stretchr/testify/assert" 33 34 "github.com/klaytn/klaytn/common" 35 "github.com/klaytn/klaytn/crypto" 36 "github.com/klaytn/klaytn/rlp" 37 ) 38 39 func TestLondonSigningWithoutChainID(t *testing.T) { 40 key, _ := crypto.GenerateKey() 41 addr := crypto.PubkeyToAddress(key.PublicKey) 42 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 43 44 signer := NewLondonSigner(big.NewInt(10)) 45 tx, err := SignTx(NewTx(&TxInternalDataEthereumDynamicFee{ 46 AccountNonce: 1, 47 Amount: big.NewInt(10), 48 GasFeeCap: big.NewInt(10), 49 GasTipCap: big.NewInt(10), 50 GasLimit: 100, 51 AccessList: accessList, 52 Recipient: &addr, 53 }), signer, key) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 from, err := Sender(signer, tx) 59 if from != addr { 60 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 61 } 62 } 63 64 func TestLondonSigningWithChainID(t *testing.T) { 65 key, _ := crypto.GenerateKey() 66 addr := crypto.PubkeyToAddress(key.PublicKey) 67 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 68 69 signer := NewLondonSigner(big.NewInt(10)) 70 tx, err := SignTx(NewTx(&TxInternalDataEthereumDynamicFee{ 71 AccountNonce: 1, 72 Amount: big.NewInt(10), 73 GasFeeCap: big.NewInt(10), 74 GasTipCap: big.NewInt(10), 75 GasLimit: 100, 76 AccessList: accessList, 77 Recipient: &addr, 78 ChainID: big.NewInt(10), 79 }), signer, key) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 from, err := Sender(signer, tx) 85 if from != addr { 86 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 87 } 88 } 89 90 func TestLondonSigningWithNoBitChainID(t *testing.T) { 91 key, _ := crypto.GenerateKey() 92 addr := crypto.PubkeyToAddress(key.PublicKey) 93 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 94 95 signer := NewLondonSigner(big.NewInt(10)) 96 tx, err := SignTx(NewTx(&TxInternalDataEthereumDynamicFee{ 97 AccountNonce: 1, 98 Amount: big.NewInt(10), 99 GasFeeCap: big.NewInt(10), 100 GasTipCap: big.NewInt(10), 101 GasLimit: 100, 102 AccessList: accessList, 103 Recipient: &addr, 104 ChainID: new(big.Int), 105 }), signer, key) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 from, err := Sender(signer, tx) 111 if from != addr { 112 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 113 } 114 } 115 116 func TestEIP2930SigningWithoutChainID(t *testing.T) { 117 key, _ := crypto.GenerateKey() 118 addr := crypto.PubkeyToAddress(key.PublicKey) 119 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 120 121 signer := NewEIP2930Signer(big.NewInt(10)) 122 tx, err := SignTx(NewTx(&TxInternalDataEthereumAccessList{ 123 AccountNonce: 1, 124 Amount: big.NewInt(10), 125 Price: big.NewInt(1), 126 GasLimit: 100, 127 AccessList: accessList, 128 Recipient: &addr, 129 }), signer, key) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 from, err := Sender(signer, tx) 135 if from != addr { 136 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 137 } 138 } 139 140 func TestEIP2930SigningWithChainID(t *testing.T) { 141 key, _ := crypto.GenerateKey() 142 addr := crypto.PubkeyToAddress(key.PublicKey) 143 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 144 145 signer := NewEIP2930Signer(big.NewInt(10)) 146 tx, err := SignTx(NewTx(&TxInternalDataEthereumAccessList{ 147 AccountNonce: 1, 148 Amount: big.NewInt(10), 149 Price: big.NewInt(1), 150 GasLimit: 100, 151 AccessList: accessList, 152 Recipient: &addr, 153 ChainID: big.NewInt(10), 154 }), signer, key) 155 if err != nil { 156 t.Fatal(err) 157 } 158 159 from, err := Sender(signer, tx) 160 if from != addr { 161 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 162 } 163 } 164 165 func TestEIP2930SigningWithNoBitChainID(t *testing.T) { 166 key, _ := crypto.GenerateKey() 167 addr := crypto.PubkeyToAddress(key.PublicKey) 168 accessList := AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 169 170 signer := NewEIP2930Signer(big.NewInt(10)) 171 tx, err := SignTx(NewTx(&TxInternalDataEthereumAccessList{ 172 AccountNonce: 1, 173 Amount: big.NewInt(10), 174 Price: big.NewInt(1), 175 GasLimit: 100, 176 AccessList: accessList, 177 Recipient: &addr, 178 ChainID: new(big.Int), 179 }), signer, key) 180 if err != nil { 181 t.Fatal(err) 182 } 183 184 from, err := Sender(signer, tx) 185 if from != addr { 186 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 187 } 188 } 189 190 func TestEIP155Signing(t *testing.T) { 191 key, _ := crypto.GenerateKey() 192 addr := crypto.PubkeyToAddress(key.PublicKey) 193 194 signer := NewEIP155Signer(big.NewInt(18)) 195 tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key) 196 if err != nil { 197 t.Fatal(err) 198 } 199 200 from, err := Sender(signer, tx) 201 if err != nil { 202 t.Fatal(err) 203 } 204 if from != addr { 205 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 206 } 207 } 208 209 func TestEIP155RawSignatureValues(t *testing.T) { 210 key, _ := crypto.GenerateKey() 211 addr := crypto.PubkeyToAddress(key.PublicKey) 212 213 signer := NewEIP155Signer(big.NewInt(18)) 214 tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key) 215 if err != nil { 216 t.Fatal(err) 217 } 218 219 h := signer.Hash(tx) 220 sig, err := crypto.Sign(h[:], key) 221 if err != nil { 222 t.Fatal(err) 223 } 224 r, s, v, err := signer.SignatureValues(tx, sig) 225 226 sigs := tx.RawSignatureValues() 227 txV, txR, txS := sigs[0].V, sigs[0].R, sigs[0].S 228 229 assert.Equal(t, r, txR) 230 assert.Equal(t, s, txS) 231 assert.Equal(t, v, txV) 232 } 233 234 func TestEIP155ChainId(t *testing.T) { 235 key, _ := crypto.GenerateKey() 236 addr := crypto.PubkeyToAddress(key.PublicKey) 237 238 signer := NewEIP155Signer(big.NewInt(18)) 239 tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key) 240 if err != nil { 241 t.Fatal(err) 242 } 243 244 if tx.ChainId().Cmp(signer.chainId) != 0 { 245 t.Error("expected chainId to be", signer.chainId, "got", tx.ChainId()) 246 } 247 } 248 249 func TestEIP155SigningVitalik(t *testing.T) { 250 // Test vectors come from http://vitalik.ca/files/eip155_testvec.txt 251 for i, test := range []struct { 252 txRlp, addr string 253 }{ 254 {"f864808504a817c800825208943535353535353535353535353535353535353535808025a0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116da0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", "0xf0f6f18bca1b28cd68e4357452947e021241e9ce"}, 255 {"f864018504a817c80182a410943535353535353535353535353535353535353535018025a0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bcaa0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6", "0x23ef145a395ea3fa3deb533b8a9e1b4c6c25d112"}, 256 {"f864028504a817c80282f618943535353535353535353535353535353535353535088025a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5", "0x2e485e0c23b4c3c542628a5f672eeab0ad4888be"}, 257 {"f865038504a817c803830148209435353535353535353535353535353535353535351b8025a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4e0a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4de", "0x82a88539669a3fd524d669e858935de5e5410cf0"}, 258 {"f865048504a817c80483019a28943535353535353535353535353535353535353535408025a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c063a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c060", "0xf9358f2538fd5ccfeb848b64a96b743fcc930554"}, 259 {"f865058504a817c8058301ec309435353535353535353535353535353535353535357d8025a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1", "0xa8f7aba377317440bc5b26198a363ad22af1f3a4"}, 260 {"f866068504a817c80683023e3894353535353535353535353535353535353535353581d88025a06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2fa06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2d", "0xf1f571dc362a0e5b2696b8e775f8491d3e50de35"}, 261 {"f867078504a817c807830290409435353535353535353535353535353535353535358201578025a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021", "0xd37922162ab7cea97c97a87551ed02c9a38b7332"}, 262 {"f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10", "0x9bddad43f934d313c2b79ca28a432dd2b7281029"}, 263 {"f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb", "0x3c24d7329e92f84f08556ceb6df1cdb0104ca49f"}, 264 } { 265 signer := NewEIP155Signer(big.NewInt(1)) 266 267 var tx *Transaction 268 err := rlp.DecodeBytes(common.Hex2Bytes(test.txRlp), &tx) 269 if err != nil { 270 t.Errorf("%d: %v", i, err) 271 continue 272 } 273 274 from, err := Sender(signer, tx) 275 if err != nil { 276 t.Errorf("%d: %v", i, err) 277 continue 278 } 279 280 addr := common.HexToAddress(test.addr) 281 if from != addr { 282 t.Errorf("%d: expected %x got %x", i, addr, from) 283 } 284 285 } 286 } 287 288 func TestChainId(t *testing.T) { 289 key, _ := defaultTestKey() 290 291 tx := NewTransaction(0, common.Address{}, new(big.Int), 0, new(big.Int), nil) 292 293 var err error 294 tx, err = SignTx(tx, LatestSignerForChainID(big.NewInt(1)), key) 295 if err != nil { 296 t.Fatal(err) 297 } 298 299 _, err = Sender(LatestSignerForChainID(big.NewInt(2)), tx) 300 if err != ErrInvalidChainId { 301 t.Error("expected error:", ErrInvalidChainId) 302 } 303 304 _, err = Sender(LatestSignerForChainID(big.NewInt(1)), tx) 305 if err != nil { 306 t.Error("expected no error") 307 } 308 } 309 310 // AccountKeyPickerForTest is an temporary object for testing. 311 // It simulates GetKey() instead of using `StateDB` directly in order to avoid cycle imports. 312 type AccountKeyPickerForTest struct { 313 AddrKeyMap map[common.Address]accountkey.AccountKey 314 } 315 316 func (a *AccountKeyPickerForTest) GetKey(addr common.Address) accountkey.AccountKey { 317 return a.AddrKeyMap[addr] 318 } 319 320 func (a *AccountKeyPickerForTest) SetKey(addr common.Address, key accountkey.AccountKey) { 321 a.AddrKeyMap[addr] = key 322 } 323 324 func (a *AccountKeyPickerForTest) Exist(addr common.Address) bool { 325 return a.AddrKeyMap[addr] != nil 326 } 327 328 type testTx func(t *testing.T) 329 330 // TestValidateTransaction tests validation process of transactions. 331 // To check that each tx makes a msg for the signature well, 332 // this test generates the msg manually and then performs validation process. 333 func TestValidateTransaction(t *testing.T) { 334 testfns := []testTx{ 335 testValidateValueTransfer, 336 testValidateFeeDelegatedValueTransfer, 337 testValidateFeeDelegatedValueTransferWithRatio, 338 testValidateValueTransferMemo, 339 testValidateFeeDelegatedValueTransferMemo, 340 testValidateFeeDelegatedValueTransferMemoWithRatio, 341 testValidateAccountUpdate, 342 testValidateFeeDelegatedAccountUpdate, 343 testValidateFeeDelegatedAccountUpdateWithRatio, 344 testValidateSmartContractDeploy, 345 testValidateFeeDelegatedSmartContractDeploy, 346 testValidateFeeDelegatedSmartContractDeployWithRatio, 347 testValidateSmartContractExecution, 348 testValidateFeeDelegatedSmartContractExecution, 349 testValidateFeeDelegatedSmartContractExecutionWithRatio, 350 testValidateCancel, 351 testValidateFeeDelegatedCancel, 352 testValidateFeeDelegatedCancelWithRatio, 353 testValidateChainDataAnchoring, 354 testValidateFeeDelegatedChainDataAnchoring, 355 testValidateFeeDelegatedChainDataAnchoringWithRatio, 356 } 357 358 for _, fn := range testfns { 359 fnname := getFunctionName(fn) 360 fnname = fnname[strings.LastIndex(fnname, ".")+1:] 361 testFunc := func(t *testing.T) { 362 t.Parallel() 363 fn(t) 364 } 365 366 t.Run(fnname, testFunc) 367 } 368 } 369 370 func testValidateValueTransfer(t *testing.T) { 371 // Transaction generation 372 internalTx := genValueTransferTransaction().(*TxInternalDataValueTransfer) 373 tx := &Transaction{data: internalTx} 374 375 chainid := big.NewInt(1) 376 signer := LatestSignerForChainID(chainid) 377 378 prv, from := defaultTestKey() 379 internalTx.From = from 380 381 // Sign 382 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), chainid, 0, 0 ]) 383 b, err := rlp.EncodeToBytes([]interface{}{ 384 internalTx.Type(), 385 internalTx.AccountNonce, 386 internalTx.Price, 387 internalTx.GasLimit, 388 internalTx.Recipient, 389 internalTx.Amount, 390 internalTx.From, 391 }) 392 assert.Equal(t, nil, err) 393 394 h := rlpHash([]interface{}{ 395 b, 396 chainid, 397 uint(0), 398 uint(0), 399 }) 400 401 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 402 assert.Equal(t, nil, err) 403 404 tx.SetSignature(sig) 405 406 // AccountKeyPicker initialization 407 p := &AccountKeyPickerForTest{ 408 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 409 } 410 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 411 p.SetKey(from, key) 412 413 // Validate 414 _, err = tx.ValidateSender(signer, p, 0) 415 assert.Equal(t, nil, err) 416 assert.Equal(t, from, tx.ValidatedSender()) 417 } 418 419 func testValidateFeeDelegatedValueTransfer(t *testing.T) { 420 // Transaction generation 421 internalTx := genFeeDelegatedValueTransferTransaction().(*TxInternalDataFeeDelegatedValueTransfer) 422 tx := &Transaction{data: internalTx} 423 424 chainid := big.NewInt(1) 425 signer := LatestSignerForChainID(chainid) 426 427 prv, from := defaultTestKey() 428 internalTx.From = from 429 430 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 431 assert.Equal(t, nil, err) 432 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 433 internalTx.FeePayer = feePayer 434 435 // Sign 436 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), chainid, 0, 0 ]) 437 b, err := rlp.EncodeToBytes([]interface{}{ 438 internalTx.Type(), 439 internalTx.AccountNonce, 440 internalTx.Price, 441 internalTx.GasLimit, 442 internalTx.Recipient, 443 internalTx.Amount, 444 internalTx.From, 445 }) 446 assert.Equal(t, nil, err) 447 448 h := rlpHash([]interface{}{ 449 b, 450 chainid, 451 uint(0), 452 uint(0), 453 }) 454 455 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 456 assert.Equal(t, nil, err) 457 458 tx.SetSignature(sig) 459 460 // Sign fee payer 461 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), feePayer, chainid, 0, 0 ]) 462 b, err = rlp.EncodeToBytes([]interface{}{ 463 internalTx.Type(), 464 internalTx.AccountNonce, 465 internalTx.Price, 466 internalTx.GasLimit, 467 internalTx.Recipient, 468 internalTx.Amount, 469 internalTx.From, 470 }) 471 assert.Equal(t, nil, err) 472 473 h = rlpHash([]interface{}{ 474 b, 475 feePayer, 476 chainid, 477 uint(0), 478 uint(0), 479 }) 480 481 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 482 assert.Equal(t, nil, err) 483 484 tx.SetFeePayerSignatures(feePayerSig) 485 486 // AccountKeyPicker initialization 487 p := &AccountKeyPickerForTest{ 488 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 489 } 490 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 491 p.SetKey(from, key) 492 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 493 p.SetKey(feePayer, feePayerKey) 494 495 // Validate 496 _, err = tx.ValidateSender(signer, p, 0) 497 assert.Equal(t, nil, err) 498 assert.Equal(t, from, tx.ValidatedSender()) 499 500 // Validate fee payer 501 _, err = tx.ValidateFeePayer(signer, p, 0) 502 assert.Equal(t, nil, err) 503 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 504 } 505 506 func testValidateFeeDelegatedValueTransferWithRatio(t *testing.T) { 507 // Transaction generation 508 internalTx := genFeeDelegatedValueTransferWithRatioTransaction().(*TxInternalDataFeeDelegatedValueTransferWithRatio) 509 tx := &Transaction{data: internalTx} 510 511 chainid := big.NewInt(1) 512 signer := LatestSignerForChainID(chainid) 513 514 prv, from := defaultTestKey() 515 internalTx.From = from 516 517 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 518 assert.Equal(t, nil, err) 519 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 520 internalTx.FeePayer = feePayer 521 522 // Sign 523 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, feeRatio]), chainid, 0, 0 ]) 524 b, err := rlp.EncodeToBytes([]interface{}{ 525 internalTx.Type(), 526 internalTx.AccountNonce, 527 internalTx.Price, 528 internalTx.GasLimit, 529 internalTx.Recipient, 530 internalTx.Amount, 531 internalTx.From, 532 internalTx.FeeRatio, 533 }) 534 assert.Equal(t, nil, err) 535 536 h := rlpHash([]interface{}{ 537 b, 538 chainid, 539 uint(0), 540 uint(0), 541 }) 542 543 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 544 assert.Equal(t, nil, err) 545 546 tx.SetSignature(sig) 547 548 // Sign fee payer 549 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, feeRatio]), feePayer, chainid, 0, 0 ]) 550 b, err = rlp.EncodeToBytes([]interface{}{ 551 internalTx.Type(), 552 internalTx.AccountNonce, 553 internalTx.Price, 554 internalTx.GasLimit, 555 internalTx.Recipient, 556 internalTx.Amount, 557 internalTx.From, 558 internalTx.FeeRatio, 559 }) 560 assert.Equal(t, nil, err) 561 562 h = rlpHash([]interface{}{ 563 b, 564 feePayer, 565 chainid, 566 uint(0), 567 uint(0), 568 }) 569 570 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 571 assert.Equal(t, nil, err) 572 573 tx.SetFeePayerSignatures(feePayerSig) 574 575 // AccountKeyPicker initialization 576 p := &AccountKeyPickerForTest{ 577 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 578 } 579 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 580 p.SetKey(from, key) 581 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 582 p.SetKey(feePayer, feePayerKey) 583 584 // Validate 585 _, err = tx.ValidateSender(signer, p, 0) 586 assert.Equal(t, nil, err) 587 assert.Equal(t, from, tx.ValidatedSender()) 588 589 // Validate fee payer 590 _, err = tx.ValidateFeePayer(signer, p, 0) 591 assert.Equal(t, nil, err) 592 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 593 } 594 595 func testValidateValueTransferMemo(t *testing.T) { 596 // Transaction generation 597 internalTx := genValueTransferMemoTransaction().(*TxInternalDataValueTransferMemo) 598 tx := &Transaction{data: internalTx} 599 600 chainid := big.NewInt(1) 601 signer := LatestSignerForChainID(chainid) 602 603 prv, from := defaultTestKey() 604 internalTx.From = from 605 606 // Sign 607 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 608 b, err := rlp.EncodeToBytes([]interface{}{ 609 internalTx.Type(), 610 internalTx.AccountNonce, 611 internalTx.Price, 612 internalTx.GasLimit, 613 internalTx.Recipient, 614 internalTx.Amount, 615 internalTx.From, 616 internalTx.Payload, 617 }) 618 assert.Equal(t, nil, err) 619 620 h := rlpHash([]interface{}{ 621 b, 622 chainid, 623 uint(0), 624 uint(0), 625 }) 626 627 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 628 assert.Equal(t, nil, err) 629 630 tx.SetSignature(sig) 631 632 // AccountKeyPicker initialization 633 p := &AccountKeyPickerForTest{ 634 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 635 } 636 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 637 p.SetKey(from, key) 638 639 // Validate 640 _, err = tx.ValidateSender(signer, p, 0) 641 assert.Equal(t, nil, err) 642 assert.Equal(t, from, tx.ValidatedSender()) 643 } 644 645 func testValidateFeeDelegatedValueTransferMemo(t *testing.T) { 646 // Transaction generation 647 internalTx := genFeeDelegatedValueTransferMemoTransaction().(*TxInternalDataFeeDelegatedValueTransferMemo) 648 tx := &Transaction{data: internalTx} 649 650 chainid := big.NewInt(1) 651 signer := LatestSignerForChainID(chainid) 652 653 prv, from := defaultTestKey() 654 internalTx.From = from 655 656 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 657 assert.Equal(t, nil, err) 658 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 659 internalTx.FeePayer = feePayer 660 661 // Sign 662 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 663 b, err := rlp.EncodeToBytes([]interface{}{ 664 internalTx.Type(), 665 internalTx.AccountNonce, 666 internalTx.Price, 667 internalTx.GasLimit, 668 internalTx.Recipient, 669 internalTx.Amount, 670 internalTx.From, 671 internalTx.Payload, 672 }) 673 assert.Equal(t, nil, err) 674 675 h := rlpHash([]interface{}{ 676 b, 677 chainid, 678 uint(0), 679 uint(0), 680 }) 681 682 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 683 assert.Equal(t, nil, err) 684 685 tx.SetSignature(sig) 686 687 // Sign fee payer 688 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), feePayer, chainid, 0, 0 ]) 689 b, err = rlp.EncodeToBytes([]interface{}{ 690 internalTx.Type(), 691 internalTx.AccountNonce, 692 internalTx.Price, 693 internalTx.GasLimit, 694 internalTx.Recipient, 695 internalTx.Amount, 696 internalTx.From, 697 internalTx.Payload, 698 }) 699 assert.Equal(t, nil, err) 700 701 h = rlpHash([]interface{}{ 702 b, 703 feePayer, 704 chainid, 705 uint(0), 706 uint(0), 707 }) 708 709 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 710 assert.Equal(t, nil, err) 711 712 tx.SetFeePayerSignatures(feePayerSig) 713 714 // AccountKeyPicker initialization 715 p := &AccountKeyPickerForTest{ 716 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 717 } 718 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 719 p.SetKey(from, key) 720 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 721 p.SetKey(feePayer, feePayerKey) 722 723 // Validate 724 _, err = tx.ValidateSender(signer, p, 0) 725 assert.Equal(t, nil, err) 726 assert.Equal(t, from, tx.ValidatedSender()) 727 728 // Validate fee payer 729 _, err = tx.ValidateFeePayer(signer, p, 0) 730 assert.Equal(t, nil, err) 731 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 732 } 733 734 func testValidateFeeDelegatedValueTransferMemoWithRatio(t *testing.T) { 735 // Transaction generation 736 internalTx := genFeeDelegatedValueTransferMemoWithRatioTransaction().(*TxInternalDataFeeDelegatedValueTransferMemoWithRatio) 737 tx := &Transaction{data: internalTx} 738 739 chainid := big.NewInt(1) 740 signer := LatestSignerForChainID(chainid) 741 742 prv, from := defaultTestKey() 743 internalTx.From = from 744 745 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 746 assert.Equal(t, nil, err) 747 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 748 internalTx.FeePayer = feePayer 749 750 // Sign 751 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), chainid, 0, 0 ]) 752 b, err := rlp.EncodeToBytes([]interface{}{ 753 internalTx.Type(), 754 internalTx.AccountNonce, 755 internalTx.Price, 756 internalTx.GasLimit, 757 internalTx.Recipient, 758 internalTx.Amount, 759 internalTx.From, 760 internalTx.Payload, 761 internalTx.FeeRatio, 762 }) 763 assert.Equal(t, nil, err) 764 765 h := rlpHash([]interface{}{ 766 b, 767 chainid, 768 uint(0), 769 uint(0), 770 }) 771 772 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 773 assert.Equal(t, nil, err) 774 775 tx.SetSignature(sig) 776 777 // Sign fee payer 778 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), feePayer, chainid, 0, 0 ]) 779 b, err = rlp.EncodeToBytes([]interface{}{ 780 internalTx.Type(), 781 internalTx.AccountNonce, 782 internalTx.Price, 783 internalTx.GasLimit, 784 internalTx.Recipient, 785 internalTx.Amount, 786 internalTx.From, 787 internalTx.Payload, 788 internalTx.FeeRatio, 789 }) 790 assert.Equal(t, nil, err) 791 792 h = rlpHash([]interface{}{ 793 b, 794 feePayer, 795 chainid, 796 uint(0), 797 uint(0), 798 }) 799 800 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 801 assert.Equal(t, nil, err) 802 803 tx.SetFeePayerSignatures(feePayerSig) 804 805 // AccountKeyPicker initialization 806 p := &AccountKeyPickerForTest{ 807 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 808 } 809 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 810 p.SetKey(from, key) 811 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 812 p.SetKey(feePayer, feePayerKey) 813 814 // Validate 815 _, err = tx.ValidateSender(signer, p, 0) 816 assert.Equal(t, nil, err) 817 assert.Equal(t, from, tx.ValidatedSender()) 818 819 // Validate fee payer 820 _, err = tx.ValidateFeePayer(signer, p, 0) 821 assert.Equal(t, nil, err) 822 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 823 } 824 825 //func testValidateAccountCreation(t *testing.T) { 826 // // Transaction generation 827 // internalTx := genAccountCreationTransaction().(*TxInternalDataAccountCreation) 828 // tx := &Transaction{data: internalTx} 829 // 830 // chainid := big.NewInt(1) 831 // signer := NewEIP155Signer(chainid) 832 // 833 // prv, from := defaultTestKey() 834 // internalTx.From = from 835 // 836 // // Sign 837 // // encode([ encode([type, nonce, gasPrice, gas, to, value, from, humanReadable, encodedKey]), chainid, 0, 0 ]) 838 // encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 839 // assert.Equal(t, nil, err) 840 // b, err := rlp.EncodeToBytes([]interface{}{ 841 // internalTx.Type(), 842 // internalTx.AccountNonce, 843 // internalTx.Price, 844 // internalTx.GasLimit, 845 // internalTx.Recipient, 846 // internalTx.Amount, 847 // internalTx.From, 848 // internalTx.HumanReadable, 849 // encodedKey, 850 // }) 851 // assert.Equal(t, nil, err) 852 // 853 // h := rlpHash([]interface{}{ 854 // b, 855 // chainid, 856 // uint(0), 857 // uint(0), 858 // }) 859 // 860 // sig, err := NewTxSignaturesWithValues(signer, h, []*ecdsa.PrivateKey{prv}) 861 // assert.Equal(t, nil, err) 862 // 863 // tx.SetSignature(sig) 864 // 865 // // AccountKeyPicker initialization 866 // p := &AccountKeyPickerForTest{ 867 // AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 868 // } 869 // key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 870 // p.SetKey(from, key) 871 // 872 // // Validate 873 // _, err = tx.ValidateSender(signer, p, 0) 874 // assert.Equal(t, nil, err) 875 // assert.Equal(t, from, tx.ValidatedSender()) 876 //} 877 878 func testValidateAccountUpdate(t *testing.T) { 879 // Transaction generation 880 internalTx := genAccountUpdateTransaction().(*TxInternalDataAccountUpdate) 881 tx := &Transaction{data: internalTx} 882 883 chainid := big.NewInt(1) 884 signer := LatestSignerForChainID(chainid) 885 886 prv, from := defaultTestKey() 887 internalTx.From = from 888 889 // Sign 890 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), chainid, 0, 0 ]) 891 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 892 assert.Equal(t, nil, err) 893 b, err := rlp.EncodeToBytes([]interface{}{ 894 internalTx.Type(), 895 internalTx.AccountNonce, 896 internalTx.Price, 897 internalTx.GasLimit, 898 internalTx.From, 899 encodedKey, 900 }) 901 assert.Equal(t, nil, err) 902 903 h := rlpHash([]interface{}{ 904 b, 905 chainid, 906 uint(0), 907 uint(0), 908 }) 909 910 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 911 assert.Equal(t, nil, err) 912 913 tx.SetSignature(sig) 914 915 // AccountKeyPicker initialization 916 p := &AccountKeyPickerForTest{ 917 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 918 } 919 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 920 p.SetKey(from, key) 921 922 // Validate 923 _, err = tx.ValidateSender(signer, p, 0) 924 assert.Equal(t, nil, err) 925 assert.Equal(t, from, tx.ValidatedSender()) 926 } 927 928 func testValidateFeeDelegatedAccountUpdate(t *testing.T) { 929 // Transaction generation 930 internalTx := genFeeDelegatedAccountUpdateTransaction().(*TxInternalDataFeeDelegatedAccountUpdate) 931 tx := &Transaction{data: internalTx} 932 933 chainid := big.NewInt(1) 934 signer := LatestSignerForChainID(chainid) 935 936 prv, from := defaultTestKey() 937 internalTx.From = from 938 939 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 940 assert.Equal(t, nil, err) 941 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 942 internalTx.FeePayer = feePayer 943 944 // Sign 945 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), chainid, 0, 0 ]) 946 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 947 assert.Equal(t, nil, err) 948 b, err := rlp.EncodeToBytes([]interface{}{ 949 internalTx.Type(), 950 internalTx.AccountNonce, 951 internalTx.Price, 952 internalTx.GasLimit, 953 internalTx.From, 954 encodedKey, 955 }) 956 assert.Equal(t, nil, err) 957 958 h := rlpHash([]interface{}{ 959 b, 960 chainid, 961 uint(0), 962 uint(0), 963 }) 964 965 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 966 assert.Equal(t, nil, err) 967 968 tx.SetSignature(sig) 969 970 // Sign fee payer 971 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), feePayer, chainid, 0, 0 ]) 972 encodedKey, err = rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 973 assert.Equal(t, nil, err) 974 b, err = rlp.EncodeToBytes([]interface{}{ 975 internalTx.Type(), 976 internalTx.AccountNonce, 977 internalTx.Price, 978 internalTx.GasLimit, 979 internalTx.From, 980 encodedKey, 981 }) 982 assert.Equal(t, nil, err) 983 984 h = rlpHash([]interface{}{ 985 b, 986 feePayer, 987 chainid, 988 uint(0), 989 uint(0), 990 }) 991 992 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 993 assert.Equal(t, nil, err) 994 995 tx.SetFeePayerSignatures(feePayerSig) 996 997 // AccountKeyPicker initialization 998 p := &AccountKeyPickerForTest{ 999 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1000 } 1001 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1002 p.SetKey(from, key) 1003 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1004 p.SetKey(feePayer, feePayerKey) 1005 1006 // Validate 1007 _, err = tx.ValidateSender(signer, p, 0) 1008 assert.Equal(t, nil, err) 1009 assert.Equal(t, from, tx.ValidatedSender()) 1010 1011 // Validate fee payer 1012 _, err = tx.ValidateFeePayer(signer, p, 0) 1013 assert.Equal(t, nil, err) 1014 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1015 } 1016 1017 func testValidateFeeDelegatedAccountUpdateWithRatio(t *testing.T) { 1018 // Transaction generation 1019 internalTx := genFeeDelegatedAccountUpdateWithRatioTransaction().(*TxInternalDataFeeDelegatedAccountUpdateWithRatio) 1020 tx := &Transaction{data: internalTx} 1021 1022 chainid := big.NewInt(1) 1023 signer := LatestSignerForChainID(chainid) 1024 1025 prv, from := defaultTestKey() 1026 internalTx.From = from 1027 1028 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1029 assert.Equal(t, nil, err) 1030 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1031 internalTx.FeePayer = feePayer 1032 1033 // Sign 1034 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey, feeRatio]), chainid, 0, 0 ]) 1035 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 1036 assert.Equal(t, nil, err) 1037 b, err := rlp.EncodeToBytes([]interface{}{ 1038 internalTx.Type(), 1039 internalTx.AccountNonce, 1040 internalTx.Price, 1041 internalTx.GasLimit, 1042 internalTx.From, 1043 encodedKey, 1044 internalTx.FeeRatio, 1045 }) 1046 assert.Equal(t, nil, err) 1047 1048 h := rlpHash([]interface{}{ 1049 b, 1050 chainid, 1051 uint(0), 1052 uint(0), 1053 }) 1054 1055 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1056 assert.Equal(t, nil, err) 1057 1058 tx.SetSignature(sig) 1059 1060 // Sign fee payer 1061 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey, feeRatio]), feePayer, chainid, 0, 0 ]) 1062 encodedKey, err = rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 1063 assert.Equal(t, nil, err) 1064 b, err = rlp.EncodeToBytes([]interface{}{ 1065 internalTx.Type(), 1066 internalTx.AccountNonce, 1067 internalTx.Price, 1068 internalTx.GasLimit, 1069 internalTx.From, 1070 encodedKey, 1071 internalTx.FeeRatio, 1072 }) 1073 assert.Equal(t, nil, err) 1074 1075 h = rlpHash([]interface{}{ 1076 b, 1077 feePayer, 1078 chainid, 1079 uint(0), 1080 uint(0), 1081 }) 1082 1083 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1084 assert.Equal(t, nil, err) 1085 1086 tx.SetFeePayerSignatures(feePayerSig) 1087 1088 // AccountKeyPicker initialization 1089 p := &AccountKeyPickerForTest{ 1090 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1091 } 1092 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1093 p.SetKey(from, key) 1094 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1095 p.SetKey(feePayer, feePayerKey) 1096 1097 // Validate 1098 _, err = tx.ValidateSender(signer, p, 0) 1099 assert.Equal(t, nil, err) 1100 assert.Equal(t, from, tx.ValidatedSender()) 1101 1102 // Validate fee payer 1103 _, err = tx.ValidateFeePayer(signer, p, 0) 1104 assert.Equal(t, nil, err) 1105 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1106 } 1107 1108 func testValidateSmartContractDeploy(t *testing.T) { 1109 // Transaction generation 1110 internalTx := genSmartContractDeployTransaction().(*TxInternalDataSmartContractDeploy) 1111 tx := &Transaction{data: internalTx} 1112 1113 chainid := big.NewInt(1) 1114 signer := LatestSignerForChainID(chainid) 1115 1116 prv, from := defaultTestKey() 1117 internalTx.From = from 1118 1119 // Sign 1120 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), chainid, 0, 0 ]) 1121 b, err := rlp.EncodeToBytes([]interface{}{ 1122 internalTx.Type(), 1123 internalTx.AccountNonce, 1124 internalTx.Price, 1125 internalTx.GasLimit, 1126 internalTx.Recipient, 1127 internalTx.Amount, 1128 internalTx.From, 1129 internalTx.Payload, 1130 internalTx.HumanReadable, 1131 }) 1132 assert.Equal(t, nil, err) 1133 1134 h := rlpHash([]interface{}{ 1135 b, 1136 chainid, 1137 uint(0), 1138 uint(0), 1139 }) 1140 1141 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1142 assert.Equal(t, nil, err) 1143 1144 tx.SetSignature(sig) 1145 1146 // AccountKeyPicker initialization 1147 p := &AccountKeyPickerForTest{ 1148 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1149 } 1150 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1151 p.SetKey(from, key) 1152 1153 // Validate 1154 _, err = tx.ValidateSender(signer, p, 0) 1155 assert.Equal(t, nil, err) 1156 assert.Equal(t, from, tx.ValidatedSender()) 1157 } 1158 1159 func testValidateFeeDelegatedSmartContractDeploy(t *testing.T) { 1160 // Transaction generation 1161 internalTx := genFeeDelegatedSmartContractDeployTransaction().(*TxInternalDataFeeDelegatedSmartContractDeploy) 1162 tx := &Transaction{data: internalTx} 1163 1164 chainid := big.NewInt(1) 1165 signer := LatestSignerForChainID(chainid) 1166 1167 prv, from := defaultTestKey() 1168 internalTx.From = from 1169 1170 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1171 assert.Equal(t, nil, err) 1172 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1173 internalTx.FeePayer = feePayer 1174 1175 // Sign 1176 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), chainid, 0, 0 ]) 1177 b, err := rlp.EncodeToBytes([]interface{}{ 1178 internalTx.Type(), 1179 internalTx.AccountNonce, 1180 internalTx.Price, 1181 internalTx.GasLimit, 1182 internalTx.Recipient, 1183 internalTx.Amount, 1184 internalTx.From, 1185 internalTx.Payload, 1186 internalTx.HumanReadable, 1187 }) 1188 assert.Equal(t, nil, err) 1189 1190 h := rlpHash([]interface{}{ 1191 b, 1192 chainid, 1193 uint(0), 1194 uint(0), 1195 }) 1196 1197 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1198 assert.Equal(t, nil, err) 1199 1200 tx.SetSignature(sig) 1201 1202 // Sign fee payer 1203 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), feePayer, chainid, 0, 0 ]) 1204 b, err = rlp.EncodeToBytes([]interface{}{ 1205 internalTx.Type(), 1206 internalTx.AccountNonce, 1207 internalTx.Price, 1208 internalTx.GasLimit, 1209 internalTx.Recipient, 1210 internalTx.Amount, 1211 internalTx.From, 1212 internalTx.Payload, 1213 internalTx.HumanReadable, 1214 }) 1215 assert.Equal(t, nil, err) 1216 1217 h = rlpHash([]interface{}{ 1218 b, 1219 feePayer, 1220 chainid, 1221 uint(0), 1222 uint(0), 1223 }) 1224 1225 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1226 assert.Equal(t, nil, err) 1227 1228 tx.SetFeePayerSignatures(feePayerSig) 1229 1230 // AccountKeyPicker initialization 1231 p := &AccountKeyPickerForTest{ 1232 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1233 } 1234 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1235 p.SetKey(from, key) 1236 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1237 p.SetKey(feePayer, feePayerKey) 1238 1239 // Validate 1240 _, err = tx.ValidateSender(signer, p, 0) 1241 assert.Equal(t, nil, err) 1242 assert.Equal(t, from, tx.ValidatedSender()) 1243 1244 // Validate fee payer 1245 _, err = tx.ValidateFeePayer(signer, p, 0) 1246 assert.Equal(t, nil, err) 1247 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1248 } 1249 1250 func testValidateFeeDelegatedSmartContractDeployWithRatio(t *testing.T) { 1251 // Transaction generation 1252 internalTx := genFeeDelegatedSmartContractDeployWithRatioTransaction().(*TxInternalDataFeeDelegatedSmartContractDeployWithRatio) 1253 tx := &Transaction{data: internalTx} 1254 1255 chainid := big.NewInt(1) 1256 signer := LatestSignerForChainID(chainid) 1257 1258 prv, from := defaultTestKey() 1259 internalTx.From = from 1260 1261 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1262 assert.Equal(t, nil, err) 1263 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1264 internalTx.FeePayer = feePayer 1265 1266 // Sign 1267 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable, feeRatio]), chainid, 0, 0 ]) 1268 b, err := rlp.EncodeToBytes([]interface{}{ 1269 internalTx.Type(), 1270 internalTx.AccountNonce, 1271 internalTx.Price, 1272 internalTx.GasLimit, 1273 internalTx.Recipient, 1274 internalTx.Amount, 1275 internalTx.From, 1276 internalTx.Payload, 1277 internalTx.HumanReadable, 1278 internalTx.FeeRatio, 1279 }) 1280 assert.Equal(t, nil, err) 1281 1282 h := rlpHash([]interface{}{ 1283 b, 1284 chainid, 1285 uint(0), 1286 uint(0), 1287 }) 1288 1289 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1290 assert.Equal(t, nil, err) 1291 1292 tx.SetSignature(sig) 1293 1294 // Sign fee payer 1295 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable, feeRatio]), feePayer, chainid, 0, 0 ]) 1296 b, err = rlp.EncodeToBytes([]interface{}{ 1297 internalTx.Type(), 1298 internalTx.AccountNonce, 1299 internalTx.Price, 1300 internalTx.GasLimit, 1301 internalTx.Recipient, 1302 internalTx.Amount, 1303 internalTx.From, 1304 internalTx.Payload, 1305 internalTx.HumanReadable, 1306 internalTx.FeeRatio, 1307 }) 1308 assert.Equal(t, nil, err) 1309 1310 h = rlpHash([]interface{}{ 1311 b, 1312 feePayer, 1313 chainid, 1314 uint(0), 1315 uint(0), 1316 }) 1317 1318 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1319 assert.Equal(t, nil, err) 1320 1321 tx.SetFeePayerSignatures(feePayerSig) 1322 1323 // AccountKeyPicker initialization 1324 p := &AccountKeyPickerForTest{ 1325 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1326 } 1327 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1328 p.SetKey(from, key) 1329 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1330 p.SetKey(feePayer, feePayerKey) 1331 1332 // Validate 1333 _, err = tx.ValidateSender(signer, p, 0) 1334 assert.Equal(t, nil, err) 1335 assert.Equal(t, from, tx.ValidatedSender()) 1336 1337 // Validate fee payer 1338 _, err = tx.ValidateFeePayer(signer, p, 0) 1339 assert.Equal(t, nil, err) 1340 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1341 } 1342 1343 func testValidateSmartContractExecution(t *testing.T) { 1344 // Transaction generation 1345 internalTx := genSmartContractExecutionTransaction().(*TxInternalDataSmartContractExecution) 1346 tx := &Transaction{data: internalTx} 1347 1348 chainid := big.NewInt(1) 1349 signer := LatestSignerForChainID(chainid) 1350 1351 prv, from := defaultTestKey() 1352 internalTx.From = from 1353 1354 // Sign 1355 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 1356 b, err := rlp.EncodeToBytes([]interface{}{ 1357 internalTx.Type(), 1358 internalTx.AccountNonce, 1359 internalTx.Price, 1360 internalTx.GasLimit, 1361 internalTx.Recipient, 1362 internalTx.Amount, 1363 internalTx.From, 1364 internalTx.Payload, 1365 }) 1366 assert.Equal(t, nil, err) 1367 1368 h := rlpHash([]interface{}{ 1369 b, 1370 chainid, 1371 uint(0), 1372 uint(0), 1373 }) 1374 1375 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1376 assert.Equal(t, nil, err) 1377 1378 tx.SetSignature(sig) 1379 1380 // AccountKeyPicker initialization 1381 p := &AccountKeyPickerForTest{ 1382 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1383 } 1384 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1385 p.SetKey(from, key) 1386 1387 // Validate 1388 _, err = tx.ValidateSender(signer, p, 0) 1389 assert.Equal(t, nil, err) 1390 assert.Equal(t, from, tx.ValidatedSender()) 1391 } 1392 1393 func testValidateFeeDelegatedSmartContractExecution(t *testing.T) { 1394 // Transaction generation 1395 internalTx := genFeeDelegatedSmartContractExecutionTransaction().(*TxInternalDataFeeDelegatedSmartContractExecution) 1396 tx := &Transaction{data: internalTx} 1397 1398 chainid := big.NewInt(1) 1399 signer := LatestSignerForChainID(chainid) 1400 1401 prv, from := defaultTestKey() 1402 internalTx.From = from 1403 1404 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1405 assert.Equal(t, nil, err) 1406 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1407 internalTx.FeePayer = feePayer 1408 1409 // Sign 1410 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 1411 b, err := rlp.EncodeToBytes([]interface{}{ 1412 internalTx.Type(), 1413 internalTx.AccountNonce, 1414 internalTx.Price, 1415 internalTx.GasLimit, 1416 internalTx.Recipient, 1417 internalTx.Amount, 1418 internalTx.From, 1419 internalTx.Payload, 1420 }) 1421 assert.Equal(t, nil, err) 1422 1423 h := rlpHash([]interface{}{ 1424 b, 1425 chainid, 1426 uint(0), 1427 uint(0), 1428 }) 1429 1430 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1431 assert.Equal(t, nil, err) 1432 1433 tx.SetSignature(sig) 1434 1435 // Sign fee payer 1436 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), feePayer, chainid, 0, 0 ]) 1437 b, err = rlp.EncodeToBytes([]interface{}{ 1438 internalTx.Type(), 1439 internalTx.AccountNonce, 1440 internalTx.Price, 1441 internalTx.GasLimit, 1442 internalTx.Recipient, 1443 internalTx.Amount, 1444 internalTx.From, 1445 internalTx.Payload, 1446 }) 1447 assert.Equal(t, nil, err) 1448 1449 h = rlpHash([]interface{}{ 1450 b, 1451 feePayer, 1452 chainid, 1453 uint(0), 1454 uint(0), 1455 }) 1456 1457 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1458 assert.Equal(t, nil, err) 1459 1460 tx.SetFeePayerSignatures(feePayerSig) 1461 1462 // AccountKeyPicker initialization 1463 p := &AccountKeyPickerForTest{ 1464 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1465 } 1466 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1467 p.SetKey(from, key) 1468 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1469 p.SetKey(feePayer, feePayerKey) 1470 1471 // Validate 1472 _, err = tx.ValidateSender(signer, p, 0) 1473 assert.Equal(t, nil, err) 1474 assert.Equal(t, from, tx.ValidatedSender()) 1475 1476 // Validate fee payer 1477 _, err = tx.ValidateFeePayer(signer, p, 0) 1478 assert.Equal(t, nil, err) 1479 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1480 } 1481 1482 func testValidateFeeDelegatedSmartContractExecutionWithRatio(t *testing.T) { 1483 // Transaction generation 1484 internalTx := genFeeDelegatedSmartContractExecutionWithRatioTransaction().(*TxInternalDataFeeDelegatedSmartContractExecutionWithRatio) 1485 tx := &Transaction{data: internalTx} 1486 1487 chainid := big.NewInt(1) 1488 signer := LatestSignerForChainID(chainid) 1489 1490 prv, from := defaultTestKey() 1491 internalTx.From = from 1492 1493 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1494 assert.Equal(t, nil, err) 1495 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1496 internalTx.FeePayer = feePayer 1497 1498 // Sign 1499 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), chainid, 0, 0 ]) 1500 b, err := rlp.EncodeToBytes([]interface{}{ 1501 internalTx.Type(), 1502 internalTx.AccountNonce, 1503 internalTx.Price, 1504 internalTx.GasLimit, 1505 internalTx.Recipient, 1506 internalTx.Amount, 1507 internalTx.From, 1508 internalTx.Payload, 1509 internalTx.FeeRatio, 1510 }) 1511 assert.Equal(t, nil, err) 1512 1513 h := rlpHash([]interface{}{ 1514 b, 1515 chainid, 1516 uint(0), 1517 uint(0), 1518 }) 1519 1520 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1521 assert.Equal(t, nil, err) 1522 1523 tx.SetSignature(sig) 1524 1525 // Sign fee payer 1526 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), feePayer, chainid, 0, 0 ]) 1527 b, err = rlp.EncodeToBytes([]interface{}{ 1528 internalTx.Type(), 1529 internalTx.AccountNonce, 1530 internalTx.Price, 1531 internalTx.GasLimit, 1532 internalTx.Recipient, 1533 internalTx.Amount, 1534 internalTx.From, 1535 internalTx.Payload, 1536 internalTx.FeeRatio, 1537 }) 1538 assert.Equal(t, nil, err) 1539 1540 h = rlpHash([]interface{}{ 1541 b, 1542 feePayer, 1543 chainid, 1544 uint(0), 1545 uint(0), 1546 }) 1547 1548 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1549 assert.Equal(t, nil, err) 1550 1551 tx.SetFeePayerSignatures(feePayerSig) 1552 1553 // AccountKeyPicker initialization 1554 p := &AccountKeyPickerForTest{ 1555 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1556 } 1557 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1558 p.SetKey(from, key) 1559 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1560 p.SetKey(feePayer, feePayerKey) 1561 1562 // Validate 1563 _, err = tx.ValidateSender(signer, p, 0) 1564 assert.Equal(t, nil, err) 1565 assert.Equal(t, from, tx.ValidatedSender()) 1566 1567 // Validate fee payer 1568 _, err = tx.ValidateFeePayer(signer, p, 0) 1569 assert.Equal(t, nil, err) 1570 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1571 } 1572 1573 func testValidateCancel(t *testing.T) { 1574 // Transaction generation 1575 internalTx := genCancelTransaction().(*TxInternalDataCancel) 1576 tx := &Transaction{data: internalTx} 1577 1578 chainid := big.NewInt(1) 1579 signer := LatestSignerForChainID(chainid) 1580 1581 prv, from := defaultTestKey() 1582 internalTx.From = from 1583 1584 // Sign 1585 // encode([ encode([type, nonce, gasPrice, gas, from]), chainid, 0, 0 ]) 1586 b, err := rlp.EncodeToBytes([]interface{}{ 1587 internalTx.Type(), 1588 internalTx.AccountNonce, 1589 internalTx.Price, 1590 internalTx.GasLimit, 1591 internalTx.From, 1592 }) 1593 assert.Equal(t, nil, err) 1594 1595 h := rlpHash([]interface{}{ 1596 b, 1597 chainid, 1598 uint(0), 1599 uint(0), 1600 }) 1601 1602 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1603 assert.Equal(t, nil, err) 1604 1605 tx.SetSignature(sig) 1606 1607 // AccountKeyPicker initialization 1608 p := &AccountKeyPickerForTest{ 1609 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1610 } 1611 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1612 p.SetKey(from, key) 1613 1614 // Validate 1615 _, err = tx.ValidateSender(signer, p, 0) 1616 assert.Equal(t, nil, err) 1617 assert.Equal(t, from, tx.ValidatedSender()) 1618 } 1619 1620 func testValidateFeeDelegatedCancel(t *testing.T) { 1621 // Transaction generation 1622 internalTx := genFeeDelegatedCancelTransaction().(*TxInternalDataFeeDelegatedCancel) 1623 tx := &Transaction{data: internalTx} 1624 1625 chainid := big.NewInt(1) 1626 signer := LatestSignerForChainID(chainid) 1627 1628 prv, from := defaultTestKey() 1629 internalTx.From = from 1630 1631 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1632 assert.Equal(t, nil, err) 1633 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1634 internalTx.FeePayer = feePayer 1635 1636 // Sign 1637 // encode([ encode([type, nonce, gasPrice, gas, from]), chainid, 0, 0 ]) 1638 b, err := rlp.EncodeToBytes([]interface{}{ 1639 internalTx.Type(), 1640 internalTx.AccountNonce, 1641 internalTx.Price, 1642 internalTx.GasLimit, 1643 internalTx.From, 1644 }) 1645 assert.Equal(t, nil, err) 1646 1647 h := rlpHash([]interface{}{ 1648 b, 1649 chainid, 1650 uint(0), 1651 uint(0), 1652 }) 1653 1654 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1655 assert.Equal(t, nil, err) 1656 1657 tx.SetSignature(sig) 1658 1659 // Sign fee payer 1660 // encode([ encode([type, nonce, gasPrice, gas, from]), feePayer, chainid, 0, 0 ]) 1661 b, err = rlp.EncodeToBytes([]interface{}{ 1662 internalTx.Type(), 1663 internalTx.AccountNonce, 1664 internalTx.Price, 1665 internalTx.GasLimit, 1666 internalTx.From, 1667 }) 1668 assert.Equal(t, nil, err) 1669 1670 h = rlpHash([]interface{}{ 1671 b, 1672 feePayer, 1673 chainid, 1674 uint(0), 1675 uint(0), 1676 }) 1677 1678 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1679 assert.Equal(t, nil, err) 1680 1681 tx.SetFeePayerSignatures(feePayerSig) 1682 1683 // AccountKeyPicker initialization 1684 p := &AccountKeyPickerForTest{ 1685 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1686 } 1687 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1688 p.SetKey(from, key) 1689 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1690 p.SetKey(feePayer, feePayerKey) 1691 1692 // Validate 1693 _, err = tx.ValidateSender(signer, p, 0) 1694 assert.Equal(t, nil, err) 1695 assert.Equal(t, from, tx.ValidatedSender()) 1696 1697 // Validate fee payer 1698 _, err = tx.ValidateFeePayer(signer, p, 0) 1699 assert.Equal(t, nil, err) 1700 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1701 } 1702 1703 func testValidateFeeDelegatedCancelWithRatio(t *testing.T) { 1704 // Transaction generation 1705 internalTx := genFeeDelegatedCancelWithRatioTransaction().(*TxInternalDataFeeDelegatedCancelWithRatio) 1706 tx := &Transaction{data: internalTx} 1707 1708 chainid := big.NewInt(1) 1709 signer := LatestSignerForChainID(chainid) 1710 1711 prv, from := defaultTestKey() 1712 internalTx.From = from 1713 1714 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1715 assert.Equal(t, nil, err) 1716 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1717 internalTx.FeePayer = feePayer 1718 1719 // Sign 1720 // encode([ encode([type, nonce, gasPrice, gas, from, feeRatio]), chainid, 0, 0 ]) 1721 b, err := rlp.EncodeToBytes([]interface{}{ 1722 internalTx.Type(), 1723 internalTx.AccountNonce, 1724 internalTx.Price, 1725 internalTx.GasLimit, 1726 internalTx.From, 1727 internalTx.FeeRatio, 1728 }) 1729 assert.Equal(t, nil, err) 1730 1731 h := rlpHash([]interface{}{ 1732 b, 1733 chainid, 1734 uint(0), 1735 uint(0), 1736 }) 1737 1738 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1739 assert.Equal(t, nil, err) 1740 1741 tx.SetSignature(sig) 1742 1743 // Sign fee payer 1744 // encode([ encode([type, nonce, gasPrice, gas, from, feeRatio]), feePayer, chainid, 0, 0 ]) 1745 b, err = rlp.EncodeToBytes([]interface{}{ 1746 internalTx.Type(), 1747 internalTx.AccountNonce, 1748 internalTx.Price, 1749 internalTx.GasLimit, 1750 internalTx.From, 1751 internalTx.FeeRatio, 1752 }) 1753 assert.Equal(t, nil, err) 1754 1755 h = rlpHash([]interface{}{ 1756 b, 1757 feePayer, 1758 chainid, 1759 uint(0), 1760 uint(0), 1761 }) 1762 1763 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1764 assert.Equal(t, nil, err) 1765 1766 tx.SetFeePayerSignatures(feePayerSig) 1767 1768 // AccountKeyPicker initialization 1769 p := &AccountKeyPickerForTest{ 1770 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1771 } 1772 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1773 p.SetKey(from, key) 1774 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1775 p.SetKey(feePayer, feePayerKey) 1776 1777 // Validate 1778 _, err = tx.ValidateSender(signer, p, 0) 1779 assert.Equal(t, nil, err) 1780 assert.Equal(t, from, tx.ValidatedSender()) 1781 1782 // Validate fee payer 1783 _, err = tx.ValidateFeePayer(signer, p, 0) 1784 assert.Equal(t, nil, err) 1785 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1786 } 1787 1788 func testValidateChainDataAnchoring(t *testing.T) { 1789 // Transaction generation 1790 internalTx := genChainDataTransaction().(*TxInternalDataChainDataAnchoring) 1791 tx := &Transaction{data: internalTx} 1792 1793 chainid := big.NewInt(1) 1794 signer := LatestSignerForChainID(chainid) 1795 1796 prv, from := defaultTestKey() 1797 internalTx.From = from 1798 1799 // Sign 1800 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), chainid, 0, 0 ]) 1801 b, err := rlp.EncodeToBytes([]interface{}{ 1802 internalTx.Type(), 1803 internalTx.AccountNonce, 1804 internalTx.Price, 1805 internalTx.GasLimit, 1806 internalTx.From, 1807 internalTx.Payload, 1808 }) 1809 assert.Equal(t, nil, err) 1810 1811 h := rlpHash([]interface{}{ 1812 b, 1813 chainid, 1814 uint(0), 1815 uint(0), 1816 }) 1817 1818 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1819 assert.Equal(t, nil, err) 1820 1821 tx.SetSignature(sig) 1822 1823 // AccountKeyPicker initialization 1824 p := &AccountKeyPickerForTest{ 1825 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1826 } 1827 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1828 p.SetKey(from, key) 1829 1830 // Validate 1831 _, err = tx.ValidateSender(signer, p, 0) 1832 assert.Equal(t, nil, err) 1833 assert.Equal(t, from, tx.ValidatedSender()) 1834 } 1835 1836 func testValidateFeeDelegatedChainDataAnchoring(t *testing.T) { 1837 // Transaction generation 1838 internalTx := genFeeDelegatedChainDataTransaction().(*TxInternalDataFeeDelegatedChainDataAnchoring) 1839 tx := &Transaction{data: internalTx} 1840 1841 chainid := big.NewInt(1) 1842 signer := LatestSignerForChainID(chainid) 1843 1844 prv, from := defaultTestKey() 1845 internalTx.From = from 1846 1847 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1848 assert.Equal(t, nil, err) 1849 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1850 internalTx.FeePayer = feePayer 1851 1852 // Sign 1853 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), chainid, 0, 0 ]) 1854 b, err := rlp.EncodeToBytes([]interface{}{ 1855 internalTx.Type(), 1856 internalTx.AccountNonce, 1857 internalTx.Price, 1858 internalTx.GasLimit, 1859 internalTx.From, 1860 internalTx.Payload, 1861 }) 1862 assert.Equal(t, nil, err) 1863 1864 h := rlpHash([]interface{}{ 1865 b, 1866 chainid, 1867 uint(0), 1868 uint(0), 1869 }) 1870 1871 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1872 assert.Equal(t, nil, err) 1873 1874 tx.SetSignature(sig) 1875 1876 // Sign fee payer 1877 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), feePayer, chainid, 0, 0 ]) 1878 b, err = rlp.EncodeToBytes([]interface{}{ 1879 internalTx.Type(), 1880 internalTx.AccountNonce, 1881 internalTx.Price, 1882 internalTx.GasLimit, 1883 internalTx.From, 1884 internalTx.Payload, 1885 }) 1886 assert.Equal(t, nil, err) 1887 1888 h = rlpHash([]interface{}{ 1889 b, 1890 feePayer, 1891 chainid, 1892 uint(0), 1893 uint(0), 1894 }) 1895 1896 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1897 assert.Equal(t, nil, err) 1898 1899 tx.SetFeePayerSignatures(feePayerSig) 1900 1901 // AccountKeyPicker initialization 1902 p := &AccountKeyPickerForTest{ 1903 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1904 } 1905 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1906 p.SetKey(from, key) 1907 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1908 p.SetKey(feePayer, feePayerKey) 1909 1910 // Validate 1911 _, err = tx.ValidateSender(signer, p, 0) 1912 assert.Equal(t, nil, err) 1913 assert.Equal(t, from, tx.ValidatedSender()) 1914 1915 // Validate fee payer 1916 _, err = tx.ValidateFeePayer(signer, p, 0) 1917 assert.Equal(t, nil, err) 1918 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1919 } 1920 1921 func testValidateFeeDelegatedChainDataAnchoringWithRatio(t *testing.T) { 1922 // Transaction generation 1923 internalTx := genFeeDelegatedChainDataWithRatioTransaction().(*TxInternalDataFeeDelegatedChainDataAnchoringWithRatio) 1924 tx := &Transaction{data: internalTx} 1925 1926 chainid := big.NewInt(1) 1927 signer := LatestSignerForChainID(chainid) 1928 1929 prv, from := defaultTestKey() 1930 internalTx.From = from 1931 1932 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1933 assert.Equal(t, nil, err) 1934 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1935 internalTx.FeePayer = feePayer 1936 1937 // Sign 1938 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData, FeeRatio]), chainid, 0, 0 ]) 1939 b, err := rlp.EncodeToBytes([]interface{}{ 1940 internalTx.Type(), 1941 internalTx.AccountNonce, 1942 internalTx.Price, 1943 internalTx.GasLimit, 1944 internalTx.From, 1945 internalTx.Payload, 1946 internalTx.FeeRatio, 1947 }) 1948 assert.Equal(t, nil, err) 1949 1950 h := rlpHash([]interface{}{ 1951 b, 1952 chainid, 1953 uint(0), 1954 uint(0), 1955 }) 1956 1957 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1958 assert.Equal(t, nil, err) 1959 1960 tx.SetSignature(sig) 1961 1962 // Sign fee payer 1963 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData, FeeRatio]), feePayer, chainid, 0, 0 ]) 1964 b, err = rlp.EncodeToBytes([]interface{}{ 1965 internalTx.Type(), 1966 internalTx.AccountNonce, 1967 internalTx.Price, 1968 internalTx.GasLimit, 1969 internalTx.From, 1970 internalTx.Payload, 1971 internalTx.FeeRatio, 1972 }) 1973 assert.Equal(t, nil, err) 1974 1975 h = rlpHash([]interface{}{ 1976 b, 1977 feePayer, 1978 chainid, 1979 uint(0), 1980 uint(0), 1981 }) 1982 1983 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1984 assert.Equal(t, nil, err) 1985 1986 tx.SetFeePayerSignatures(feePayerSig) 1987 1988 // AccountKeyPicker initialization 1989 p := &AccountKeyPickerForTest{ 1990 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1991 } 1992 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1993 p.SetKey(from, key) 1994 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1995 p.SetKey(feePayer, feePayerKey) 1996 1997 // Validate 1998 _, err = tx.ValidateSender(signer, p, 0) 1999 assert.Equal(t, nil, err) 2000 assert.Equal(t, from, tx.ValidatedSender()) 2001 2002 // Validate fee payer 2003 _, err = tx.ValidateFeePayer(signer, p, 0) 2004 assert.Equal(t, nil, err) 2005 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 2006 } 2007 2008 func getFunctionName(i interface{}) string { 2009 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() 2010 }