Skip to content

Commit e75f729

Browse files
authored
Add support for creating wallet from private key (#24)
1 parent 811005c commit e75f729

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

ethwallet/ethwallet.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"math/big"
77

88
"github.com/0xsequence/ethkit/ethrpc"
9-
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
109
"github.com/0xsequence/ethkit/go-ethereum/accounts"
10+
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
1111
"github.com/0xsequence/ethkit/go-ethereum/common"
1212
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
1313
"github.com/0xsequence/ethkit/go-ethereum/core/types"
@@ -30,6 +30,14 @@ type WalletOptions struct {
3030
RandomWalletEntropyBitSize int
3131
}
3232

33+
func NewWalletFromPrivateKey(key string) (*Wallet, error) {
34+
hdnode, err := NewHDNodeFromPrivateKey(key)
35+
if err != nil {
36+
return nil, err
37+
}
38+
return &Wallet{hdnode: hdnode}, nil
39+
}
40+
3341
func NewWalletFromHDNode(hdnode *HDNode, optPath ...accounts.DerivationPath) (*Wallet, error) {
3442
var err error
3543
derivationPath := DefaultBaseDerivationPath

ethwallet/ethwallet_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ func TestWalletSignMessage(t *testing.T) {
3434
assert.NoError(t, err)
3535
assert.True(t, valid)
3636
}
37+
38+
func TestWalletSignMessageFromPrivateKey(t *testing.T) {
39+
wallet, err := ethwallet.NewWalletFromPrivateKey("3c121e5b2c2b2426f386bfc0257820846d77610c20e0fd4144417fb8fd79bfb8")
40+
assert.NoError(t, err)
41+
42+
address := wallet.Address()
43+
assert.NoError(t, err)
44+
assert.Equal(t, "0x95a7D93FEf729ed829C761FF0e035BB6Dd2c7052", address.String())
45+
46+
sig, err := wallet.SignMessage([]byte("hi"))
47+
assert.NoError(t, err)
48+
49+
sigHash := hexutil.Encode(sig)
50+
expected := "0x14c0b4cbb654b3da1140cdf5c000bfbf5db810f5a7fb339dd4514230d20e1bae4bf9ab78b6431b975260676a020cb4f7c164161776ee6fedbce39eb4103b257f1c"
51+
assert.Equal(t, expected, sigHash)
52+
53+
// Lets validate the signature for good measure
54+
valid, err := ethwallet.ValidateEthereumSignature(address.String(), []byte("hi"), sigHash)
55+
assert.NoError(t, err)
56+
assert.True(t, valid)
57+
}

ethwallet/hdnode.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"crypto/rand"
66
"fmt"
77

8-
"github.com/btcsuite/btcd/chaincfg"
9-
"github.com/btcsuite/btcutil/hdkeychain"
108
"github.com/0xsequence/ethkit/go-ethereum/accounts"
119
"github.com/0xsequence/ethkit/go-ethereum/common"
1210
"github.com/0xsequence/ethkit/go-ethereum/crypto"
11+
"github.com/btcsuite/btcd/chaincfg"
12+
"github.com/btcsuite/btcutil/hdkeychain"
1313
"github.com/tyler-smith/go-bip39"
1414
)
1515

@@ -36,6 +36,19 @@ type HDNode struct {
3636
address common.Address
3737
}
3838

39+
func NewHDNodeFromPrivateKey(privateKey string) (*HDNode, error) {
40+
key, err := crypto.HexToECDSA(privateKey)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return &HDNode{
46+
privateKey: key,
47+
publicKey: &key.PublicKey,
48+
address: crypto.PubkeyToAddress(key.PublicKey),
49+
}, nil
50+
}
51+
3952
func NewHDNodeFromMnemonic(mnemonic string, path *accounts.DerivationPath) (*HDNode, error) {
4053
entropy, err := MnemonicToEntropy(mnemonic)
4154
if err != nil {

0 commit comments

Comments
 (0)