/
bip38.go
156 lines (115 loc) · 2.93 KB
/
bip38.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package bip38
import (
"golang.org/x/crypto/scrypt"
"crypto/aes"
"crypto/rand"
"github.com/sour-is/bitcoin/address"
"github.com/sour-is/koblitz/kelliptic"
)
type BIP38Key struct {
Flag byte
Hash [4]byte
Data [32]byte
}
func Encrypt(p *address.PrivateKey, passphrase string) string {
bip38 := new(BIP38Key)
ah := address.Hash256(p.AddressBytes())[:4]
dh, _ := scrypt.Key([]byte(passphrase), ah, 16384, 8, 8, 64)
bip38.Flag = byte(0xC0)
copy(bip38.Hash[:], ah)
copy(bip38.Data[:], encrypt(p.Bytes(), dh[:32], dh[32:]))
return bip38.String()
}
func Decrypt(b38 string, passphrase string) (priv *address.PrivateKey, err error) {
b, err := address.FromBase58(b38)
if err != nil {
return nil, err
}
bip38 := new(BIP38Key)
bip38.Flag = b[2]
copy(bip38.Hash[:], b[3:7])
copy(bip38.Data[:], b[7:])
dh, _ := scrypt.Key([]byte(passphrase), bip38.Hash[:], 16384, 8, 8, 64)
priv = new(address.PrivateKey)
p := decrypt(bip38.Data[:], dh[:32], dh[32:])
priv.SetBytes(p)
return
}
func (bip BIP38Key) String() string {
return address.ToBase58(bip.Bytes(), 58)
}
func (bip BIP38Key) Bytes() []byte {
dst := make([]byte, 39)
dst[0] = byte(0x01)
dst[1] = byte(0x42)
dst[2] = bip.Flag
copy(dst[3:], bip.Hash[:])
copy(dst[7:], bip.Data[:])
return dst
}
func encrypt(pk, dh1, dh2 []byte) (dst []byte) {
c, _ := aes.NewCipher(dh2)
for i, _ := range dh1 {
dh1[i] ^= pk[i]
}
dst = make([]byte, 48)
c.Encrypt(dst, dh1[:16])
c.Encrypt(dst[16:], dh1[16:])
dst = dst[:32]
return
}
func decrypt(src, dh1, dh2 []byte) (dst []byte) {
c, _ := aes.NewCipher(dh2)
dst = make([]byte, 48)
c.Decrypt(dst, src[:16])
c.Decrypt(dst[16:], src[16:])
dst = dst[:32]
for i := range dst {
dst[i] ^= dh1[i]
}
return
}
// Intermediate Code Generation
func NewIntermediate(p string) (string, error) {
in := make([]byte, 49)
copy(in, []byte{0x2C, 0xE9, 0xB3, 0xE1, 0xFF, 0x39, 0xE2, 0x53})
rand.Read(in[8:16])
sc, _ := scrypt.Key([]byte(p), in[8:16], 16384, 8, 8, 32)
s256 := kelliptic.S256()
x, y := s256.ScalarBaseMult(sc)
cp := s256.CompressPoint(x, y)
copy(in[16:], cp)
return address.ToBase58(in, 72), nil
}
/*
func NewIntermediateLot(p string, lot, seq int) (string, error) {
if lot < 0 || lot > 4096 {
return "", errors.New("BIP38: lot out of range")
}
if seq < 0 || seq > 1048575 {
return "", errors.New("BIP38: sequence out of range")
}
lotseq := make([]byte, 8)
n := binary.PutVarint(lotseq, int64(lot*4096+seq))
salt := make([]byte, 16)
rand.Read(salt[8:12])
copy(salt[12-n:16], lotseq[:4])
sc, _ := scrypt.Key([]byte(p), salt[8:16], 16384, 8, 8, 32)
copy(salt[:8], sc)
// copy(in, []byte{0x2C, 0xE9, 0xB3, 0xE1, 0xFF, 0x39, 0xE2, 0x53})
return "", nil
}
func DecodeIntermediate(p string) error {
in, err := FromBase58(p)
if err != nil {
return err
}
s256 := kelliptic.S256()
x, y, err := s256.DecompressPoint(in[16:])
if err != nil {
return err
}
fmt.Printf("End X: %x Y: %x\n", x, y)
return nil
}
*/