Skip to content

Commit

Permalink
sign hash impl
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 8, 2018
1 parent 77eb9ae commit 562f1a7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func main() {
var data []byte

tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)

signedTx, err := wallet.SignTx(account, tx, nil)
if err != nil {
log.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion example/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func main() {
var data []byte

tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)

signedTx, err := wallet.SignTx(account, tx, nil)
if err != nil {
log.Fatal(err)
Expand Down
35 changes: 26 additions & 9 deletions hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,27 @@ func NewFromSeed(seed []byte) (*Wallet, error) {
return newWallet(seed)
}

// URL implements accounts.Wallet, returning the URL of the device that the wallet is on, however this does nothing since this is not a hardware device.
// URL implements accounts.Wallet, returning the URL of the device that
// the wallet is on, however this does nothing since this is not a hardware device.
func (w *Wallet) URL() accounts.URL {
return w.url
}

// Status implements accounts.Wallet, returning a custom status message from the
// underlying vendor-specific hardware wallet implementation, however this does nothing since this is not a hardware device.
// Status implements accounts.Wallet, returning a custom status message
// from the underlying vendor-specific hardware wallet implementation,
// however this does nothing since this is not a hardware device.
func (w *Wallet) Status() (string, error) {
return "ok", nil
}

// Open implements accounts.Wallet, however this does nothing since this is not a hardware device.
// Open implements accounts.Wallet, however this does nothing since this
// is not a hardware device.
func (w *Wallet) Open(passphrase string) error {
return nil
}

// Close implements accounts.Wallet, however this does nothing since this is not a hardware device.
// Close implements accounts.Wallet, however this does nothing since this
// is not a hardware device.
func (w *Wallet) Close() error {
return nil
}
Expand Down Expand Up @@ -203,8 +207,18 @@ func (w *Wallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainSt

// SignHash implements accounts.Wallet, which allows signing arbitrary data.
func (w *Wallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) {
return nil, nil
// Make sure the requested account is contained within
path, ok := w.paths[account.Address]
if !ok {
return nil, accounts.ErrUnknownAccount
}

privateKey, err := w.derivePrivateKey(path)
if err != nil {
return nil, err
}

return crypto.Sign(hash, privateKey)
}

// SignTx implements accounts.Wallet, which allows the account to sign an Ethereum transaction.
Expand Down Expand Up @@ -242,9 +256,11 @@ func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID
return signedTx, nil
}

// SignHashWithPassphrase implements accounts.Wallet, which signs a hash.
// SignHashWithPassphrase implements accounts.Wallet, attempting
// to sign the given hash with the given account using the
// passphrase as extra authentication.
func (w *Wallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
return nil, nil
return w.SignHash(account, hash)
}

// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
Expand Down Expand Up @@ -359,7 +375,8 @@ func ParseDerivationPath(path string) (accounts.DerivationPath, error) {
return accounts.ParseDerivationPath(path)
}

// MustParseDerivationPath parses the derivation path in string format into []uint32 but will panic if it can't parse it.
// MustParseDerivationPath parses the derivation path in string format into
// []uint32 but will panic if it can't parse it.
func MustParseDerivationPath(path string) accounts.DerivationPath {
parsed, err := accounts.ParseDerivationPath(path)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions hdwallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)

// TODO: table test
Expand Down Expand Up @@ -141,6 +143,35 @@ func TestWallet(t *testing.T) {
t.Error("expected s value")
}

signedTx2, err := wallet.SignTxWithPassphrase(account, "", tx, nil)
if err != nil {
t.Error(err)
}
if signedTx.Hash() != signedTx2.Hash() {
t.Error("expected match")
}

data = []byte("hello")
hash := crypto.Keccak256Hash(data)
sig, err := wallet.SignHash(account, hash.Bytes())
if err != nil {
t.Error(err)
}
if len(sig) == 0 {
t.Error("expected signature")
}

sig2, err := wallet.SignHashWithPassphrase(account, "", hash.Bytes())
if err != nil {
t.Error(err)
}
if len(sig2) == 0 {
t.Error("expected signature")
}
if hexutil.Encode(sig) != hexutil.Encode(sig2) {
t.Error("expected match")
}

err = wallet.Unpin(account)
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 562f1a7

Please sign in to comment.