-
Notifications
You must be signed in to change notification settings - Fork 43
/
util.go
143 lines (129 loc) · 3.74 KB
/
util.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
// Package hdwallet implements heirarchical deterministic Bitcoin wallets, as defined in BIP 32.
//
// BIP 32 - https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
//
// This package provides utilities for generating hierarchical deterministic Bitcoin wallets.
//
// Examples
//
// // Generate a random 256 bit seed
// seed, err := hdwallet.GenSeed(256)
//
// // Create a master private key
// masterprv := hdwallet.MasterKey(seed)
//
// // Convert a private key to public key
// masterpub := masterprv.Pub()
//
// // Generate new child key based on private or public key
// childprv, err := masterprv.Child(0)
// childpub, err := masterpub.Child(0)
//
// // Create bitcoin address from public key
// address := childpub.Address()
//
// // Convenience string -> string Child and Address functions
// walletstring := childpub.String()
// childstring, err := hdwallet.StringChild(walletstring,0)
// childaddress, err := hdwallet.StringAddress(childstring)
//
// Extended Keys
//
// Hierarchical deterministic wallets are simply deserialized extended keys. Extended Keys can be imported and exported as base58-encoded strings. Here are two examples:
// public key: "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
// private key: "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
//
package hdwallet
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"math/big"
"github.com/btcsuite/btcd/btcec"
"golang.org/x/crypto/ripemd160"
)
var curve *btcec.KoblitzCurve = btcec.S256()
func hash160(data []byte) []byte {
sha := sha256.New()
ripe := ripemd160.New()
sha.Write(data)
ripe.Write(sha.Sum(nil))
return ripe.Sum(nil)
}
func dblSha256(data []byte) []byte {
sha1 := sha256.New()
sha2 := sha256.New()
sha1.Write(data)
sha2.Write(sha1.Sum(nil))
return sha2.Sum(nil)
}
func privToPub(key []byte) []byte {
return compress(curve.ScalarBaseMult(key))
}
func onCurve(x, y *big.Int) bool {
return curve.IsOnCurve(x, y)
}
func compress(x, y *big.Int) []byte {
two := big.NewInt(2)
rem := two.Mod(y, two).Uint64()
rem += 2
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(rem))
rest := x.Bytes()
pad := 32 - len(rest)
if pad != 0 {
zeroes := make([]byte, pad)
rest = append(zeroes, rest...)
}
return append(b[1:], rest...)
}
//2.3.4 of SEC1 - http://www.secg.org/index.php?action=secg,docs_secg
func expand(key []byte) (*big.Int, *big.Int) {
params := curve.Params()
exp := big.NewInt(1)
exp.Add(params.P, exp)
exp.Div(exp, big.NewInt(4))
x := big.NewInt(0).SetBytes(key[1:33])
y := big.NewInt(0).SetBytes(key[:1])
beta := big.NewInt(0)
beta.Exp(x, big.NewInt(3), nil)
beta.Add(beta, big.NewInt(7))
beta.Exp(beta, exp, params.P)
if y.Add(beta, y).Mod(y, big.NewInt(2)).Int64() == 0 {
y = beta
} else {
y = beta.Sub(params.P, beta)
}
return x, y
}
func addPrivKeys(k1, k2 []byte) []byte {
i1 := big.NewInt(0).SetBytes(k1)
i2 := big.NewInt(0).SetBytes(k2)
i1.Add(i1, i2)
i1.Mod(i1, curve.Params().N)
k := i1.Bytes()
zero, _ := hex.DecodeString("00")
return append(zero, k...)
}
func addPubKeys(k1, k2 []byte) []byte {
x1, y1 := expand(k1)
x2, y2 := expand(k2)
return compress(curve.Add(x1, y1, x2, y2))
}
func uint32ToByte(i uint32) []byte {
a := make([]byte, 4)
binary.BigEndian.PutUint32(a, i)
return a
}
func uint16ToByte(i uint16) []byte {
a := make([]byte, 2)
binary.BigEndian.PutUint16(a, i)
return a[1:]
}
func byteToUint16(b []byte) uint16 {
if len(b) == 1 {
zero := make([]byte, 1)
b = append(zero, b...)
}
return binary.BigEndian.Uint16(b)
}