-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeterministicWallet.scala
339 lines (295 loc) · 9.42 KB
/
DeterministicWallet.scala
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package scoin
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, OutputStream}
import java.math.BigInteger
import java.nio.ByteOrder
import scala.language.implicitConversions
import scoin.Crypto.{PrivateKey, PublicKey}
import scoin.Protocol._
import scodec.bits.ByteVector
/** see https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
*/
object DeterministicWallet {
case class KeyPath(path: Seq[Long]) {
def lastChildNumber: Long = if (path.isEmpty) 0L else path.last
def derive(number: Long) = KeyPath(path :+ number)
override def toString =
path.map(KeyPath.childNumberToString).foldLeft("m")(_ + "/" + _)
}
object KeyPath {
val Root = KeyPath(Nil)
/** @param path
* key path. A list of integers separated by a `/`. May start with "/" or
* "m/". A single quote appended at the end means use the hardened
* version of the ley index (example: m/44'/0'/0'/0)
* @return
* a KeyPath instance
*/
def apply(path: String): KeyPath = {
def toNumber(value: String): Long = if (value.last == '\'')
hardened(value.dropRight(1).toLong)
else value.toLong
val path1 = path.stripPrefix("m").stripPrefix("/")
if (path1.isEmpty) KeyPath.Root
else new KeyPath(path1.split('/').map(toNumber).toSeq)
}
def childNumberToString(childNumber: Long) = if (isHardened(childNumber))
(childNumber - hardenedKeyIndex).toString + "'"
else childNumber.toString
}
implicit def keypath2longseq(input: KeyPath): Seq[Long] = input.path
implicit def longseq2keypath(input: Seq[Long]): KeyPath = KeyPath(input)
val hardenedKeyIndex = 0x80000000L
def hardened(index: Long): Long = hardenedKeyIndex + index
def isHardened(index: Long): Boolean = index >= hardenedKeyIndex
case class ExtendedPrivateKey(
secretkeybytes: ByteVector32,
chaincode: ByteVector32,
depth: Int,
path: KeyPath,
parent: Long
) {
def privateKey: PrivateKey = PrivateKey(secretkeybytes)
def publicKey: PublicKey = privateKey.publicKey
}
object ExtendedPrivateKey {
def decode(
input: String,
parentPath: KeyPath = KeyPath.Root
): (Int, ExtendedPrivateKey) = {
val (prefix, bin) = Base58Check.decodeWithIntPrefix(input)
val bis = new ByteArrayInputStream(bin.toArray)
val depth = Protocol.uint8(bis)
val parent = Protocol.uint32(bis, ByteOrder.BIG_ENDIAN)
val childNumber = Protocol.uint32(bis, ByteOrder.BIG_ENDIAN)
val chaincode = ByteVector32(Protocol.bytes(bis, 32))
require(bis.read() == 0)
val secretkeybytes = ByteVector32(Protocol.bytes(bis, 32))
(
prefix,
ExtendedPrivateKey(
secretkeybytes,
chaincode,
depth,
parentPath.derive(childNumber),
parent
)
)
}
}
def encode(input: ExtendedPrivateKey, prefix: Int): String = {
val out = new ByteArrayOutputStream()
writeUInt8(input.depth, out)
writeUInt32(input.parent.toInt, out, ByteOrder.BIG_ENDIAN)
writeUInt32(input.path.lastChildNumber.toInt, out, ByteOrder.BIG_ENDIAN)
out.write(input.chaincode.toArray)
out.write(0)
out.write(input.secretkeybytes.toArray)
val buffer = ByteVector.view(out.toByteArray)
Base58Check.encode(prefix, buffer)
}
case class ExtendedPublicKey(
publickeybytes: ByteVector,
chaincode: ByteVector32,
depth: Int,
path: KeyPath,
parent: Long
) {
require(publickeybytes.length == 33)
require(chaincode.length == 32)
def publicKey: PublicKey = PublicKey(publickeybytes)
}
object ExtendedPublicKey {
def decode(
input: String,
parentPath: KeyPath = KeyPath.Root
): (Int, ExtendedPublicKey) = {
val (prefix, bin) = Base58Check.decodeWithIntPrefix(input)
val bis = new ByteArrayInputStream(bin.toArray)
val depth = Protocol.uint8(bis)
val parent = Protocol.uint32(bis, ByteOrder.BIG_ENDIAN)
val childNumber = Protocol.uint32(bis, ByteOrder.BIG_ENDIAN)
val chaincode = ByteVector32(Protocol.bytes(bis, 32))
val publickeybytes = Protocol.bytes(bis, 33)
(
prefix.toInt,
ExtendedPublicKey(
publickeybytes,
chaincode,
depth,
parentPath.derive(childNumber),
parent
)
)
}
}
def encode(input: ExtendedPublicKey, prefix: Int): String = {
val out = new ByteArrayOutputStream()
write(input, out)
val buffer = ByteVector.view(out.toByteArray)
Base58Check.encode(prefix, buffer)
}
def write(input: ExtendedPublicKey, output: OutputStream): Unit = {
writeUInt8(input.depth, output)
writeUInt32(input.parent.toInt, output, ByteOrder.BIG_ENDIAN)
writeUInt32(input.path.lastChildNumber.toInt, output, ByteOrder.BIG_ENDIAN)
writeBytes(input.chaincode.toArray, output)
writeBytes(input.publickeybytes.toArray, output)
}
/** @param seed
* random seed
* @return
* a "master" private key
*/
def generate(seed: ByteVector): ExtendedPrivateKey = {
val I =
Crypto.hmac512(ByteVector.view("Bitcoin seed".getBytes("UTF-8")), seed)
val IL = ByteVector32(I.take(32))
val IR = ByteVector32(I.takeRight(32))
ExtendedPrivateKey(IL, IR, depth = 0, path = List.empty[Long], parent = 0L)
}
/** @param input
* extended private key
* @return
* the public key for this private key
*/
def publicKey(input: ExtendedPrivateKey): ExtendedPublicKey = {
ExtendedPublicKey(
input.publicKey.value,
input.chaincode,
depth = input.depth,
path = input.path,
parent = input.parent
)
}
/** @param input
* extended public key
* @return
* the fingerprint for this public key
*/
def fingerprint(input: ExtendedPublicKey): Long = uint32(
new ByteArrayInputStream(
Crypto.hash160(input.publickeybytes).take(4).reverse.toArray
)
)
/** @param input
* extended private key
* @return
* the fingerprint for this private key (which is based on the
* corresponding public key)
*/
def fingerprint(input: ExtendedPrivateKey): Long = fingerprint(
publicKey(input)
)
/** @param parent
* extended private key
* @param index
* index of the child key
* @return
* the derived private key at the specified index
*/
def derivePrivateKey(
parent: ExtendedPrivateKey,
index: Long
): ExtendedPrivateKey = {
val I = if (isHardened(index)) {
val buffer = 0.toByte +: parent.secretkeybytes
Crypto.hmac512(
parent.chaincode,
buffer ++ writeUInt32(index.toInt, ByteOrder.BIG_ENDIAN)
)
} else {
val pub = publicKey(parent).publickeybytes
Crypto.hmac512(
parent.chaincode,
pub ++ writeUInt32(index.toInt, ByteOrder.BIG_ENDIAN)
)
}
val IL = ByteVector32(I.take(32))
val IR = ByteVector32(I.takeRight(32))
val p = new BigInteger(1, IL.toArray)
if (p.compareTo(Crypto.N) >= 0) {
throw new RuntimeException("cannot generated child private key")
}
val key = PrivateKey(IL).add(parent.privateKey)
if (new BigInteger(1, key.value.toArray) == BigInteger.ZERO) {
throw new RuntimeException("cannot generated child private key")
}
val buffer = key.value
ExtendedPrivateKey(
buffer,
chaincode = IR,
depth = parent.depth + 1,
path = parent.path.derive(index),
parent = fingerprint(parent)
)
}
/** @param parent
* extended public key
* @param index
* index of the child key
* @return
* the derived public key at the specified index
*/
def derivePublicKey(
parent: ExtendedPublicKey,
index: Long
): ExtendedPublicKey = {
require(
!isHardened(index),
"Cannot derive public keys from public hardened keys"
)
val I = Crypto.hmac512(
parent.chaincode,
parent.publickeybytes ++ writeUInt32(index.toInt, ByteOrder.BIG_ENDIAN)
)
val IL = ByteVector32(I.take(32))
val IR = ByteVector32(I.takeRight(32))
val p = new BigInteger(1, IL.toArray)
if (p.compareTo(Crypto.N) >= 0) {
throw new RuntimeException("cannot generated child public key")
}
val Ki = PrivateKey(p).publicKey.add(parent.publicKey)
val buffer = Ki.value
ExtendedPublicKey(
buffer,
chaincode = IR,
depth = parent.depth + 1,
path = parent.path.derive(index),
parent = fingerprint(parent)
)
}
def derivePrivateKey(
parent: ExtendedPrivateKey,
chain: Seq[Long]
): ExtendedPrivateKey = chain.foldLeft(parent)(derivePrivateKey)
def derivePrivateKey(
parent: ExtendedPrivateKey,
keyPath: KeyPath
): ExtendedPrivateKey = derivePrivateKey(parent, keyPath.path)
def derivePublicKey(
parent: ExtendedPublicKey,
chain: Seq[Long]
): ExtendedPublicKey = chain.foldLeft(parent)(derivePublicKey)
def derivePublicKey(
parent: ExtendedPublicKey,
keyPath: KeyPath
): ExtendedPublicKey = derivePublicKey(parent, keyPath.path)
// p2pkh mainnet
val xprv = 0x0488ade4
val xpub = 0x0488b21e
// p2sh-of-p2wpkh mainnet
val yprv = 0x049d7878
val ypub = 0x049d7cb2
// p2wpkh mainnet
val zprv = 0x04b2430c
val zpub = 0x04b24746
// p2pkh testnet
val tprv = 0x04358394
val tpub = 0x043587cf
// p2sh-of-p2wpkh testnet
val uprv = 0x044a4e28
val upub = 0x044a5262
// p2wpkh testnet
val vprv = 0x045f18bc
val vpub = 0x045f1cf6
}