Skip to content

Commit

Permalink
add signtx example
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 8, 2018
1 parent d902ab0 commit 06f4e6e
Show file tree
Hide file tree
Showing 15 changed files with 2,264 additions and 26 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ build:
test:
@go test -v .

.PHONY: ensure
ensure:
@dep ensure
$(MAKE) deps/fix

.PHONY: deps/fix
deps/fix:
@cp -r "${GOPATH}/src/github.com/ethereum/go-ethereum/crypto/secp256k1/libsecp256k1" "vendor/github.com/ethereum/go-ethereum/crypto/secp256k1/"

.PHONY: example
example:
@go run example/example.go
.PHONY: run/example/1
run/example/1:
@go run example/derive.go

.PHONY: run/example/2
run/example/2:
@go run example/sign.go
File renamed without changes.
44 changes: 44 additions & 0 deletions example/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"log"
"math/big"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/miguelmota/go-ethereum-hdwallet"
)

func main() {
mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy"
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
if err != nil {
log.Fatal(err)
}

path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
account, err := wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}

fmt.Println(account.Address.Hex()) // 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947

nonce := uint64(0)
value := big.NewInt(1000000000000000000)
toAddress := common.HexToAddress("0x0")
gasLimit := uint64(21000)
gasPrice := big.NewInt(21000000000)
var data []byte

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

signedTx, err := wallet.SignTx(account, tx, nil)
if err != nil {
log.Fatal(signedTx)
}

spew.Dump(signedTx)
}
37 changes: 24 additions & 13 deletions hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hdwallet
import (
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"sync"

Expand Down Expand Up @@ -53,6 +54,7 @@ func NewFromMnemonic(mnemonic string) (*Wallet, error) {
if err != nil {
return nil, err
}

wallet, err := newWallet(seed)
if err != nil {
return nil, err
Expand Down Expand Up @@ -206,22 +208,31 @@ func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID
return nil, accounts.ErrUnknownAccount
}

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

// Sign the transaction and verify the sender to avoid hardware fault surprises
/*
sender, signed, err := w.signTx(path, tx, chainID)
if err != nil {
return nil, err
}
if sender != account.Address {
return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
}
return signed, nil
*/
_ = path
return nil, nil
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
if err != nil {
return nil, err
}

msg, err := signedTx.AsMessage(types.HomesteadSigner{})
if err != nil {
return nil, err
}

sender := msg.From()
if sender != account.Address {
return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
}

return signedTx, nil
}

// SignHashWithPassphrase implements accounts.Wallet, with signs a hash.
// SignHashWithPassphrase implements accounts.Wallet, which signs a hash.
func (w *Wallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
return nil, nil
}
Expand Down
47 changes: 38 additions & 9 deletions hdwallet_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package hdwallet

import (
"math/big"
"strings"
"testing"

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

// TODO: table test
Expand Down Expand Up @@ -54,15 +58,6 @@ func TestWallet(t *testing.T) {
t.Error("expected to contain account")
}

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

if wallet.Contains(account) {
t.Error("expected to not contain account")
}

url := wallet.URL()
if url.String() != "" {
t.Error("expected empty url")
Expand Down Expand Up @@ -121,6 +116,40 @@ func TestWallet(t *testing.T) {
t.Error("wrong address")
}

nonce := uint64(0)
value := big.NewInt(1000000000000000000)
toAddress := common.HexToAddress("0x0")
gasLimit := uint64(21000)
gasPrice := big.NewInt(21000000000)
data := []byte{}

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

signedTx, err := wallet.SignTx(account, tx, nil)
if err != nil {
t.Error(err)
}

v, r, s := signedTx.RawSignatureValues()
if v.Cmp(big.NewInt(0)) != 1 {
t.Error("expected v value")
}
if r.Cmp(big.NewInt(0)) != 1 {
t.Error("expected r value")
}
if s.Cmp(big.NewInt(0)) != 1 {
t.Error("expected s value")
}

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

if wallet.Contains(account) {
t.Error("expected to not contain account")
}

// seed test

seed, err := NewSeedFromMnemonic(mnemonic)
Expand Down
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 06f4e6e

Please sign in to comment.