-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
108 lines (82 loc) · 2.33 KB
/
helper_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
package bip39_test
import (
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sammyne/bip39/dict"
"golang.org/x/text/unicode/norm"
)
//go:generate curl -s https://raw.githubusercontent.com/trezor/python-mnemonic/master/vectors.json -o testdata/trezor.json
//go:generate curl -s https://raw.githubusercontent.com/bip32JP/bip32JP.github.io/master/test_JP_BIP39.json -o testdata/test_JP_BIP39.json
const GoldenBase = "testdata"
const GoldenTrezor = "trezor.json"
const GoldenJP = "test_JP_BIP39.json"
type Goldie struct {
Entropy []byte
Mnemonic string
Seed []byte
}
type GoldieJP struct {
Entropy []byte
Mnemonic string
Passphrase string
Seed []byte
ExtendedPrivateKey string
}
type GoldieTrezor GoldieJP
func (goldie *GoldieJP) UnmarshalJSON(data []byte) error {
var jp map[string]string
//fmt.Println("hello")
if err := json.Unmarshal(data, &jp); nil != err {
return err
}
var err error
if goldie.Entropy, err = hex.DecodeString(jp["entropy"]); nil != err {
return err
}
//goldie.Mnemonic = jp["mnemonic"]
//goldie.Mnemonic = norm.NFKD.String(jp["mnemonic"]) // NFKD normalization
words := strings.Fields(norm.NFKD.String(jp["mnemonic"]))
goldie.Mnemonic = strings.Join(words, dict.IdeographicSpaces)
goldie.Passphrase = jp["passphrase"]
if goldie.Seed, err = hex.DecodeString(jp["seed"]); nil != err {
return err
}
goldie.ExtendedPrivateKey = jp["bip32_xprv"]
return nil
}
func (goldie *GoldieTrezor) UnmarshalJSON(data []byte) error {
var trezor []string
if err := json.Unmarshal(data, &trezor); nil != err {
return err
}
var err error
if goldie.Entropy, err = hex.DecodeString(trezor[0]); nil != err {
return err
}
goldie.Mnemonic = trezor[1]
goldie.Passphrase = "TREZOR"
if goldie.Seed, err = hex.DecodeString(trezor[2]); nil != err {
return err
}
goldie.ExtendedPrivateKey = trezor[3]
return nil
}
func ReadGoldenJSON(t *testing.T, name string, v interface{}) {
fd, err := os.Open(filepath.Join(GoldenBase, name))
if nil != err {
t.Fatal(err)
}
defer fd.Close()
if err := json.NewDecoder(fd).Decode(v); nil != err {
t.Fatal(err)
}
}
func ReadTrezorGoldenJSON(t *testing.T) []*GoldieTrezor {
trezor := make(map[string][]*GoldieTrezor)
ReadGoldenJSON(t, GoldenTrezor, &trezor)
return trezor["english"]
}