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/test/sig.js ADDED
@@ -0,0 +1,662 @@
1
+ /* global describe,it */
2
+ 'use strict'
3
+ import { Bn } from '../lib/bn'
4
+ import should from 'should'
5
+ import { Sig } from '../lib/sig'
6
+ import vectors from './vectors/sig.json'
7
+
8
+ describe('Sig', function () {
9
+ it('should make a blank signature', function () {
10
+ const sig = new Sig()
11
+ should.exist(sig)
12
+ })
13
+
14
+ it('should work with conveniently setting r, s', function () {
15
+ const r = new Bn()
16
+ const s = new Bn()
17
+ const sig = new Sig(r, s)
18
+ should.exist(sig)
19
+ sig.r.toString().should.equal(r.toString())
20
+ sig.s.toString().should.equal(s.toString())
21
+ })
22
+
23
+ describe('#fromObject', function () {
24
+ it('should set compressed', function () {
25
+ should.exist(new Sig().fromObject({ compressed: true }))
26
+ })
27
+ })
28
+
29
+ describe('#fromHex', function () {
30
+ it('should parse this known signature and rebuild it', function () {
31
+ const hex =
32
+ '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'
33
+ const sig = new Sig().fromHex(hex)
34
+ sig.toHex().should.equal(hex)
35
+ })
36
+
37
+ it('should create a signature from a compressed signature', function () {
38
+ const blank = Buffer.alloc(32)
39
+ blank.fill(0)
40
+ const compressed = Buffer.concat([Buffer.from([0 + 27 + 4]), blank, blank])
41
+ const sig = new Sig()
42
+ sig.fromHex(compressed.toString('hex'))
43
+ sig.r.cmp(0).should.equal(0)
44
+ sig.s.cmp(0).should.equal(0)
45
+ })
46
+
47
+ it('should parse this DER format signature', function () {
48
+ const buf = Buffer.from(
49
+ '3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2',
50
+ 'hex'
51
+ )
52
+ const sig = new Sig()
53
+ sig.fromHex(buf.toString('hex'))
54
+ sig.r
55
+ .toBuffer({ size: 32 })
56
+ .toString('hex')
57
+ .should.equal(
58
+ '75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'
59
+ )
60
+ sig.s
61
+ .toBuffer({ size: 32 })
62
+ .toString('hex')
63
+ .should.equal(
64
+ '729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'
65
+ )
66
+ })
67
+
68
+ it('should parse this known signature and rebuild it', function () {
69
+ const hex =
70
+ '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'
71
+ const sig = new Sig().fromHex(hex)
72
+ sig
73
+ .toTxFormat()
74
+ .toString('hex')
75
+ .should.equal(hex)
76
+ })
77
+ })
78
+
79
+ describe('#fromBuffer', function () {
80
+ it('should create a signature from a compressed signature', function () {
81
+ const blank = Buffer.alloc(32)
82
+ blank.fill(0)
83
+ const compressed = Buffer.concat([Buffer.from([0 + 27 + 4]), blank, blank])
84
+ const sig = new Sig()
85
+ sig.fromBuffer(compressed)
86
+ sig.r.cmp(0).should.equal(0)
87
+ sig.s.cmp(0).should.equal(0)
88
+ })
89
+
90
+ it('should parse this DER format signature', function () {
91
+ const buf = Buffer.from(
92
+ '3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2',
93
+ 'hex'
94
+ )
95
+ const sig = new Sig()
96
+ sig.fromBuffer(buf)
97
+ sig.r
98
+ .toBuffer({ size: 32 })
99
+ .toString('hex')
100
+ .should.equal(
101
+ '75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'
102
+ )
103
+ sig.s
104
+ .toBuffer({ size: 32 })
105
+ .toString('hex')
106
+ .should.equal(
107
+ '729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'
108
+ )
109
+ })
110
+
111
+ it('should parse this known signature and rebuild it', function () {
112
+ const hex =
113
+ '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'
114
+ const buf = Buffer.from(hex, 'hex')
115
+ const sig = new Sig().fromBuffer(buf)
116
+ sig
117
+ .toTxFormat()
118
+ .toString('hex')
119
+ .should.equal(hex)
120
+ })
121
+ })
122
+
123
+ describe('#fromCompact', function () {
124
+ it('should create a signature from a compressed signature', function () {
125
+ const blank = Buffer.alloc(32)
126
+ blank.fill(0)
127
+ const compact = Buffer.concat([Buffer.from([0 + 27 + 4]), blank, blank])
128
+ const sig = new Sig()
129
+ sig.fromCompact(compact)
130
+ sig.compressed.should.equal(true)
131
+ sig.r.cmp(0).should.equal(0)
132
+ sig.s.cmp(0).should.equal(0)
133
+ })
134
+ })
135
+
136
+ describe('@fromCompact', function () {
137
+ it('should create a signature from a compressed signature', function () {
138
+ const blank = Buffer.alloc(32)
139
+ blank.fill(0)
140
+ const compact = Buffer.concat([Buffer.from([0 + 27 + 4]), blank, blank])
141
+ const sig = Sig.fromCompact(compact)
142
+ sig.compressed.should.equal(true)
143
+ sig.r.cmp(0).should.equal(0)
144
+ sig.s.cmp(0).should.equal(0)
145
+ })
146
+ })
147
+
148
+ describe('#fromDer', function () {
149
+ const buf = Buffer.from(
150
+ '3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2',
151
+ 'hex'
152
+ )
153
+
154
+ it('should parse this DER format signature', function () {
155
+ const sig = new Sig()
156
+ sig.fromDer(buf)
157
+ sig.r
158
+ .toBuffer({ size: 32 })
159
+ .toString('hex')
160
+ .should.equal(
161
+ '75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'
162
+ )
163
+ sig.s
164
+ .toBuffer({ size: 32 })
165
+ .toString('hex')
166
+ .should.equal(
167
+ '729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'
168
+ )
169
+ })
170
+ })
171
+
172
+ describe('@fromDer', function () {
173
+ const buf = Buffer.from(
174
+ '3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2',
175
+ 'hex'
176
+ )
177
+
178
+ it('should parse this DER format signature', function () {
179
+ const sig = Sig.fromDer(buf)
180
+ sig.r
181
+ .toBuffer({ size: 32 })
182
+ .toString('hex')
183
+ .should.equal(
184
+ '75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'
185
+ )
186
+ sig.s
187
+ .toBuffer({ size: 32 })
188
+ .toString('hex')
189
+ .should.equal(
190
+ '729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'
191
+ )
192
+ })
193
+ })
194
+
195
+ describe('#fromTxFormat', function () {
196
+ it('should convert from this known tx-format buffer', function () {
197
+ const buf = Buffer.from(
198
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201',
199
+ 'hex'
200
+ )
201
+ const sig = new Sig().fromTxFormat(buf)
202
+ sig.r
203
+ .toString()
204
+ .should.equal(
205
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
206
+ )
207
+ sig.s
208
+ .toString()
209
+ .should.equal(
210
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
211
+ )
212
+ sig.nHashType.should.equal(Sig.SIGHASH_ALL)
213
+ })
214
+
215
+ it('should parse this known signature and rebuild it', function () {
216
+ const hex =
217
+ '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'
218
+ const buf = Buffer.from(hex, 'hex')
219
+ const sig = new Sig().fromTxFormat(buf)
220
+ sig
221
+ .toTxFormat()
222
+ .toString('hex')
223
+ .should.equal(hex)
224
+ })
225
+ })
226
+
227
+ describe('@fromTxFormat', function () {
228
+ it('should convert from this known tx-format buffer', function () {
229
+ const buf = Buffer.from(
230
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201',
231
+ 'hex'
232
+ )
233
+ const sig = Sig.fromTxFormat(buf)
234
+ sig.r
235
+ .toString()
236
+ .should.equal(
237
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
238
+ )
239
+ sig.s
240
+ .toString()
241
+ .should.equal(
242
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
243
+ )
244
+ sig.nHashType.should.equal(Sig.SIGHASH_ALL)
245
+ })
246
+
247
+ it('should parse this known signature and rebuild it', function () {
248
+ const hex =
249
+ '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'
250
+ const buf = Buffer.from(hex, 'hex')
251
+ const sig = Sig.fromTxFormat(buf)
252
+ sig
253
+ .toTxFormat()
254
+ .toString('hex')
255
+ .should.equal(hex)
256
+ })
257
+ })
258
+
259
+ describe('#fromString', function () {
260
+ const buf = Buffer.from(
261
+ '3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2',
262
+ 'hex'
263
+ )
264
+
265
+ it('should parse this DER format signature in hex', function () {
266
+ const sig = new Sig()
267
+ sig.fromString(buf.toString('hex'))
268
+ sig.r
269
+ .toBuffer({ size: 32 })
270
+ .toString('hex')
271
+ .should.equal(
272
+ '75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'
273
+ )
274
+ sig.s
275
+ .toBuffer({ size: 32 })
276
+ .toString('hex')
277
+ .should.equal(
278
+ '729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'
279
+ )
280
+ })
281
+ })
282
+
283
+ describe('#parseDer', function () {
284
+ it('should parse this signature generated in node', function () {
285
+ const sighex =
286
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
287
+ const sig = Buffer.from(sighex, 'hex')
288
+ const parsed = Sig.parseDer(sig)
289
+ parsed.header.should.equal(0x30)
290
+ parsed.length.should.equal(69)
291
+ parsed.rlength.should.equal(33)
292
+ parsed.rneg.should.equal(true)
293
+ parsed.rbuf
294
+ .toString('hex')
295
+ .should.equal(
296
+ '008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa'
297
+ )
298
+ parsed.r
299
+ .toString()
300
+ .should.equal(
301
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
302
+ )
303
+ parsed.slength.should.equal(32)
304
+ parsed.sneg.should.equal(false)
305
+ parsed.sbuf
306
+ .toString('hex')
307
+ .should.equal(
308
+ '0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
309
+ )
310
+ parsed.s
311
+ .toString()
312
+ .should.equal(
313
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
314
+ )
315
+ })
316
+
317
+ it('should parse this 69 byte signature', function () {
318
+ const sighex =
319
+ '3043021f59e4705959cc78acbfcf8bd0114e9cc1b389a4287fb33152b73a38c319b50302202f7428a27284c757e409bf41506183e9e49dfb54d5063796dfa0d403a4deccfa'
320
+ const sig = Buffer.from(sighex, 'hex')
321
+ const parsed = Sig.parseDer(sig)
322
+ parsed.header.should.equal(0x30)
323
+ parsed.length.should.equal(67)
324
+ parsed.rlength.should.equal(31)
325
+ parsed.rneg.should.equal(false)
326
+ parsed.rbuf
327
+ .toString('hex')
328
+ .should.equal(
329
+ '59e4705959cc78acbfcf8bd0114e9cc1b389a4287fb33152b73a38c319b503'
330
+ )
331
+ parsed.r
332
+ .toString()
333
+ .should.equal(
334
+ '158826015856106182499128681792325160381907915189052224498209222621383996675'
335
+ )
336
+ parsed.slength.should.equal(32)
337
+ parsed.sneg.should.equal(false)
338
+ parsed.sbuf
339
+ .toString('hex')
340
+ .should.equal(
341
+ '2f7428a27284c757e409bf41506183e9e49dfb54d5063796dfa0d403a4deccfa'
342
+ )
343
+ parsed.s
344
+ .toString()
345
+ .should.equal(
346
+ '21463938592353267769710297084836796652964571266930856168996063301532842380538'
347
+ )
348
+ })
349
+
350
+ it('should parse this 68 byte signature', function () {
351
+ const sighex =
352
+ '3042021e17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632022061bfa677f825ded82ba0863fb0c46ca1388dd3e647f6a93c038168b59d131a51'
353
+ const sig = Buffer.from(sighex, 'hex')
354
+ const parsed = Sig.parseDer(sig)
355
+ parsed.header.should.equal(0x30)
356
+ parsed.length.should.equal(66)
357
+ parsed.rlength.should.equal(30)
358
+ parsed.rneg.should.equal(false)
359
+ parsed.rbuf
360
+ .toString('hex')
361
+ .should.equal(
362
+ '17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632'
363
+ )
364
+ parsed.r
365
+ .toString()
366
+ .should.equal(
367
+ '164345250294671732127776123343329699648286106708464198588053542748255794'
368
+ )
369
+ parsed.slength.should.equal(32)
370
+ parsed.sneg.should.equal(false)
371
+ parsed.sbuf
372
+ .toString('hex')
373
+ .should.equal(
374
+ '61bfa677f825ded82ba0863fb0c46ca1388dd3e647f6a93c038168b59d131a51'
375
+ )
376
+ parsed.s
377
+ .toString()
378
+ .should.equal(
379
+ '44212963026209759051804639008236126356702363229859210154760104982946304432721'
380
+ )
381
+ })
382
+ })
383
+
384
+ describe('@IsTxDer', function () {
385
+ it('should know this is a DER signature', function () {
386
+ const sighex =
387
+ '3042021e17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632022061bfa677f825ded82ba0863fb0c46ca1388dd3e647f6a93c038168b59d131a5101'
388
+ const sigbuf = Buffer.from(sighex, 'hex')
389
+ Sig.IsTxDer(sigbuf).should.equal(true)
390
+ })
391
+
392
+ it('should know this is not a DER signature', function () {
393
+ // for more extensive tests, see the script interpreter
394
+ const sighex =
395
+ '3042021e17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632022061bfa677f825ded82ba0863fb0c46ca1388dd3e647f6a93c038168b59d131a5101'
396
+ const sigbuf = Buffer.from(sighex, 'hex')
397
+ sigbuf[0] = 0x31
398
+ Sig.IsTxDer(sigbuf).should.equal(false)
399
+ })
400
+ })
401
+
402
+ describe('#toHex', function () {
403
+ it('should convert these known r and s values into a known signature', function () {
404
+ const r = new Bn(
405
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
406
+ )
407
+ const s = new Bn(
408
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
409
+ )
410
+ const sig = new Sig(r, s)
411
+ sig
412
+ .toHex()
413
+ .should.equal(
414
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
415
+ )
416
+ })
417
+
418
+ it('should convert these known r, s, nHashType values into a known signature', function () {
419
+ const r = new Bn(
420
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
421
+ )
422
+ const s = new Bn(
423
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
424
+ )
425
+ const nHashType = Sig.SIGHASH_ALL
426
+ const sig = new Sig(r, s, nHashType)
427
+ sig
428
+ .toHex()
429
+ .should.equal(
430
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201'
431
+ )
432
+ })
433
+
434
+ it('should convert these known r and s values and guessed i values into signature', function () {
435
+ const r = new Bn(
436
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
437
+ )
438
+ const s = new Bn(
439
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
440
+ )
441
+ let sig = Sig.fromObject({ r: r, s: s, recovery: 0 })
442
+ sig
443
+ .toHex()
444
+ .toString('hex')
445
+ .should.equal(
446
+ '1f8bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
447
+ )
448
+ sig = Sig.fromObject({ r: r, s: s, recovery: 1 })
449
+ sig
450
+ .toHex()
451
+ .toString('hex')
452
+ .should.equal(
453
+ '208bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
454
+ )
455
+ sig = Sig.fromObject({ r: r, s: s, recovery: 2 })
456
+ sig
457
+ .toHex()
458
+ .toString('hex')
459
+ .should.equal(
460
+ '218bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
461
+ )
462
+ sig = Sig.fromObject({ r: r, s: s, recovery: 3 })
463
+ sig
464
+ .toHex()
465
+ .toString('hex')
466
+ .should.equal(
467
+ '228bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
468
+ )
469
+ })
470
+ })
471
+
472
+ describe('#toBuffer', function () {
473
+ it('should convert these known r and s values into a known signature', function () {
474
+ const r = new Bn(
475
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
476
+ )
477
+ const s = new Bn(
478
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
479
+ )
480
+ const sig = Sig.fromObject({ r: r, s: s })
481
+ const der = sig.toBuffer()
482
+ der
483
+ .toString('hex')
484
+ .should.equal(
485
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
486
+ )
487
+ })
488
+
489
+ it('should convert these known r, s, nHashType values into a known signature', function () {
490
+ const r = new Bn(
491
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
492
+ )
493
+ const s = new Bn(
494
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
495
+ )
496
+ const nHashType = Sig.SIGHASH_ALL
497
+ const sig = new Sig(r, s, nHashType)
498
+ const buf = sig.toBuffer()
499
+ buf
500
+ .toString('hex')
501
+ .should.equal(
502
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201'
503
+ )
504
+ })
505
+
506
+ it('should convert these known r and s values and guessed recovery values into signature', function () {
507
+ const r = new Bn(
508
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
509
+ )
510
+ const s = new Bn(
511
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
512
+ )
513
+ let sig = Sig.fromObject({ r: r, s: s, recovery: 0 })
514
+ sig
515
+ .toBuffer()
516
+ .toString('hex')
517
+ .should.equal(
518
+ '1f8bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
519
+ )
520
+ sig = Sig.fromObject({ r: r, s: s, recovery: 1 })
521
+ sig
522
+ .toBuffer()
523
+ .toString('hex')
524
+ .should.equal(
525
+ '208bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
526
+ )
527
+ sig = Sig.fromObject({ r: r, s: s, recovery: 2 })
528
+ sig
529
+ .toBuffer()
530
+ .toString('hex')
531
+ .should.equal(
532
+ '218bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
533
+ )
534
+ sig = Sig.fromObject({ r: r, s: s, recovery: 3 })
535
+ sig
536
+ .toBuffer()
537
+ .toString('hex')
538
+ .should.equal(
539
+ '228bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
540
+ )
541
+ })
542
+ })
543
+
544
+ describe('#toCompact', function () {
545
+ it('should convert these known r and s values and guessed i values into signature', function () {
546
+ const r = new Bn(
547
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
548
+ )
549
+ const s = new Bn(
550
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
551
+ )
552
+ let sig = Sig.fromObject({ r: r, s: s, recovery: 0 })
553
+ sig
554
+ .toCompact()
555
+ .toString('hex')
556
+ .should.equal(
557
+ '1f8bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
558
+ )
559
+ sig = Sig.fromObject({ r: r, s: s, recovery: 1 })
560
+ sig
561
+ .toCompact()
562
+ .toString('hex')
563
+ .should.equal(
564
+ '208bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
565
+ )
566
+ sig = Sig.fromObject({ r: r, s: s, recovery: 2 })
567
+ sig
568
+ .toCompact()
569
+ .toString('hex')
570
+ .should.equal(
571
+ '218bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
572
+ )
573
+ sig = Sig.fromObject({ r: r, s: s, recovery: 3 })
574
+ sig
575
+ .toCompact()
576
+ .toString('hex')
577
+ .should.equal(
578
+ '228bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa0993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
579
+ )
580
+ })
581
+ })
582
+
583
+ describe('#toDer', function () {
584
+ it('should convert these known r and s values into a known signature', function () {
585
+ const r = new Bn(
586
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
587
+ )
588
+ const s = new Bn(
589
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
590
+ )
591
+ const sig = Sig.fromObject({ r: r, s: s })
592
+ const der = sig.toDer()
593
+ der
594
+ .toString('hex')
595
+ .should.equal(
596
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
597
+ )
598
+ })
599
+ })
600
+
601
+ describe('#toTxFormat', function () {
602
+ it('should convert these known r, s, nHashType values into a known signature', function () {
603
+ const r = new Bn(
604
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
605
+ )
606
+ const s = new Bn(
607
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
608
+ )
609
+ const nHashType = Sig.SIGHASH_ALL
610
+ const sig = new Sig(r, s, nHashType)
611
+ const buf = sig.toTxFormat()
612
+ buf
613
+ .toString('hex')
614
+ .should.equal(
615
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201'
616
+ )
617
+ })
618
+ })
619
+
620
+ describe('#toString', function () {
621
+ it('should convert this signature in to hex DER', function () {
622
+ const r = new Bn(
623
+ '63173831029936981022572627018246571655303050627048489594159321588908385378810'
624
+ )
625
+ const s = new Bn(
626
+ '4331694221846364448463828256391194279133231453999942381442030409253074198130'
627
+ )
628
+ const sig = Sig.fromObject({ r: r, s: s })
629
+ const hex = sig.toString()
630
+ hex.should.equal(
631
+ '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'
632
+ )
633
+ })
634
+ })
635
+
636
+ describe('vectors', function () {
637
+ // TODO: These vectors were taken from BitcoinJS-lib during a debugging
638
+ // expedition. I only took a subset relevant for the stuff I wanted to
639
+ // test, but it would be valuable to revisit these test vectors and make
640
+ // sure all of them pass.
641
+ vectors.valid.forEach(function (vector) {
642
+ it('should pass this vector', function () {
643
+ const compact = vector.compact
644
+ const sig = Sig.fromCompact(Buffer.from(compact.hex, 'hex'))
645
+ sig.recovery.should.equal(compact.i)
646
+ sig.compressed.should.equal(compact.compressed)
647
+ sig
648
+ .toCompact()
649
+ .toString('hex')
650
+ .should.equal(compact.hex)
651
+ })
652
+ })
653
+
654
+ vectors.invalid.compact.forEach(function (compact) {
655
+ it('should pass this vector', function () {
656
+ ;(function () {
657
+ Sig.fromCompact(Buffer.from(compact.hex, 'hex'))
658
+ }.should.throw())
659
+ })
660
+ })
661
+ })
662
+ })