Skip to content

Commit

Permalink
signer helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Oct 28, 2024
1 parent fc623ca commit 1fd3b1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 16 additions & 0 deletions signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func NewSigner(privateKey *ecdsa.PrivateKey) Signer {
}
}

// NewSignerFromHexPrivateKey creates new signer from 0x-prefixed hex-encoded private key
func NewSignerFromHexPrivateKey(hexPrivateKey string) (*Signer, error) {
privateKeyBytes, err := hexutil.Decode(hexPrivateKey)
if err != nil {
return nil, err
}

privateKey, err := crypto.ToECDSA(privateKeyBytes)
if err != nil {
return nil, err
}

signer := NewSigner(privateKey)
return &signer, nil
}

func NewRandomSigner() (*Signer, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions signature/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,9 @@ func TestSignatureCreateCompareToCastAndEthers(t *testing.T) {
// This purposefully uses the already highly compromised keypair from the go-ethereum book:
// https://goethereumbook.org/transfer-eth/
// privateKey = fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19
privateKeyBytes, err := hexutil.Decode("0xfad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
signer, err := signature.NewSignerFromHexPrivateKey("0xfad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
require.NoError(t, err)

privateKey, err := crypto.ToECDSA(privateKeyBytes)
require.NoError(t, err)

signer := signature.NewSigner(privateKey)

address := signer.Address()
body := []byte("Hello")

Expand Down

0 comments on commit 1fd3b1f

Please sign in to comment.