@ -53,7 +53,7 @@ func MakeSigner(config *params.ChainConfig, blockNumber *big.Int) Signer {
// SignECDSA signs the transaction using the given signer and private key
// SignECDSA signs the transaction using the given signer and private key
func SignECDSA ( s Signer , tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
func SignECDSA ( s Signer , tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
h := s . Hash ( tx )
h := s . Hash ( tx )
sig , err := crypto . SignEthereum ( h [ : ] , prv )
sig , err := crypto . Sign ( h [ : ] , prv )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -91,11 +91,6 @@ func Sender(signer Signer, tx *Transaction) (common.Address, error) {
return addr , nil
return addr , nil
}
}
// SignatureValues returns the ECDSA signature values contained in the transaction.
func SignatureValues ( signer Signer , tx * Transaction ) ( v byte , r * big . Int , s * big . Int ) {
return normaliseV ( signer , tx . data . V ) , new ( big . Int ) . Set ( tx . data . R ) , new ( big . Int ) . Set ( tx . data . S )
}
type Signer interface {
type Signer interface {
// Hash returns the rlp encoded hash for signatures
// Hash returns the rlp encoded hash for signatures
Hash ( tx * Transaction ) common . Hash
Hash ( tx * Transaction ) common . Hash
@ -143,17 +138,16 @@ func (s EIP155Signer) PublicKey(tx *Transaction) ([]byte, error) {
return nil , ErrInvalidChainId
return nil , ErrInvalidChainId
}
}
V := normaliseV ( s , tx . data . V )
V := byte ( new ( big . Int ) . Sub ( tx . data . V , s . chainIdMul ) . Uint64 ( ) - 35 )
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , true ) {
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , true ) {
return nil , ErrInvalidSig
return nil , ErrInvalidSig
}
}
// encode the signature in uncompressed format
// encode the signature in uncompressed format
R , S := tx . data . R . Bytes ( ) , tx . data . S . Bytes ( )
R , S := tx . data . R . Bytes ( ) , tx . data . S . Bytes ( )
sig := make ( [ ] byte , 65 )
sig := make ( [ ] byte , 65 )
copy ( sig [ 32 - len ( R ) : 32 ] , R )
copy ( sig [ 32 - len ( R ) : 32 ] , R )
copy ( sig [ 64 - len ( S ) : 64 ] , S )
copy ( sig [ 64 - len ( S ) : 64 ] , S )
sig [ 64 ] = V - 27
sig [ 64 ] = V
// recover the public key from the signature
// recover the public key from the signature
hash := s . Hash ( tx )
hash := s . Hash ( tx )
@ -167,8 +161,8 @@ func (s EIP155Signer) PublicKey(tx *Transaction) ([]byte, error) {
return pub , nil
return pub , nil
}
}
// WithSignature returns a new transaction with the given signature.
// WithSignature returns a new transaction with the given signature. This signature
// This signature needs to be formatted as described in the yellow paper (v+27) .
// needs to be in the [R || S || V] format where V is 0 or 1 .
func ( s EIP155Signer ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
func ( s EIP155Signer ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
if len ( sig ) != 65 {
if len ( sig ) != 65 {
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
@ -179,7 +173,7 @@ func (s EIP155Signer) WithSignature(tx *Transaction, sig []byte) (*Transaction,
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] } )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] } )
if s . chainId . BitLen ( ) > 0 {
if s . chainId . BitLen ( ) > 0 {
cpy . data . V = big . NewInt ( int64 ( sig [ 64 ] - 27 + 35 ) )
cpy . data . V = big . NewInt ( int64 ( sig [ 64 ] + 35 ) )
cpy . data . V . Add ( cpy . data . V , s . chainIdMul )
cpy . data . V . Add ( cpy . data . V , s . chainIdMul )
}
}
return cpy , nil
return cpy , nil
@ -201,7 +195,7 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
func ( s EIP155Signer ) SigECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
func ( s EIP155Signer ) SigECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
h := s . Hash ( tx )
h := s . Hash ( tx )
sig , err := crypto . SignEthereum ( h [ : ] , prv )
sig , err := crypto . Sign ( h [ : ] , prv )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -217,8 +211,8 @@ func (s HomesteadSigner) Equal(s2 Signer) bool {
return ok
return ok
}
}
// WithSignature returns a new transaction with the given snature.
// WithSignature returns a new transaction with the given sig nature. This signature
// This snature needs to be formatted as described in the yellow paper (v+27) .
// needs to be in the [R || S || V] format where V is 0 or 1 .
func ( hs HomesteadSigner ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
func ( hs HomesteadSigner ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
if len ( sig ) != 65 {
if len ( sig ) != 65 {
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
@ -226,13 +220,13 @@ func (hs HomesteadSigner) WithSignature(tx *Transaction, sig []byte) (*Transacti
cpy := & Transaction { data : tx . data }
cpy := & Transaction { data : tx . data }
cpy . data . R = new ( big . Int ) . SetBytes ( sig [ : 32 ] )
cpy . data . R = new ( big . Int ) . SetBytes ( sig [ : 32 ] )
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] } )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] + 27 } )
return cpy , nil
return cpy , nil
}
}
func ( hs HomesteadSigner ) SignECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
func ( hs HomesteadSigner ) SignECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
h := hs . Hash ( tx )
h := hs . Hash ( tx )
sig , err := crypto . SignEthereum ( h [ : ] , prv )
sig , err := crypto . Sign ( h [ : ] , prv )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -243,7 +237,7 @@ func (hs HomesteadSigner) PublicKey(tx *Transaction) ([]byte, error) {
if tx . data . V . BitLen ( ) > 8 {
if tx . data . V . BitLen ( ) > 8 {
return nil , ErrInvalidSig
return nil , ErrInvalidSig
}
}
V := byte ( tx . data . V . Uint64 ( ) )
V := byte ( tx . data . V . Uint64 ( ) - 27 )
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , true ) {
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , true ) {
return nil , ErrInvalidSig
return nil , ErrInvalidSig
}
}
@ -252,7 +246,7 @@ func (hs HomesteadSigner) PublicKey(tx *Transaction) ([]byte, error) {
sig := make ( [ ] byte , 65 )
sig := make ( [ ] byte , 65 )
copy ( sig [ 32 - len ( r ) : 32 ] , r )
copy ( sig [ 32 - len ( r ) : 32 ] , r )
copy ( sig [ 64 - len ( s ) : 64 ] , s )
copy ( sig [ 64 - len ( s ) : 64 ] , s )
sig [ 64 ] = V - 27
sig [ 64 ] = V
// recover the public key from the snature
// recover the public key from the snature
hash := hs . Hash ( tx )
hash := hs . Hash ( tx )
@ -273,8 +267,8 @@ func (s FrontierSigner) Equal(s2 Signer) bool {
return ok
return ok
}
}
// WithSignature returns a new transaction with the given snature.
// WithSignature returns a new transaction with the given sig nature. This signature
// This snature needs to be formatted as described in the yellow paper (v+27) .
// needs to be in the [R || S || V] format where V is 0 or 1 .
func ( fs FrontierSigner ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
func ( fs FrontierSigner ) WithSignature ( tx * Transaction , sig [ ] byte ) ( * Transaction , error ) {
if len ( sig ) != 65 {
if len ( sig ) != 65 {
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
panic ( fmt . Sprintf ( "wrong size for snature: got %d, want 65" , len ( sig ) ) )
@ -282,13 +276,13 @@ func (fs FrontierSigner) WithSignature(tx *Transaction, sig []byte) (*Transactio
cpy := & Transaction { data : tx . data }
cpy := & Transaction { data : tx . data }
cpy . data . R = new ( big . Int ) . SetBytes ( sig [ : 32 ] )
cpy . data . R = new ( big . Int ) . SetBytes ( sig [ : 32 ] )
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . S = new ( big . Int ) . SetBytes ( sig [ 32 : 64 ] )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] } )
cpy . data . V = new ( big . Int ) . SetBytes ( [ ] byte { sig [ 64 ] + 27 } )
return cpy , nil
return cpy , nil
}
}
func ( fs FrontierSigner ) SignECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
func ( fs FrontierSigner ) SignECDSA ( tx * Transaction , prv * ecdsa . PrivateKey ) ( * Transaction , error ) {
h := fs . Hash ( tx )
h := fs . Hash ( tx )
sig , err := crypto . SignEthereum ( h [ : ] , prv )
sig , err := crypto . Sign ( h [ : ] , prv )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -313,7 +307,7 @@ func (fs FrontierSigner) PublicKey(tx *Transaction) ([]byte, error) {
return nil , ErrInvalidSig
return nil , ErrInvalidSig
}
}
V := byte ( tx . data . V . Uint64 ( ) )
V := byte ( tx . data . V . Uint64 ( ) - 27 )
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , false ) {
if ! crypto . ValidateSignatureValues ( V , tx . data . R , tx . data . S , false ) {
return nil , ErrInvalidSig
return nil , ErrInvalidSig
}
}
@ -322,7 +316,7 @@ func (fs FrontierSigner) PublicKey(tx *Transaction) ([]byte, error) {
sig := make ( [ ] byte , 65 )
sig := make ( [ ] byte , 65 )
copy ( sig [ 32 - len ( r ) : 32 ] , r )
copy ( sig [ 32 - len ( r ) : 32 ] , r )
copy ( sig [ 64 - len ( s ) : 64 ] , s )
copy ( sig [ 64 - len ( s ) : 64 ] , s )
sig [ 64 ] = V - 27
sig [ 64 ] = V
// recover the public key from the snature
// recover the public key from the snature
hash := fs . Hash ( tx )
hash := fs . Hash ( tx )
@ -336,18 +330,6 @@ func (fs FrontierSigner) PublicKey(tx *Transaction) ([]byte, error) {
return pub , nil
return pub , nil
}
}
// normaliseV returns the Ethereum version of the V parameter
func normaliseV ( s Signer , v * big . Int ) byte {
if s , ok := s . ( EIP155Signer ) ; ok {
stdV := v . BitLen ( ) <= 8 && ( v . Uint64 ( ) == 27 || v . Uint64 ( ) == 28 )
if s . chainId . BitLen ( ) > 0 && ! stdV {
nv := byte ( ( new ( big . Int ) . Sub ( v , s . chainIdMul ) . Uint64 ( ) ) - 35 + 27 )
return nv
}
}
return byte ( v . Uint64 ( ) )
}
// deriveChainId derives the chain id from the given v parameter
// deriveChainId derives the chain id from the given v parameter
func deriveChainId ( v * big . Int ) * big . Int {
func deriveChainId ( v * big . Int ) * big . Int {
if v . BitLen ( ) <= 64 {
if v . BitLen ( ) <= 64 {