bsv2 2.1.0 → 2.1.2

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 (241) hide show
  1. package/dist/bsv2.mjs +1 -1
  2. package/dist/bsv2.umd.js +1 -1
  3. package/package.json +7 -3
  4. package/.babel.test +0 -10
  5. package/.editorconfig +0 -4
  6. package/.env.defaults +0 -1
  7. package/.env.schema +0 -1
  8. package/.husky/pre-commit +0 -1
  9. package/.mocharc.yml +0 -4
  10. package/.nvmrc +0 -1
  11. package/.travis.yml +0 -7
  12. package/babel.config.cjs +0 -44
  13. package/benchmark.js +0 -42
  14. package/bin/bsv.js +0 -11
  15. package/bin/network.js +0 -93
  16. package/build/index.html +0 -20
  17. package/doc/assets/anchor.js +0 -350
  18. package/doc/assets/bass-addons.css +0 -12
  19. package/doc/assets/bass.css +0 -544
  20. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  21. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  22. package/doc/assets/fonts/LICENSE.txt +0 -93
  23. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  24. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  25. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  26. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  27. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  28. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  29. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  30. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  31. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  32. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  33. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  34. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  35. package/doc/assets/fonts/source-code-pro.css +0 -23
  36. package/doc/assets/github.css +0 -123
  37. package/doc/assets/site.js +0 -168
  38. package/doc/assets/split.css +0 -15
  39. package/doc/assets/split.js +0 -782
  40. package/doc/assets/style.css +0 -147
  41. package/doc/index.html +0 -9461
  42. package/entry.js +0 -81
  43. package/eslint.config.mjs +0 -8
  44. package/index.js +0 -98
  45. package/karma.conf.ts +0 -77
  46. package/lib/ach.js +0 -59
  47. package/lib/address.js +0 -266
  48. package/lib/aes.js +0 -57
  49. package/lib/aescbc.js +0 -38
  50. package/lib/base-58-check.js +0 -83
  51. package/lib/base-58.js +0 -59
  52. package/lib/bip-32.js +0 -472
  53. package/lib/bip-39-en-wordlist.js +0 -2054
  54. package/lib/bip-39-en.js +0 -10
  55. package/lib/bip-39-es-wordlist.js +0 -2055
  56. package/lib/bip-39-es.js +0 -10
  57. package/lib/bip-39-jp-wordlist.js +0 -2053
  58. package/lib/bip-39-jp.js +0 -10
  59. package/lib/bip-39-words.js +0 -5
  60. package/lib/bip-39.js +0 -286
  61. package/lib/block-header.js +0 -79
  62. package/lib/block.js +0 -132
  63. package/lib/bn.js +0 -462
  64. package/lib/br.js +0 -172
  65. package/lib/bsm.js +0 -127
  66. package/lib/bw.js +0 -188
  67. package/lib/cbc.js +0 -140
  68. package/lib/cmp.js +0 -37
  69. package/lib/config.js +0 -50
  70. package/lib/constants.js +0 -150
  71. package/lib/ecdsa.js +0 -496
  72. package/lib/ecies.js +0 -151
  73. package/lib/hash-cache.js +0 -41
  74. package/lib/hash.js +0 -191
  75. package/lib/interp.js +0 -2017
  76. package/lib/inv.js +0 -49
  77. package/lib/key-pair.js +0 -133
  78. package/lib/mainnet-address.js +0 -5
  79. package/lib/mainnet-bip-32.js +0 -5
  80. package/lib/mainnet-key-pair.js +0 -5
  81. package/lib/mainnet-priv-key.js +0 -5
  82. package/lib/merkle.js +0 -92
  83. package/lib/msg.js +0 -184
  84. package/lib/op-code.js +0 -400
  85. package/lib/point.js +0 -134
  86. package/lib/priv-key.js +0 -157
  87. package/lib/pub-key.js +0 -230
  88. package/lib/random.js +0 -28
  89. package/lib/script.js +0 -828
  90. package/lib/sig-operations.js +0 -120
  91. package/lib/sig.js +0 -399
  92. package/lib/struct.js +0 -392
  93. package/lib/testnet-address.js +0 -5
  94. package/lib/testnet-bip-32.js +0 -5
  95. package/lib/testnet-key-pair.js +0 -5
  96. package/lib/testnet-priv-key.js +0 -5
  97. package/lib/tx-builder.js +0 -593
  98. package/lib/tx-in.js +0 -172
  99. package/lib/tx-out-map.js +0 -57
  100. package/lib/tx-out.js +0 -73
  101. package/lib/tx-verifier.js +0 -209
  102. package/lib/tx.js +0 -430
  103. package/lib/var-int.js +0 -73
  104. package/lib/version.js +0 -92
  105. package/lib/workers-result.js +0 -65
  106. package/lib/workers.js +0 -77
  107. package/load-babel.js +0 -5
  108. package/mtest/test.html +0 -17
  109. package/mtest/test.js +0 -2
  110. package/mtest/test.mjs +0 -2
  111. package/mtest/webpack.config.js +0 -115
  112. package/out/Sig.html +0 -541
  113. package/out/bip-32.js.html +0 -523
  114. package/out/bip-39.js.html +0 -336
  115. package/out/block.js.html +0 -183
  116. package/out/bn.js.html +0 -512
  117. package/out/ecdsa.js.html +0 -546
  118. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  119. package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
  120. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  121. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  122. package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
  123. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  124. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  125. package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
  126. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  127. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  128. package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
  129. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  130. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  131. package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
  132. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  133. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  134. package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
  135. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  136. package/out/global.html +0 -220
  137. package/out/index.html +0 -128
  138. package/out/interp.js.html +0 -2064
  139. package/out/merkle.js.html +0 -143
  140. package/out/msg.js.html +0 -235
  141. package/out/priv-key.js.html +0 -208
  142. package/out/pub-key.js.html +0 -281
  143. package/out/script.js.html +0 -876
  144. package/out/scripts/linenumber.js +0 -25
  145. package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
  146. package/out/scripts/prettify/lang-css.js +0 -2
  147. package/out/scripts/prettify/prettify.js +0 -28
  148. package/out/sig-operations.js.html +0 -171
  149. package/out/sig.js.html +0 -450
  150. package/out/struct.js.html +0 -440
  151. package/out/styles/jsdoc-default.css +0 -358
  152. package/out/styles/prettify-jsdoc.css +0 -111
  153. package/out/styles/prettify-tomorrow.css +0 -132
  154. package/out/tx-builder.js.html +0 -644
  155. package/out/tx-in.js.html +0 -223
  156. package/out/tx-verifier.js.html +0 -260
  157. package/out/tx.js.html +0 -481
  158. package/test/ach.js +0 -172
  159. package/test/address.js +0 -464
  160. package/test/aes.js +0 -96
  161. package/test/aescbc.js +0 -65
  162. package/test/base-58-check.js +0 -128
  163. package/test/base-58.js +0 -113
  164. package/test/bip-32.js +0 -721
  165. package/test/bip-39.js +0 -282
  166. package/test/block-header.js +0 -152
  167. package/test/block.js +0 -243
  168. package/test/bn.js +0 -643
  169. package/test/br.js +0 -353
  170. package/test/bsm.js +0 -141
  171. package/test/bw.js +0 -292
  172. package/test/cbc.js +0 -335
  173. package/test/cmp.js +0 -56
  174. package/test/data/bip69.json +0 -215
  175. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  176. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  177. package/test/data/bitcoind/blocks.json +0 -27
  178. package/test/data/bitcoind/script_tests.json +0 -2244
  179. package/test/data/bitcoind/sig_canonical.json +0 -7
  180. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  181. package/test/data/bitcoind/tx_invalid.json +0 -177
  182. package/test/data/bitcoind/tx_valid.json +0 -224
  183. package/test/data/blk86756-testnet.dat +0 -0
  184. package/test/data/blk86756-testnet.js +0 -12
  185. package/test/data/blk86756-testnet.json +0 -684
  186. package/test/data/ecdsa.json +0 -230
  187. package/test/data/messages.json +0 -22
  188. package/test/data/sighash.json +0 -1004
  189. package/test/data/tx_creation.json +0 -85
  190. package/test/ecdsa.js +0 -748
  191. package/test/ecies.js +0 -238
  192. package/test/global.js +0 -13
  193. package/test/hash-cache.js +0 -52
  194. package/test/hash.js +0 -354
  195. package/test/index.js +0 -10
  196. package/test/interp.js +0 -512
  197. package/test/inv.js +0 -65
  198. package/test/key-pair.js +0 -243
  199. package/test/merkle.js +0 -179
  200. package/test/msg.js +0 -295
  201. package/test/op-code.js +0 -79
  202. package/test/point.js +0 -375
  203. package/test/priv-key.js +0 -264
  204. package/test/pub-key.js +0 -549
  205. package/test/random.js +0 -31
  206. package/test/script.js +0 -1340
  207. package/test/sig-operations.js +0 -149
  208. package/test/sig.js +0 -662
  209. package/test/struct.js +0 -552
  210. package/test/tx-builder.js +0 -2104
  211. package/test/tx-in.js +0 -198
  212. package/test/tx-out-map.js +0 -104
  213. package/test/tx-out.js +0 -162
  214. package/test/tx-verifier.js +0 -246
  215. package/test/tx.js +0 -539
  216. package/test/var-int.js +0 -132
  217. package/test/vectors/aes.json +0 -10392
  218. package/test/vectors/aescbc.json +0 -620
  219. package/test/vectors/bip39.json +0 -502
  220. package/test/vectors/bip69.json +0 -215
  221. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  222. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  223. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  224. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  225. package/test/vectors/bitcoind/script_valid.json +0 -1462
  226. package/test/vectors/bitcoind/sighash.json +0 -3503
  227. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  228. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  229. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  230. package/test/vectors/ecdsa.json +0 -300
  231. package/test/vectors/hash.json +0 -6133
  232. package/test/vectors/kdf.json +0 -47
  233. package/test/vectors/largesttx.json +0 -5
  234. package/test/vectors/largesttxblock.json +0 -6
  235. package/test/vectors/sig.json +0 -79
  236. package/test/vectors/sighash-single-bug.json +0 -8
  237. package/test/version.js +0 -81
  238. package/test/workers-result.js +0 -89
  239. package/test/workers.js +0 -91
  240. package/test-index.js +0 -3
  241. package/vite.config.js +0 -36
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,
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', import.meta.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: 1
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 }