-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix `addr` and `sign` cheatcodes to correctly left-pad private keys that do not fill up 32-byte slices
- Loading branch information
Showing
3 changed files
with
54 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetPrivateKey will return a private key object given a byte slice. Only slices between lengths 1 and 32 (inclusive) | ||
// are valid. | ||
func GetPrivateKey(b []byte) (*ecdsa.PrivateKey, error) { | ||
// Make sure that private key is not zero | ||
if len(b) < 1 || len(b) > 32 { | ||
return nil, errors.New("invalid private key") | ||
} | ||
|
||
// Then pad the private key slice to a fixed 32-byte array | ||
paddedPrivateKey := make([]byte, 32) | ||
copy(paddedPrivateKey[32-len(b):], b) | ||
|
||
// Next we will actually retrieve the private key object | ||
privateKey, err := crypto.ToECDSA(paddedPrivateKey[:]) | ||
return privateKey, errors.WithStack(err) | ||
} |