@@ -68,9 +68,6 @@ func Keccak512(data ...[]byte) []byte {
68
68
return d .Sum (nil )
69
69
}
70
70
71
- // Deprecated: For backward compatibility as other packages depend on these
72
- func Sha3Hash (data ... []byte ) common.Hash { return Keccak256Hash (data ... ) }
73
-
74
71
// Creates an ethereum address given the bytes and the nonce
75
72
func CreateAddress (b common.Address , nonce uint64 ) common.Address {
76
73
data , _ := rlp .EncodeToBytes ([]interface {}{b , nonce })
@@ -79,21 +76,37 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
79
76
80
77
// ToECDSA creates a private key with the given D value.
81
78
func 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 ) {
82
94
priv := new (ecdsa.PrivateKey )
83
95
priv .PublicKey .Curve = S256 ()
84
- if 8 * len (d ) != priv .Params ().BitSize {
96
+ if strict && 8 * len (d ) != priv .Params ().BitSize {
85
97
return nil , fmt .Errorf ("invalid length, need %d bits" , priv .Params ().BitSize )
86
98
}
87
99
priv .D = new (big.Int ).SetBytes (d )
88
100
priv .PublicKey .X , priv .PublicKey .Y = priv .PublicKey .Curve .ScalarBaseMult (d )
89
101
return priv , nil
90
102
}
91
103
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 {
94
107
return nil
95
108
}
96
- return math .PaddedBigBytes (prv .D , 32 )
109
+ return math .PaddedBigBytes (priv .D , priv . Params (). BitSize / 8 )
97
110
}
98
111
99
112
func ToECDSAPub (pub []byte ) * ecdsa.PublicKey {
@@ -121,7 +134,6 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
121
134
}
122
135
123
136
// LoadECDSA loads a secp256k1 private key from the given file.
124
- // The key data is expected to be hex-encoded.
125
137
func LoadECDSA (file string ) (* ecdsa.PrivateKey , error ) {
126
138
buf := make ([]byte , 64 )
127
139
fd , err := os .Open (file )
0 commit comments