bsv2 2.0.23 → 2.1.0

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 (113) hide show
  1. package/{.babelrc.test → .babel.test} +1 -1
  2. package/.husky/pre-commit +1 -1
  3. package/.mocharc.yml +1 -1
  4. package/.travis.yml +1 -1
  5. package/babel.config.cjs +44 -0
  6. package/benchmark.js +10 -18
  7. package/bin/bsv.js +1 -1
  8. package/bin/network.js +2 -1
  9. package/dist/bsv2.mjs +36061 -4
  10. package/dist/bsv2.umd.js +28 -0
  11. package/doc/assets/anchor.js +350 -0
  12. package/doc/assets/bass-addons.css +12 -0
  13. package/doc/assets/bass.css +544 -0
  14. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  15. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  16. package/doc/assets/fonts/LICENSE.txt +93 -0
  17. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  18. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  19. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  20. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  21. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  22. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  23. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  24. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  25. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  26. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  27. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  28. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  29. package/doc/assets/fonts/source-code-pro.css +23 -0
  30. package/doc/assets/github.css +123 -0
  31. package/doc/assets/site.js +168 -0
  32. package/doc/assets/split.css +15 -0
  33. package/doc/assets/split.js +782 -0
  34. package/doc/assets/style.css +147 -0
  35. package/doc/index.html +9461 -0
  36. package/entry.js +2 -3
  37. package/eslint.config.mjs +8 -0
  38. package/index.js +1 -5
  39. package/{karma.conf.js → karma.conf.ts} +2 -3
  40. package/lib/ach.js +5 -5
  41. package/lib/address.js +11 -9
  42. package/lib/aescbc.js +3 -3
  43. package/lib/bip-39.js +12 -11
  44. package/lib/block.js +2 -2
  45. package/lib/bn.js +12 -17
  46. package/lib/bsm.js +1 -1
  47. package/lib/config.js +1 -1
  48. package/lib/constants.js +1 -1
  49. package/lib/ecdsa.js +24 -23
  50. package/lib/interp.js +190 -26
  51. package/lib/merkle.js +1 -1
  52. package/lib/op-code.js +137 -135
  53. package/lib/priv-key.js +1 -1
  54. package/lib/script.js +44 -42
  55. package/lib/sig.js +12 -12
  56. package/lib/struct.js +9 -6
  57. package/lib/tx.js +8 -7
  58. package/load-babel.js +3 -1
  59. package/mtest/test.html +6 -4
  60. package/mtest/test.js +1 -1
  61. package/out/Sig.html +541 -0
  62. package/out/bip-32.js.html +523 -0
  63. package/out/bip-39.js.html +336 -0
  64. package/out/block.js.html +183 -0
  65. package/out/bn.js.html +512 -0
  66. package/out/ecdsa.js.html +546 -0
  67. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  68. package/out/fonts/OpenSans-Bold-webfont.svg +1830 -0
  69. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  70. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  71. package/out/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  72. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  73. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  74. package/out/fonts/OpenSans-Italic-webfont.svg +1830 -0
  75. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  76. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  77. package/out/fonts/OpenSans-Light-webfont.svg +1831 -0
  78. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  79. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  80. package/out/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  81. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  82. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  83. package/out/fonts/OpenSans-Regular-webfont.svg +1831 -0
  84. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  85. package/out/global.html +220 -0
  86. package/out/index.html +128 -0
  87. package/out/interp.js.html +2064 -0
  88. package/out/merkle.js.html +143 -0
  89. package/out/msg.js.html +235 -0
  90. package/out/priv-key.js.html +208 -0
  91. package/out/pub-key.js.html +281 -0
  92. package/out/script.js.html +876 -0
  93. package/out/scripts/linenumber.js +25 -0
  94. package/out/scripts/prettify/Apache-License-2.0.txt +202 -0
  95. package/out/scripts/prettify/lang-css.js +2 -0
  96. package/out/scripts/prettify/prettify.js +28 -0
  97. package/out/sig-operations.js.html +171 -0
  98. package/out/sig.js.html +450 -0
  99. package/out/struct.js.html +440 -0
  100. package/out/styles/jsdoc-default.css +358 -0
  101. package/out/styles/prettify-jsdoc.css +111 -0
  102. package/out/styles/prettify-tomorrow.css +132 -0
  103. package/out/tx-builder.js.html +644 -0
  104. package/out/tx-in.js.html +223 -0
  105. package/out/tx-verifier.js.html +260 -0
  106. package/out/tx.js.html +481 -0
  107. package/package.json +34 -39
  108. package/vite.config.js +36 -0
  109. package/dist/bsv2.bundle.js +0 -8
  110. package/dist/bsv2.cjs.js +0 -2
  111. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  112. package/dist/bsv2.module.js +0 -2
  113. /package/{webpack.config.js → mtest/webpack.config.js} +0 -0
@@ -0,0 +1,143 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>JSDoc: Source: merkle.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"> </script>
8
+ <script src="scripts/prettify/lang-css.js"> </script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14
+ </head>
15
+
16
+ <body>
17
+
18
+ <div id="main">
19
+
20
+ <h1 class="page-title">Source: merkle.js</h1>
21
+
22
+
23
+
24
+
25
+
26
+
27
+ <section>
28
+ <article>
29
+ <pre class="prettyprint source linenums"><code>/**
30
+ * Merkle
31
+ * ======
32
+ *
33
+ * A node in a Merkle tree (possibly the root node, in which case it is the
34
+ * Merkle root). A node either contains a buffer or links to two other nodes.
35
+ */
36
+ 'use strict'
37
+
38
+ import { Hash } from './hash'
39
+ import { Struct } from './struct'
40
+
41
+ class Merkle extends Struct {
42
+ constructor (hashBuf, buf, merkle1, merkle2) {
43
+ super({ hashBuf, buf, merkle1, merkle2 })
44
+ }
45
+
46
+ hash () {
47
+ if (this.hashBuf) {
48
+ return this.hashBuf
49
+ }
50
+ if (this.buf) {
51
+ return Hash.sha256Sha256(this.buf)
52
+ }
53
+ const hashBuf1 = this.merkle1.hash()
54
+ const hashBuf2 = this.merkle2.hash()
55
+ this.buf = Buffer.concat([hashBuf1, hashBuf2])
56
+ return Hash.sha256Sha256(this.buf)
57
+ }
58
+
59
+ fromBuffers (bufs) {
60
+ if (bufs.length &lt; 1) {
61
+ throw new Error('buffers must have a length')
62
+ }
63
+ bufs = bufs.slice()
64
+ const log = Math.log2(bufs.length)
65
+ if (!Number.isInteger(log)) {
66
+ // If a merkle tree does not have a number of ends that is a power of 2,
67
+ // then we have to copy the last value until it is a power of 2. Note
68
+ // that we copy *the actual object* over and over again, which ensures
69
+ // that when we finds its hash, the hash is cached.
70
+ const lastval = bufs[bufs.length - 1]
71
+ const len = Math.pow(2, Math.ceil(log))
72
+ for (let i = bufs.length; i &lt; len; i++) {
73
+ bufs.push(lastval)
74
+ }
75
+ }
76
+ const bufs1 = bufs.slice(0, bufs.length / 2)
77
+ const bufs2 = bufs.slice(bufs.length / 2)
78
+ this.fromBufferArrays(bufs1, bufs2)
79
+ return this
80
+ }
81
+
82
+ static fromBuffers (bufs) {
83
+ return new this().fromBuffers(bufs)
84
+ }
85
+
86
+ /**
87
+ * Takes two arrays, both of which *must* be of a length that is a power of
88
+ * two.
89
+ */
90
+ fromBufferArrays (bufs1, bufs2) {
91
+ if (bufs1.length === 1) {
92
+ this.merkle1 = new Merkle(undefined, bufs1[0])
93
+ this.merkle2 = new Merkle(undefined, bufs2[0])
94
+ return this
95
+ }
96
+ const bufs11 = bufs1.slice(0, bufs1.length / 2)
97
+ const bufs12 = bufs1.slice(bufs1.length / 2)
98
+ this.merkle1 = new Merkle().fromBufferArrays(bufs11, bufs12)
99
+ const bufs21 = bufs2.slice(0, bufs2.length / 2)
100
+ const bufs22 = bufs2.slice(bufs2.length / 2)
101
+ this.merkle2 = new Merkle().fromBufferArrays(bufs21, bufs22)
102
+ return this
103
+ }
104
+
105
+ static fromBufferArrays (bufs1, bufs2) {
106
+ return new this().fromBufferArrays(bufs1, bufs2)
107
+ }
108
+
109
+ leavesNum () {
110
+ if (this.merkle1) {
111
+ return this.merkle1.leavesNum() + this.merkle2.leavesNum()
112
+ }
113
+ if (this.buf) {
114
+ return 1
115
+ }
116
+ throw new Error('invalid number of leaves')
117
+ }
118
+ }
119
+
120
+ export { Merkle }
121
+ </code></pre>
122
+ </article>
123
+ </section>
124
+
125
+
126
+
127
+
128
+ </div>
129
+
130
+ <nav>
131
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sig.html">Sig</a></li></ul><h3>Global</h3><ul><li><a href="global.html#decorate">decorate</a></li></ul>
132
+ </nav>
133
+
134
+ <br class="clear">
135
+
136
+ <footer>
137
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Feb 28 2025 03:51:24 GMT+0100 (hora estándar de Europa central)
138
+ </footer>
139
+
140
+ <script> prettyPrint(); </script>
141
+ <script src="scripts/linenumber.js"> </script>
142
+ </body>
143
+ </html>
@@ -0,0 +1,235 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>JSDoc: Source: msg.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"> </script>
8
+ <script src="scripts/prettify/lang-css.js"> </script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14
+ </head>
15
+
16
+ <body>
17
+
18
+ <div id="main">
19
+
20
+ <h1 class="page-title">Source: msg.js</h1>
21
+
22
+
23
+
24
+
25
+
26
+
27
+ <section>
28
+ <article>
29
+ <pre class="prettyprint source linenums"><code>/**
30
+ * Peer-to-Peer Network Message
31
+ * ============================
32
+ *
33
+ * A message on the bitcoin p2p network.
34
+ */
35
+ 'use strict'
36
+
37
+ import { Br } from './br'
38
+ import { Bw } from './bw'
39
+ import { Constants, getConstants } from './constants'
40
+ import { Hash } from './hash'
41
+ import { Struct } from './struct'
42
+ import { cmp } from './cmp'
43
+
44
+ class Msg extends Struct {
45
+ constructor (magicNum, cmdbuf, datasize, checksumbuf, dataBuf, constants = null) {
46
+ super()
47
+ this.constants = constants || Constants.Default
48
+ this.magicNum = this.constants.Msg.magicNum
49
+ this.fromObject({ magicNum, cmdbuf, datasize, checksumbuf, dataBuf })
50
+ }
51
+
52
+ setCmd (cmdname) {
53
+ this.cmdbuf = Buffer.alloc(12)
54
+ this.cmdbuf.fill(0)
55
+ this.cmdbuf.write(cmdname)
56
+ return this
57
+ }
58
+
59
+ getCmd () {
60
+ let end = this.cmdbuf.length
61
+ for (let i = end; i > 0; i--) {
62
+ if (this.cmdbuf[i - 1] !== 0) {
63
+ end = i
64
+ break
65
+ }
66
+ }
67
+ return this.cmdbuf.toString('utf8', 0, end)
68
+ }
69
+
70
+ static checksum (dataBuf) {
71
+ return Hash.sha256Sha256(dataBuf).slice(0, 4)
72
+ }
73
+
74
+ static async asyncChecksum (dataBuf) {
75
+ const hashBuf = await Hash.asyncSha256Sha256(dataBuf)
76
+ return hashBuf.slice(0, 4)
77
+ }
78
+
79
+ setData (dataBuf) {
80
+ this.dataBuf = dataBuf
81
+ this.datasize = dataBuf.length
82
+ this.checksumbuf = Msg.checksum(dataBuf)
83
+ return this
84
+ }
85
+
86
+ async asyncSetData (dataBuf) {
87
+ this.dataBuf = dataBuf
88
+ this.datasize = dataBuf.length
89
+ this.checksumbuf = await Msg.asyncChecksum(dataBuf)
90
+ return this
91
+ }
92
+
93
+ /**
94
+ * An iterator to produce a message from a series of buffers. Set opts.strict
95
+ * to throw an error if an invalid message occurs in stream.
96
+ */
97
+ * genFromBuffers (opts) {
98
+ opts = opts === undefined ? {} : opts
99
+ let res
100
+ res = yield * this.expect(4)
101
+ this.magicNum = new Br(res.buf).readUInt32BE()
102
+ if (opts.strict &amp;&amp; this.magicNum !== this.constants.Msg.magicNum) {
103
+ throw new Error('invalid magicNum')
104
+ }
105
+ res = yield * this.expect(12, res.remainderbuf)
106
+ this.cmdbuf = new Br(res.buf).read(12)
107
+ res = yield * this.expect(4, res.remainderbuf)
108
+ this.datasize = new Br(res.buf).readUInt32BE()
109
+ if (opts.strict &amp;&amp; this.datasize > this.constants.MaxSize) {
110
+ throw new Error('message size greater than maxsize')
111
+ }
112
+ res = yield * this.expect(4, res.remainderbuf)
113
+ this.checksumbuf = new Br(res.buf).read(4)
114
+ res = yield * this.expect(this.datasize, res.remainderbuf)
115
+ this.dataBuf = new Br(res.buf).read(this.datasize)
116
+ return res.remainderbuf
117
+ }
118
+
119
+ fromBr (br) {
120
+ this.magicNum = br.readUInt32BE()
121
+ this.constants = getConstants(this.magicNum)
122
+ this.cmdbuf = br.read(12)
123
+ this.datasize = br.readUInt32BE()
124
+ this.checksumbuf = br.read(4)
125
+ this.dataBuf = br.read()
126
+ return this
127
+ }
128
+
129
+ toBw (bw) {
130
+ if (!bw) {
131
+ bw = new Bw()
132
+ }
133
+ bw.writeUInt32BE(this.magicNum)
134
+ bw.write(this.cmdbuf)
135
+ bw.writeUInt32BE(this.datasize)
136
+ bw.write(this.checksumbuf)
137
+ bw.write(this.dataBuf)
138
+ return bw
139
+ }
140
+
141
+ fromJSON (json) {
142
+ this.magicNum = json.magicNum
143
+ this.constants = getConstants(this.magicNum)
144
+ this.cmdbuf = Buffer.from(json.cmdbuf, 'hex')
145
+ this.datasize = json.datasize
146
+ this.checksumbuf = Buffer.from(json.checksumbuf, 'hex')
147
+ this.dataBuf = Buffer.from(json.dataBuf, 'hex')
148
+ return this
149
+ }
150
+
151
+ toJSON () {
152
+ return {
153
+ magicNum: this.magicNum,
154
+ cmdbuf: this.cmdbuf.toString('hex'),
155
+ datasize: this.datasize,
156
+ checksumbuf: this.checksumbuf.toString('hex'),
157
+ dataBuf: this.dataBuf.toString('hex')
158
+ }
159
+ }
160
+
161
+ isValid () {
162
+ // TODO: Add more checks
163
+ const checksumbuf = Msg.checksum(this.dataBuf)
164
+ return cmp(checksumbuf, this.checksumbuf)
165
+ }
166
+
167
+ async asyncIsValid () {
168
+ // TODO: Add more checks
169
+ const checksumbuf = await Msg.asyncChecksum(this.dataBuf)
170
+ return cmp(checksumbuf, this.checksumbuf)
171
+ }
172
+
173
+ validate () {
174
+ if (!this.isValid()) {
175
+ throw new Error('invalid message')
176
+ }
177
+ return this
178
+ }
179
+
180
+ async asyncValidate () {
181
+ const isValid = await this.asyncIsValid()
182
+ if (isValid !== true) {
183
+ throw new Error('invalid message')
184
+ }
185
+ }
186
+ }
187
+
188
+ Msg.Mainnet = class extends Msg {
189
+ constructor (magicNum, cmdbuf, datasize, checksumbuf, dataBuf) {
190
+ super(magicNum, cmdbuf, datasize, checksumbuf, dataBuf, Constants.Mainnet)
191
+ }
192
+ }
193
+
194
+ Msg.Testnet = class extends Msg {
195
+ constructor (magicNum, cmdbuf, datasize, checksumbuf, dataBuf) {
196
+ super(magicNum, cmdbuf, datasize, checksumbuf, dataBuf, Constants.Testnet)
197
+ }
198
+ }
199
+
200
+ Msg.Regtest = class extends Msg {
201
+ constructor (magicNum, cmdbuf, datasize, checksumbuf, dataBuf) {
202
+ super(magicNum, cmdbuf, datasize, checksumbuf, dataBuf, Constants.Regtest)
203
+ }
204
+ }
205
+
206
+ Msg.STN = class extends Msg {
207
+ constructor (magicNum, cmdbuf, datasize, checksumbuf, dataBuf) {
208
+ super(magicNum, cmdbuf, datasize, checksumbuf, dataBuf, Constants.STN)
209
+ }
210
+ }
211
+
212
+ export { Msg }
213
+ </code></pre>
214
+ </article>
215
+ </section>
216
+
217
+
218
+
219
+
220
+ </div>
221
+
222
+ <nav>
223
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sig.html">Sig</a></li></ul><h3>Global</h3><ul><li><a href="global.html#decorate">decorate</a></li></ul>
224
+ </nav>
225
+
226
+ <br class="clear">
227
+
228
+ <footer>
229
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Feb 28 2025 03:51:24 GMT+0100 (hora estándar de Europa central)
230
+ </footer>
231
+
232
+ <script> prettyPrint(); </script>
233
+ <script src="scripts/linenumber.js"> </script>
234
+ </body>
235
+ </html>
@@ -0,0 +1,208 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>JSDoc: Source: priv-key.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"> </script>
8
+ <script src="scripts/prettify/lang-css.js"> </script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14
+ </head>
15
+
16
+ <body>
17
+
18
+ <div id="main">
19
+
20
+ <h1 class="page-title">Source: priv-key.js</h1>
21
+
22
+
23
+
24
+
25
+
26
+
27
+ <section>
28
+ <article>
29
+ <pre class="prettyprint source linenums"><code>/**
30
+ * Private Key
31
+ * ===========
32
+ *
33
+ * A private key is used for signing transactions (or messages). The primary
34
+ * way to use this is new PrivKey().fromRandom(), or new PrivKey().fromBuffer(buf).
35
+ */
36
+ 'use strict'
37
+
38
+ import { Bn } from './bn'
39
+ import { Point } from './point'
40
+ import { Constants } from './constants'
41
+ import { Base58Check } from './base-58-check'
42
+ import { Random } from './random'
43
+ import { Struct } from './struct'
44
+
45
+ class PrivKey extends Struct {
46
+ constructor (bn, compressed, constants = null) {
47
+ super({ bn, compressed })
48
+ constants = constants || Constants.Default.PrivKey
49
+ this.Constants = constants
50
+ }
51
+
52
+ fromJSON (json) {
53
+ this.fromHex(json)
54
+ return this
55
+ }
56
+
57
+ toJSON () {
58
+ return this.toHex()
59
+ }
60
+
61
+ fromRandom () {
62
+ let privBuf, bn, condition
63
+
64
+ do {
65
+ privBuf = Random.getRandomBuffer(32)
66
+ bn = new Bn().fromBuffer(privBuf)
67
+ condition = bn.lt(Point.getN())
68
+ } while (!condition)
69
+
70
+ this.fromObject({
71
+ bn,
72
+ compressed: true
73
+ })
74
+ return this
75
+ }
76
+
77
+ static fromRandom () {
78
+ return new this().fromRandom()
79
+ }
80
+
81
+ toBuffer () {
82
+ let compressed = this.compressed
83
+
84
+ if (compressed === undefined) {
85
+ compressed = true
86
+ }
87
+
88
+ const privBuf = this.bn.toBuffer({ size: 32 })
89
+ let buf
90
+ if (compressed) {
91
+ buf = Buffer.concat([
92
+ Buffer.from([this.Constants.versionByteNum]),
93
+ privBuf,
94
+ Buffer.from([0x01])
95
+ ])
96
+ } else {
97
+ buf = Buffer.concat([Buffer.from([this.Constants.versionByteNum]), privBuf])
98
+ }
99
+
100
+ return buf
101
+ }
102
+
103
+ fromBuffer (buf) {
104
+ if (buf.length === 1 + 32 + 1 &amp;&amp; buf[1 + 32 + 1 - 1] === 1) {
105
+ this.compressed = true
106
+ } else if (buf.length === 1 + 32) {
107
+ this.compressed = false
108
+ } else {
109
+ throw new Error(
110
+ 'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
111
+ )
112
+ }
113
+
114
+ if (buf[0] !== this.Constants.versionByteNum) {
115
+ throw new Error('Invalid versionByteNum byte')
116
+ }
117
+
118
+ return this.fromBn(new Bn().fromBuffer(buf.slice(1, 1 + 32)))
119
+ }
120
+
121
+ toBn () {
122
+ return this.bn
123
+ }
124
+
125
+ fromBn (bn) {
126
+ this.bn = bn
127
+ return this
128
+ }
129
+
130
+ static fromBn (bn) {
131
+ return new this().fromBn(bn)
132
+ }
133
+
134
+ validate () {
135
+ if (!this.bn.lt(Point.getN())) {
136
+ throw new Error('Number must be less than N')
137
+ }
138
+ if (typeof this.compressed !== 'boolean') {
139
+ throw new Error(
140
+ 'Must specify whether the corresponding public key is compressed or not (true or false)'
141
+ )
142
+ }
143
+ return this
144
+ }
145
+
146
+ /**
147
+ * Output the private key a Wallet Import Format (Wif) string.
148
+ */
149
+ toWif () {
150
+ return Base58Check.encode(this.toBuffer())
151
+ }
152
+
153
+ /**
154
+ * Input the private key from a Wallet Import Format (Wif) string.
155
+ */
156
+ fromWif (str) {
157
+ return this.fromBuffer(Base58Check.decode(str))
158
+ }
159
+
160
+ static fromWif (str) {
161
+ return new this().fromWif(str)
162
+ }
163
+
164
+ toString () {
165
+ return this.toWif()
166
+ }
167
+
168
+ fromString (str) {
169
+ return this.fromWif(str)
170
+ }
171
+ }
172
+
173
+ PrivKey.Mainnet = class extends PrivKey {
174
+ constructor (bn, compressed) {
175
+ super(bn, compressed, Constants.Mainnet.PrivKey)
176
+ }
177
+ }
178
+
179
+ PrivKey.Testnet = class extends PrivKey {
180
+ constructor (bn, compressed) {
181
+ super(bn, compressed, Constants.Testnet.PrivKey)
182
+ }
183
+ }
184
+
185
+ export { PrivKey }
186
+ </code></pre>
187
+ </article>
188
+ </section>
189
+
190
+
191
+
192
+
193
+ </div>
194
+
195
+ <nav>
196
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sig.html">Sig</a></li></ul><h3>Global</h3><ul><li><a href="global.html#decorate">decorate</a></li></ul>
197
+ </nav>
198
+
199
+ <br class="clear">
200
+
201
+ <footer>
202
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Feb 28 2025 03:51:24 GMT+0100 (hora estándar de Europa central)
203
+ </footer>
204
+
205
+ <script> prettyPrint(); </script>
206
+ <script src="scripts/linenumber.js"> </script>
207
+ </body>
208
+ </html>