bsv2 2.0.24 → 2.1.1
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/dist/bsv2.mjs +36061 -4
- package/package.json +35 -39
- package/.babelrc.test +0 -10
- package/.editorconfig +0 -4
- package/.env.defaults +0 -1
- package/.env.schema +0 -1
- package/.husky/pre-commit +0 -1
- package/.mocharc.yml +0 -4
- package/.nvmrc +0 -1
- package/.travis.yml +0 -7
- package/benchmark.js +0 -50
- package/bin/bsv.js +0 -11
- package/bin/network.js +0 -92
- package/build/index.html +0 -20
- package/dist/bsv2.bundle.js +0 -8
- package/dist/bsv2.cjs.js +0 -2
- package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
- package/dist/bsv2.module.js +0 -2
- package/entry.js +0 -82
- package/index.js +0 -102
- package/karma.conf.js +0 -78
- package/lib/ach.js +0 -59
- package/lib/address.js +0 -264
- package/lib/aes.js +0 -57
- package/lib/aescbc.js +0 -38
- package/lib/base-58-check.js +0 -83
- package/lib/base-58.js +0 -59
- package/lib/bip-32.js +0 -472
- package/lib/bip-39-en-wordlist.js +0 -2054
- package/lib/bip-39-en.js +0 -10
- package/lib/bip-39-es-wordlist.js +0 -2055
- package/lib/bip-39-es.js +0 -10
- package/lib/bip-39-jp-wordlist.js +0 -2053
- package/lib/bip-39-jp.js +0 -10
- package/lib/bip-39-words.js +0 -5
- package/lib/bip-39.js +0 -285
- package/lib/block-header.js +0 -79
- package/lib/block.js +0 -132
- package/lib/bn.js +0 -467
- package/lib/br.js +0 -172
- package/lib/bsm.js +0 -127
- package/lib/bw.js +0 -188
- package/lib/cbc.js +0 -140
- package/lib/cmp.js +0 -37
- package/lib/config.js +0 -50
- package/lib/constants.js +0 -150
- package/lib/ecdsa.js +0 -495
- package/lib/ecies.js +0 -151
- package/lib/hash-cache.js +0 -41
- package/lib/hash.js +0 -191
- package/lib/interp.js +0 -2013
- package/lib/inv.js +0 -49
- package/lib/key-pair.js +0 -133
- package/lib/mainnet-address.js +0 -5
- package/lib/mainnet-bip-32.js +0 -5
- package/lib/mainnet-key-pair.js +0 -5
- package/lib/mainnet-priv-key.js +0 -5
- package/lib/merkle.js +0 -92
- package/lib/msg.js +0 -184
- package/lib/op-code.js +0 -398
- package/lib/point.js +0 -134
- package/lib/priv-key.js +0 -157
- package/lib/pub-key.js +0 -230
- package/lib/random.js +0 -28
- package/lib/script.js +0 -826
- package/lib/sig-operations.js +0 -120
- package/lib/sig.js +0 -399
- package/lib/struct.js +0 -389
- package/lib/testnet-address.js +0 -5
- package/lib/testnet-bip-32.js +0 -5
- package/lib/testnet-key-pair.js +0 -5
- package/lib/testnet-priv-key.js +0 -5
- package/lib/tx-builder.js +0 -593
- package/lib/tx-in.js +0 -172
- package/lib/tx-out-map.js +0 -57
- package/lib/tx-out.js +0 -73
- package/lib/tx-verifier.js +0 -209
- package/lib/tx.js +0 -430
- package/lib/var-int.js +0 -73
- package/lib/version.js +0 -92
- package/lib/workers-result.js +0 -65
- package/lib/workers.js +0 -77
- package/load-babel.js +0 -3
- package/mtest/test.html +0 -15
- package/mtest/test.js +0 -2
- package/mtest/test.mjs +0 -2
- package/test/ach.js +0 -172
- package/test/address.js +0 -464
- package/test/aes.js +0 -96
- package/test/aescbc.js +0 -65
- package/test/base-58-check.js +0 -128
- package/test/base-58.js +0 -113
- package/test/bip-32.js +0 -721
- package/test/bip-39.js +0 -282
- package/test/block-header.js +0 -152
- package/test/block.js +0 -243
- package/test/bn.js +0 -643
- package/test/br.js +0 -353
- package/test/bsm.js +0 -141
- package/test/bw.js +0 -292
- package/test/cbc.js +0 -335
- package/test/cmp.js +0 -56
- package/test/data/bip69.json +0 -215
- package/test/data/bitcoind/base58_keys_invalid.json +0 -152
- package/test/data/bitcoind/base58_keys_valid.json +0 -452
- package/test/data/bitcoind/blocks.json +0 -27
- package/test/data/bitcoind/script_tests.json +0 -2244
- package/test/data/bitcoind/sig_canonical.json +0 -7
- package/test/data/bitcoind/sig_noncanonical.json +0 -22
- package/test/data/bitcoind/tx_invalid.json +0 -177
- package/test/data/bitcoind/tx_valid.json +0 -224
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +0 -12
- package/test/data/blk86756-testnet.json +0 -684
- package/test/data/ecdsa.json +0 -230
- package/test/data/messages.json +0 -22
- package/test/data/sighash.json +0 -1004
- package/test/data/tx_creation.json +0 -85
- package/test/ecdsa.js +0 -748
- package/test/ecies.js +0 -238
- package/test/global.js +0 -13
- package/test/hash-cache.js +0 -52
- package/test/hash.js +0 -354
- package/test/index.js +0 -10
- package/test/interp.js +0 -512
- package/test/inv.js +0 -65
- package/test/key-pair.js +0 -243
- package/test/merkle.js +0 -179
- package/test/msg.js +0 -295
- package/test/op-code.js +0 -79
- package/test/point.js +0 -375
- package/test/priv-key.js +0 -264
- package/test/pub-key.js +0 -549
- package/test/random.js +0 -31
- package/test/script.js +0 -1340
- package/test/sig-operations.js +0 -149
- package/test/sig.js +0 -662
- package/test/struct.js +0 -552
- package/test/tx-builder.js +0 -2104
- package/test/tx-in.js +0 -198
- package/test/tx-out-map.js +0 -104
- package/test/tx-out.js +0 -162
- package/test/tx-verifier.js +0 -246
- package/test/tx.js +0 -539
- package/test/var-int.js +0 -132
- package/test/vectors/aes.json +0 -10392
- package/test/vectors/aescbc.json +0 -620
- package/test/vectors/bip39.json +0 -502
- package/test/vectors/bip69.json +0 -215
- package/test/vectors/bitcoin-abc/script_tests.json +0 -48
- package/test/vectors/bitcoin-abc/sighash.json +0 -7004
- package/test/vectors/bitcoin-sv/script_tests.json +0 -248
- package/test/vectors/bitcoind/script_invalid.json +0 -1151
- package/test/vectors/bitcoind/script_valid.json +0 -1462
- package/test/vectors/bitcoind/sighash.json +0 -3503
- package/test/vectors/bitcoind/tx_invalid.json +0 -883
- package/test/vectors/bitcoind/tx_valid.json +0 -1150
- package/test/vectors/coolest-tx-ever-sent.json +0 -8
- package/test/vectors/ecdsa.json +0 -300
- package/test/vectors/hash.json +0 -6133
- package/test/vectors/kdf.json +0 -47
- package/test/vectors/largesttx.json +0 -5
- package/test/vectors/largesttxblock.json +0 -6
- package/test/vectors/sig.json +0 -79
- package/test/vectors/sighash-single-bug.json +0 -8
- package/test/version.js +0 -81
- package/test/workers-result.js +0 -89
- package/test/workers.js +0 -91
- package/test-index.js +0 -3
- package/webpack.config.js +0 -115
package/lib/bsm.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bitcoin Signed Message
|
|
3
|
-
* ======================
|
|
4
|
-
*
|
|
5
|
-
* "Bitcoin Signed Message" just refers to a standard way of signing and
|
|
6
|
-
* verifying an arbitrary message. The standard way to do this involves using a
|
|
7
|
-
* "Bitcoin Signed Message:\n" prefix, which this code does. You are probably
|
|
8
|
-
* interested in the static Bsm.sign( ... ) and Bsm.verify( ... ) functions,
|
|
9
|
-
* which deal with a base64 string representing the compressed format of a
|
|
10
|
-
* signature.
|
|
11
|
-
*/
|
|
12
|
-
'use strict'
|
|
13
|
-
|
|
14
|
-
import { Address } from './address'
|
|
15
|
-
import { Bw } from './bw'
|
|
16
|
-
import { cmp } from './cmp'
|
|
17
|
-
import { Ecdsa } from './ecdsa'
|
|
18
|
-
import { Hash } from './hash'
|
|
19
|
-
import { KeyPair } from './key-pair'
|
|
20
|
-
import { Sig } from './sig'
|
|
21
|
-
import { Struct } from './struct'
|
|
22
|
-
import { Workers } from './workers'
|
|
23
|
-
|
|
24
|
-
class Bsm extends Struct {
|
|
25
|
-
constructor (messageBuf, keyPair, sig, address, verified) {
|
|
26
|
-
super({ messageBuf, keyPair, sig, address, verified })
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
static magicHash (messageBuf) {
|
|
30
|
-
if (!Buffer.isBuffer(messageBuf)) {
|
|
31
|
-
throw new Error('messageBuf must be a buffer')
|
|
32
|
-
}
|
|
33
|
-
const bw = new Bw()
|
|
34
|
-
bw.writeVarIntNum(Bsm.magicBytes.length)
|
|
35
|
-
bw.write(Bsm.magicBytes)
|
|
36
|
-
bw.writeVarIntNum(messageBuf.length)
|
|
37
|
-
bw.write(messageBuf)
|
|
38
|
-
const buf = bw.toBuffer()
|
|
39
|
-
|
|
40
|
-
const hashBuf = Hash.sha256Sha256(buf)
|
|
41
|
-
|
|
42
|
-
return hashBuf
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
static async asyncMagicHash (messageBuf) {
|
|
46
|
-
const args = [messageBuf]
|
|
47
|
-
const workersResult = await Workers.asyncClassMethod(Bsm, 'magicHash', args)
|
|
48
|
-
return workersResult.resbuf
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
static sign (messageBuf, keyPair) {
|
|
52
|
-
const m = new Bsm(messageBuf, keyPair)
|
|
53
|
-
m.sign()
|
|
54
|
-
const sigbuf = m.sig.toCompact()
|
|
55
|
-
const sigstr = sigbuf.toString('base64')
|
|
56
|
-
return sigstr
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static async asyncSign (messageBuf, keyPair) {
|
|
60
|
-
const args = [messageBuf, keyPair]
|
|
61
|
-
const workersResult = await Workers.asyncClassMethod(Bsm, 'sign', args)
|
|
62
|
-
const sigstr = JSON.parse(workersResult.resbuf.toString())
|
|
63
|
-
return sigstr
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
static verify (messageBuf, sigstr, address) {
|
|
67
|
-
const sigbuf = Buffer.from(sigstr, 'base64')
|
|
68
|
-
const message = new Bsm()
|
|
69
|
-
message.messageBuf = messageBuf
|
|
70
|
-
message.sig = new Sig().fromCompact(sigbuf)
|
|
71
|
-
message.address = address
|
|
72
|
-
|
|
73
|
-
return message.verify().verified
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
static async asyncVerify (messageBuf, sigstr, address) {
|
|
77
|
-
const args = [messageBuf, sigstr, address]
|
|
78
|
-
const workersResult = await Workers.asyncClassMethod(Bsm, 'verify', args)
|
|
79
|
-
const res = JSON.parse(workersResult.resbuf.toString())
|
|
80
|
-
return res
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
sign () {
|
|
84
|
-
const hashBuf = Bsm.magicHash(this.messageBuf)
|
|
85
|
-
const ecdsa = new Ecdsa().fromObject({
|
|
86
|
-
hashBuf: hashBuf,
|
|
87
|
-
keyPair: this.keyPair
|
|
88
|
-
})
|
|
89
|
-
ecdsa.sign()
|
|
90
|
-
ecdsa.calcrecovery()
|
|
91
|
-
this.sig = ecdsa.sig
|
|
92
|
-
return this
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
verify () {
|
|
96
|
-
const hashBuf = Bsm.magicHash(this.messageBuf)
|
|
97
|
-
|
|
98
|
-
const ecdsa = new Ecdsa()
|
|
99
|
-
ecdsa.hashBuf = hashBuf
|
|
100
|
-
ecdsa.sig = this.sig
|
|
101
|
-
ecdsa.keyPair = new KeyPair()
|
|
102
|
-
ecdsa.keyPair.pubKey = ecdsa.sig2PubKey()
|
|
103
|
-
|
|
104
|
-
if (!ecdsa.verify()) {
|
|
105
|
-
this.verified = false
|
|
106
|
-
return this
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const address = new Address().fromPubKey(
|
|
110
|
-
ecdsa.keyPair.pubKey,
|
|
111
|
-
undefined,
|
|
112
|
-
this.sig.compressed
|
|
113
|
-
)
|
|
114
|
-
// TODO: what if livenet/testnet mismatch?
|
|
115
|
-
if (cmp(address.hashBuf, this.address.hashBuf)) {
|
|
116
|
-
this.verified = true
|
|
117
|
-
} else {
|
|
118
|
-
this.verified = false
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return this
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
Bsm.magicBytes = Buffer.from('Bitcoin Signed Message:\n')
|
|
126
|
-
|
|
127
|
-
export { Bsm }
|
package/lib/bw.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Buffer Writer
|
|
3
|
-
* =============
|
|
4
|
-
*
|
|
5
|
-
* This is the writing complement of the Br. You can easily write
|
|
6
|
-
* VarInts and other basic number types. The way to use it is: buf =
|
|
7
|
-
* new Bw().write(buf1).write(buf2).toBuffer()
|
|
8
|
-
*/
|
|
9
|
-
'use strict'
|
|
10
|
-
|
|
11
|
-
class Bw {
|
|
12
|
-
constructor (bufs) {
|
|
13
|
-
this.fromObject({ bufs })
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
fromObject (obj) {
|
|
17
|
-
this.bufs = obj.bufs || this.bufs || []
|
|
18
|
-
return this
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
getLength () {
|
|
22
|
-
let len = 0
|
|
23
|
-
for (const i in this.bufs) {
|
|
24
|
-
const buf = this.bufs[i]
|
|
25
|
-
len = len + buf.length
|
|
26
|
-
}
|
|
27
|
-
return len
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
toBuffer () {
|
|
31
|
-
return Buffer.concat(this.bufs)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
write (buf) {
|
|
35
|
-
this.bufs.push(buf)
|
|
36
|
-
return this
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
writeReverse (buf) {
|
|
40
|
-
const buf2 = Buffer.alloc(buf.length)
|
|
41
|
-
for (let i = 0; i < buf2.length; i++) {
|
|
42
|
-
buf2[i] = buf[buf.length - 1 - i]
|
|
43
|
-
}
|
|
44
|
-
this.bufs.push(buf2)
|
|
45
|
-
return this
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
writeUInt8 (n) {
|
|
49
|
-
const buf = Buffer.alloc(1)
|
|
50
|
-
buf.writeUInt8(n, 0)
|
|
51
|
-
this.write(buf)
|
|
52
|
-
return this
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
writeInt8 (n) {
|
|
56
|
-
const buf = Buffer.alloc(1)
|
|
57
|
-
buf.writeInt8(n, 0)
|
|
58
|
-
this.write(buf)
|
|
59
|
-
return this
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
writeUInt16BE (n) {
|
|
63
|
-
const buf = Buffer.alloc(2)
|
|
64
|
-
buf.writeUInt16BE(n, 0)
|
|
65
|
-
this.write(buf)
|
|
66
|
-
return this
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
writeInt16BE (n) {
|
|
70
|
-
const buf = Buffer.alloc(2)
|
|
71
|
-
buf.writeInt16BE(n, 0)
|
|
72
|
-
this.write(buf)
|
|
73
|
-
return this
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
writeUInt16LE (n) {
|
|
77
|
-
const buf = Buffer.alloc(2)
|
|
78
|
-
buf.writeUInt16LE(n, 0)
|
|
79
|
-
this.write(buf)
|
|
80
|
-
return this
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
writeInt16LE (n) {
|
|
84
|
-
const buf = Buffer.alloc(2)
|
|
85
|
-
buf.writeInt16LE(n, 0)
|
|
86
|
-
this.write(buf)
|
|
87
|
-
return this
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
writeUInt32BE (n) {
|
|
91
|
-
const buf = Buffer.alloc(4)
|
|
92
|
-
buf.writeUInt32BE(n, 0)
|
|
93
|
-
this.write(buf)
|
|
94
|
-
return this
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
writeInt32BE (n) {
|
|
98
|
-
const buf = Buffer.alloc(4)
|
|
99
|
-
buf.writeInt32BE(n, 0)
|
|
100
|
-
this.write(buf)
|
|
101
|
-
return this
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
writeUInt32LE (n) {
|
|
105
|
-
const buf = Buffer.alloc(4)
|
|
106
|
-
buf.writeUInt32LE(n, 0)
|
|
107
|
-
this.write(buf)
|
|
108
|
-
return this
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
writeInt32LE (n) {
|
|
112
|
-
const buf = Buffer.alloc(4)
|
|
113
|
-
buf.writeInt32LE(n, 0)
|
|
114
|
-
this.write(buf)
|
|
115
|
-
return this
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
writeUInt64BEBn (bn) {
|
|
119
|
-
const buf = bn.toBuffer({ size: 8 })
|
|
120
|
-
this.write(buf)
|
|
121
|
-
return this
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
writeUInt64LEBn (bn) {
|
|
125
|
-
const buf = bn.toBuffer({ size: 8 })
|
|
126
|
-
this.writeReverse(buf)
|
|
127
|
-
return this
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
writeVarIntNum (n) {
|
|
131
|
-
const buf = Bw.varIntBufNum(n)
|
|
132
|
-
this.write(buf)
|
|
133
|
-
return this
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
writeVarIntBn (bn) {
|
|
137
|
-
const buf = Bw.varIntBufBn(bn)
|
|
138
|
-
this.write(buf)
|
|
139
|
-
return this
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
static varIntBufNum (n) {
|
|
143
|
-
let buf
|
|
144
|
-
if (n < 253) {
|
|
145
|
-
buf = Buffer.alloc(1)
|
|
146
|
-
buf.writeUInt8(n, 0)
|
|
147
|
-
} else if (n < 0x10000) {
|
|
148
|
-
buf = Buffer.alloc(1 + 2)
|
|
149
|
-
buf.writeUInt8(253, 0)
|
|
150
|
-
buf.writeUInt16LE(n, 1)
|
|
151
|
-
} else if (n < 0x100000000) {
|
|
152
|
-
buf = Buffer.alloc(1 + 4)
|
|
153
|
-
buf.writeUInt8(254, 0)
|
|
154
|
-
buf.writeUInt32LE(n, 1)
|
|
155
|
-
} else {
|
|
156
|
-
buf = Buffer.alloc(1 + 8)
|
|
157
|
-
buf.writeUInt8(255, 0)
|
|
158
|
-
buf.writeInt32LE(n & -1, 1)
|
|
159
|
-
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5)
|
|
160
|
-
}
|
|
161
|
-
return buf
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
static varIntBufBn (bn) {
|
|
165
|
-
let buf
|
|
166
|
-
const n = bn.toNumber()
|
|
167
|
-
if (n < 253) {
|
|
168
|
-
buf = Buffer.alloc(1)
|
|
169
|
-
buf.writeUInt8(n, 0)
|
|
170
|
-
} else if (n < 0x10000) {
|
|
171
|
-
buf = Buffer.alloc(1 + 2)
|
|
172
|
-
buf.writeUInt8(253, 0)
|
|
173
|
-
buf.writeUInt16LE(n, 1)
|
|
174
|
-
} else if (n < 0x100000000) {
|
|
175
|
-
buf = Buffer.alloc(1 + 4)
|
|
176
|
-
buf.writeUInt8(254, 0)
|
|
177
|
-
buf.writeUInt32LE(n, 1)
|
|
178
|
-
} else {
|
|
179
|
-
const bw = new Bw()
|
|
180
|
-
bw.writeUInt8(255)
|
|
181
|
-
bw.writeUInt64LEBn(bn)
|
|
182
|
-
buf = bw.toBuffer()
|
|
183
|
-
}
|
|
184
|
-
return buf
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export { Bw }
|
package/lib/cbc.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cbc
|
|
3
|
-
* ===
|
|
4
|
-
*
|
|
5
|
-
* Cipher Block Chaining (Cbc). This is a low-level tool for chaining multiple
|
|
6
|
-
* encrypted blocks together, usually with Aes. This is a low-level tool that
|
|
7
|
-
* does not include authentication. You should only be using this if you have
|
|
8
|
-
* authentication at another step. It is best combined with Hmac.
|
|
9
|
-
*
|
|
10
|
-
* http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28Cbc.29
|
|
11
|
-
*/
|
|
12
|
-
'use strict'
|
|
13
|
-
|
|
14
|
-
import { cmp } from './cmp'
|
|
15
|
-
|
|
16
|
-
class Cbc {}
|
|
17
|
-
|
|
18
|
-
Cbc.buf2BlocksBuf = function (buf, blockSize) {
|
|
19
|
-
const bytesize = blockSize / 8
|
|
20
|
-
const blockBufs = []
|
|
21
|
-
|
|
22
|
-
for (let i = 0; i <= buf.length / bytesize; i++) {
|
|
23
|
-
let blockBuf = buf.slice(i * bytesize, i * bytesize + bytesize)
|
|
24
|
-
|
|
25
|
-
if (blockBuf.length < blockSize) {
|
|
26
|
-
blockBuf = Cbc.pkcs7Pad(blockBuf, blockSize)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
blockBufs.push(blockBuf)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return blockBufs
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
Cbc.blockBufs2Buf = function (blockBufs) {
|
|
36
|
-
let last = blockBufs[blockBufs.length - 1]
|
|
37
|
-
last = Cbc.pkcs7Unpad(last)
|
|
38
|
-
blockBufs[blockBufs.length - 1] = last
|
|
39
|
-
|
|
40
|
-
const buf = Buffer.concat(blockBufs)
|
|
41
|
-
|
|
42
|
-
return buf
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
Cbc.encrypt = function (messageBuf, ivBuf, blockCipher, cipherKeyBuf) {
|
|
46
|
-
const blockSize = ivBuf.length * 8
|
|
47
|
-
const blockBufs = Cbc.buf2BlocksBuf(messageBuf, blockSize)
|
|
48
|
-
const encBufs = Cbc.encryptBlocks(blockBufs, ivBuf, blockCipher, cipherKeyBuf)
|
|
49
|
-
const encBuf = Buffer.concat(encBufs)
|
|
50
|
-
return encBuf
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
Cbc.decrypt = function (encBuf, ivBuf, blockCipher, cipherKeyBuf) {
|
|
54
|
-
const bytesize = ivBuf.length
|
|
55
|
-
const encBufs = []
|
|
56
|
-
for (let i = 0; i < encBuf.length / bytesize; i++) {
|
|
57
|
-
encBufs.push(encBuf.slice(i * bytesize, i * bytesize + bytesize))
|
|
58
|
-
}
|
|
59
|
-
const blockBufs = Cbc.decryptBlocks(encBufs, ivBuf, blockCipher, cipherKeyBuf)
|
|
60
|
-
const buf = Cbc.blockBufs2Buf(blockBufs)
|
|
61
|
-
return buf
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
Cbc.encryptBlock = function (blockBuf, ivBuf, blockCipher, cipherKeyBuf) {
|
|
65
|
-
const xorbuf = Cbc.xorBufs(blockBuf, ivBuf)
|
|
66
|
-
const encBuf = blockCipher.encrypt(xorbuf, cipherKeyBuf)
|
|
67
|
-
return encBuf
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
Cbc.decryptBlock = function (encBuf, ivBuf, blockCipher, cipherKeyBuf) {
|
|
71
|
-
const xorbuf = blockCipher.decrypt(encBuf, cipherKeyBuf)
|
|
72
|
-
const blockBuf = Cbc.xorBufs(xorbuf, ivBuf)
|
|
73
|
-
return blockBuf
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
Cbc.encryptBlocks = function (blockBufs, ivBuf, blockCipher, cipherKeyBuf) {
|
|
77
|
-
const encBufs = []
|
|
78
|
-
|
|
79
|
-
for (let i = 0; i < blockBufs.length; i++) {
|
|
80
|
-
const blockBuf = blockBufs[i]
|
|
81
|
-
const encBuf = Cbc.encryptBlock(blockBuf, ivBuf, blockCipher, cipherKeyBuf)
|
|
82
|
-
|
|
83
|
-
encBufs.push(encBuf)
|
|
84
|
-
|
|
85
|
-
ivBuf = encBuf
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return encBufs
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
Cbc.decryptBlocks = function (encBufs, ivBuf, blockCipher, cipherKeyBuf) {
|
|
92
|
-
const blockBufs = []
|
|
93
|
-
|
|
94
|
-
for (let i = 0; i < encBufs.length; i++) {
|
|
95
|
-
const encBuf = encBufs[i]
|
|
96
|
-
const blockBuf = Cbc.decryptBlock(encBuf, ivBuf, blockCipher, cipherKeyBuf)
|
|
97
|
-
|
|
98
|
-
blockBufs.push(blockBuf)
|
|
99
|
-
|
|
100
|
-
ivBuf = encBuf
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return blockBufs
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
Cbc.pkcs7Pad = function (buf, blockSize) {
|
|
107
|
-
const bytesize = blockSize / 8
|
|
108
|
-
const padbytesize = bytesize - buf.length
|
|
109
|
-
const pad = Buffer.alloc(padbytesize)
|
|
110
|
-
pad.fill(padbytesize)
|
|
111
|
-
const paddedbuf = Buffer.concat([buf, pad])
|
|
112
|
-
return paddedbuf
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
Cbc.pkcs7Unpad = function (paddedbuf) {
|
|
116
|
-
const padlength = paddedbuf[paddedbuf.length - 1]
|
|
117
|
-
const padbuf = paddedbuf.slice(paddedbuf.length - padlength, paddedbuf.length)
|
|
118
|
-
const padbuf2 = Buffer.alloc(padlength)
|
|
119
|
-
padbuf2.fill(padlength)
|
|
120
|
-
if (!cmp(padbuf, padbuf2)) {
|
|
121
|
-
throw new Error('invalid padding')
|
|
122
|
-
}
|
|
123
|
-
return paddedbuf.slice(0, paddedbuf.length - padlength)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
Cbc.xorBufs = function (buf1, buf2) {
|
|
127
|
-
if (buf1.length !== buf2.length) {
|
|
128
|
-
throw new Error('bufs must have the same length')
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const buf = Buffer.alloc(buf1.length)
|
|
132
|
-
|
|
133
|
-
for (let i = 0; i < buf1.length; i++) {
|
|
134
|
-
buf[i] = buf1[i] ^ buf2[i]
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return buf
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export { Cbc }
|
package/lib/cmp.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Constant-Time Buffer Compare
|
|
3
|
-
* ============================
|
|
4
|
-
*
|
|
5
|
-
* A constant-time comparison function. This should be used in any security
|
|
6
|
-
* sensitive code where leaking timing information may lead to lessened
|
|
7
|
-
* security. Note that if the buffers are not equal in length, this function
|
|
8
|
-
* loops for the longest buffer, which may not be necessary. Usually this
|
|
9
|
-
* function should be used for buffers that would otherwise be equal length,
|
|
10
|
-
* such as a hash, particularly Hmacs.
|
|
11
|
-
*
|
|
12
|
-
* The algorithm here, which is XORs each byte (or, if undefined, 0) with the
|
|
13
|
-
* corresponding other byte, and then ORs that with a running total (d), is
|
|
14
|
-
* adapted from here:
|
|
15
|
-
*
|
|
16
|
-
* https://groups.google.com/forum/#!topic/keyczar-discuss/VXHsoJSLKhM
|
|
17
|
-
*/
|
|
18
|
-
'use strict'
|
|
19
|
-
const cmp = (buf1, buf2) => {
|
|
20
|
-
if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2)) {
|
|
21
|
-
throw new Error('buf1 and buf2 must be buffers')
|
|
22
|
-
}
|
|
23
|
-
if (buf1.length !== buf2.length) {
|
|
24
|
-
return false
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
let d = 0
|
|
28
|
-
for (let i = 0; i < buf1.length; i++) {
|
|
29
|
-
const x = buf1[i]
|
|
30
|
-
const y = buf2[i]
|
|
31
|
-
d |= x ^ y
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return d === 0
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { cmp }
|
package/lib/config.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
class Config {
|
|
4
|
-
constructor (values) {
|
|
5
|
-
this.keyDefined = (key) => key in values
|
|
6
|
-
this.getValue = (key) => values[key]
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
get (key) {
|
|
10
|
-
if (this.keyDefined(key)) {
|
|
11
|
-
return this.getValue(key)
|
|
12
|
-
} else {
|
|
13
|
-
throw new Error(`Unknown configuration: ${key}`)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class ConfigBuilder {
|
|
19
|
-
constructor () {
|
|
20
|
-
this.variables = {}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
build () {
|
|
24
|
-
return new Config(this.variables)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
addValue (key, value) {
|
|
28
|
-
if (value === undefined) {
|
|
29
|
-
throw new Error(`Failed to add "${key}" property. The value cannot be undefined`)
|
|
30
|
-
}
|
|
31
|
-
if (key in this.variables) {
|
|
32
|
-
throw new Error(`"${key}" already has a value defined.`)
|
|
33
|
-
}
|
|
34
|
-
this.variables[key] = value
|
|
35
|
-
return this
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
addValueWithDefault (key, value, defaultValue) {
|
|
39
|
-
if (defaultValue === undefined) {
|
|
40
|
-
throw new Error(`Failed to add "${key}" property. Default value cannot be undefined`)
|
|
41
|
-
}
|
|
42
|
-
return this.addValue(key, value === undefined ? defaultValue : value)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const config = new ConfigBuilder()
|
|
47
|
-
.addValue('NETWORK', process.env.NETWORK || 'mainnet')
|
|
48
|
-
.build()
|
|
49
|
-
|
|
50
|
-
export { config }
|
package/lib/constants.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Constants
|
|
3
|
-
* =========
|
|
4
|
-
*
|
|
5
|
-
* Constants used to distinguish mainnet from testnet.
|
|
6
|
-
*/
|
|
7
|
-
'use strict'
|
|
8
|
-
import { config } from './config'
|
|
9
|
-
|
|
10
|
-
const Constants = {}
|
|
11
|
-
|
|
12
|
-
Constants.Mainnet = {
|
|
13
|
-
MaxSize: 0x02000000, // MAX_SIZE
|
|
14
|
-
Port: 8333,
|
|
15
|
-
Address: {
|
|
16
|
-
pubKeyHash: 0x00,
|
|
17
|
-
payToScriptHash: 0x05
|
|
18
|
-
},
|
|
19
|
-
Bip32: {
|
|
20
|
-
pubKey: 0x0488b21e,
|
|
21
|
-
privKey: 0x0488ade4
|
|
22
|
-
},
|
|
23
|
-
Block: {
|
|
24
|
-
maxNBits: 0x1d00ffff,
|
|
25
|
-
magicNum: 0xe3e1f3e8
|
|
26
|
-
},
|
|
27
|
-
Msg: {
|
|
28
|
-
magicNum: 0xe3e1f3e8,
|
|
29
|
-
versionBytesNum: 70015 // as of Bitcoin SV v1.0.5
|
|
30
|
-
},
|
|
31
|
-
PrivKey: {
|
|
32
|
-
versionByteNum: 0x80
|
|
33
|
-
},
|
|
34
|
-
TxBuilder: {
|
|
35
|
-
dust: 0, // number of satoshis that an output can't be less than
|
|
36
|
-
feePerKbNum: 0.00000050e8
|
|
37
|
-
},
|
|
38
|
-
Workers: {
|
|
39
|
-
// Cannot be 5 seconds. This is actually too low for low end devices. We
|
|
40
|
-
// have found by experimenting with Chrome developer tools that 60 seconds
|
|
41
|
-
// works on low end mobile.
|
|
42
|
-
timeout: 60000
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
Constants.Testnet = Object.assign({}, Constants.Mainnet, {
|
|
47
|
-
Port: 18333,
|
|
48
|
-
Address: {
|
|
49
|
-
pubKeyHash: 0x6f,
|
|
50
|
-
payToScriptHash: 0xc4
|
|
51
|
-
},
|
|
52
|
-
Bip32: {
|
|
53
|
-
pubKey: 0x043587cf,
|
|
54
|
-
privKey: 0x04358394
|
|
55
|
-
},
|
|
56
|
-
Block: {
|
|
57
|
-
maxNBits: 0x1d00ffff,
|
|
58
|
-
magicNum: 0xf4e5f3f4
|
|
59
|
-
},
|
|
60
|
-
Msg: {
|
|
61
|
-
magicNum: 0xf4e5f3f4,
|
|
62
|
-
versionBytesNum: 70015 // as of Bitcoin SV v1.0.5
|
|
63
|
-
},
|
|
64
|
-
PrivKey: {
|
|
65
|
-
versionByteNum: 0xef
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
Constants.Regtest = Object.assign({}, Constants.Mainnet, {
|
|
70
|
-
Port: 18444,
|
|
71
|
-
Address: {
|
|
72
|
-
pubKeyHash: 0x6f,
|
|
73
|
-
payToScriptHash: 0xc4
|
|
74
|
-
},
|
|
75
|
-
Bip32: {
|
|
76
|
-
pubKey: 0x043587cf,
|
|
77
|
-
privKey: 0x04358394
|
|
78
|
-
},
|
|
79
|
-
Block: {
|
|
80
|
-
maxNBits: 0x207fffff,
|
|
81
|
-
magicNum: 0xdab5bffa
|
|
82
|
-
},
|
|
83
|
-
Msg: {
|
|
84
|
-
magicNum: 0xdab5bffa,
|
|
85
|
-
versionBytesNum: 70015 // as of Bitcoin SV v1.0.5
|
|
86
|
-
},
|
|
87
|
-
PrivKey: {
|
|
88
|
-
versionByteNum: 0xef
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
Constants.STN = Object.assign({}, Constants.Mainnet, {
|
|
93
|
-
Port: 9333,
|
|
94
|
-
Address: {
|
|
95
|
-
pubKeyHash: 0x6f,
|
|
96
|
-
payToScriptHash: 0xc4
|
|
97
|
-
},
|
|
98
|
-
Bip32: {
|
|
99
|
-
pubKey: 0x043587cf,
|
|
100
|
-
privKey: 0x04358394
|
|
101
|
-
},
|
|
102
|
-
Block: {
|
|
103
|
-
maxNBits: 0x1d00ffff,
|
|
104
|
-
magicNum: 0xfbcec4f9
|
|
105
|
-
},
|
|
106
|
-
Msg: {
|
|
107
|
-
magicNum: 0xfbcec4f9,
|
|
108
|
-
versionBytesNum: 70015 // as of Bitcoin SV v1.0.5
|
|
109
|
-
},
|
|
110
|
-
PrivKey: {
|
|
111
|
-
versionByteNum: 0xef
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Yours Bitcoin can be globally configured to mainnet or testnet. Via the
|
|
117
|
-
* inject pattern, you always have access to the other network at any time.
|
|
118
|
-
* However, it is very convenient to be able to change the default
|
|
119
|
-
* configuration. The default is mainnet, which can be changed to testnet.
|
|
120
|
-
*/
|
|
121
|
-
// Constants.Default = Object.assign({}, Constants.Mainnet)
|
|
122
|
-
if (config.get('NETWORK') === 'testnet') {
|
|
123
|
-
Constants.Default = Object.assign({}, Constants.Testnet)
|
|
124
|
-
} else if (config.get('NETWORK') === 'mainnet') {
|
|
125
|
-
Constants.Default = Object.assign({}, Constants.Mainnet)
|
|
126
|
-
} else if (config.get('NETWORK') === 'regtest') {
|
|
127
|
-
Constants.Default = Object.assign({}, Constants.Regtest)
|
|
128
|
-
} else if (config.get('NETWORK') === 'stn') {
|
|
129
|
-
Constants.Default = Object.assign({}, Constants.STN)
|
|
130
|
-
} else {
|
|
131
|
-
throw new Error(
|
|
132
|
-
`must set network in environment variable - mainnet, testnet, regtest or stn?, received ${config.get('NETWORK')}`
|
|
133
|
-
)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const getConstants = (magicNum) => {
|
|
137
|
-
if (Constants.Mainnet.Msg.magicNum === magicNum) {
|
|
138
|
-
return Constants.Mainnet
|
|
139
|
-
} else if (Constants.Testnet.Msg.magicNum === magicNum) {
|
|
140
|
-
return Constants.Testnet
|
|
141
|
-
} else if (Constants.Regtest.Msg.magicNum === magicNum) {
|
|
142
|
-
return Constants.Regtest
|
|
143
|
-
} else if (Constants.STN.Msg.magicNum === magicNum) {
|
|
144
|
-
return Constants.STN
|
|
145
|
-
} else {
|
|
146
|
-
return Constants.Default
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export { Constants, getConstants }
|