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.
Files changed (212) hide show
  1. package/.babelrc.test +10 -0
  2. package/.editorconfig +4 -0
  3. package/.env.defaults +1 -0
  4. package/.env.schema +1 -0
  5. package/.mocharc.yml +4 -0
  6. package/.nvmrc +1 -0
  7. package/.travis.yml +7 -0
  8. package/LICENSE +39 -0
  9. package/README.md +53 -0
  10. package/benchmark.js +50 -0
  11. package/bin/bsv.js +11 -0
  12. package/bin/network.js +92 -0
  13. package/build/bsv.js +3 -0
  14. package/build/index.html +20 -0
  15. package/dist/bsv.bundle.js +3 -0
  16. package/dist/bsv.bundle.js.LICENSE.txt +10 -0
  17. package/dist/bsv.bundle.js.map +1 -0
  18. package/dist/bsv.cjs.js +3 -0
  19. package/dist/bsv.cjs.js.LICENSE.txt +1 -0
  20. package/dist/bsv.cjs.js.map +1 -0
  21. package/dist/bsv.min.js +2 -0
  22. package/dist/bsv.module.js +3 -0
  23. package/dist/bsv.module.js.LICENSE.txt +1 -0
  24. package/dist/bsv.module.js.map +1 -0
  25. package/doc/assets/anchor.js +350 -0
  26. package/doc/assets/bass-addons.css +12 -0
  27. package/doc/assets/bass.css +544 -0
  28. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  29. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  30. package/doc/assets/fonts/LICENSE.txt +93 -0
  31. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  32. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  33. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  34. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  35. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  36. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  37. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  38. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  39. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  40. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  41. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  42. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  43. package/doc/assets/fonts/source-code-pro.css +23 -0
  44. package/doc/assets/github.css +123 -0
  45. package/doc/assets/site.js +168 -0
  46. package/doc/assets/split.css +15 -0
  47. package/doc/assets/split.js +784 -0
  48. package/doc/assets/style.css +147 -0
  49. package/doc/index.html +9387 -0
  50. package/docs/block.html +36 -0
  51. package/docs/ecies.html +81 -0
  52. package/docs/index.html +84 -0
  53. package/docs/networks.html +43 -0
  54. package/docs/script.html +101 -0
  55. package/docs/transaction.html +150 -0
  56. package/docs/unspentoutput.html +32 -0
  57. package/entry.js +82 -0
  58. package/index.js +102 -0
  59. package/jsdoc.json +5 -0
  60. package/karma.conf.js +78 -0
  61. package/lib/ach.js +59 -0
  62. package/lib/address.js +264 -0
  63. package/lib/aes.js +57 -0
  64. package/lib/aescbc.js +38 -0
  65. package/lib/base-58-check.js +83 -0
  66. package/lib/base-58.js +59 -0
  67. package/lib/bip-32.js +472 -0
  68. package/lib/bip-39-en-wordlist.js +2054 -0
  69. package/lib/bip-39-en.js +10 -0
  70. package/lib/bip-39-es-wordlist.js +2055 -0
  71. package/lib/bip-39-es.js +10 -0
  72. package/lib/bip-39-jp-wordlist.js +2053 -0
  73. package/lib/bip-39-jp.js +10 -0
  74. package/lib/bip-39-words.js +5 -0
  75. package/lib/bip-39.js +275 -0
  76. package/lib/block-header.js +79 -0
  77. package/lib/block.js +132 -0
  78. package/lib/bn.js +467 -0
  79. package/lib/br.js +172 -0
  80. package/lib/bsm.js +127 -0
  81. package/lib/bw.js +188 -0
  82. package/lib/cbc.js +140 -0
  83. package/lib/cmp.js +37 -0
  84. package/lib/config.js +50 -0
  85. package/lib/constants.js +150 -0
  86. package/lib/ecdsa.js +495 -0
  87. package/lib/ecies.js +151 -0
  88. package/lib/hash-cache.js +41 -0
  89. package/lib/hash.js +191 -0
  90. package/lib/interp.js +1853 -0
  91. package/lib/inv.js +49 -0
  92. package/lib/key-pair.js +133 -0
  93. package/lib/mainnet-address.js +5 -0
  94. package/lib/mainnet-bip-32.js +5 -0
  95. package/lib/mainnet-key-pair.js +5 -0
  96. package/lib/mainnet-priv-key.js +5 -0
  97. package/lib/merkle.js +92 -0
  98. package/lib/msg.js +184 -0
  99. package/lib/op-code.js +398 -0
  100. package/lib/point.js +134 -0
  101. package/lib/priv-key.js +157 -0
  102. package/lib/pub-key.js +230 -0
  103. package/lib/rabin.js +300 -0
  104. package/lib/random.js +28 -0
  105. package/lib/script.js +826 -0
  106. package/lib/sig-operations.js +120 -0
  107. package/lib/sig.js +399 -0
  108. package/lib/struct.js +389 -0
  109. package/lib/testnet-address.js +5 -0
  110. package/lib/testnet-bip-32.js +5 -0
  111. package/lib/testnet-key-pair.js +5 -0
  112. package/lib/testnet-priv-key.js +5 -0
  113. package/lib/tx-builder.js +593 -0
  114. package/lib/tx-in.js +172 -0
  115. package/lib/tx-out-map.js +57 -0
  116. package/lib/tx-out.js +73 -0
  117. package/lib/tx-verifier.js +209 -0
  118. package/lib/tx.js +429 -0
  119. package/lib/var-int.js +73 -0
  120. package/lib/version.js +92 -0
  121. package/lib/workers-result.js +65 -0
  122. package/lib/workers.js +77 -0
  123. package/load-babel.js +3 -0
  124. package/mkdocs.yml +37 -0
  125. package/package.json +138 -0
  126. package/test/ach.js +172 -0
  127. package/test/address.js +464 -0
  128. package/test/aes.js +96 -0
  129. package/test/aescbc.js +65 -0
  130. package/test/base-58-check.js +128 -0
  131. package/test/base-58.js +113 -0
  132. package/test/bip-32.js +721 -0
  133. package/test/bip-39.js +282 -0
  134. package/test/block-header.js +152 -0
  135. package/test/block.js +243 -0
  136. package/test/bn.js +643 -0
  137. package/test/br.js +353 -0
  138. package/test/bsm.js +141 -0
  139. package/test/bw.js +292 -0
  140. package/test/cbc.js +335 -0
  141. package/test/cmp.js +56 -0
  142. package/test/data/bip69.json +215 -0
  143. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  144. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  145. package/test/data/bitcoind/blocks.json +27 -0
  146. package/test/data/bitcoind/script_tests.json +2244 -0
  147. package/test/data/bitcoind/sig_canonical.json +7 -0
  148. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  149. package/test/data/bitcoind/tx_invalid.json +177 -0
  150. package/test/data/bitcoind/tx_valid.json +224 -0
  151. package/test/data/blk86756-testnet.dat +0 -0
  152. package/test/data/blk86756-testnet.js +12 -0
  153. package/test/data/blk86756-testnet.json +684 -0
  154. package/test/data/ecdsa.json +230 -0
  155. package/test/data/messages.json +22 -0
  156. package/test/data/sighash.json +1004 -0
  157. package/test/data/tx_creation.json +85 -0
  158. package/test/ecdsa.js +748 -0
  159. package/test/ecies.js +238 -0
  160. package/test/global.js +13 -0
  161. package/test/hash-cache.js +52 -0
  162. package/test/hash.js +354 -0
  163. package/test/index.js +10 -0
  164. package/test/interp.js +512 -0
  165. package/test/inv.js +65 -0
  166. package/test/key-pair.js +243 -0
  167. package/test/merkle.js +179 -0
  168. package/test/msg.js +295 -0
  169. package/test/op-code.js +79 -0
  170. package/test/point.js +375 -0
  171. package/test/priv-key.js +264 -0
  172. package/test/pub-key.js +549 -0
  173. package/test/random.js +31 -0
  174. package/test/script.js +1340 -0
  175. package/test/sig-operations.js +149 -0
  176. package/test/sig.js +662 -0
  177. package/test/struct.js +552 -0
  178. package/test/tx-builder.js +2104 -0
  179. package/test/tx-in.js +198 -0
  180. package/test/tx-out-map.js +104 -0
  181. package/test/tx-out.js +162 -0
  182. package/test/tx-verifier.js +246 -0
  183. package/test/tx.js +539 -0
  184. package/test/var-int.js +132 -0
  185. package/test/vectors/aes.json +10392 -0
  186. package/test/vectors/aescbc.json +620 -0
  187. package/test/vectors/bip39.json +502 -0
  188. package/test/vectors/bip69.json +215 -0
  189. package/test/vectors/bitcoin-abc/script_tests.json +48 -0
  190. package/test/vectors/bitcoin-abc/sighash.json +7004 -0
  191. package/test/vectors/bitcoin-sv/script_tests.json +248 -0
  192. package/test/vectors/bitcoind/script_invalid.json +1151 -0
  193. package/test/vectors/bitcoind/script_valid.json +1462 -0
  194. package/test/vectors/bitcoind/sighash.json +3503 -0
  195. package/test/vectors/bitcoind/tx_invalid.json +883 -0
  196. package/test/vectors/bitcoind/tx_valid.json +1150 -0
  197. package/test/vectors/coolest-tx-ever-sent.json +8 -0
  198. package/test/vectors/ecdsa.json +300 -0
  199. package/test/vectors/hash.json +6133 -0
  200. package/test/vectors/kdf.json +47 -0
  201. package/test/vectors/largesttx.json +5 -0
  202. package/test/vectors/largesttxblock.json +6 -0
  203. package/test/vectors/sig.json +79 -0
  204. package/test/vectors/sighash-single-bug.json +8 -0
  205. package/test/version.js +81 -0
  206. package/test/workers-result.js +89 -0
  207. package/test/workers.js +91 -0
  208. package/test-index.js +3 -0
  209. package/test1.html +88 -0
  210. package/webpack.config.js +60 -0
  211. package/webpack.config.js_entry +57 -0
  212. package/yarn-error.log +6199 -0
package/lib/bsm.js ADDED
@@ -0,0 +1,127 @@
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 ADDED
@@ -0,0 +1,188 @@
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 ADDED
@@ -0,0 +1,140 @@
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 ADDED
@@ -0,0 +1,37 @@
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 ADDED
@@ -0,0 +1,50 @@
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 }
@@ -0,0 +1,150 @@
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 }