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
@@ -1,120 +0,0 @@
1
- /**
2
- * PubKey Map
3
- * ==========
4
- *
5
- * A map from (transaction hash, output number) to (script chunk index, pubKey).
6
- * Whening signing a bitcoin transaction, we need to be able to sign an input
7
- * with the correct key and also we need to know where to put signature when we
8
- * get it. This mapping allows us to find the key for an associated input (which
9
- * is identified by tx output hash and number) with which to sign the
10
- * transaction and then also to know where to insert the signature into the
11
- * input script. This gets us the public key, and we need a different method to
12
- * get the private key. That is because we often know the public key to be used
13
- * but may not have access to the private key until the entire tx is sent to
14
- * where the private keys are.
15
- */
16
- 'use strict'
17
-
18
- import { Struct } from './struct'
19
- import { Sig } from './sig'
20
-
21
- class SigOperations extends Struct {
22
- constructor (map = new Map()) {
23
- super({ map })
24
- }
25
-
26
- toJSON () {
27
- const json = {}
28
- this.map.forEach((arr, label) => {
29
- json[label] = arr.map(obj => ({
30
- nScriptChunk: obj.nScriptChunk,
31
- type: obj.type, // 'sig' or 'pubKey'
32
- addressStr: obj.addressStr,
33
- nHashType: obj.nHashType,
34
- log: obj.log
35
- }))
36
- })
37
- return json
38
- }
39
-
40
- fromJSON (json) {
41
- Object.keys(json).forEach(label => {
42
- this.map.set(label, json[label].map(obj => ({
43
- nScriptChunk: obj.nScriptChunk,
44
- type: obj.type,
45
- addressStr: obj.addressStr,
46
- nHashType: obj.nHashType,
47
- log: obj.log
48
- })))
49
- })
50
- return this
51
- }
52
-
53
- /**
54
- * Set an address to in the map for use with single-sig.
55
- *
56
- * @param {Buffer} txHashBuf The hash of a transsaction. Note that this is
57
- * *not* the reversed transaction id, but is the raw hash.
58
- * @param {Number} txOutNum The output number, a.k.a. the "vout".
59
- * @param {Number} nScriptChunk The index of the chunk of the script where
60
- * we are going to place the signature.
61
- * @param {String} addressStr The addressStr coresponding to this (txHashBuf,
62
- * txOutNum, nScriptChunk) where we are going to sign and insert the
63
- * signature or public key.
64
- * @param {Number} nHashType Usually = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
65
- */
66
- setOne (txHashBuf, txOutNum, nScriptChunk, type = 'sig', addressStr, nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID) {
67
- const label = txHashBuf.toString('hex') + ':' + txOutNum
68
- const obj = { nScriptChunk, type, addressStr, nHashType }
69
- this.map.set(label, [obj])
70
- return this
71
- }
72
-
73
- /**
74
- * Set a bunch of addresses for signing an input such as for use with multi-sig.
75
- *
76
- * @param {Buffer} txHashBuf The hash of a transsaction. Note that this is
77
- * *not* the reversed transaction id, but is the raw hash.
78
- * @param {Number} txOutNum The output number, a.k.a. the "vout".
79
- * @param {Array} arr Must take the form of [{nScriptChunk, type, addressStr, nHashType}, ...]
80
- */
81
- setMany (txHashBuf, txOutNum, arr) {
82
- const label = txHashBuf.toString('hex') + ':' + txOutNum
83
- arr = arr.map(obj => ({
84
- type: obj.type || 'sig',
85
- nHashType: obj.nHashType || Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID,
86
- ...obj
87
- }))
88
- this.map.set(label, arr)
89
- return this
90
- }
91
-
92
- addOne (txHashBuf, txOutNum, nScriptChunk, type = 'sig', addressStr, nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID) {
93
- const arr = this.get(txHashBuf, txOutNum) || []
94
- arr.push({
95
- nScriptChunk,
96
- type,
97
- addressStr,
98
- nHashType
99
- })
100
- this.setMany(txHashBuf, txOutNum, arr)
101
- return this
102
- }
103
-
104
- /**
105
- * Get an address from the map
106
- *
107
- * @param {Buffer} txHashBuf The hash of a transction. Note that this is *not*
108
- * the reversed transaction id, but is the raw hash.
109
- * @param {Number} txOutNum The output number, a.k.a. the "vout".
110
- * @param {Number} nScriptChunk The index of the chunk of the script where
111
- * we are going to place the signature.
112
- * @returns {PubKey}
113
- */
114
- get (txHashBuf, txOutNum) {
115
- const label = txHashBuf.toString('hex') + ':' + txOutNum
116
- return this.map.get(label) || []
117
- }
118
- }
119
-
120
- export { SigOperations }
package/lib/sig.js DELETED
@@ -1,399 +0,0 @@
1
- /**
2
- * Signature
3
- * =========
4
- *
5
- * A signature is the thing you make when you want to sign a transaction, or
6
- * the thing you want to verify if you want to ensure that someone signed a
7
- * transaction. It has an r and s value, which are the cryptographic big
8
- * numbers that define a signature. And since this is a bitcoin library, it
9
- * also has nHashType, which is the way to hash a transaction and is used in
10
- * the binary format of a signature when it is in a transaction. We also
11
- * support a public key recover value, recovery, allowing one to compute the
12
- * public key from a signature. The "compressed" value is also necessary to
13
- * accurately compute the public key from a signature.
14
- *
15
- * There are a few different formats of a signature in bitcoin. One is DER, the
16
- * other is the TxFormat which is the same as DER but with the nHashType byte
17
- * appended, and the final one is Compact, which is used by Bitcoin Signed
18
- * Message (Bsm).
19
- */
20
- 'use strict'
21
-
22
- import { Bn } from './bn'
23
- import { Struct } from './struct'
24
-
25
- /**
26
- * r, s: big numbers constiting a cryptographic signature
27
- * nHashType: found at the end of a signature in a transaction
28
- * recovery: public key recovery number
29
- * compressed: whether the recovered pubKey is compressed
30
- */
31
- class Sig extends Struct {
32
- constructor (r, s, nHashType, recovery, compressed) {
33
- super({ r, s, nHashType, recovery, compressed })
34
- }
35
-
36
- fromBuffer (buf) {
37
- try {
38
- return this.fromDer(buf, true)
39
- } catch (e) {}
40
- try {
41
- return this.fromCompact(buf)
42
- } catch (e) {}
43
- return this.fromTxFormat(buf)
44
- }
45
-
46
- toBuffer () {
47
- if (this.nHashType !== undefined) {
48
- return this.toTxFormat()
49
- } else if (this.recovery !== undefined) {
50
- return this.toCompact()
51
- }
52
- return this.toDer()
53
- }
54
-
55
- // The format used by "message"
56
- fromCompact (buf) {
57
- let compressed = true
58
- let recovery = buf.slice(0, 1)[0] - 27 - 4
59
- if (recovery < 0) {
60
- compressed = false
61
- recovery = recovery + 4
62
- }
63
-
64
- if (
65
- !(recovery === 0 || recovery === 1 || recovery === 2 || recovery === 3)
66
- ) {
67
- throw new Error('i must be 0, 1, 2, or 3')
68
- }
69
-
70
- this.compressed = compressed
71
- this.recovery = recovery
72
-
73
- const rsbuf = buf.slice(1)
74
- this.fromRS(rsbuf)
75
-
76
- return this
77
- }
78
-
79
- static fromCompact (buf) {
80
- return new this().fromCompact(buf)
81
- }
82
-
83
- fromRS (rsbuf) {
84
- const b2 = rsbuf.slice(0, 32)
85
- const b3 = rsbuf.slice(32, 64)
86
- if (b2.length !== 32) {
87
- throw new Error('r must be 32 bytes')
88
- }
89
- if (b3.length !== 32 || rsbuf.length > 64) {
90
- throw new Error('s must be 32 bytes')
91
- }
92
- this.r = new Bn().fromBuffer(b2)
93
- this.s = new Bn().fromBuffer(b3)
94
- return this
95
- }
96
-
97
- static fromRS (rsbuf) {
98
- return new this().fromRS(rsbuf)
99
- }
100
-
101
- // The format used in a tx, except without the nHashType at the end
102
- fromDer (buf, strict) {
103
- const obj = Sig.parseDer(buf, strict)
104
- this.r = obj.r
105
- this.s = obj.s
106
-
107
- return this
108
- }
109
-
110
- static fromDer (buf, strict) {
111
- return new this().fromDer(buf, strict)
112
- }
113
-
114
- // The format used in a tx
115
- fromTxFormat (buf) {
116
- if (buf.length === 0) {
117
- // allow setting a "blank" signature
118
- this.r = new Bn(1)
119
- this.s = new Bn(1)
120
- this.nHashType = 1
121
- return this
122
- }
123
- const nHashType = buf.readUInt8(buf.length - 1)
124
- const derbuf = buf.slice(0, buf.length - 1)
125
- this.fromDer(derbuf, false)
126
- this.nHashType = nHashType
127
- return this
128
- }
129
-
130
- static fromTxFormat (buf) {
131
- return new this().fromTxFormat(buf)
132
- }
133
-
134
- fromString (str) {
135
- return this.fromHex(str)
136
- }
137
-
138
- /**
139
- * In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
140
- */
141
- static parseDer (buf, strict) {
142
- if (strict === undefined) {
143
- strict = true
144
- }
145
-
146
- if (!Buffer.isBuffer(buf)) {
147
- throw new Error('DER formatted signature should be a buffer')
148
- }
149
-
150
- const header = buf[0]
151
-
152
- if (header !== 0x30) {
153
- throw new Error('Header byte should be 0x30')
154
- }
155
-
156
- let length = buf[1]
157
- const buflength = buf.slice(2).length
158
- if (strict && length !== buflength) {
159
- throw new Error('LEngth byte should length of what follows')
160
- } else {
161
- length = length < buflength ? length : buflength
162
- }
163
-
164
- const rheader = buf[2 + 0]
165
- if (rheader !== 0x02) {
166
- throw new Error('Integer byte for r should be 0x02')
167
- }
168
-
169
- const rlength = buf[2 + 1]
170
- const rbuf = buf.slice(2 + 2, 2 + 2 + rlength)
171
- const r = new Bn().fromBuffer(rbuf)
172
- const rneg = buf[2 + 1 + 1] === 0x00
173
- if (rlength !== rbuf.length) {
174
- throw new Error('LEngth of r incorrect')
175
- }
176
-
177
- const sheader = buf[2 + 2 + rlength + 0]
178
- if (sheader !== 0x02) {
179
- throw new Error('Integer byte for s should be 0x02')
180
- }
181
-
182
- const slength = buf[2 + 2 + rlength + 1]
183
- const sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength)
184
- const s = new Bn().fromBuffer(sbuf)
185
- const sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00
186
- if (slength !== sbuf.length) {
187
- throw new Error('LEngth of s incorrect')
188
- }
189
-
190
- const sumlength = 2 + 2 + rlength + 2 + slength
191
- if (length !== sumlength - 2) {
192
- throw new Error('LEngth of signature incorrect')
193
- }
194
-
195
- const obj = {
196
- header,
197
- length,
198
- rheader,
199
- rlength,
200
- rneg,
201
- rbuf,
202
- r,
203
- sheader,
204
- slength,
205
- sneg,
206
- sbuf,
207
- s
208
- }
209
-
210
- return obj
211
- }
212
-
213
- /**
214
- * This function is translated from bitcoind's IsDERSignature and is used in
215
- * the script interpreter. This "DER" format actually includes an extra byte,
216
- * the nHashType, at the end. It is really the tx format, not DER format.
217
- *
218
- * A canonical signature exists of: [30] [total len] [02] [len R] [R] [02] [len S] [S] [hashtype]
219
- * Where R and S are not negative (their first byte has its highest bit not set), and not
220
- * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
221
- * in which case a single 0 byte is necessary and even required).
222
- *
223
- * See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
224
- */
225
- static IsTxDer (buf) {
226
- if (buf.length < 9) {
227
- // Non-canonical signature: too short
228
- return false
229
- }
230
- if (buf.length > 73) {
231
- // Non-canonical signature: too long
232
- return false
233
- }
234
- if (buf[0] !== 0x30) {
235
- // Non-canonical signature: wrong type
236
- return false
237
- }
238
- if (buf[1] !== buf.length - 3) {
239
- // Non-canonical signature: wrong length marker
240
- return false
241
- }
242
- const nLEnR = buf[3]
243
- if (5 + nLEnR >= buf.length) {
244
- // Non-canonical signature: S length misplaced
245
- return false
246
- }
247
- const nLEnS = buf[5 + nLEnR]
248
- if (nLEnR + nLEnS + 7 !== buf.length) {
249
- // Non-canonical signature: R+S length mismatch
250
- return false
251
- }
252
-
253
- const R = buf.slice(4)
254
- if (buf[4 - 2] !== 0x02) {
255
- // Non-canonical signature: R value type mismatch
256
- return false
257
- }
258
- if (nLEnR === 0) {
259
- // Non-canonical signature: R length is zero
260
- return false
261
- }
262
- if (R[0] & 0x80) {
263
- // Non-canonical signature: R value negative
264
- return false
265
- }
266
- if (nLEnR > 1 && R[0] === 0x00 && !(R[1] & 0x80)) {
267
- // Non-canonical signature: R value excessively padded
268
- return false
269
- }
270
-
271
- const S = buf.slice(6 + nLEnR)
272
- if (buf[6 + nLEnR - 2] !== 0x02) {
273
- // Non-canonical signature: S value type mismatch
274
- return false
275
- }
276
- if (nLEnS === 0) {
277
- // Non-canonical signature: S length is zero
278
- return false
279
- }
280
- if (S[0] & 0x80) {
281
- // Non-canonical signature: S value negative
282
- return false
283
- }
284
- if (nLEnS > 1 && S[0] === 0x00 && !(S[1] & 0x80)) {
285
- // Non-canonical signature: S value excessively padded
286
- return false
287
- }
288
- return true
289
- }
290
-
291
- /**
292
- * Compares to bitcoind's IsLowDERSignature
293
- * See also Ecdsa signature algorithm which enforces this.
294
- * See also Bip 62, "low S values in signatures"
295
- */
296
- hasLowS () {
297
- if (
298
- this.s.lt(1) ||
299
- this.s.gt(
300
- Bn.fromBuffer(
301
- Buffer.from(
302
- '7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0',
303
- 'hex'
304
- )
305
- )
306
- )
307
- ) {
308
- return false
309
- }
310
- return true
311
- }
312
-
313
- /**
314
- * Ensures the nHashType is exactly equal to one of the standard options or combinations thereof.
315
- * Translated from bitcoind's IsDefinedHashtypeSignature
316
- */
317
- hasDefinedHashType () {
318
- if (
319
- this.nHashType < Sig.SIGHASH_ALL ||
320
- this.nHashType > Sig.SIGHASH_SINGLE
321
- ) {
322
- return false
323
- }
324
- return true
325
- }
326
-
327
- toCompact (recovery, compressed) {
328
- recovery = typeof recovery === 'number' ? recovery : this.recovery
329
- compressed =
330
- typeof compressed === 'boolean' ? compressed : this.compressed
331
-
332
- if (
333
- !(recovery === 0 || recovery === 1 || recovery === 2 || recovery === 3)
334
- ) {
335
- throw new Error('recovery must be equal to 0, 1, 2, or 3')
336
- }
337
-
338
- let val = recovery + 27 + 4
339
- if (compressed === false) {
340
- val = val - 4
341
- }
342
- const b1 = Buffer.from([val])
343
- const b2 = this.r.toBuffer({ size: 32 })
344
- const b3 = this.s.toBuffer({ size: 32 })
345
- return Buffer.concat([b1, b2, b3])
346
- }
347
-
348
- toRS () {
349
- return Buffer.concat([
350
- this.r.toBuffer({ size: 32 }),
351
- this.s.toBuffer({ size: 32 })
352
- ])
353
- }
354
-
355
- toDer () {
356
- const rnbuf = this.r.toBuffer()
357
- const snbuf = this.s.toBuffer()
358
-
359
- const rneg = rnbuf[0] & 0x80
360
- const sneg = snbuf[0] & 0x80
361
-
362
- const rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf
363
- const sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf
364
-
365
- const length = 2 + rbuf.length + 2 + sbuf.length
366
- const rlength = rbuf.length
367
- const slength = sbuf.length
368
- const rheader = 0x02
369
- const sheader = 0x02
370
- const header = 0x30
371
-
372
- const der = Buffer.concat([
373
- Buffer.from([header, length, rheader, rlength]),
374
- rbuf,
375
- Buffer.from([sheader, slength]),
376
- sbuf
377
- ])
378
- return der
379
- }
380
-
381
- toTxFormat () {
382
- const derbuf = this.toDer()
383
- const buf = Buffer.alloc(1)
384
- buf.writeUInt8(this.nHashType, 0)
385
- return Buffer.concat([derbuf, buf])
386
- }
387
-
388
- toString () {
389
- return this.toHex()
390
- }
391
- }
392
-
393
- Sig.SIGHASH_ALL = 0x00000001
394
- Sig.SIGHASH_NONE = 0x00000002
395
- Sig.SIGHASH_SINGLE = 0x00000003
396
- Sig.SIGHASH_FORKID = 0x00000040
397
- Sig.SIGHASH_ANYONECANPAY = 0x00000080
398
-
399
- export { Sig }