bsv2 2.0.24 → 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.
- package/{.babelrc.test → .babel.test} +1 -1
- package/.husky/pre-commit +1 -1
- package/.mocharc.yml +1 -1
- package/.travis.yml +1 -1
- package/babel.config.cjs +44 -0
- package/benchmark.js +10 -18
- package/bin/bsv.js +1 -1
- package/bin/network.js +2 -1
- package/dist/bsv2.mjs +36061 -4
- package/dist/bsv2.umd.js +28 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +93 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +782 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9461 -0
- package/entry.js +2 -3
- package/eslint.config.mjs +8 -0
- package/index.js +1 -5
- package/{karma.conf.js → karma.conf.ts} +2 -3
- package/lib/ach.js +5 -5
- package/lib/address.js +11 -9
- package/lib/aescbc.js +3 -3
- package/lib/bip-39.js +12 -11
- package/lib/block.js +2 -2
- package/lib/bn.js +12 -17
- package/lib/bsm.js +1 -1
- package/lib/config.js +1 -1
- package/lib/constants.js +1 -1
- package/lib/ecdsa.js +24 -23
- package/lib/interp.js +18 -14
- package/lib/merkle.js +1 -1
- package/lib/op-code.js +137 -135
- package/lib/priv-key.js +1 -1
- package/lib/script.js +44 -42
- package/lib/sig.js +12 -12
- package/lib/struct.js +9 -6
- package/lib/tx.js +6 -6
- package/load-babel.js +3 -1
- package/mtest/test.html +6 -4
- package/mtest/test.js +1 -1
- package/out/Sig.html +541 -0
- package/out/bip-32.js.html +523 -0
- package/out/bip-39.js.html +336 -0
- package/out/block.js.html +183 -0
- package/out/bn.js.html +512 -0
- package/out/ecdsa.js.html +546 -0
- package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/out/fonts/OpenSans-Bold-webfont.svg +1830 -0
- package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/out/fonts/OpenSans-Italic-webfont.svg +1830 -0
- package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Light-webfont.eot +0 -0
- package/out/fonts/OpenSans-Light-webfont.svg +1831 -0
- package/out/fonts/OpenSans-Light-webfont.woff +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
- package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/out/fonts/OpenSans-Regular-webfont.svg +1831 -0
- package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/out/global.html +220 -0
- package/out/index.html +128 -0
- package/out/interp.js.html +2064 -0
- package/out/merkle.js.html +143 -0
- package/out/msg.js.html +235 -0
- package/out/priv-key.js.html +208 -0
- package/out/pub-key.js.html +281 -0
- package/out/script.js.html +876 -0
- package/out/scripts/linenumber.js +25 -0
- package/out/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/out/scripts/prettify/lang-css.js +2 -0
- package/out/scripts/prettify/prettify.js +28 -0
- package/out/sig-operations.js.html +171 -0
- package/out/sig.js.html +450 -0
- package/out/struct.js.html +440 -0
- package/out/styles/jsdoc-default.css +358 -0
- package/out/styles/prettify-jsdoc.css +111 -0
- package/out/styles/prettify-tomorrow.css +132 -0
- package/out/tx-builder.js.html +644 -0
- package/out/tx-in.js.html +223 -0
- package/out/tx-verifier.js.html +260 -0
- package/out/tx.js.html +481 -0
- package/package.json +34 -39
- package/vite.config.js +36 -0
- package/dist/bsv2.bundle.js +0 -8
- package/dist/bsv2.cjs.js +0 -2
- package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
- package/dist/bsv2.module.js +0 -2
- /package/{webpack.config.js → mtest/webpack.config.js} +0 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>JSDoc: Source: bip-39.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: bip-39.js</h1>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
<section>
|
|
28
|
+
<article>
|
|
29
|
+
<pre class="prettyprint source linenums"><code>/**
|
|
30
|
+
* Bip39: Mnemonic Seeds
|
|
31
|
+
* =====================
|
|
32
|
+
*
|
|
33
|
+
* Bip39 is a way to turn random entropy into a mnemonic (a string of words
|
|
34
|
+
* from a wordlist), and then that mnemonic into a seed. The seed can then be
|
|
35
|
+
* used in Bip32 to derive hierarchical deterministic keys. It does not go the
|
|
36
|
+
* other way around (i.e., you cannot turn a seed into a mnemonic). The usual
|
|
37
|
+
* way to use it is either to generate a new one, like this:
|
|
38
|
+
*
|
|
39
|
+
* const mnemonic = new Bip39().fromRandom().toString()
|
|
40
|
+
*
|
|
41
|
+
* or from a known mnemonic:
|
|
42
|
+
*
|
|
43
|
+
* const seed = new Bip39().fromString(mnemonic).toSeed()
|
|
44
|
+
*/
|
|
45
|
+
'use strict'
|
|
46
|
+
|
|
47
|
+
import { Bw } from './bw'
|
|
48
|
+
import { Hash } from './hash'
|
|
49
|
+
import pbkdf2 from 'pbkdf2'
|
|
50
|
+
import { Random } from './random'
|
|
51
|
+
import { Struct } from './struct'
|
|
52
|
+
import { en, es, jp } from './bip-39-words'
|
|
53
|
+
import { Workers } from './workers'
|
|
54
|
+
|
|
55
|
+
class Bip39 extends Struct {
|
|
56
|
+
constructor (mnemonic, seed, wordlist) {
|
|
57
|
+
super({ mnemonic, seed, wordlist })
|
|
58
|
+
this.Wordlist = en
|
|
59
|
+
this.es = es
|
|
60
|
+
this.jp = jp
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
setLang (lang) {
|
|
64
|
+
switch (lang) {
|
|
65
|
+
case 'es': this.Wordlist = es; break
|
|
66
|
+
case 'jp': this.Wordlist = jp; break
|
|
67
|
+
case 'en': default: this.Wordlist = en; break
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
toBw (bw) {
|
|
72
|
+
if (!bw) {
|
|
73
|
+
bw = new Bw()
|
|
74
|
+
}
|
|
75
|
+
if (this.mnemonic) {
|
|
76
|
+
const buf = Buffer.from(this.mnemonic)
|
|
77
|
+
bw.writeVarIntNum(buf.length)
|
|
78
|
+
bw.write(buf)
|
|
79
|
+
} else {
|
|
80
|
+
bw.writeVarIntNum(0)
|
|
81
|
+
}
|
|
82
|
+
if (this.seed) {
|
|
83
|
+
bw.writeVarIntNum(this.seed.length)
|
|
84
|
+
bw.write(this.seed)
|
|
85
|
+
} else {
|
|
86
|
+
bw.writeVarIntNum(0)
|
|
87
|
+
}
|
|
88
|
+
return bw
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fromBr (br) {
|
|
92
|
+
const mnemoniclen = br.readVarIntNum()
|
|
93
|
+
if (mnemoniclen > 0) {
|
|
94
|
+
this.mnemonic = br.read(mnemoniclen).toString()
|
|
95
|
+
}
|
|
96
|
+
const seedlen = br.readVarIntNum()
|
|
97
|
+
if (seedlen > 0) {
|
|
98
|
+
this.seed = br.read(seedlen)
|
|
99
|
+
}
|
|
100
|
+
return this
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Generate a random new mnemonic from the wordlist.
|
|
105
|
+
*/
|
|
106
|
+
fromRandom (bits) {
|
|
107
|
+
if (!bits) {
|
|
108
|
+
bits = 128
|
|
109
|
+
}
|
|
110
|
+
if (bits % 32 !== 0) {
|
|
111
|
+
throw new Error('bits must be multiple of 32')
|
|
112
|
+
}
|
|
113
|
+
if (bits < 128) {
|
|
114
|
+
throw new Error('bits must be at least 128')
|
|
115
|
+
}
|
|
116
|
+
const buf = Random.getRandomBuffer(bits / 8)
|
|
117
|
+
this.entropy2Mnemonic(buf)
|
|
118
|
+
this.mnemonic2Seed()
|
|
119
|
+
return this
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static fromRandom (bits) {
|
|
123
|
+
return new this().fromRandom(bits)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async asyncFromRandom (bits) {
|
|
127
|
+
if (!bits) {
|
|
128
|
+
bits = 128
|
|
129
|
+
}
|
|
130
|
+
const buf = Random.getRandomBuffer(bits / 8)
|
|
131
|
+
let workersResult = await Workers.asyncObjectMethod(
|
|
132
|
+
this,
|
|
133
|
+
'entropy2Mnemonic',
|
|
134
|
+
[buf]
|
|
135
|
+
)
|
|
136
|
+
const bip39 = new Bip39().fromFastBuffer(workersResult.resbuf)
|
|
137
|
+
workersResult = await Workers.asyncObjectMethod(
|
|
138
|
+
bip39,
|
|
139
|
+
'mnemonic2Seed',
|
|
140
|
+
[]
|
|
141
|
+
)
|
|
142
|
+
return this.fromFastBuffer(workersResult.resbuf)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static asyncFromRandom (bits) {
|
|
146
|
+
return new this().asyncFromRandom(bits)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
fromEntropy (buf) {
|
|
150
|
+
this.entropy2Mnemonic(buf)
|
|
151
|
+
return this
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static fromEntropy (buf) {
|
|
155
|
+
return new this().fromEntropy(buf)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async asyncFromEntropy (buf) {
|
|
159
|
+
const workersResult = await Workers.asyncObjectMethod(this, 'fromEntropy', [
|
|
160
|
+
buf
|
|
161
|
+
])
|
|
162
|
+
return this.fromFastBuffer(workersResult.resbuf)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static asyncFromEntropy (buf) {
|
|
166
|
+
return new this().asyncFromEntropy(buf)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fromString (mnemonic) {
|
|
170
|
+
this.mnemonic = mnemonic.trim()
|
|
171
|
+
return this
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
toString () {
|
|
175
|
+
return this.mnemonic
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
toSeed (passphrase) {
|
|
179
|
+
this.mnemonic2Seed(passphrase)
|
|
180
|
+
return this.seed
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async asyncToSeed (passphrase) {
|
|
184
|
+
if (passphrase === undefined) {
|
|
185
|
+
passphrase = ''
|
|
186
|
+
}
|
|
187
|
+
const args = [passphrase]
|
|
188
|
+
const workersResult = await Workers.asyncObjectMethod(this, 'toSeed', args)
|
|
189
|
+
return workersResult.resbuf
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Generate a new mnemonic from some entropy generated somewhere else. The
|
|
194
|
+
* entropy must be at least 128 bits.
|
|
195
|
+
*/
|
|
196
|
+
entropy2Mnemonic (buf) {
|
|
197
|
+
if (!Buffer.isBuffer(buf) || buf.length < 128 / 8) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
'Entropy is less than 128 bits. It must be 128 bits or more.'
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const hash = Hash.sha256(buf)
|
|
204
|
+
let bin = ''
|
|
205
|
+
const bits = buf.length * 8
|
|
206
|
+
for (let i = 0; i < buf.length; i++) {
|
|
207
|
+
bin = bin + ('00000000' + buf[i].toString(2)).slice(-8)
|
|
208
|
+
}
|
|
209
|
+
let hashbits = hash[0].toString(2)
|
|
210
|
+
hashbits = ('00000000' + hashbits).slice(-8).slice(0, bits / 32)
|
|
211
|
+
bin = bin + hashbits
|
|
212
|
+
|
|
213
|
+
if (bin.length % 11 !== 0) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
'internal error - entropy not an even multiple of 11 bits - ' +
|
|
216
|
+
bin.length
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
let mnemonic = ''
|
|
221
|
+
for (let i = 0; i < bin.length / 11; i++) {
|
|
222
|
+
if (mnemonic !== '') {
|
|
223
|
+
mnemonic = mnemonic + this.Wordlist.space
|
|
224
|
+
}
|
|
225
|
+
const wi = parseInt(bin.slice(i * 11, (i + 1) * 11), 2)
|
|
226
|
+
mnemonic = mnemonic + this.Wordlist[wi]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
this.mnemonic = mnemonic
|
|
230
|
+
return this
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Check that a mnemonic is valid. This means there should be no superfluous
|
|
235
|
+
* whitespace, no invalid words, and the checksum should match.
|
|
236
|
+
*/
|
|
237
|
+
check () {
|
|
238
|
+
const mnemonic = this.mnemonic
|
|
239
|
+
|
|
240
|
+
// confirm no invalid words
|
|
241
|
+
const words = mnemonic.split(this.Wordlist.space)
|
|
242
|
+
let bin = ''
|
|
243
|
+
for (let i = 0; i < words.length; i++) {
|
|
244
|
+
const ind = this.Wordlist.indexOf(words[i])
|
|
245
|
+
if (ind < 0) {
|
|
246
|
+
return false
|
|
247
|
+
}
|
|
248
|
+
bin = bin + ('00000000000' + ind.toString(2)).slice(-11)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (bin.length % 11 !== 0) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
'internal error - entropy not an even multiple of 11 bits - ' +
|
|
254
|
+
bin.length
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// confirm checksum
|
|
259
|
+
const cs = bin.length / 33
|
|
260
|
+
const hashBits = bin.slice(-cs)
|
|
261
|
+
const nonhashBits = bin.slice(0, bin.length - cs)
|
|
262
|
+
const buf = Buffer.alloc(nonhashBits.length / 8)
|
|
263
|
+
for (let i = 0; i < nonhashBits.length / 8; i++) {
|
|
264
|
+
buf.writeUInt8(parseInt(bin.slice(i * 8, (i + 1) * 8), 2), i)
|
|
265
|
+
}
|
|
266
|
+
const hash = Hash.sha256(buf)
|
|
267
|
+
let expectedHashBits = hash[0].toString(2)
|
|
268
|
+
expectedHashBits = ('00000000' + expectedHashBits).slice(-8).slice(0, cs)
|
|
269
|
+
|
|
270
|
+
return expectedHashBits === hashBits
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Convert a mnemonic to a seed. Does not check for validity of the mnemonic -
|
|
275
|
+
* for that, you should manually run check() first.
|
|
276
|
+
*/
|
|
277
|
+
mnemonic2Seed (passphrase = '') {
|
|
278
|
+
let mnemonic = this.mnemonic
|
|
279
|
+
if (!this.check()) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
'Mnemonic does not pass the check - was the mnemonic typed incorrectly? Are there extra spaces?'
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
if (typeof passphrase !== 'string') {
|
|
285
|
+
throw new Error('passphrase must be a string or undefined')
|
|
286
|
+
}
|
|
287
|
+
mnemonic = mnemonic.normalize('NFKD')
|
|
288
|
+
passphrase = passphrase.normalize('NFKD')
|
|
289
|
+
const mbuf = Buffer.from(mnemonic)
|
|
290
|
+
const pbuf = Buffer.concat([
|
|
291
|
+
Buffer.from('mnemonic'),
|
|
292
|
+
Buffer.from(passphrase)
|
|
293
|
+
])
|
|
294
|
+
this.seed = pbkdf2.pbkdf2Sync(mbuf, pbuf, 2048, 64, 'sha512')
|
|
295
|
+
return this
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
isValid (passphrase = '') {
|
|
299
|
+
let isValid
|
|
300
|
+
try {
|
|
301
|
+
isValid = !!this.mnemonic2Seed(passphrase)
|
|
302
|
+
} catch (err) {
|
|
303
|
+
isValid = false
|
|
304
|
+
}
|
|
305
|
+
return isValid
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static isValid (mnemonic, passphrase = '') {
|
|
309
|
+
return new Bip39(mnemonic).isValid(passphrase)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { Bip39 }
|
|
314
|
+
</code></pre>
|
|
315
|
+
</article>
|
|
316
|
+
</section>
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
</div>
|
|
322
|
+
|
|
323
|
+
<nav>
|
|
324
|
+
<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>
|
|
325
|
+
</nav>
|
|
326
|
+
|
|
327
|
+
<br class="clear">
|
|
328
|
+
|
|
329
|
+
<footer>
|
|
330
|
+
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)
|
|
331
|
+
</footer>
|
|
332
|
+
|
|
333
|
+
<script> prettyPrint(); </script>
|
|
334
|
+
<script src="scripts/linenumber.js"> </script>
|
|
335
|
+
</body>
|
|
336
|
+
</html>
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>JSDoc: Source: block.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: block.js</h1>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
<section>
|
|
28
|
+
<article>
|
|
29
|
+
<pre class="prettyprint source linenums"><code>/**
|
|
30
|
+
* Block
|
|
31
|
+
* =====
|
|
32
|
+
*
|
|
33
|
+
* A block, of course, is a collection of transactions. This class is somewhat
|
|
34
|
+
* incompconste at the moment. In the future, it should support the ability to
|
|
35
|
+
* check to see if a transaction is in a block (thanks to the magic of merkle
|
|
36
|
+
* trees). You will probably never use Yours Bitcoin to create a block, since almost
|
|
37
|
+
* everyone will use bitcoind for that. As such, the primary way to use this is
|
|
38
|
+
* new Block().fromBuffer(buf), which will parse the block and prepare its insides
|
|
39
|
+
* for you to inspect.
|
|
40
|
+
*/
|
|
41
|
+
'use strict'
|
|
42
|
+
|
|
43
|
+
import { Br } from './br'
|
|
44
|
+
import { Bw } from './bw'
|
|
45
|
+
import { BlockHeader } from './block-header'
|
|
46
|
+
import { Hash } from './hash'
|
|
47
|
+
import { Merkle } from './merkle'
|
|
48
|
+
import { Struct } from './struct'
|
|
49
|
+
import { Tx } from './tx'
|
|
50
|
+
import { VarInt } from './var-int'
|
|
51
|
+
import { Workers } from './workers'
|
|
52
|
+
|
|
53
|
+
class Block extends Struct {
|
|
54
|
+
constructor (blockHeader, txsVi, txs) {
|
|
55
|
+
super({ blockHeader, txsVi, txs })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fromJSON (json) {
|
|
59
|
+
const txs = []
|
|
60
|
+
json.txs.forEach(function (tx) {
|
|
61
|
+
txs.push(new Tx().fromJSON(tx))
|
|
62
|
+
})
|
|
63
|
+
this.fromObject({
|
|
64
|
+
blockHeader: new BlockHeader().fromJSON(json.blockHeader),
|
|
65
|
+
txsVi: new VarInt().fromJSON(json.txsVi),
|
|
66
|
+
txs
|
|
67
|
+
})
|
|
68
|
+
return this
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
toJSON () {
|
|
72
|
+
const txs = []
|
|
73
|
+
this.txs.forEach(function (tx) {
|
|
74
|
+
txs.push(tx.toJSON())
|
|
75
|
+
})
|
|
76
|
+
return {
|
|
77
|
+
blockHeader: this.blockHeader.toJSON(),
|
|
78
|
+
txsVi: this.txsVi.toJSON(),
|
|
79
|
+
txs
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fromBr (br) {
|
|
84
|
+
this.blockHeader = new BlockHeader().fromBr(br)
|
|
85
|
+
this.txsVi = new VarInt(br.readVarIntBuf())
|
|
86
|
+
const txsNum = this.txsVi.toNumber()
|
|
87
|
+
this.txs = []
|
|
88
|
+
for (let i = 0; i < txsNum; i++) {
|
|
89
|
+
this.txs.push(new Tx().fromBr(br))
|
|
90
|
+
}
|
|
91
|
+
return this
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
toBw (bw) {
|
|
95
|
+
if (!bw) {
|
|
96
|
+
bw = new Bw()
|
|
97
|
+
}
|
|
98
|
+
bw.write(this.blockHeader.toBuffer())
|
|
99
|
+
bw.write(this.txsVi.buf)
|
|
100
|
+
const txsNum = this.txsVi.toNumber()
|
|
101
|
+
for (let i = 0; i < txsNum; i++) {
|
|
102
|
+
this.txs[i].toBw(bw)
|
|
103
|
+
}
|
|
104
|
+
return bw
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
hash () {
|
|
108
|
+
return Hash.sha256Sha256(this.blockHeader.toBuffer())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async asyncHash () {
|
|
112
|
+
const workersResult = await Workers.asyncObjectMethod(this, 'hash', [])
|
|
113
|
+
return workersResult.resbuf
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
id () {
|
|
117
|
+
return new Br(this.hash()).readReverse().toString('hex')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async asyncId () {
|
|
121
|
+
const workersResult = await Workers.asyncObjectMethod(this, 'id', [])
|
|
122
|
+
return JSON.parse(workersResult.resbuf.toString())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
verifyMerkleRoot () {
|
|
126
|
+
const txsbufs = this.txs.map(tx => tx.toBuffer())
|
|
127
|
+
const merkleRootBuf = Merkle.fromBuffers(txsbufs).hash()
|
|
128
|
+
return Buffer.compare(merkleRootBuf, this.blockHeader.merkleRootBuf)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Sometimes we don't want to parse an entire block into memory. Instead, we
|
|
133
|
+
* simply want to iterate through all transactions in the block. That is what
|
|
134
|
+
* this method is for. This method returns an efficient iterator which can be
|
|
135
|
+
* used in a `for (tx of txs)` construct that returns each tx one at a time
|
|
136
|
+
* without first parsing all of them into memory.
|
|
137
|
+
*
|
|
138
|
+
* @param {Buffer} blockBuf A buffer of a block.
|
|
139
|
+
*/
|
|
140
|
+
static iterateTxs (blockBuf) {
|
|
141
|
+
const br = new Br(blockBuf)
|
|
142
|
+
const blockHeader = new BlockHeader().fromBr(br)
|
|
143
|
+
const txsVi = new VarInt(br.readVarIntBuf())
|
|
144
|
+
const txsNum = txsVi.toNumber()
|
|
145
|
+
return {
|
|
146
|
+
blockHeader,
|
|
147
|
+
txsVi,
|
|
148
|
+
txsNum,
|
|
149
|
+
* [Symbol.iterator] () {
|
|
150
|
+
for (let i = 0; i < txsNum; i++) {
|
|
151
|
+
yield new Tx().fromBr(br)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
Block.MAX_BLOCK_SIZE = 1000000
|
|
159
|
+
|
|
160
|
+
export { Block }
|
|
161
|
+
</code></pre>
|
|
162
|
+
</article>
|
|
163
|
+
</section>
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
</div>
|
|
169
|
+
|
|
170
|
+
<nav>
|
|
171
|
+
<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>
|
|
172
|
+
</nav>
|
|
173
|
+
|
|
174
|
+
<br class="clear">
|
|
175
|
+
|
|
176
|
+
<footer>
|
|
177
|
+
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)
|
|
178
|
+
</footer>
|
|
179
|
+
|
|
180
|
+
<script> prettyPrint(); </script>
|
|
181
|
+
<script src="scripts/linenumber.js"> </script>
|
|
182
|
+
</body>
|
|
183
|
+
</html>
|