/
bip85_test.go
111 lines (90 loc) · 2.39 KB
/
bip85_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
package bip85_test
import (
"encoding/hex"
"strings"
"testing"
"github.com/e4coder/bip85"
"github.com/ethereum/go-ethereum/accounts"
"github.com/tyler-smith/go-bip32"
)
const MASTER_BIP32_ROOT_KEY = "xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb"
func TestEntropyFromKey(t *testing.T) {
mrootkey, err := bip32.B58Deserialize(MASTER_BIP32_ROOT_KEY)
if err != nil {
t.Log(err)
t.FailNow()
}
pathStr1 := "m/83696968'/0'/0'"
path1, err := accounts.ParseDerivationPath(pathStr1)
if err != nil {
t.Log(err)
t.FailNow()
}
// derive the child key
var currKey1 *bip32.Key = mrootkey
for _, childIdX := range path1 {
k, err := currKey1.NewChildKey(childIdX)
if err != nil {
t.Log(err)
t.FailNow()
}
currKey1 = k
}
childKey1 := currKey1
t.Log(hex.EncodeToString(childKey1.Key))
if strings.Compare(hex.EncodeToString(childKey1.Key), "cca20ccb0e9a90feb0912870c3323b24874b0ca3d8018c4b96d0b97c0e82ded0") != 0 {
t.Log("childKey1 does not match")
t.FailNow()
}
entropyNew1, err := bip85.EntropyFromKey(childKey1)
if err != nil {
t.Log(err)
t.FailNow()
}
if strings.Compare(hex.EncodeToString(entropyNew1), "efecfbccffea313214232d29e71563d941229afb4338c21f9517c41aaa0d16f00b83d2a09ef747e7a64e8e2bd5a14869e693da66ce94ac2da570ab7ee48618f7") != 0 {
t.Log("entropyNew1 does not match")
t.FailNow()
}
}
func TestNewBip39FromEntropy(t *testing.T) {
mrootkey, err := bip32.B58Deserialize(MASTER_BIP32_ROOT_KEY)
if err != nil {
t.Log(err)
t.FailNow()
}
// pathStr := "m/83696968'/0'/0'"
pathStr := "m/83696968'/39'/0'/12'/0'"
path, err := accounts.ParseDerivationPath(pathStr)
if err != nil {
t.Log(err)
t.FailNow()
}
// derive the child key
var currKey *bip32.Key = mrootkey
for _, childIdX := range path {
k, err := currKey.NewChildKey(childIdX)
if err != nil {
t.Log(err)
t.FailNow()
}
currKey = k
}
childKey := currKey
t.Log(hex.EncodeToString(childKey.Key))
entropyNew, err := bip85.EntropyFromKey(childKey)
if err != nil {
t.Log(err)
t.FailNow()
}
t.Log(hex.EncodeToString(entropyNew[:16]))
mnemonic, err := bip85.NewBip39FromEntropy(entropyNew)
if err != nil {
t.Log(err)
t.FailNow()
}
t.Log(mnemonic)
if strings.Compare(mnemonic, "girl mad pet galaxy egg matter matrix prison refuse sense ordinary nose") != 0 {
t.Log("Mnemonic does not match")
t.FailNow()
}
}