-
Notifications
You must be signed in to change notification settings - Fork 1
/
MnemonicCode.scala
171 lines (149 loc) · 4.98 KB
/
MnemonicCode.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
package scoin
import java.nio.{ByteBuffer, IntBuffer}
import scala.annotation.tailrec
import scala.io.Source
import scodec.bits.ByteVector
/** see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
*/
object MnemonicCode {
lazy val englishWordlist = {
val stream =
MnemonicCode.getClass.getResourceAsStream("/bip39_english_wordlist.txt")
Source.fromInputStream(stream, "UTF-8").getLines().toSeq
}
private def toBinary(x: Byte): List[Boolean] = {
@tailrec
def loop(x: Int, acc: List[Boolean] = List.empty[Boolean]): List[Boolean] =
if (x == 0) acc else loop(x / 2, ((x % 2) != 0) :: acc)
val digits = loop(x & 0xff)
val zeroes = List.fill(8 - digits.length)(false)
zeroes ++ digits
}
private def toBinary(x: ByteVector): List[Boolean] =
x.toSeq.flatMap(toBinary).toList
private def fromBinary(bin: Seq[Boolean]): Int = bin.foldLeft(0) {
case (acc, flag) => if (flag) 2 * acc + 1 else 2 * acc
}
/** BIP39 entropy encoding
*
* @param entropy
* input entropy
* @param wordlist
* word list (must be 2048 words long)
* @return
* a list of mnemonic words that encodes the input entropy
*/
def toMnemonics(
entropy: ByteVector,
wordlist: Seq[String] = englishWordlist
): List[String] = {
require(wordlist.length == 2048, "invalid word list (size should be 2048)")
val digits = toBinary(entropy) ++ toBinary(Crypto.sha256(entropy))
.take(entropy.length.toInt / 4)
digits.grouped(11).map(fromBinary).map(index => wordlist(index)).toList
}
/** validate that a mnemonic seed is valid
*
* @param mnemonics
* list of mnemomic words
*/
def validate(
mnemonics: Seq[String],
wordlist: Seq[String] = englishWordlist
): Unit = {
require(wordlist.length == 2048, "invalid word list (size should be 2048)")
require(mnemonics.nonEmpty, "mnemonic code cannot be empty")
require(
mnemonics.length % 3 == 0,
s"invalid mnemonic word count ${mnemonics.length}, it must be a multiple of 3"
)
val wordMap = wordlist.zipWithIndex.toMap
mnemonics.foreach(word =>
require(wordMap.contains(word), s"invalid mnemonic word $word")
)
val indexes = mnemonics.map(word => wordMap(word))
@tailrec
def toBits(
index: Int,
acc: Seq[Boolean] = Seq.empty[Boolean]
): Seq[Boolean] =
if (acc.length == 11) acc else toBits(index / 2, (index % 2 != 0) +: acc)
val bits = indexes.flatMap(i => toBits(i))
val bitlength = (bits.length * 32) / 33
val (databits, checksumbits) = bits.splitAt(bitlength)
val data = ByteVector(databits.grouped(8).map(fromBinary).map(_.toByte))
val check = toBinary(Crypto.sha256(data)).take(data.length.toInt / 4)
require(check == checksumbits, "invalid checksum")
}
def validate(mnemonics: String): Unit = validate(mnemonics.split(" ").toSeq)
/** BIP39 seed derivation
*
* @param mnemonics
* mnemonic words
* @param passphrase
* passphrase
* @return
* a seed derived from the mnemonic words and passphrase
*/
def toSeed(mnemonics: Seq[String], passphrase: String): ByteVector =
pbkdf2Sha512(
ByteVector.view(mnemonics.mkString(" ").getBytes("UTF-8")),
ByteVector.view(("mnemonic" + passphrase).getBytes("UTF-8")),
2048,
64
)
def toSeed(mnemonics: String, passphrase: String): ByteVector =
toSeed(mnemonics.split(" ").toSeq, passphrase)
private def pbkdf2Sha512(
password: ByteVector,
salt: ByteVector,
iterations: Int,
keyLen: Int
): ByteVector = {
val hashLen = 64
val numBlocks = (keyLen + hashLen - 1) / hashLen
// pseudo-random function defined in the spec
@inline def prf(buf: ByteVector) = Crypto.hmac512(password, buf)
// this is a translation of the helper function "F" defined in the spec
def calculateBlock(blockNum: Int): Array[Byte] = {
// u_1
val u_1 = prf(
ByteVector.concat(List(salt ++ ByteVector.fromInt(blockNum)))
)
val buf = IntBuffer
.allocate(u_1.size.toInt / 4)
.put(ByteBuffer.wrap(u_1.toArray).asIntBuffer)
.array
.clone
var u = u_1
var iter = 1
while (iter < iterations) {
// u_2 through u_c : calculate u_n and xor it with the previous value
u = prf(u)
xorInPlace(buf, u.toArray)
iter += 1
}
val ret = ByteBuffer.allocate(u_1.size.toInt)
ret.asIntBuffer.put(buf)
ret.array
}
// how many blocks we'll need to calculate (the last may be truncated)
val blocksNeeded = (keyLen.toFloat / 20).ceil.toInt
ByteVector.view(
(1 to blocksNeeded).iterator
.map(calculateBlock)
.flatten
.take(keyLen)
.toArray
)
}
private def xorInPlace(buff: Array[Int], a2: Array[Byte]): Unit = {
val b2 = ByteBuffer.wrap(a2).asIntBuffer
val len = buff.array.size
var i = 0
while (i < len) {
buff(i) ^= b2.get(i)
i += 1
}
}
}