github.com/klaytn/klaytn@v1.10.2/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 t.Run(fnname, func(t *testing.T) { 362 t.Parallel() 363 fn(t) 364 }) 365 } 366 } 367 368 func testValidateValueTransfer(t *testing.T) { 369 // Transaction generation 370 internalTx := genValueTransferTransaction().(*TxInternalDataValueTransfer) 371 tx := &Transaction{data: internalTx} 372 373 chainid := big.NewInt(1) 374 signer := LatestSignerForChainID(chainid) 375 376 prv, from := defaultTestKey() 377 internalTx.From = from 378 379 // Sign 380 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), chainid, 0, 0 ]) 381 b, err := rlp.EncodeToBytes([]interface{}{ 382 internalTx.Type(), 383 internalTx.AccountNonce, 384 internalTx.Price, 385 internalTx.GasLimit, 386 internalTx.Recipient, 387 internalTx.Amount, 388 internalTx.From, 389 }) 390 assert.Equal(t, nil, err) 391 392 h := rlpHash([]interface{}{ 393 b, 394 chainid, 395 uint(0), 396 uint(0), 397 }) 398 399 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 400 assert.Equal(t, nil, err) 401 402 tx.SetSignature(sig) 403 404 // AccountKeyPicker initialization 405 p := &AccountKeyPickerForTest{ 406 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 407 } 408 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 409 p.SetKey(from, key) 410 411 // Validate 412 _, err = tx.ValidateSender(signer, p, 0) 413 assert.Equal(t, nil, err) 414 assert.Equal(t, from, tx.ValidatedSender()) 415 } 416 417 func testValidateFeeDelegatedValueTransfer(t *testing.T) { 418 // Transaction generation 419 internalTx := genFeeDelegatedValueTransferTransaction().(*TxInternalDataFeeDelegatedValueTransfer) 420 tx := &Transaction{data: internalTx} 421 422 chainid := big.NewInt(1) 423 signer := LatestSignerForChainID(chainid) 424 425 prv, from := defaultTestKey() 426 internalTx.From = from 427 428 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 429 assert.Equal(t, nil, err) 430 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 431 internalTx.FeePayer = feePayer 432 433 // Sign 434 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), chainid, 0, 0 ]) 435 b, err := rlp.EncodeToBytes([]interface{}{ 436 internalTx.Type(), 437 internalTx.AccountNonce, 438 internalTx.Price, 439 internalTx.GasLimit, 440 internalTx.Recipient, 441 internalTx.Amount, 442 internalTx.From, 443 }) 444 assert.Equal(t, nil, err) 445 446 h := rlpHash([]interface{}{ 447 b, 448 chainid, 449 uint(0), 450 uint(0), 451 }) 452 453 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 454 assert.Equal(t, nil, err) 455 456 tx.SetSignature(sig) 457 458 // Sign fee payer 459 // encode([ encode([type, nonce, gasPrice, gas, to, value, from]), feePayer, chainid, 0, 0 ]) 460 b, err = rlp.EncodeToBytes([]interface{}{ 461 internalTx.Type(), 462 internalTx.AccountNonce, 463 internalTx.Price, 464 internalTx.GasLimit, 465 internalTx.Recipient, 466 internalTx.Amount, 467 internalTx.From, 468 }) 469 assert.Equal(t, nil, err) 470 471 h = rlpHash([]interface{}{ 472 b, 473 feePayer, 474 chainid, 475 uint(0), 476 uint(0), 477 }) 478 479 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 480 assert.Equal(t, nil, err) 481 482 tx.SetFeePayerSignatures(feePayerSig) 483 484 // AccountKeyPicker initialization 485 p := &AccountKeyPickerForTest{ 486 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 487 } 488 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 489 p.SetKey(from, key) 490 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 491 p.SetKey(feePayer, feePayerKey) 492 493 // Validate 494 _, err = tx.ValidateSender(signer, p, 0) 495 assert.Equal(t, nil, err) 496 assert.Equal(t, from, tx.ValidatedSender()) 497 498 // Validate fee payer 499 _, err = tx.ValidateFeePayer(signer, p, 0) 500 assert.Equal(t, nil, err) 501 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 502 } 503 504 func testValidateFeeDelegatedValueTransferWithRatio(t *testing.T) { 505 // Transaction generation 506 internalTx := genFeeDelegatedValueTransferWithRatioTransaction().(*TxInternalDataFeeDelegatedValueTransferWithRatio) 507 tx := &Transaction{data: internalTx} 508 509 chainid := big.NewInt(1) 510 signer := LatestSignerForChainID(chainid) 511 512 prv, from := defaultTestKey() 513 internalTx.From = from 514 515 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 516 assert.Equal(t, nil, err) 517 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 518 internalTx.FeePayer = feePayer 519 520 // Sign 521 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, feeRatio]), chainid, 0, 0 ]) 522 b, err := rlp.EncodeToBytes([]interface{}{ 523 internalTx.Type(), 524 internalTx.AccountNonce, 525 internalTx.Price, 526 internalTx.GasLimit, 527 internalTx.Recipient, 528 internalTx.Amount, 529 internalTx.From, 530 internalTx.FeeRatio, 531 }) 532 assert.Equal(t, nil, err) 533 534 h := rlpHash([]interface{}{ 535 b, 536 chainid, 537 uint(0), 538 uint(0), 539 }) 540 541 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 542 assert.Equal(t, nil, err) 543 544 tx.SetSignature(sig) 545 546 // Sign fee payer 547 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, feeRatio]), feePayer, chainid, 0, 0 ]) 548 b, err = rlp.EncodeToBytes([]interface{}{ 549 internalTx.Type(), 550 internalTx.AccountNonce, 551 internalTx.Price, 552 internalTx.GasLimit, 553 internalTx.Recipient, 554 internalTx.Amount, 555 internalTx.From, 556 internalTx.FeeRatio, 557 }) 558 assert.Equal(t, nil, err) 559 560 h = rlpHash([]interface{}{ 561 b, 562 feePayer, 563 chainid, 564 uint(0), 565 uint(0), 566 }) 567 568 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 569 assert.Equal(t, nil, err) 570 571 tx.SetFeePayerSignatures(feePayerSig) 572 573 // AccountKeyPicker initialization 574 p := &AccountKeyPickerForTest{ 575 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 576 } 577 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 578 p.SetKey(from, key) 579 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 580 p.SetKey(feePayer, feePayerKey) 581 582 // Validate 583 _, err = tx.ValidateSender(signer, p, 0) 584 assert.Equal(t, nil, err) 585 assert.Equal(t, from, tx.ValidatedSender()) 586 587 // Validate fee payer 588 _, err = tx.ValidateFeePayer(signer, p, 0) 589 assert.Equal(t, nil, err) 590 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 591 } 592 593 func testValidateValueTransferMemo(t *testing.T) { 594 // Transaction generation 595 internalTx := genValueTransferMemoTransaction().(*TxInternalDataValueTransferMemo) 596 tx := &Transaction{data: internalTx} 597 598 chainid := big.NewInt(1) 599 signer := LatestSignerForChainID(chainid) 600 601 prv, from := defaultTestKey() 602 internalTx.From = from 603 604 // Sign 605 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 606 b, err := rlp.EncodeToBytes([]interface{}{ 607 internalTx.Type(), 608 internalTx.AccountNonce, 609 internalTx.Price, 610 internalTx.GasLimit, 611 internalTx.Recipient, 612 internalTx.Amount, 613 internalTx.From, 614 internalTx.Payload, 615 }) 616 assert.Equal(t, nil, err) 617 618 h := rlpHash([]interface{}{ 619 b, 620 chainid, 621 uint(0), 622 uint(0), 623 }) 624 625 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 626 assert.Equal(t, nil, err) 627 628 tx.SetSignature(sig) 629 630 // AccountKeyPicker initialization 631 p := &AccountKeyPickerForTest{ 632 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 633 } 634 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 635 p.SetKey(from, key) 636 637 // Validate 638 _, err = tx.ValidateSender(signer, p, 0) 639 assert.Equal(t, nil, err) 640 assert.Equal(t, from, tx.ValidatedSender()) 641 } 642 643 func testValidateFeeDelegatedValueTransferMemo(t *testing.T) { 644 // Transaction generation 645 internalTx := genFeeDelegatedValueTransferMemoTransaction().(*TxInternalDataFeeDelegatedValueTransferMemo) 646 tx := &Transaction{data: internalTx} 647 648 chainid := big.NewInt(1) 649 signer := LatestSignerForChainID(chainid) 650 651 prv, from := defaultTestKey() 652 internalTx.From = from 653 654 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 655 assert.Equal(t, nil, err) 656 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 657 internalTx.FeePayer = feePayer 658 659 // Sign 660 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 661 b, err := rlp.EncodeToBytes([]interface{}{ 662 internalTx.Type(), 663 internalTx.AccountNonce, 664 internalTx.Price, 665 internalTx.GasLimit, 666 internalTx.Recipient, 667 internalTx.Amount, 668 internalTx.From, 669 internalTx.Payload, 670 }) 671 assert.Equal(t, nil, err) 672 673 h := rlpHash([]interface{}{ 674 b, 675 chainid, 676 uint(0), 677 uint(0), 678 }) 679 680 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 681 assert.Equal(t, nil, err) 682 683 tx.SetSignature(sig) 684 685 // Sign fee payer 686 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), feePayer, chainid, 0, 0 ]) 687 b, err = rlp.EncodeToBytes([]interface{}{ 688 internalTx.Type(), 689 internalTx.AccountNonce, 690 internalTx.Price, 691 internalTx.GasLimit, 692 internalTx.Recipient, 693 internalTx.Amount, 694 internalTx.From, 695 internalTx.Payload, 696 }) 697 assert.Equal(t, nil, err) 698 699 h = rlpHash([]interface{}{ 700 b, 701 feePayer, 702 chainid, 703 uint(0), 704 uint(0), 705 }) 706 707 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 708 assert.Equal(t, nil, err) 709 710 tx.SetFeePayerSignatures(feePayerSig) 711 712 // AccountKeyPicker initialization 713 p := &AccountKeyPickerForTest{ 714 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 715 } 716 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 717 p.SetKey(from, key) 718 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 719 p.SetKey(feePayer, feePayerKey) 720 721 // Validate 722 _, err = tx.ValidateSender(signer, p, 0) 723 assert.Equal(t, nil, err) 724 assert.Equal(t, from, tx.ValidatedSender()) 725 726 // Validate fee payer 727 _, err = tx.ValidateFeePayer(signer, p, 0) 728 assert.Equal(t, nil, err) 729 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 730 } 731 732 func testValidateFeeDelegatedValueTransferMemoWithRatio(t *testing.T) { 733 // Transaction generation 734 internalTx := genFeeDelegatedValueTransferMemoWithRatioTransaction().(*TxInternalDataFeeDelegatedValueTransferMemoWithRatio) 735 tx := &Transaction{data: internalTx} 736 737 chainid := big.NewInt(1) 738 signer := LatestSignerForChainID(chainid) 739 740 prv, from := defaultTestKey() 741 internalTx.From = from 742 743 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 744 assert.Equal(t, nil, err) 745 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 746 internalTx.FeePayer = feePayer 747 748 // Sign 749 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), chainid, 0, 0 ]) 750 b, err := rlp.EncodeToBytes([]interface{}{ 751 internalTx.Type(), 752 internalTx.AccountNonce, 753 internalTx.Price, 754 internalTx.GasLimit, 755 internalTx.Recipient, 756 internalTx.Amount, 757 internalTx.From, 758 internalTx.Payload, 759 internalTx.FeeRatio, 760 }) 761 assert.Equal(t, nil, err) 762 763 h := rlpHash([]interface{}{ 764 b, 765 chainid, 766 uint(0), 767 uint(0), 768 }) 769 770 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 771 assert.Equal(t, nil, err) 772 773 tx.SetSignature(sig) 774 775 // Sign fee payer 776 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), feePayer, chainid, 0, 0 ]) 777 b, err = rlp.EncodeToBytes([]interface{}{ 778 internalTx.Type(), 779 internalTx.AccountNonce, 780 internalTx.Price, 781 internalTx.GasLimit, 782 internalTx.Recipient, 783 internalTx.Amount, 784 internalTx.From, 785 internalTx.Payload, 786 internalTx.FeeRatio, 787 }) 788 assert.Equal(t, nil, err) 789 790 h = rlpHash([]interface{}{ 791 b, 792 feePayer, 793 chainid, 794 uint(0), 795 uint(0), 796 }) 797 798 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 799 assert.Equal(t, nil, err) 800 801 tx.SetFeePayerSignatures(feePayerSig) 802 803 // AccountKeyPicker initialization 804 p := &AccountKeyPickerForTest{ 805 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 806 } 807 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 808 p.SetKey(from, key) 809 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 810 p.SetKey(feePayer, feePayerKey) 811 812 // Validate 813 _, err = tx.ValidateSender(signer, p, 0) 814 assert.Equal(t, nil, err) 815 assert.Equal(t, from, tx.ValidatedSender()) 816 817 // Validate fee payer 818 _, err = tx.ValidateFeePayer(signer, p, 0) 819 assert.Equal(t, nil, err) 820 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 821 } 822 823 //func testValidateAccountCreation(t *testing.T) { 824 // // Transaction generation 825 // internalTx := genAccountCreationTransaction().(*TxInternalDataAccountCreation) 826 // tx := &Transaction{data: internalTx} 827 // 828 // chainid := big.NewInt(1) 829 // signer := NewEIP155Signer(chainid) 830 // 831 // prv, from := defaultTestKey() 832 // internalTx.From = from 833 // 834 // // Sign 835 // // encode([ encode([type, nonce, gasPrice, gas, to, value, from, humanReadable, encodedKey]), chainid, 0, 0 ]) 836 // encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 837 // assert.Equal(t, nil, err) 838 // b, err := rlp.EncodeToBytes([]interface{}{ 839 // internalTx.Type(), 840 // internalTx.AccountNonce, 841 // internalTx.Price, 842 // internalTx.GasLimit, 843 // internalTx.Recipient, 844 // internalTx.Amount, 845 // internalTx.From, 846 // internalTx.HumanReadable, 847 // encodedKey, 848 // }) 849 // assert.Equal(t, nil, err) 850 // 851 // h := rlpHash([]interface{}{ 852 // b, 853 // chainid, 854 // uint(0), 855 // uint(0), 856 // }) 857 // 858 // sig, err := NewTxSignaturesWithValues(signer, h, []*ecdsa.PrivateKey{prv}) 859 // assert.Equal(t, nil, err) 860 // 861 // tx.SetSignature(sig) 862 // 863 // // AccountKeyPicker initialization 864 // p := &AccountKeyPickerForTest{ 865 // AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 866 // } 867 // key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 868 // p.SetKey(from, key) 869 // 870 // // Validate 871 // _, err = tx.ValidateSender(signer, p, 0) 872 // assert.Equal(t, nil, err) 873 // assert.Equal(t, from, tx.ValidatedSender()) 874 //} 875 876 func testValidateAccountUpdate(t *testing.T) { 877 // Transaction generation 878 internalTx := genAccountUpdateTransaction().(*TxInternalDataAccountUpdate) 879 tx := &Transaction{data: internalTx} 880 881 chainid := big.NewInt(1) 882 signer := LatestSignerForChainID(chainid) 883 884 prv, from := defaultTestKey() 885 internalTx.From = from 886 887 // Sign 888 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), chainid, 0, 0 ]) 889 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 890 assert.Equal(t, nil, err) 891 b, err := rlp.EncodeToBytes([]interface{}{ 892 internalTx.Type(), 893 internalTx.AccountNonce, 894 internalTx.Price, 895 internalTx.GasLimit, 896 internalTx.From, 897 encodedKey, 898 }) 899 assert.Equal(t, nil, err) 900 901 h := rlpHash([]interface{}{ 902 b, 903 chainid, 904 uint(0), 905 uint(0), 906 }) 907 908 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 909 assert.Equal(t, nil, err) 910 911 tx.SetSignature(sig) 912 913 // AccountKeyPicker initialization 914 p := &AccountKeyPickerForTest{ 915 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 916 } 917 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 918 p.SetKey(from, key) 919 920 // Validate 921 _, err = tx.ValidateSender(signer, p, 0) 922 assert.Equal(t, nil, err) 923 assert.Equal(t, from, tx.ValidatedSender()) 924 } 925 926 func testValidateFeeDelegatedAccountUpdate(t *testing.T) { 927 // Transaction generation 928 internalTx := genFeeDelegatedAccountUpdateTransaction().(*TxInternalDataFeeDelegatedAccountUpdate) 929 tx := &Transaction{data: internalTx} 930 931 chainid := big.NewInt(1) 932 signer := LatestSignerForChainID(chainid) 933 934 prv, from := defaultTestKey() 935 internalTx.From = from 936 937 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 938 assert.Equal(t, nil, err) 939 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 940 internalTx.FeePayer = feePayer 941 942 // Sign 943 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), chainid, 0, 0 ]) 944 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 945 assert.Equal(t, nil, err) 946 b, err := rlp.EncodeToBytes([]interface{}{ 947 internalTx.Type(), 948 internalTx.AccountNonce, 949 internalTx.Price, 950 internalTx.GasLimit, 951 internalTx.From, 952 encodedKey, 953 }) 954 assert.Equal(t, nil, err) 955 956 h := rlpHash([]interface{}{ 957 b, 958 chainid, 959 uint(0), 960 uint(0), 961 }) 962 963 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 964 assert.Equal(t, nil, err) 965 966 tx.SetSignature(sig) 967 968 // Sign fee payer 969 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey]), feePayer, chainid, 0, 0 ]) 970 encodedKey, err = rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 971 assert.Equal(t, nil, err) 972 b, err = rlp.EncodeToBytes([]interface{}{ 973 internalTx.Type(), 974 internalTx.AccountNonce, 975 internalTx.Price, 976 internalTx.GasLimit, 977 internalTx.From, 978 encodedKey, 979 }) 980 assert.Equal(t, nil, err) 981 982 h = rlpHash([]interface{}{ 983 b, 984 feePayer, 985 chainid, 986 uint(0), 987 uint(0), 988 }) 989 990 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 991 assert.Equal(t, nil, err) 992 993 tx.SetFeePayerSignatures(feePayerSig) 994 995 // AccountKeyPicker initialization 996 p := &AccountKeyPickerForTest{ 997 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 998 } 999 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1000 p.SetKey(from, key) 1001 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1002 p.SetKey(feePayer, feePayerKey) 1003 1004 // Validate 1005 _, err = tx.ValidateSender(signer, p, 0) 1006 assert.Equal(t, nil, err) 1007 assert.Equal(t, from, tx.ValidatedSender()) 1008 1009 // Validate fee payer 1010 _, err = tx.ValidateFeePayer(signer, p, 0) 1011 assert.Equal(t, nil, err) 1012 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1013 } 1014 1015 func testValidateFeeDelegatedAccountUpdateWithRatio(t *testing.T) { 1016 // Transaction generation 1017 internalTx := genFeeDelegatedAccountUpdateWithRatioTransaction().(*TxInternalDataFeeDelegatedAccountUpdateWithRatio) 1018 tx := &Transaction{data: internalTx} 1019 1020 chainid := big.NewInt(1) 1021 signer := LatestSignerForChainID(chainid) 1022 1023 prv, from := defaultTestKey() 1024 internalTx.From = from 1025 1026 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1027 assert.Equal(t, nil, err) 1028 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1029 internalTx.FeePayer = feePayer 1030 1031 // Sign 1032 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey, feeRatio]), chainid, 0, 0 ]) 1033 encodedKey, err := rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 1034 assert.Equal(t, nil, err) 1035 b, err := rlp.EncodeToBytes([]interface{}{ 1036 internalTx.Type(), 1037 internalTx.AccountNonce, 1038 internalTx.Price, 1039 internalTx.GasLimit, 1040 internalTx.From, 1041 encodedKey, 1042 internalTx.FeeRatio, 1043 }) 1044 assert.Equal(t, nil, err) 1045 1046 h := rlpHash([]interface{}{ 1047 b, 1048 chainid, 1049 uint(0), 1050 uint(0), 1051 }) 1052 1053 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1054 assert.Equal(t, nil, err) 1055 1056 tx.SetSignature(sig) 1057 1058 // Sign fee payer 1059 // encode([ encode([type, nonce, gasPrice, gas, from, encodedKey, feeRatio]), feePayer, chainid, 0, 0 ]) 1060 encodedKey, err = rlp.EncodeToBytes(accountkey.NewAccountKeySerializerWithAccountKey(internalTx.Key)) 1061 assert.Equal(t, nil, err) 1062 b, err = rlp.EncodeToBytes([]interface{}{ 1063 internalTx.Type(), 1064 internalTx.AccountNonce, 1065 internalTx.Price, 1066 internalTx.GasLimit, 1067 internalTx.From, 1068 encodedKey, 1069 internalTx.FeeRatio, 1070 }) 1071 assert.Equal(t, nil, err) 1072 1073 h = rlpHash([]interface{}{ 1074 b, 1075 feePayer, 1076 chainid, 1077 uint(0), 1078 uint(0), 1079 }) 1080 1081 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1082 assert.Equal(t, nil, err) 1083 1084 tx.SetFeePayerSignatures(feePayerSig) 1085 1086 // AccountKeyPicker initialization 1087 p := &AccountKeyPickerForTest{ 1088 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1089 } 1090 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1091 p.SetKey(from, key) 1092 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1093 p.SetKey(feePayer, feePayerKey) 1094 1095 // Validate 1096 _, err = tx.ValidateSender(signer, p, 0) 1097 assert.Equal(t, nil, err) 1098 assert.Equal(t, from, tx.ValidatedSender()) 1099 1100 // Validate fee payer 1101 _, err = tx.ValidateFeePayer(signer, p, 0) 1102 assert.Equal(t, nil, err) 1103 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1104 } 1105 1106 func testValidateSmartContractDeploy(t *testing.T) { 1107 // Transaction generation 1108 internalTx := genSmartContractDeployTransaction().(*TxInternalDataSmartContractDeploy) 1109 tx := &Transaction{data: internalTx} 1110 1111 chainid := big.NewInt(1) 1112 signer := LatestSignerForChainID(chainid) 1113 1114 prv, from := defaultTestKey() 1115 internalTx.From = from 1116 1117 // Sign 1118 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), chainid, 0, 0 ]) 1119 b, err := rlp.EncodeToBytes([]interface{}{ 1120 internalTx.Type(), 1121 internalTx.AccountNonce, 1122 internalTx.Price, 1123 internalTx.GasLimit, 1124 internalTx.Recipient, 1125 internalTx.Amount, 1126 internalTx.From, 1127 internalTx.Payload, 1128 internalTx.HumanReadable, 1129 }) 1130 assert.Equal(t, nil, err) 1131 1132 h := rlpHash([]interface{}{ 1133 b, 1134 chainid, 1135 uint(0), 1136 uint(0), 1137 }) 1138 1139 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1140 assert.Equal(t, nil, err) 1141 1142 tx.SetSignature(sig) 1143 1144 // AccountKeyPicker initialization 1145 p := &AccountKeyPickerForTest{ 1146 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1147 } 1148 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1149 p.SetKey(from, key) 1150 1151 // Validate 1152 _, err = tx.ValidateSender(signer, p, 0) 1153 assert.Equal(t, nil, err) 1154 assert.Equal(t, from, tx.ValidatedSender()) 1155 } 1156 1157 func testValidateFeeDelegatedSmartContractDeploy(t *testing.T) { 1158 // Transaction generation 1159 internalTx := genFeeDelegatedSmartContractDeployTransaction().(*TxInternalDataFeeDelegatedSmartContractDeploy) 1160 tx := &Transaction{data: internalTx} 1161 1162 chainid := big.NewInt(1) 1163 signer := LatestSignerForChainID(chainid) 1164 1165 prv, from := defaultTestKey() 1166 internalTx.From = from 1167 1168 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1169 assert.Equal(t, nil, err) 1170 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1171 internalTx.FeePayer = feePayer 1172 1173 // Sign 1174 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), chainid, 0, 0 ]) 1175 b, err := rlp.EncodeToBytes([]interface{}{ 1176 internalTx.Type(), 1177 internalTx.AccountNonce, 1178 internalTx.Price, 1179 internalTx.GasLimit, 1180 internalTx.Recipient, 1181 internalTx.Amount, 1182 internalTx.From, 1183 internalTx.Payload, 1184 internalTx.HumanReadable, 1185 }) 1186 assert.Equal(t, nil, err) 1187 1188 h := rlpHash([]interface{}{ 1189 b, 1190 chainid, 1191 uint(0), 1192 uint(0), 1193 }) 1194 1195 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1196 assert.Equal(t, nil, err) 1197 1198 tx.SetSignature(sig) 1199 1200 // Sign fee payer 1201 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable]), feePayer, chainid, 0, 0 ]) 1202 b, err = rlp.EncodeToBytes([]interface{}{ 1203 internalTx.Type(), 1204 internalTx.AccountNonce, 1205 internalTx.Price, 1206 internalTx.GasLimit, 1207 internalTx.Recipient, 1208 internalTx.Amount, 1209 internalTx.From, 1210 internalTx.Payload, 1211 internalTx.HumanReadable, 1212 }) 1213 assert.Equal(t, nil, err) 1214 1215 h = rlpHash([]interface{}{ 1216 b, 1217 feePayer, 1218 chainid, 1219 uint(0), 1220 uint(0), 1221 }) 1222 1223 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1224 assert.Equal(t, nil, err) 1225 1226 tx.SetFeePayerSignatures(feePayerSig) 1227 1228 // AccountKeyPicker initialization 1229 p := &AccountKeyPickerForTest{ 1230 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1231 } 1232 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1233 p.SetKey(from, key) 1234 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1235 p.SetKey(feePayer, feePayerKey) 1236 1237 // Validate 1238 _, err = tx.ValidateSender(signer, p, 0) 1239 assert.Equal(t, nil, err) 1240 assert.Equal(t, from, tx.ValidatedSender()) 1241 1242 // Validate fee payer 1243 _, err = tx.ValidateFeePayer(signer, p, 0) 1244 assert.Equal(t, nil, err) 1245 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1246 } 1247 1248 func testValidateFeeDelegatedSmartContractDeployWithRatio(t *testing.T) { 1249 // Transaction generation 1250 internalTx := genFeeDelegatedSmartContractDeployWithRatioTransaction().(*TxInternalDataFeeDelegatedSmartContractDeployWithRatio) 1251 tx := &Transaction{data: internalTx} 1252 1253 chainid := big.NewInt(1) 1254 signer := LatestSignerForChainID(chainid) 1255 1256 prv, from := defaultTestKey() 1257 internalTx.From = from 1258 1259 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1260 assert.Equal(t, nil, err) 1261 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1262 internalTx.FeePayer = feePayer 1263 1264 // Sign 1265 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable, feeRatio]), chainid, 0, 0 ]) 1266 b, err := rlp.EncodeToBytes([]interface{}{ 1267 internalTx.Type(), 1268 internalTx.AccountNonce, 1269 internalTx.Price, 1270 internalTx.GasLimit, 1271 internalTx.Recipient, 1272 internalTx.Amount, 1273 internalTx.From, 1274 internalTx.Payload, 1275 internalTx.HumanReadable, 1276 internalTx.FeeRatio, 1277 }) 1278 assert.Equal(t, nil, err) 1279 1280 h := rlpHash([]interface{}{ 1281 b, 1282 chainid, 1283 uint(0), 1284 uint(0), 1285 }) 1286 1287 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1288 assert.Equal(t, nil, err) 1289 1290 tx.SetSignature(sig) 1291 1292 // Sign fee payer 1293 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, humanReadable, feeRatio]), feePayer, chainid, 0, 0 ]) 1294 b, err = rlp.EncodeToBytes([]interface{}{ 1295 internalTx.Type(), 1296 internalTx.AccountNonce, 1297 internalTx.Price, 1298 internalTx.GasLimit, 1299 internalTx.Recipient, 1300 internalTx.Amount, 1301 internalTx.From, 1302 internalTx.Payload, 1303 internalTx.HumanReadable, 1304 internalTx.FeeRatio, 1305 }) 1306 assert.Equal(t, nil, err) 1307 1308 h = rlpHash([]interface{}{ 1309 b, 1310 feePayer, 1311 chainid, 1312 uint(0), 1313 uint(0), 1314 }) 1315 1316 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1317 assert.Equal(t, nil, err) 1318 1319 tx.SetFeePayerSignatures(feePayerSig) 1320 1321 // AccountKeyPicker initialization 1322 p := &AccountKeyPickerForTest{ 1323 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1324 } 1325 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1326 p.SetKey(from, key) 1327 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1328 p.SetKey(feePayer, feePayerKey) 1329 1330 // Validate 1331 _, err = tx.ValidateSender(signer, p, 0) 1332 assert.Equal(t, nil, err) 1333 assert.Equal(t, from, tx.ValidatedSender()) 1334 1335 // Validate fee payer 1336 _, err = tx.ValidateFeePayer(signer, p, 0) 1337 assert.Equal(t, nil, err) 1338 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1339 } 1340 1341 func testValidateSmartContractExecution(t *testing.T) { 1342 // Transaction generation 1343 internalTx := genSmartContractExecutionTransaction().(*TxInternalDataSmartContractExecution) 1344 tx := &Transaction{data: internalTx} 1345 1346 chainid := big.NewInt(1) 1347 signer := LatestSignerForChainID(chainid) 1348 1349 prv, from := defaultTestKey() 1350 internalTx.From = from 1351 1352 // Sign 1353 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 1354 b, err := rlp.EncodeToBytes([]interface{}{ 1355 internalTx.Type(), 1356 internalTx.AccountNonce, 1357 internalTx.Price, 1358 internalTx.GasLimit, 1359 internalTx.Recipient, 1360 internalTx.Amount, 1361 internalTx.From, 1362 internalTx.Payload, 1363 }) 1364 assert.Equal(t, nil, err) 1365 1366 h := rlpHash([]interface{}{ 1367 b, 1368 chainid, 1369 uint(0), 1370 uint(0), 1371 }) 1372 1373 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1374 assert.Equal(t, nil, err) 1375 1376 tx.SetSignature(sig) 1377 1378 // AccountKeyPicker initialization 1379 p := &AccountKeyPickerForTest{ 1380 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1381 } 1382 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1383 p.SetKey(from, key) 1384 1385 // Validate 1386 _, err = tx.ValidateSender(signer, p, 0) 1387 assert.Equal(t, nil, err) 1388 assert.Equal(t, from, tx.ValidatedSender()) 1389 } 1390 1391 func testValidateFeeDelegatedSmartContractExecution(t *testing.T) { 1392 // Transaction generation 1393 internalTx := genFeeDelegatedSmartContractExecutionTransaction().(*TxInternalDataFeeDelegatedSmartContractExecution) 1394 tx := &Transaction{data: internalTx} 1395 1396 chainid := big.NewInt(1) 1397 signer := LatestSignerForChainID(chainid) 1398 1399 prv, from := defaultTestKey() 1400 internalTx.From = from 1401 1402 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1403 assert.Equal(t, nil, err) 1404 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1405 internalTx.FeePayer = feePayer 1406 1407 // Sign 1408 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), chainid, 0, 0 ]) 1409 b, err := rlp.EncodeToBytes([]interface{}{ 1410 internalTx.Type(), 1411 internalTx.AccountNonce, 1412 internalTx.Price, 1413 internalTx.GasLimit, 1414 internalTx.Recipient, 1415 internalTx.Amount, 1416 internalTx.From, 1417 internalTx.Payload, 1418 }) 1419 assert.Equal(t, nil, err) 1420 1421 h := rlpHash([]interface{}{ 1422 b, 1423 chainid, 1424 uint(0), 1425 uint(0), 1426 }) 1427 1428 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1429 assert.Equal(t, nil, err) 1430 1431 tx.SetSignature(sig) 1432 1433 // Sign fee payer 1434 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload]), feePayer, chainid, 0, 0 ]) 1435 b, err = rlp.EncodeToBytes([]interface{}{ 1436 internalTx.Type(), 1437 internalTx.AccountNonce, 1438 internalTx.Price, 1439 internalTx.GasLimit, 1440 internalTx.Recipient, 1441 internalTx.Amount, 1442 internalTx.From, 1443 internalTx.Payload, 1444 }) 1445 assert.Equal(t, nil, err) 1446 1447 h = rlpHash([]interface{}{ 1448 b, 1449 feePayer, 1450 chainid, 1451 uint(0), 1452 uint(0), 1453 }) 1454 1455 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1456 assert.Equal(t, nil, err) 1457 1458 tx.SetFeePayerSignatures(feePayerSig) 1459 1460 // AccountKeyPicker initialization 1461 p := &AccountKeyPickerForTest{ 1462 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1463 } 1464 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1465 p.SetKey(from, key) 1466 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1467 p.SetKey(feePayer, feePayerKey) 1468 1469 // Validate 1470 _, err = tx.ValidateSender(signer, p, 0) 1471 assert.Equal(t, nil, err) 1472 assert.Equal(t, from, tx.ValidatedSender()) 1473 1474 // Validate fee payer 1475 _, err = tx.ValidateFeePayer(signer, p, 0) 1476 assert.Equal(t, nil, err) 1477 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1478 } 1479 1480 func testValidateFeeDelegatedSmartContractExecutionWithRatio(t *testing.T) { 1481 // Transaction generation 1482 internalTx := genFeeDelegatedSmartContractExecutionWithRatioTransaction().(*TxInternalDataFeeDelegatedSmartContractExecutionWithRatio) 1483 tx := &Transaction{data: internalTx} 1484 1485 chainid := big.NewInt(1) 1486 signer := LatestSignerForChainID(chainid) 1487 1488 prv, from := defaultTestKey() 1489 internalTx.From = from 1490 1491 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1492 assert.Equal(t, nil, err) 1493 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1494 internalTx.FeePayer = feePayer 1495 1496 // Sign 1497 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), chainid, 0, 0 ]) 1498 b, err := rlp.EncodeToBytes([]interface{}{ 1499 internalTx.Type(), 1500 internalTx.AccountNonce, 1501 internalTx.Price, 1502 internalTx.GasLimit, 1503 internalTx.Recipient, 1504 internalTx.Amount, 1505 internalTx.From, 1506 internalTx.Payload, 1507 internalTx.FeeRatio, 1508 }) 1509 assert.Equal(t, nil, err) 1510 1511 h := rlpHash([]interface{}{ 1512 b, 1513 chainid, 1514 uint(0), 1515 uint(0), 1516 }) 1517 1518 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1519 assert.Equal(t, nil, err) 1520 1521 tx.SetSignature(sig) 1522 1523 // Sign fee payer 1524 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, payload, feeRatio]), feePayer, chainid, 0, 0 ]) 1525 b, err = rlp.EncodeToBytes([]interface{}{ 1526 internalTx.Type(), 1527 internalTx.AccountNonce, 1528 internalTx.Price, 1529 internalTx.GasLimit, 1530 internalTx.Recipient, 1531 internalTx.Amount, 1532 internalTx.From, 1533 internalTx.Payload, 1534 internalTx.FeeRatio, 1535 }) 1536 assert.Equal(t, nil, err) 1537 1538 h = rlpHash([]interface{}{ 1539 b, 1540 feePayer, 1541 chainid, 1542 uint(0), 1543 uint(0), 1544 }) 1545 1546 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1547 assert.Equal(t, nil, err) 1548 1549 tx.SetFeePayerSignatures(feePayerSig) 1550 1551 // AccountKeyPicker initialization 1552 p := &AccountKeyPickerForTest{ 1553 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1554 } 1555 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1556 p.SetKey(from, key) 1557 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1558 p.SetKey(feePayer, feePayerKey) 1559 1560 // Validate 1561 _, err = tx.ValidateSender(signer, p, 0) 1562 assert.Equal(t, nil, err) 1563 assert.Equal(t, from, tx.ValidatedSender()) 1564 1565 // Validate fee payer 1566 _, err = tx.ValidateFeePayer(signer, p, 0) 1567 assert.Equal(t, nil, err) 1568 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1569 } 1570 1571 func testValidateCancel(t *testing.T) { 1572 // Transaction generation 1573 internalTx := genCancelTransaction().(*TxInternalDataCancel) 1574 tx := &Transaction{data: internalTx} 1575 1576 chainid := big.NewInt(1) 1577 signer := LatestSignerForChainID(chainid) 1578 1579 prv, from := defaultTestKey() 1580 internalTx.From = from 1581 1582 // Sign 1583 // encode([ encode([type, nonce, gasPrice, gas, from]), chainid, 0, 0 ]) 1584 b, err := rlp.EncodeToBytes([]interface{}{ 1585 internalTx.Type(), 1586 internalTx.AccountNonce, 1587 internalTx.Price, 1588 internalTx.GasLimit, 1589 internalTx.From, 1590 }) 1591 assert.Equal(t, nil, err) 1592 1593 h := rlpHash([]interface{}{ 1594 b, 1595 chainid, 1596 uint(0), 1597 uint(0), 1598 }) 1599 1600 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1601 assert.Equal(t, nil, err) 1602 1603 tx.SetSignature(sig) 1604 1605 // AccountKeyPicker initialization 1606 p := &AccountKeyPickerForTest{ 1607 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1608 } 1609 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1610 p.SetKey(from, key) 1611 1612 // Validate 1613 _, err = tx.ValidateSender(signer, p, 0) 1614 assert.Equal(t, nil, err) 1615 assert.Equal(t, from, tx.ValidatedSender()) 1616 } 1617 1618 func testValidateFeeDelegatedCancel(t *testing.T) { 1619 // Transaction generation 1620 internalTx := genFeeDelegatedCancelTransaction().(*TxInternalDataFeeDelegatedCancel) 1621 tx := &Transaction{data: internalTx} 1622 1623 chainid := big.NewInt(1) 1624 signer := LatestSignerForChainID(chainid) 1625 1626 prv, from := defaultTestKey() 1627 internalTx.From = from 1628 1629 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1630 assert.Equal(t, nil, err) 1631 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1632 internalTx.FeePayer = feePayer 1633 1634 // Sign 1635 // encode([ encode([type, nonce, gasPrice, gas, from]), chainid, 0, 0 ]) 1636 b, err := rlp.EncodeToBytes([]interface{}{ 1637 internalTx.Type(), 1638 internalTx.AccountNonce, 1639 internalTx.Price, 1640 internalTx.GasLimit, 1641 internalTx.From, 1642 }) 1643 assert.Equal(t, nil, err) 1644 1645 h := rlpHash([]interface{}{ 1646 b, 1647 chainid, 1648 uint(0), 1649 uint(0), 1650 }) 1651 1652 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1653 assert.Equal(t, nil, err) 1654 1655 tx.SetSignature(sig) 1656 1657 // Sign fee payer 1658 // encode([ encode([type, nonce, gasPrice, gas, from]), feePayer, chainid, 0, 0 ]) 1659 b, err = rlp.EncodeToBytes([]interface{}{ 1660 internalTx.Type(), 1661 internalTx.AccountNonce, 1662 internalTx.Price, 1663 internalTx.GasLimit, 1664 internalTx.From, 1665 }) 1666 assert.Equal(t, nil, err) 1667 1668 h = rlpHash([]interface{}{ 1669 b, 1670 feePayer, 1671 chainid, 1672 uint(0), 1673 uint(0), 1674 }) 1675 1676 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1677 assert.Equal(t, nil, err) 1678 1679 tx.SetFeePayerSignatures(feePayerSig) 1680 1681 // AccountKeyPicker initialization 1682 p := &AccountKeyPickerForTest{ 1683 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1684 } 1685 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1686 p.SetKey(from, key) 1687 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1688 p.SetKey(feePayer, feePayerKey) 1689 1690 // Validate 1691 _, err = tx.ValidateSender(signer, p, 0) 1692 assert.Equal(t, nil, err) 1693 assert.Equal(t, from, tx.ValidatedSender()) 1694 1695 // Validate fee payer 1696 _, err = tx.ValidateFeePayer(signer, p, 0) 1697 assert.Equal(t, nil, err) 1698 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1699 } 1700 1701 func testValidateFeeDelegatedCancelWithRatio(t *testing.T) { 1702 // Transaction generation 1703 internalTx := genFeeDelegatedCancelWithRatioTransaction().(*TxInternalDataFeeDelegatedCancelWithRatio) 1704 tx := &Transaction{data: internalTx} 1705 1706 chainid := big.NewInt(1) 1707 signer := LatestSignerForChainID(chainid) 1708 1709 prv, from := defaultTestKey() 1710 internalTx.From = from 1711 1712 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1713 assert.Equal(t, nil, err) 1714 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1715 internalTx.FeePayer = feePayer 1716 1717 // Sign 1718 // encode([ encode([type, nonce, gasPrice, gas, from, feeRatio]), chainid, 0, 0 ]) 1719 b, err := rlp.EncodeToBytes([]interface{}{ 1720 internalTx.Type(), 1721 internalTx.AccountNonce, 1722 internalTx.Price, 1723 internalTx.GasLimit, 1724 internalTx.From, 1725 internalTx.FeeRatio, 1726 }) 1727 assert.Equal(t, nil, err) 1728 1729 h := rlpHash([]interface{}{ 1730 b, 1731 chainid, 1732 uint(0), 1733 uint(0), 1734 }) 1735 1736 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1737 assert.Equal(t, nil, err) 1738 1739 tx.SetSignature(sig) 1740 1741 // Sign fee payer 1742 // encode([ encode([type, nonce, gasPrice, gas, from, feeRatio]), feePayer, chainid, 0, 0 ]) 1743 b, err = rlp.EncodeToBytes([]interface{}{ 1744 internalTx.Type(), 1745 internalTx.AccountNonce, 1746 internalTx.Price, 1747 internalTx.GasLimit, 1748 internalTx.From, 1749 internalTx.FeeRatio, 1750 }) 1751 assert.Equal(t, nil, err) 1752 1753 h = rlpHash([]interface{}{ 1754 b, 1755 feePayer, 1756 chainid, 1757 uint(0), 1758 uint(0), 1759 }) 1760 1761 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1762 assert.Equal(t, nil, err) 1763 1764 tx.SetFeePayerSignatures(feePayerSig) 1765 1766 // AccountKeyPicker initialization 1767 p := &AccountKeyPickerForTest{ 1768 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1769 } 1770 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1771 p.SetKey(from, key) 1772 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1773 p.SetKey(feePayer, feePayerKey) 1774 1775 // Validate 1776 _, err = tx.ValidateSender(signer, p, 0) 1777 assert.Equal(t, nil, err) 1778 assert.Equal(t, from, tx.ValidatedSender()) 1779 1780 // Validate fee payer 1781 _, err = tx.ValidateFeePayer(signer, p, 0) 1782 assert.Equal(t, nil, err) 1783 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1784 } 1785 1786 func testValidateChainDataAnchoring(t *testing.T) { 1787 // Transaction generation 1788 internalTx := genChainDataTransaction().(*TxInternalDataChainDataAnchoring) 1789 tx := &Transaction{data: internalTx} 1790 1791 chainid := big.NewInt(1) 1792 signer := LatestSignerForChainID(chainid) 1793 1794 prv, from := defaultTestKey() 1795 internalTx.From = from 1796 1797 // Sign 1798 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), chainid, 0, 0 ]) 1799 b, err := rlp.EncodeToBytes([]interface{}{ 1800 internalTx.Type(), 1801 internalTx.AccountNonce, 1802 internalTx.Price, 1803 internalTx.GasLimit, 1804 internalTx.From, 1805 internalTx.Payload, 1806 }) 1807 assert.Equal(t, nil, err) 1808 1809 h := rlpHash([]interface{}{ 1810 b, 1811 chainid, 1812 uint(0), 1813 uint(0), 1814 }) 1815 1816 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1817 assert.Equal(t, nil, err) 1818 1819 tx.SetSignature(sig) 1820 1821 // AccountKeyPicker initialization 1822 p := &AccountKeyPickerForTest{ 1823 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1824 } 1825 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1826 p.SetKey(from, key) 1827 1828 // Validate 1829 _, err = tx.ValidateSender(signer, p, 0) 1830 assert.Equal(t, nil, err) 1831 assert.Equal(t, from, tx.ValidatedSender()) 1832 } 1833 1834 func testValidateFeeDelegatedChainDataAnchoring(t *testing.T) { 1835 // Transaction generation 1836 internalTx := genFeeDelegatedChainDataTransaction().(*TxInternalDataFeeDelegatedChainDataAnchoring) 1837 tx := &Transaction{data: internalTx} 1838 1839 chainid := big.NewInt(1) 1840 signer := LatestSignerForChainID(chainid) 1841 1842 prv, from := defaultTestKey() 1843 internalTx.From = from 1844 1845 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1846 assert.Equal(t, nil, err) 1847 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1848 internalTx.FeePayer = feePayer 1849 1850 // Sign 1851 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), chainid, 0, 0 ]) 1852 b, err := rlp.EncodeToBytes([]interface{}{ 1853 internalTx.Type(), 1854 internalTx.AccountNonce, 1855 internalTx.Price, 1856 internalTx.GasLimit, 1857 internalTx.From, 1858 internalTx.Payload, 1859 }) 1860 assert.Equal(t, nil, err) 1861 1862 h := rlpHash([]interface{}{ 1863 b, 1864 chainid, 1865 uint(0), 1866 uint(0), 1867 }) 1868 1869 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1870 assert.Equal(t, nil, err) 1871 1872 tx.SetSignature(sig) 1873 1874 // Sign fee payer 1875 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData]), feePayer, chainid, 0, 0 ]) 1876 b, err = rlp.EncodeToBytes([]interface{}{ 1877 internalTx.Type(), 1878 internalTx.AccountNonce, 1879 internalTx.Price, 1880 internalTx.GasLimit, 1881 internalTx.From, 1882 internalTx.Payload, 1883 }) 1884 assert.Equal(t, nil, err) 1885 1886 h = rlpHash([]interface{}{ 1887 b, 1888 feePayer, 1889 chainid, 1890 uint(0), 1891 uint(0), 1892 }) 1893 1894 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1895 assert.Equal(t, nil, err) 1896 1897 tx.SetFeePayerSignatures(feePayerSig) 1898 1899 // AccountKeyPicker initialization 1900 p := &AccountKeyPickerForTest{ 1901 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1902 } 1903 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1904 p.SetKey(from, key) 1905 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1906 p.SetKey(feePayer, feePayerKey) 1907 1908 // Validate 1909 _, err = tx.ValidateSender(signer, p, 0) 1910 assert.Equal(t, nil, err) 1911 assert.Equal(t, from, tx.ValidatedSender()) 1912 1913 // Validate fee payer 1914 _, err = tx.ValidateFeePayer(signer, p, 0) 1915 assert.Equal(t, nil, err) 1916 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 1917 } 1918 1919 func testValidateFeeDelegatedChainDataAnchoringWithRatio(t *testing.T) { 1920 // Transaction generation 1921 internalTx := genFeeDelegatedChainDataWithRatioTransaction().(*TxInternalDataFeeDelegatedChainDataAnchoringWithRatio) 1922 tx := &Transaction{data: internalTx} 1923 1924 chainid := big.NewInt(1) 1925 signer := LatestSignerForChainID(chainid) 1926 1927 prv, from := defaultTestKey() 1928 internalTx.From = from 1929 1930 feePayerPrv, err := crypto.HexToECDSA("b9d5558443585bca6f225b935950e3f6e69f9da8a5809a83f51c3365dff53936") 1931 assert.Equal(t, nil, err) 1932 feePayer := crypto.PubkeyToAddress(feePayerPrv.PublicKey) 1933 internalTx.FeePayer = feePayer 1934 1935 // Sign 1936 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData, FeeRatio]), chainid, 0, 0 ]) 1937 b, err := rlp.EncodeToBytes([]interface{}{ 1938 internalTx.Type(), 1939 internalTx.AccountNonce, 1940 internalTx.Price, 1941 internalTx.GasLimit, 1942 internalTx.From, 1943 internalTx.Payload, 1944 internalTx.FeeRatio, 1945 }) 1946 assert.Equal(t, nil, err) 1947 1948 h := rlpHash([]interface{}{ 1949 b, 1950 chainid, 1951 uint(0), 1952 uint(0), 1953 }) 1954 1955 sig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{prv}) 1956 assert.Equal(t, nil, err) 1957 1958 tx.SetSignature(sig) 1959 1960 // Sign fee payer 1961 // encode([ encode([type, nonce, gasPrice, gas, to, value, from, anchoredData, FeeRatio]), feePayer, chainid, 0, 0 ]) 1962 b, err = rlp.EncodeToBytes([]interface{}{ 1963 internalTx.Type(), 1964 internalTx.AccountNonce, 1965 internalTx.Price, 1966 internalTx.GasLimit, 1967 internalTx.From, 1968 internalTx.Payload, 1969 internalTx.FeeRatio, 1970 }) 1971 assert.Equal(t, nil, err) 1972 1973 h = rlpHash([]interface{}{ 1974 b, 1975 feePayer, 1976 chainid, 1977 uint(0), 1978 uint(0), 1979 }) 1980 1981 feePayerSig, err := NewTxSignaturesWithValues(signer, tx, h, []*ecdsa.PrivateKey{feePayerPrv}) 1982 assert.Equal(t, nil, err) 1983 1984 tx.SetFeePayerSignatures(feePayerSig) 1985 1986 // AccountKeyPicker initialization 1987 p := &AccountKeyPickerForTest{ 1988 AddrKeyMap: make(map[common.Address]accountkey.AccountKey), 1989 } 1990 key := accountkey.NewAccountKeyPublicWithValue(&prv.PublicKey) 1991 p.SetKey(from, key) 1992 feePayerKey := accountkey.NewAccountKeyPublicWithValue(&feePayerPrv.PublicKey) 1993 p.SetKey(feePayer, feePayerKey) 1994 1995 // Validate 1996 _, err = tx.ValidateSender(signer, p, 0) 1997 assert.Equal(t, nil, err) 1998 assert.Equal(t, from, tx.ValidatedSender()) 1999 2000 // Validate fee payer 2001 _, err = tx.ValidateFeePayer(signer, p, 0) 2002 assert.Equal(t, nil, err) 2003 assert.Equal(t, feePayer, tx.ValidatedFeePayer()) 2004 } 2005 2006 func getFunctionName(i interface{}) string { 2007 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() 2008 }