github.com/snowblossomcoin/go-ethereum@v1.9.25/core/types/transaction_signing_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package types 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/crypto" 25 "github.com/ethereum/go-ethereum/rlp" 26 ) 27 28 func TestEIP155Signing(t *testing.T) { 29 key, _ := crypto.GenerateKey() 30 addr := crypto.PubkeyToAddress(key.PublicKey) 31 32 signer := NewEIP155Signer(big.NewInt(18)) 33 tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 from, err := Sender(signer, tx) 39 if err != nil { 40 t.Fatal(err) 41 } 42 if from != addr { 43 t.Errorf("exected from and address to be equal. Got %x want %x", from, addr) 44 } 45 } 46 47 func TestEIP155ChainId(t *testing.T) { 48 key, _ := crypto.GenerateKey() 49 addr := crypto.PubkeyToAddress(key.PublicKey) 50 51 signer := NewEIP155Signer(big.NewInt(18)) 52 tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if !tx.Protected() { 57 t.Fatal("expected tx to be protected") 58 } 59 60 if tx.ChainId().Cmp(signer.chainId) != 0 { 61 t.Error("expected chainId to be", signer.chainId, "got", tx.ChainId()) 62 } 63 64 tx = NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil) 65 tx, err = SignTx(tx, HomesteadSigner{}, key) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if tx.Protected() { 71 t.Error("didn't expect tx to be protected") 72 } 73 74 if tx.ChainId().Sign() != 0 { 75 t.Error("expected chain id to be 0 got", tx.ChainId()) 76 } 77 } 78 79 func TestEIP155SigningVitalik(t *testing.T) { 80 // Test vectors come from http://vitalik.ca/files/eip155_testvec.txt 81 for i, test := range []struct { 82 txRlp, addr string 83 }{ 84 {"f864808504a817c800825208943535353535353535353535353535353535353535808025a0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116da0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", "0xf0f6f18bca1b28cd68e4357452947e021241e9ce"}, 85 {"f864018504a817c80182a410943535353535353535353535353535353535353535018025a0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bcaa0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6", "0x23ef145a395ea3fa3deb533b8a9e1b4c6c25d112"}, 86 {"f864028504a817c80282f618943535353535353535353535353535353535353535088025a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5", "0x2e485e0c23b4c3c542628a5f672eeab0ad4888be"}, 87 {"f865038504a817c803830148209435353535353535353535353535353535353535351b8025a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4e0a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4de", "0x82a88539669a3fd524d669e858935de5e5410cf0"}, 88 {"f865048504a817c80483019a28943535353535353535353535353535353535353535408025a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c063a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c060", "0xf9358f2538fd5ccfeb848b64a96b743fcc930554"}, 89 {"f865058504a817c8058301ec309435353535353535353535353535353535353535357d8025a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1", "0xa8f7aba377317440bc5b26198a363ad22af1f3a4"}, 90 {"f866068504a817c80683023e3894353535353535353535353535353535353535353581d88025a06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2fa06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2d", "0xf1f571dc362a0e5b2696b8e775f8491d3e50de35"}, 91 {"f867078504a817c807830290409435353535353535353535353535353535353535358201578025a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021", "0xd37922162ab7cea97c97a87551ed02c9a38b7332"}, 92 {"f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10", "0x9bddad43f934d313c2b79ca28a432dd2b7281029"}, 93 {"f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb", "0x3c24d7329e92f84f08556ceb6df1cdb0104ca49f"}, 94 } { 95 signer := NewEIP155Signer(big.NewInt(1)) 96 97 var tx *Transaction 98 err := rlp.DecodeBytes(common.Hex2Bytes(test.txRlp), &tx) 99 if err != nil { 100 t.Errorf("%d: %v", i, err) 101 continue 102 } 103 104 from, err := Sender(signer, tx) 105 if err != nil { 106 t.Errorf("%d: %v", i, err) 107 continue 108 } 109 110 addr := common.HexToAddress(test.addr) 111 if from != addr { 112 t.Errorf("%d: expected %x got %x", i, addr, from) 113 } 114 115 } 116 } 117 118 func TestChainId(t *testing.T) { 119 key, _ := defaultTestKey() 120 121 tx := NewTransaction(0, common.Address{}, new(big.Int), 0, new(big.Int), nil) 122 123 var err error 124 tx, err = SignTx(tx, NewEIP155Signer(big.NewInt(1)), key) 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 _, err = Sender(NewEIP155Signer(big.NewInt(2)), tx) 130 if err != ErrInvalidChainId { 131 t.Error("expected error:", ErrInvalidChainId) 132 } 133 134 _, err = Sender(NewEIP155Signer(big.NewInt(1)), tx) 135 if err != nil { 136 t.Error("expected no error") 137 } 138 }