bsv2 2.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc.test +10 -0
- package/.editorconfig +4 -0
- package/.env.defaults +1 -0
- package/.env.schema +1 -0
- package/.mocharc.yml +4 -0
- package/.nvmrc +1 -0
- package/.travis.yml +7 -0
- package/LICENSE +39 -0
- package/README.md +53 -0
- package/benchmark.js +50 -0
- package/bin/bsv.js +11 -0
- package/bin/network.js +92 -0
- package/build/bsv.js +3 -0
- package/build/index.html +20 -0
- package/dist/bsv.bundle.js +3 -0
- package/dist/bsv.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv.bundle.js.map +1 -0
- package/dist/bsv.cjs.js +3 -0
- package/dist/bsv.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv.cjs.js.map +1 -0
- package/dist/bsv.min.js +2 -0
- package/dist/bsv.module.js +3 -0
- package/dist/bsv.module.js.LICENSE.txt +1 -0
- package/dist/bsv.module.js.map +1 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +93 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +784 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9387 -0
- package/docs/block.html +36 -0
- package/docs/ecies.html +81 -0
- package/docs/index.html +84 -0
- package/docs/networks.html +43 -0
- package/docs/script.html +101 -0
- package/docs/transaction.html +150 -0
- package/docs/unspentoutput.html +32 -0
- package/entry.js +82 -0
- package/index.js +102 -0
- package/jsdoc.json +5 -0
- package/karma.conf.js +78 -0
- package/lib/ach.js +59 -0
- package/lib/address.js +264 -0
- package/lib/aes.js +57 -0
- package/lib/aescbc.js +38 -0
- package/lib/base-58-check.js +83 -0
- package/lib/base-58.js +59 -0
- package/lib/bip-32.js +472 -0
- package/lib/bip-39-en-wordlist.js +2054 -0
- package/lib/bip-39-en.js +10 -0
- package/lib/bip-39-es-wordlist.js +2055 -0
- package/lib/bip-39-es.js +10 -0
- package/lib/bip-39-jp-wordlist.js +2053 -0
- package/lib/bip-39-jp.js +10 -0
- package/lib/bip-39-words.js +5 -0
- package/lib/bip-39.js +275 -0
- package/lib/block-header.js +79 -0
- package/lib/block.js +132 -0
- package/lib/bn.js +467 -0
- package/lib/br.js +172 -0
- package/lib/bsm.js +127 -0
- package/lib/bw.js +188 -0
- package/lib/cbc.js +140 -0
- package/lib/cmp.js +37 -0
- package/lib/config.js +50 -0
- package/lib/constants.js +150 -0
- package/lib/ecdsa.js +495 -0
- package/lib/ecies.js +151 -0
- package/lib/hash-cache.js +41 -0
- package/lib/hash.js +191 -0
- package/lib/interp.js +1853 -0
- package/lib/inv.js +49 -0
- package/lib/key-pair.js +133 -0
- package/lib/mainnet-address.js +5 -0
- package/lib/mainnet-bip-32.js +5 -0
- package/lib/mainnet-key-pair.js +5 -0
- package/lib/mainnet-priv-key.js +5 -0
- package/lib/merkle.js +92 -0
- package/lib/msg.js +184 -0
- package/lib/op-code.js +398 -0
- package/lib/point.js +134 -0
- package/lib/priv-key.js +157 -0
- package/lib/pub-key.js +230 -0
- package/lib/rabin.js +300 -0
- package/lib/random.js +28 -0
- package/lib/script.js +826 -0
- package/lib/sig-operations.js +120 -0
- package/lib/sig.js +399 -0
- package/lib/struct.js +389 -0
- package/lib/testnet-address.js +5 -0
- package/lib/testnet-bip-32.js +5 -0
- package/lib/testnet-key-pair.js +5 -0
- package/lib/testnet-priv-key.js +5 -0
- package/lib/tx-builder.js +593 -0
- package/lib/tx-in.js +172 -0
- package/lib/tx-out-map.js +57 -0
- package/lib/tx-out.js +73 -0
- package/lib/tx-verifier.js +209 -0
- package/lib/tx.js +429 -0
- package/lib/var-int.js +73 -0
- package/lib/version.js +92 -0
- package/lib/workers-result.js +65 -0
- package/lib/workers.js +77 -0
- package/load-babel.js +3 -0
- package/mkdocs.yml +37 -0
- package/package.json +138 -0
- package/test/ach.js +172 -0
- package/test/address.js +464 -0
- package/test/aes.js +96 -0
- package/test/aescbc.js +65 -0
- package/test/base-58-check.js +128 -0
- package/test/base-58.js +113 -0
- package/test/bip-32.js +721 -0
- package/test/bip-39.js +282 -0
- package/test/block-header.js +152 -0
- package/test/block.js +243 -0
- package/test/bn.js +643 -0
- package/test/br.js +353 -0
- package/test/bsm.js +141 -0
- package/test/bw.js +292 -0
- package/test/cbc.js +335 -0
- package/test/cmp.js +56 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +152 -0
- package/test/data/bitcoind/base58_keys_valid.json +452 -0
- package/test/data/bitcoind/blocks.json +27 -0
- package/test/data/bitcoind/script_tests.json +2244 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +22 -0
- package/test/data/bitcoind/tx_invalid.json +177 -0
- package/test/data/bitcoind/tx_valid.json +224 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +12 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +1004 -0
- package/test/data/tx_creation.json +85 -0
- package/test/ecdsa.js +748 -0
- package/test/ecies.js +238 -0
- package/test/global.js +13 -0
- package/test/hash-cache.js +52 -0
- package/test/hash.js +354 -0
- package/test/index.js +10 -0
- package/test/interp.js +512 -0
- package/test/inv.js +65 -0
- package/test/key-pair.js +243 -0
- package/test/merkle.js +179 -0
- package/test/msg.js +295 -0
- package/test/op-code.js +79 -0
- package/test/point.js +375 -0
- package/test/priv-key.js +264 -0
- package/test/pub-key.js +549 -0
- package/test/random.js +31 -0
- package/test/script.js +1340 -0
- package/test/sig-operations.js +149 -0
- package/test/sig.js +662 -0
- package/test/struct.js +552 -0
- package/test/tx-builder.js +2104 -0
- package/test/tx-in.js +198 -0
- package/test/tx-out-map.js +104 -0
- package/test/tx-out.js +162 -0
- package/test/tx-verifier.js +246 -0
- package/test/tx.js +539 -0
- package/test/var-int.js +132 -0
- package/test/vectors/aes.json +10392 -0
- package/test/vectors/aescbc.json +620 -0
- package/test/vectors/bip39.json +502 -0
- package/test/vectors/bip69.json +215 -0
- package/test/vectors/bitcoin-abc/script_tests.json +48 -0
- package/test/vectors/bitcoin-abc/sighash.json +7004 -0
- package/test/vectors/bitcoin-sv/script_tests.json +248 -0
- package/test/vectors/bitcoind/script_invalid.json +1151 -0
- package/test/vectors/bitcoind/script_valid.json +1462 -0
- package/test/vectors/bitcoind/sighash.json +3503 -0
- package/test/vectors/bitcoind/tx_invalid.json +883 -0
- package/test/vectors/bitcoind/tx_valid.json +1150 -0
- package/test/vectors/coolest-tx-ever-sent.json +8 -0
- package/test/vectors/ecdsa.json +300 -0
- package/test/vectors/hash.json +6133 -0
- package/test/vectors/kdf.json +47 -0
- package/test/vectors/largesttx.json +5 -0
- package/test/vectors/largesttxblock.json +6 -0
- package/test/vectors/sig.json +79 -0
- package/test/vectors/sighash-single-bug.json +8 -0
- package/test/version.js +81 -0
- package/test/workers-result.js +89 -0
- package/test/workers.js +91 -0
- package/test-index.js +3 -0
- package/test1.html +88 -0
- package/webpack.config.js +60 -0
- package/webpack.config.js_entry +57 -0
- package/yarn-error.log +6199 -0
package/lib/pub-key.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public Key
|
|
3
|
+
* ==========
|
|
4
|
+
*
|
|
5
|
+
* A public key corresponds to a private key. If you have a private key, you
|
|
6
|
+
* can find the corresponding public key with new PubKey().fromPrivKey(privKey).
|
|
7
|
+
*/
|
|
8
|
+
'use strict'
|
|
9
|
+
|
|
10
|
+
import { Point } from './point'
|
|
11
|
+
import { Bn } from './bn'
|
|
12
|
+
import { Bw } from './bw'
|
|
13
|
+
import { Struct } from './struct'
|
|
14
|
+
import { Workers } from './workers'
|
|
15
|
+
|
|
16
|
+
class PubKey extends Struct {
|
|
17
|
+
constructor (point, compressed) {
|
|
18
|
+
super({ point, compressed })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fromJSON (json) {
|
|
22
|
+
this.fromFastHex(json)
|
|
23
|
+
return this
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
toJSON () {
|
|
27
|
+
return this.toFastHex()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fromPrivKey (privKey) {
|
|
31
|
+
this.fromObject({
|
|
32
|
+
point: Point.getG().mul(privKey.bn),
|
|
33
|
+
compressed: privKey.compressed
|
|
34
|
+
})
|
|
35
|
+
return this
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static fromPrivKey (privKey) {
|
|
39
|
+
return new this().fromPrivKey(privKey)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async asyncFromPrivKey (privKey) {
|
|
43
|
+
const workersResult = await Workers.asyncObjectMethod(this, 'fromPrivKey', [
|
|
44
|
+
privKey
|
|
45
|
+
])
|
|
46
|
+
return this.fromFastBuffer(workersResult.resbuf)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static asyncFromPrivKey (privKey) {
|
|
50
|
+
return new this().asyncFromPrivKey(privKey)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fromBuffer (buf, strict) {
|
|
54
|
+
return this.fromDer(buf, strict)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async asyncFromBuffer (buf, strict) {
|
|
58
|
+
const args = [buf, strict]
|
|
59
|
+
const workersResult = await Workers.asyncObjectMethod(
|
|
60
|
+
this,
|
|
61
|
+
'fromBuffer',
|
|
62
|
+
args
|
|
63
|
+
)
|
|
64
|
+
return this.fromFastBuffer(workersResult.resbuf)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fromFastBuffer (buf) {
|
|
68
|
+
if (buf.length === 0) {
|
|
69
|
+
return this
|
|
70
|
+
}
|
|
71
|
+
const compressed = Boolean(buf[0])
|
|
72
|
+
buf = buf.slice(1)
|
|
73
|
+
this.fromDer(buf)
|
|
74
|
+
this.compressed = compressed
|
|
75
|
+
return this
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* In order to mimic the non-strict style of OpenSSL, set strict = false. For
|
|
80
|
+
* information and what prefixes 0x06 and 0x07 mean, in addition to the normal
|
|
81
|
+
* compressed and uncompressed public keys, see the message by Peter Wuille
|
|
82
|
+
* where he discovered these "hybrid pubKeys" on the mailing list:
|
|
83
|
+
* http://sourceforge.net/p/bitcoin/mailman/message/29416133/
|
|
84
|
+
*/
|
|
85
|
+
fromDer (buf, strict) {
|
|
86
|
+
if (strict === undefined) {
|
|
87
|
+
strict = true
|
|
88
|
+
} else {
|
|
89
|
+
strict = false
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
buf[0] === 0x04 ||
|
|
93
|
+
(!strict && (buf[0] === 0x06 || buf[0] === 0x07))
|
|
94
|
+
) {
|
|
95
|
+
const xbuf = buf.slice(1, 33)
|
|
96
|
+
const ybuf = buf.slice(33, 65)
|
|
97
|
+
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
|
|
98
|
+
throw new Error('LEngth of x and y must be 32 bytes')
|
|
99
|
+
}
|
|
100
|
+
const x = new Bn(xbuf)
|
|
101
|
+
const y = new Bn(ybuf)
|
|
102
|
+
this.point = new Point(x, y)
|
|
103
|
+
this.compressed = false
|
|
104
|
+
} else if (buf[0] === 0x03) {
|
|
105
|
+
const xbuf = buf.slice(1)
|
|
106
|
+
const x = new Bn(xbuf)
|
|
107
|
+
this.fromX(true, x)
|
|
108
|
+
this.compressed = true
|
|
109
|
+
} else if (buf[0] === 0x02) {
|
|
110
|
+
const xbuf = buf.slice(1)
|
|
111
|
+
const x = new Bn(xbuf)
|
|
112
|
+
this.fromX(false, x)
|
|
113
|
+
this.compressed = true
|
|
114
|
+
} else {
|
|
115
|
+
throw new Error('Invalid DER format pubKey')
|
|
116
|
+
}
|
|
117
|
+
return this
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static fromDer (buf, strict) {
|
|
121
|
+
return new this().fromDer(buf, strict)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fromString (str) {
|
|
125
|
+
this.fromDer(Buffer.from(str, 'hex'))
|
|
126
|
+
return this
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fromX (odd, x) {
|
|
130
|
+
if (typeof odd !== 'boolean') {
|
|
131
|
+
throw new Error('Must specify whether y is odd or not (true or false)')
|
|
132
|
+
}
|
|
133
|
+
this.point = Point.fromX(odd, x)
|
|
134
|
+
return this
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static fromX (odd, x) {
|
|
138
|
+
return new this().fromX(odd, x)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
toBuffer () {
|
|
142
|
+
const compressed = this.compressed === undefined ? true : this.compressed
|
|
143
|
+
return this.toDer(compressed)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
toFastBuffer () {
|
|
147
|
+
if (!this.point) {
|
|
148
|
+
return Buffer.alloc(0)
|
|
149
|
+
}
|
|
150
|
+
const bw = new Bw()
|
|
151
|
+
const compressed =
|
|
152
|
+
this.compressed === undefined ? true : Boolean(this.compressed)
|
|
153
|
+
bw.writeUInt8(Number(compressed))
|
|
154
|
+
bw.write(this.toDer(false))
|
|
155
|
+
return bw.toBuffer()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
toDer (compressed) {
|
|
159
|
+
compressed = compressed === undefined ? this.compressed : compressed
|
|
160
|
+
if (typeof compressed !== 'boolean') {
|
|
161
|
+
throw new Error(
|
|
162
|
+
'Must specify whether the public key is compressed or not (true or false)'
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const x = this.point.getX()
|
|
167
|
+
const y = this.point.getY()
|
|
168
|
+
|
|
169
|
+
const xbuf = x.toBuffer({ size: 32 })
|
|
170
|
+
const ybuf = y.toBuffer({ size: 32 })
|
|
171
|
+
|
|
172
|
+
let prefix
|
|
173
|
+
if (!compressed) {
|
|
174
|
+
prefix = Buffer.from([0x04])
|
|
175
|
+
return Buffer.concat([prefix, xbuf, ybuf])
|
|
176
|
+
} else {
|
|
177
|
+
const odd = ybuf[ybuf.length - 1] % 2
|
|
178
|
+
if (odd) {
|
|
179
|
+
prefix = Buffer.from([0x03])
|
|
180
|
+
} else {
|
|
181
|
+
prefix = Buffer.from([0x02])
|
|
182
|
+
}
|
|
183
|
+
return Buffer.concat([prefix, xbuf])
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
toString () {
|
|
188
|
+
const compressed = this.compressed === undefined ? true : this.compressed
|
|
189
|
+
return this.toDer(compressed).toString('hex')
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Translated from bitcoind's IsCompressedOrUncompressedPubKey
|
|
194
|
+
*/
|
|
195
|
+
static isCompressedOrUncompressed (buf) {
|
|
196
|
+
if (buf.length < 33) {
|
|
197
|
+
// Non-canonical public key: too short
|
|
198
|
+
return false
|
|
199
|
+
}
|
|
200
|
+
if (buf[0] === 0x04) {
|
|
201
|
+
if (buf.length !== 65) {
|
|
202
|
+
// Non-canonical public key: invalid length for uncompressed key
|
|
203
|
+
return false
|
|
204
|
+
}
|
|
205
|
+
} else if (buf[0] === 0x02 || buf[0] === 0x03) {
|
|
206
|
+
if (buf.length !== 33) {
|
|
207
|
+
// Non-canonical public key: invalid length for compressed key
|
|
208
|
+
return false
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
// Non-canonical public key: neither compressed nor uncompressed
|
|
212
|
+
return false
|
|
213
|
+
}
|
|
214
|
+
return true
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
|
|
218
|
+
validate () {
|
|
219
|
+
if (this.point.isInfinity()) {
|
|
220
|
+
throw new Error('point: Point cannot be equal to Infinity')
|
|
221
|
+
}
|
|
222
|
+
if (this.point.eq(new Point(new Bn(0), new Bn(0)))) {
|
|
223
|
+
throw new Error('point: Point cannot be equal to 0, 0')
|
|
224
|
+
}
|
|
225
|
+
this.point.validate()
|
|
226
|
+
return this
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { PubKey }
|
package/lib/rabin.js
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
//https://github.com/oraclesv/oracledb/blob/master/src/rabin/utils.js
|
|
2
|
+
|
|
3
|
+
//https://github.com/oraclesv/oracledb/blob/master/src/rabin/rabin.js
|
|
4
|
+
|
|
5
|
+
// utils.js
|
|
6
|
+
// Helper functions
|
|
7
|
+
function checkIfValidHexString(hexString) {
|
|
8
|
+
if(typeof hexString !== 'string')
|
|
9
|
+
return false;
|
|
10
|
+
let re = new RegExp('^(0x|0X)?[a-fA-F0-9]+$');
|
|
11
|
+
return re.test(hexString);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function decimalToHexString(number) {
|
|
15
|
+
if(typeof(number) !== 'bigint' && isNaN(number))
|
|
16
|
+
throw ("Error: Argument %s should be a Number or BigInt", number);
|
|
17
|
+
|
|
18
|
+
if (number < 0) {
|
|
19
|
+
number = 0xFFFFFFFF + number + 1;
|
|
20
|
+
}
|
|
21
|
+
return '0x'+number.toString(16);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function hexStringToDecimal(hexString) {
|
|
25
|
+
if (!checkIfValidHexString(hexString))
|
|
26
|
+
throw ("Error: Hex %s should be hexadecimal with or without '0x' at the beginning.", hexString);
|
|
27
|
+
// Remove 0x from string if necessary
|
|
28
|
+
hexString = hexString.replace('0x', '');
|
|
29
|
+
|
|
30
|
+
var i, j, digits = [0],
|
|
31
|
+
carry;
|
|
32
|
+
for (i = 0; i < hexString.length; i += 1) {
|
|
33
|
+
carry = parseInt(hexString.charAt(i), 16);
|
|
34
|
+
for (j = 0; j < digits.length; j += 1) {
|
|
35
|
+
digits[j] = digits[j] * 16 + carry;
|
|
36
|
+
carry = digits[j] / 10 | 0;
|
|
37
|
+
digits[j] %= 10;
|
|
38
|
+
}
|
|
39
|
+
while (carry > 0) {
|
|
40
|
+
digits.push(carry % 10);
|
|
41
|
+
carry = carry / 10 | 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return digits.reverse().join('');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hexStringToBigInt(hexString){
|
|
48
|
+
return BigInt(hexStringToDecimal(hexString));
|
|
49
|
+
}
|
|
50
|
+
// Test functions
|
|
51
|
+
/**
|
|
52
|
+
* Returns a random integer between min (inclusive) and max (inclusive).
|
|
53
|
+
* The value is no lower than min (or the next integer greater than min
|
|
54
|
+
* if min isn't an integer) and no greater than max (or the next integer
|
|
55
|
+
* lower than max if max isn't an integer).
|
|
56
|
+
* Using Math.round() will give you a non-uniform distribution!
|
|
57
|
+
*/
|
|
58
|
+
function getRandomInt(min, max) {
|
|
59
|
+
min = Math.ceil(min);
|
|
60
|
+
max = Math.floor(max);
|
|
61
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Random hex string generator
|
|
65
|
+
function getRandomHex(len) {
|
|
66
|
+
let output = '';
|
|
67
|
+
for (let i = 0; i < len; ++i) {
|
|
68
|
+
output += (Math.floor(Math.random() * 16)).toString(16);
|
|
69
|
+
}
|
|
70
|
+
return output;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function sign(value) {
|
|
74
|
+
if (value > 0n) {
|
|
75
|
+
return 1n;
|
|
76
|
+
}
|
|
77
|
+
if (value < 0n) {
|
|
78
|
+
return -1n;
|
|
79
|
+
}
|
|
80
|
+
return 0n;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function bigIntAbsoluteValue(value) {
|
|
84
|
+
if (sign(value) === -1n) {
|
|
85
|
+
return -value;
|
|
86
|
+
}
|
|
87
|
+
else return value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
A Rabin Signature JavaScript module adapted
|
|
93
|
+
from: https://github.com/scrypt-sv/rabin/blob/master/rabin.py
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
const { toBigIntLE } = require('bigint-buffer');
|
|
100
|
+
const { checkIfValidHexString,
|
|
101
|
+
hexStringToBigInt,
|
|
102
|
+
bigIntAbsoluteValue } = require('./utils');
|
|
103
|
+
let crypto;
|
|
104
|
+
try {
|
|
105
|
+
crypto = require('crypto');
|
|
106
|
+
} catch (err) {
|
|
107
|
+
throw('Error: crypto support is disabled!');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function greatestCommonDivisor(a, b) {
|
|
111
|
+
if ((typeof a !== 'bigint') || (typeof b !== 'bigint'))
|
|
112
|
+
throw "Error: Incorrect argument(s) to greatestCommonDivisor()";
|
|
113
|
+
|
|
114
|
+
a = bigIntAbsoluteValue(a);
|
|
115
|
+
b = bigIntAbsoluteValue(b);
|
|
116
|
+
if (b > a) {
|
|
117
|
+
let t = a;
|
|
118
|
+
a = b;
|
|
119
|
+
b = t;
|
|
120
|
+
}
|
|
121
|
+
while (b > 0) {
|
|
122
|
+
let t = b;
|
|
123
|
+
b = a % b;
|
|
124
|
+
a = t;
|
|
125
|
+
}
|
|
126
|
+
return a;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Calculates: base^exponent % modulus
|
|
130
|
+
function powerMod(base, exponent, modulus) {
|
|
131
|
+
if (modulus === 1n) return 0n;
|
|
132
|
+
let result = 1n;
|
|
133
|
+
base = base % modulus;
|
|
134
|
+
while (exponent > 0n) {
|
|
135
|
+
if (exponent % 2n === 1n) //odd number
|
|
136
|
+
result = (result * base) % modulus;
|
|
137
|
+
exponent = exponent >> 1n; //divide by 2
|
|
138
|
+
base = (base * base) % modulus;
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function rabinHashBytes(bytes) {
|
|
144
|
+
hBytes = crypto.createHash('sha256').update(bytes).digest();
|
|
145
|
+
let idx = hBytes.byteLength / 2;
|
|
146
|
+
let hl = crypto.createHash('sha256').update(hBytes.slice(0, idx)).digest();
|
|
147
|
+
let hr = crypto.createHash('sha256').update(hBytes.slice(idx, hBytes.byteLength)).digest();
|
|
148
|
+
return toBigIntLE(Buffer.concat([hl, hr]));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function calculateNextPrime(p) {
|
|
152
|
+
smallPrimesProduct = 3n * 5n * 7n * 11n * 13n * 17n * 19n * 23n * 29n;
|
|
153
|
+
while (greatestCommonDivisor(p, smallPrimesProduct) != 1) {
|
|
154
|
+
p = p + 4n;
|
|
155
|
+
}
|
|
156
|
+
if (powerMod(2n, p - 1n, p) != 1n) {
|
|
157
|
+
return calculateNextPrime(p + 4n);
|
|
158
|
+
}
|
|
159
|
+
if (powerMod(3n, p - 1n, p) != 1n) {
|
|
160
|
+
return calculateNextPrime(p + 4n);
|
|
161
|
+
}
|
|
162
|
+
if (powerMod(5n, p - 1n, p) != 1n) {
|
|
163
|
+
return calculateNextPrime(p + 4n);
|
|
164
|
+
}
|
|
165
|
+
if (powerMod(17n, p - 1n, p) != 1n) {
|
|
166
|
+
return calculateNextPrime(p + 4n);
|
|
167
|
+
}
|
|
168
|
+
return p;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getPrimeNumber(p) {
|
|
172
|
+
while (p % 4n != 3n) {
|
|
173
|
+
p = p + 1n;
|
|
174
|
+
}
|
|
175
|
+
return calculateNextPrime(p);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const paddingBuffer = Buffer.from('00', 'hex');
|
|
179
|
+
|
|
180
|
+
function root(dataBuffer, p, q, nRabin) {
|
|
181
|
+
let sig, x, paddingByteCount = 0;
|
|
182
|
+
while (true) {
|
|
183
|
+
x = rabinHashBytes(dataBuffer) % nRabin;
|
|
184
|
+
sig = powerMod(p, q - 2n, q) * p * powerMod(x, (q + 1n) / 4n, q);
|
|
185
|
+
sig = (powerMod(q, p - 2n, p) * q * powerMod(x, (p + 1n) / 4n, p) + sig) % (nRabin);
|
|
186
|
+
if (((sig * sig) % nRabin) === x) {
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
dataBuffer = Buffer.concat([dataBuffer, paddingBuffer]);
|
|
190
|
+
paddingByteCount++;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
"signature": sig,
|
|
194
|
+
"paddingByteCount": paddingByteCount
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Calculates Key nRabin (public key) from private key parts p & q
|
|
200
|
+
* @param {BigInt} p Key private key 'p' part
|
|
201
|
+
* @param {BigInt} q Key private key 'q' part
|
|
202
|
+
* @returns {BigInt} Key nRabin (public key) = p * q
|
|
203
|
+
*/
|
|
204
|
+
function privKeyToPubKey(p,q){
|
|
205
|
+
if(typeof(p) !== 'bigint' || typeof(q) !== 'bigint')
|
|
206
|
+
throw("Error: Key parts (p,q) should be BigInts (denoted by trailing 'n').")
|
|
207
|
+
return p * q;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Generates Private Key p & q parts from a PRNG seed
|
|
212
|
+
* @returns {JSON} {'p': BigInt,'q': BigInt}
|
|
213
|
+
*/
|
|
214
|
+
function generatePrivKey() {
|
|
215
|
+
// Get a seed value from a random buffer and convert it to a BigInt
|
|
216
|
+
let seed = crypto.randomBytes(2048);
|
|
217
|
+
return generatePrivKeyFromSeed(seed);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Generates Private Key p & q parts from Seed
|
|
222
|
+
* @param {Buffer} seed
|
|
223
|
+
* @returns {JSON} {'p': BigInt,'q': BigInt}
|
|
224
|
+
*/
|
|
225
|
+
function generatePrivKeyFromSeed(seed) {
|
|
226
|
+
let p = getPrimeNumber(rabinHashBytes(Buffer.from(seed, 'hex')) % ((2n ** 501n) + 1n));
|
|
227
|
+
let q = getPrimeNumber(rabinHashBytes(Buffer.from(seed + '00', 'hex')) % ((2n ** 501n) + 1n));
|
|
228
|
+
return {
|
|
229
|
+
"p": p,
|
|
230
|
+
"q": q
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Creates a Rabin signature of hexadecimal data with a given key's values
|
|
236
|
+
* @param {String} dataHex Hexadecimal data string value
|
|
237
|
+
* @param {BigInt} p Key 'p' value
|
|
238
|
+
* @param {BigInt} q Key 'q' value
|
|
239
|
+
* @param {BigInt} nRabin Key nRabin value
|
|
240
|
+
* @returns {JSON} {"signature": BigInt, "paddingByteCount": Number} Signature and padding count
|
|
241
|
+
*/
|
|
242
|
+
function sign(dataHex, p, q, nRabin) {
|
|
243
|
+
// Check if data is valid hex
|
|
244
|
+
if (!checkIfValidHexString(dataHex))
|
|
245
|
+
throw ("Error: dataHex %s should be a hexadecimal String with or without '0x' at the beginning.", dataHex);
|
|
246
|
+
// Remove 0x from data if necessary
|
|
247
|
+
dataHex = dataHex.replace('0x', '');
|
|
248
|
+
// Check key parts are correct values
|
|
249
|
+
if(typeof(p) !== 'bigint' || typeof(q) !== 'bigint' || typeof(nRabin) !== 'bigint')
|
|
250
|
+
throw("Error: Key parts (p,q) should be BigInts (denoted by trailing 'n').")
|
|
251
|
+
return root(Buffer.from(dataHex, 'hex'), p, q, nRabin);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Verifies a Rabin signature of hexadecimal data with given padding count, signature and key nRabin value
|
|
256
|
+
* @param {String} dataHex Hexadecimal data string value
|
|
257
|
+
* @param {Number} paddingByteCount Padding byte count
|
|
258
|
+
* @param {BigInt} signature Rabin signature value
|
|
259
|
+
* @param {BigInt} nRabin Public Key nRabin value
|
|
260
|
+
* @returns {Boolean} If signature is valid or not
|
|
261
|
+
*/
|
|
262
|
+
function verify(dataHex, paddingByteCount, signature, nRabin) {
|
|
263
|
+
// Check if data is valid hex
|
|
264
|
+
if (!checkIfValidHexString(dataHex))
|
|
265
|
+
throw ("Error: Data %s should be a hexadecimal String with or without '0x' at the beginning.", dataHex);
|
|
266
|
+
// Remove 0x from data if necessary
|
|
267
|
+
dataHex = dataHex.replace('0x', '');
|
|
268
|
+
// Ensure padding count is a number
|
|
269
|
+
if(typeof paddingByteCount !== 'number')
|
|
270
|
+
throw ("Error: paddingByteCount should be a number");
|
|
271
|
+
// Check if signature is a BigInt
|
|
272
|
+
if(typeof(signature) !== 'bigint')
|
|
273
|
+
throw("Error: Signature should be a BigInt (denoted by trailing 'n').");
|
|
274
|
+
// Check if nRabin is a BigInt
|
|
275
|
+
if(typeof(nRabin) !== 'bigint')
|
|
276
|
+
throw("Error: Public Key nRabin should be a BigInt (denoted by trailing 'n').");
|
|
277
|
+
|
|
278
|
+
let dataBuffer = Buffer.from(dataHex, 'hex');
|
|
279
|
+
let paddingBuffer = Buffer.from('00'.repeat(paddingByteCount), 'hex');
|
|
280
|
+
let paddedDataBuffer = Buffer.concat([dataBuffer, paddingBuffer]);
|
|
281
|
+
let dataHash = rabinHashBytes(paddedDataBuffer);
|
|
282
|
+
let hashMod = dataHash % nRabin;
|
|
283
|
+
return hashMod === (signature ** 2n % nRabin);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
module.exports = {
|
|
287
|
+
|
|
288
|
+
checkIfValidHexString,
|
|
289
|
+
decimalToHexString,
|
|
290
|
+
hexStringToDecimal,
|
|
291
|
+
hexStringToBigInt,
|
|
292
|
+
bigIntAbsoluteValue,
|
|
293
|
+
getRandomInt,
|
|
294
|
+
getRandomHex,
|
|
295
|
+
generatePrivKey,
|
|
296
|
+
generatePrivKeyFromSeed,
|
|
297
|
+
privKeyToPubKey,
|
|
298
|
+
sign,
|
|
299
|
+
verify,
|
|
300
|
+
}
|
package/lib/random.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Random Number Generator
|
|
3
|
+
* =======================
|
|
4
|
+
*
|
|
5
|
+
* Random numbers are important in bitcoin primarily for generating private
|
|
6
|
+
* keys. It is also important for creating signatures if you are using a random
|
|
7
|
+
* value of k, but Yours Bitcoin defaults to using deterministic k. That means
|
|
8
|
+
* computing a random private key, or a random seed for use in Bip39 or Bip32,
|
|
9
|
+
* is the primary use of the random number generator. Note that the simplicity
|
|
10
|
+
* of this class is extremely carefully considered. It is easy to audit that
|
|
11
|
+
* this code runs node's randomBytes function. It is also easy to audit that
|
|
12
|
+
* the randomBytes method is correctly interpreted as
|
|
13
|
+
* window.crypto.getRandomValues when this code is browserified by browserify,
|
|
14
|
+
* and thus also works correctly in the browser. We deliberately do not do
|
|
15
|
+
* anything else to this random number in order to minimize possible errors in
|
|
16
|
+
* this absolutely critical code.
|
|
17
|
+
*/
|
|
18
|
+
'use strict'
|
|
19
|
+
|
|
20
|
+
import randomBytes from 'randombytes'
|
|
21
|
+
|
|
22
|
+
class Random {}
|
|
23
|
+
|
|
24
|
+
Random.getRandomBuffer = function (size) {
|
|
25
|
+
return randomBytes(size)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { Random }
|