@@ -68,9 +68,6 @@ func Keccak512(data ...[]byte) []byte {
6868 return d .Sum (nil )
6969}
7070
71- // Deprecated: For backward compatibility as other packages depend on these
72- func Sha3Hash (data ... []byte ) common.Hash { return Keccak256Hash (data ... ) }
73-
7471// Creates an ethereum address given the bytes and the nonce
7572func CreateAddress (b common.Address , nonce uint64 ) common.Address {
7673 data , _ := rlp .EncodeToBytes ([]interface {}{b , nonce })
@@ -79,21 +76,37 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
7976
8077// ToECDSA creates a private key with the given D value.
8178func ToECDSA (d []byte ) (* ecdsa.PrivateKey , error ) {
79+ return toECDSA (d , true )
80+ }
81+
82+ // ToECDSAUnsafe blidly converts a binary blob to a private key. It should almost
83+ // never be used unless you are sure the input is valid and want to avoid hitting
84+ // errors due to bad origin encoding (0 prefixes cut off).
85+ func ToECDSAUnsafe (d []byte ) * ecdsa.PrivateKey {
86+ priv , _ := toECDSA (d , false )
87+ return priv
88+ }
89+
90+ // toECDSA creates a private key with the given D value. The strict parameter
91+ // controls whether the key's length should be enforced at the curve size or
92+ // it can also accept legacy encodings (0 prefixes).
93+ func toECDSA (d []byte , strict bool ) (* ecdsa.PrivateKey , error ) {
8294 priv := new (ecdsa.PrivateKey )
8395 priv .PublicKey .Curve = S256 ()
84- if 8 * len (d ) != priv .Params ().BitSize {
96+ if strict && 8 * len (d ) != priv .Params ().BitSize {
8597 return nil , fmt .Errorf ("invalid length, need %d bits" , priv .Params ().BitSize )
8698 }
8799 priv .D = new (big.Int ).SetBytes (d )
88100 priv .PublicKey .X , priv .PublicKey .Y = priv .PublicKey .Curve .ScalarBaseMult (d )
89101 return priv , nil
90102}
91103
92- func FromECDSA (prv * ecdsa.PrivateKey ) []byte {
93- if prv == nil {
104+ // FromECDSA exports a private key into a binary dump.
105+ func FromECDSA (priv * ecdsa.PrivateKey ) []byte {
106+ if priv == nil {
94107 return nil
95108 }
96- return math .PaddedBigBytes (prv .D , 32 )
109+ return math .PaddedBigBytes (priv .D , priv . Params (). BitSize / 8 )
97110}
98111
99112func ToECDSAPub (pub []byte ) * ecdsa.PublicKey {
@@ -121,7 +134,6 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
121134}
122135
123136// LoadECDSA loads a secp256k1 private key from the given file.
124- // The key data is expected to be hex-encoded.
125137func LoadECDSA (file string ) (* ecdsa.PrivateKey , error ) {
126138 buf := make ([]byte , 64 )
127139 fd , err := os .Open (file )
0 commit comments