forked from miguelmota/go-ethereum-hdwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
121 lines (97 loc) · 2.85 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package hdwallet_test
import (
"fmt"
"log"
"math/big"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
hdwallet "github.com/hiendaovinh/go-ethereum-hdwallet"
)
func ExampleMustParseDerivationPath() {
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())
path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
account, err = wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}
fmt.Println(account.Address.Hex())
// Output: 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947
// 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
}
func ExampleSign() {
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, true)
if err != nil {
log.Fatal(err)
}
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(err)
}
spew.Dump(signedTx)
}
func ExampleWallet() {
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.Printf("Account address: %s\n", account.Address.Hex())
privateKey, err := wallet.PrivateKeyHex(account)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Private key in hex: %s\n", privateKey)
publicKey, _ := wallet.PublicKeyHex(account)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Public key in hex: %s\n", publicKey)
}
func ExampleNewSeed() {
seed, _ := hdwallet.NewSeed()
wallet, err := hdwallet.NewFromSeed(seed)
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
path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
account, err = wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}
fmt.Println(account.Address.Hex()) // 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
}