@bsv/sdk 1.0.12 → 1.0.14
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/dist/cjs/package.json +1 -1
- package/dist/cjs/src/compat/BSM.js.map +1 -1
- package/dist/cjs/src/compat/ECIES.js +105 -76
- package/dist/cjs/src/compat/ECIES.js.map +1 -1
- package/dist/cjs/src/compat/HD.js +65 -65
- package/dist/cjs/src/compat/HD.js.map +1 -1
- package/dist/cjs/src/compat/Mnemonic.js +79 -79
- package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
- package/dist/cjs/src/compat/bip-39-wordlist-en.js +2 -2
- package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -1
- package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
- package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
- package/dist/cjs/src/primitives/DRBG.js.map +1 -1
- package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
- package/dist/cjs/src/primitives/Hash.js +26 -13
- package/dist/cjs/src/primitives/Hash.js.map +1 -1
- package/dist/cjs/src/primitives/PrivateKey.js +3 -2
- package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
- package/dist/cjs/src/primitives/PublicKey.js +1 -2
- package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
- package/dist/cjs/src/primitives/Random.js +2 -2
- package/dist/cjs/src/primitives/Random.js.map +1 -1
- package/dist/cjs/src/primitives/Signature.js +141 -4
- package/dist/cjs/src/primitives/Signature.js.map +1 -1
- package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
- package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
- package/dist/cjs/src/primitives/utils.js +14 -9
- package/dist/cjs/src/primitives/utils.js.map +1 -1
- package/dist/cjs/src/script/Spend.js.map +1 -1
- package/dist/cjs/src/script/templates/P2PKH.js +1 -1
- package/dist/cjs/src/script/templates/P2PKH.js.map +1 -1
- package/dist/cjs/src/transaction/MerklePath.js +3 -3
- package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js +15 -4
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/compat/BSM.js.map +1 -1
- package/dist/esm/src/compat/ECIES.js +105 -76
- package/dist/esm/src/compat/ECIES.js.map +1 -1
- package/dist/esm/src/compat/HD.js +65 -65
- package/dist/esm/src/compat/HD.js.map +1 -1
- package/dist/esm/src/compat/Mnemonic.js +79 -79
- package/dist/esm/src/compat/Mnemonic.js.map +1 -1
- package/dist/esm/src/compat/bip-39-wordlist-en.js +2 -2
- package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -1
- package/dist/esm/src/primitives/AESGCM.js.map +1 -1
- package/dist/esm/src/primitives/BigNumber.js.map +1 -1
- package/dist/esm/src/primitives/DRBG.js.map +1 -1
- package/dist/esm/src/primitives/ECDSA.js.map +1 -1
- package/dist/esm/src/primitives/Hash.js +26 -13
- package/dist/esm/src/primitives/Hash.js.map +1 -1
- package/dist/esm/src/primitives/PrivateKey.js +3 -2
- package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
- package/dist/esm/src/primitives/PublicKey.js +1 -2
- package/dist/esm/src/primitives/PublicKey.js.map +1 -1
- package/dist/esm/src/primitives/Random.js +2 -2
- package/dist/esm/src/primitives/Random.js.map +1 -1
- package/dist/esm/src/primitives/Signature.js +141 -4
- package/dist/esm/src/primitives/Signature.js.map +1 -1
- package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
- package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
- package/dist/esm/src/primitives/utils.js +14 -9
- package/dist/esm/src/primitives/utils.js.map +1 -1
- package/dist/esm/src/script/Spend.js.map +1 -1
- package/dist/esm/src/script/templates/P2PKH.js +1 -1
- package/dist/esm/src/script/templates/P2PKH.js.map +1 -1
- package/dist/esm/src/transaction/MerklePath.js +3 -3
- package/dist/esm/src/transaction/MerklePath.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js +16 -4
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/compat/ECIES.d.ts +36 -36
- package/dist/types/src/compat/ECIES.d.ts.map +1 -1
- package/dist/types/src/compat/HD.d.ts +65 -65
- package/dist/types/src/compat/HD.d.ts.map +1 -1
- package/dist/types/src/compat/Mnemonic.d.ts +79 -79
- package/dist/types/src/compat/Mnemonic.d.ts.map +1 -1
- package/dist/types/src/primitives/AESGCM.d.ts.map +1 -1
- package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
- package/dist/types/src/primitives/Hash.d.ts.map +1 -1
- package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -1
- package/dist/types/src/primitives/PublicKey.d.ts.map +1 -1
- package/dist/types/src/primitives/Signature.d.ts +62 -0
- package/dist/types/src/primitives/Signature.d.ts.map +1 -1
- package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
- package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -1
- package/dist/types/src/primitives/utils.d.ts.map +1 -1
- package/dist/types/src/script/Spend.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts +1 -0
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/docs/compat.md +4 -4
- package/docs/primitives.md +288 -79
- package/package.json +1 -1
- package/src/compat/BSM.ts +12 -12
- package/src/compat/ECIES.ts +417 -418
- package/src/compat/HD.ts +228 -228
- package/src/compat/Mnemonic.ts +173 -173
- package/src/compat/__tests/BSM.test.ts +13 -2
- package/src/compat/bip-39-wordlist-en.ts +2052 -2052
- package/src/primitives/AESGCM.ts +30 -30
- package/src/primitives/BigNumber.ts +0 -1
- package/src/primitives/DRBG.ts +5 -5
- package/src/primitives/ECDSA.ts +1 -1
- package/src/primitives/Hash.ts +278 -293
- package/src/primitives/PrivateKey.ts +18 -19
- package/src/primitives/PublicKey.ts +9 -10
- package/src/primitives/Random.ts +4 -4
- package/src/primitives/Signature.ts +158 -14
- package/src/primitives/SymmetricKey.ts +3 -3
- package/src/primitives/TransactionSignature.ts +9 -9
- package/src/primitives/index.ts +1 -1
- package/src/primitives/utils.ts +60 -64
- package/src/script/Spend.ts +12 -12
- package/src/script/index.ts +1 -1
- package/src/script/templates/P2PKH.ts +1 -1
- package/src/transaction/MerklePath.ts +3 -3
- package/src/transaction/Transaction.ts +36 -26
- package/src/transaction/__tests/Transaction.test.ts +5 -5
package/src/primitives/utils.ts
CHANGED
|
@@ -167,7 +167,7 @@ export const encode = (arr: number[], enc?: 'hex' | 'utf8'): string | number[] =
|
|
|
167
167
|
* const bytes = [72, 101, 108, 108, 111]; // Represents the string "Hello"
|
|
168
168
|
* console.log(toBase64(bytes)); // Outputs: SGVsbG8=
|
|
169
169
|
*/
|
|
170
|
-
export function toBase64(byteArray: number[]): string {
|
|
170
|
+
export function toBase64 (byteArray: number[]): string {
|
|
171
171
|
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
172
172
|
let result = ''
|
|
173
173
|
let i: number
|
|
@@ -198,12 +198,12 @@ const base58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
|
198
198
|
* @returns The binary representation
|
|
199
199
|
*/
|
|
200
200
|
export const fromBase58 = (str: string): number[] => {
|
|
201
|
-
if (!str || typeof str !==
|
|
202
|
-
|
|
203
|
-
if (str.match(/[IOl0]/gmu))
|
|
201
|
+
if (!str || typeof str !== 'string') { throw new Error(`Expected base58 string but got “${str}”`) }
|
|
202
|
+
if (str.match(/[IOl0]/gmu)) {
|
|
204
203
|
throw new Error(
|
|
205
204
|
`Invalid base58 character “${str.match(/[IOl0]/gmu)}”`
|
|
206
205
|
)
|
|
206
|
+
}
|
|
207
207
|
const lz = str.match(/^1+/gmu)
|
|
208
208
|
const psz: number = lz ? lz[0].length : 0
|
|
209
209
|
const size =
|
|
@@ -216,17 +216,17 @@ export const fromBase58 = (str: string): number[] => {
|
|
|
216
216
|
.map((i) => base58chars.indexOf(i))
|
|
217
217
|
.reduce((acc, i) => {
|
|
218
218
|
acc = acc.map((j) => {
|
|
219
|
-
const x = j * 58 + i
|
|
220
|
-
i = x >> 8
|
|
221
|
-
return x
|
|
222
|
-
})
|
|
223
|
-
return acc
|
|
219
|
+
const x = j * 58 + i
|
|
220
|
+
i = x >> 8
|
|
221
|
+
return x
|
|
222
|
+
})
|
|
223
|
+
return acc
|
|
224
224
|
}, new Uint8Array(size))
|
|
225
225
|
.reverse()
|
|
226
226
|
.filter(
|
|
227
227
|
(
|
|
228
228
|
(lastValue) => (value) =>
|
|
229
|
-
// @ts-
|
|
229
|
+
// @ts-expect-error
|
|
230
230
|
(lastValue = lastValue || value)
|
|
231
231
|
)(false)
|
|
232
232
|
)
|
|
@@ -240,16 +240,14 @@ export const fromBase58 = (str: string): number[] => {
|
|
|
240
240
|
* @returns The base58 string representation
|
|
241
241
|
*/
|
|
242
242
|
export const toBase58 = (bin: number[]): string => {
|
|
243
|
-
const base58Map = Array(256).fill(-1)
|
|
244
|
-
for (let i = 0; i < base58chars.length; ++i)
|
|
245
|
-
base58Map[base58chars.charCodeAt(i)] = i;
|
|
243
|
+
const base58Map = Array(256).fill(-1)
|
|
244
|
+
for (let i = 0; i < base58chars.length; ++i) { base58Map[base58chars.charCodeAt(i)] = i }
|
|
246
245
|
|
|
247
246
|
const result = []
|
|
248
247
|
|
|
249
248
|
for (const byte of bin) {
|
|
250
249
|
let carry = byte
|
|
251
250
|
for (let j = 0; j < result.length; ++j) {
|
|
252
|
-
// @ts-ignore
|
|
253
251
|
const x = (base58Map[result[j]] << 8) + carry
|
|
254
252
|
result[j] = base58chars.charCodeAt(x % 58)
|
|
255
253
|
carry = (x / 58) | 0
|
|
@@ -260,9 +258,10 @@ export const toBase58 = (bin: number[]): string => {
|
|
|
260
258
|
}
|
|
261
259
|
}
|
|
262
260
|
|
|
263
|
-
for (const byte of bin)
|
|
261
|
+
for (const byte of bin) {
|
|
264
262
|
if (byte) break
|
|
265
|
-
else result.push(
|
|
263
|
+
else result.push('1'.charCodeAt(0))
|
|
264
|
+
}
|
|
266
265
|
|
|
267
266
|
result.reverse()
|
|
268
267
|
|
|
@@ -275,7 +274,7 @@ export const toBase58 = (bin: number[]): string => {
|
|
|
275
274
|
* @returns The base58check string representation
|
|
276
275
|
*/
|
|
277
276
|
export const toBase58Check = (bin: number[], prefix: number[] = [0]) => {
|
|
278
|
-
let hash = hash256([...prefix, ...bin])
|
|
277
|
+
let hash = hash256([...prefix, ...bin])
|
|
279
278
|
hash = [...prefix, ...bin, ...hash.slice(0, 4)]
|
|
280
279
|
return toBase58(hash)
|
|
281
280
|
}
|
|
@@ -292,7 +291,7 @@ export const fromBase58Check = (str: string, enc?: 'hex', prefixLength: number =
|
|
|
292
291
|
let prefix: string | number[] = bin.slice(0, prefixLength)
|
|
293
292
|
let data: string | number[] = bin.slice(prefixLength, -4)
|
|
294
293
|
let hash = [...prefix, ...data]
|
|
295
|
-
hash = hash256(hash)
|
|
294
|
+
hash = hash256(hash)
|
|
296
295
|
bin.slice(-4).forEach((check, index) => {
|
|
297
296
|
if (check !== hash[index]) {
|
|
298
297
|
throw new Error('Invalid checksum')
|
|
@@ -308,11 +307,11 @@ export const fromBase58Check = (str: string, enc?: 'hex', prefixLength: number =
|
|
|
308
307
|
export class Writer {
|
|
309
308
|
public bufs: number[][]
|
|
310
309
|
|
|
311
|
-
constructor(bufs?: number[][]) {
|
|
310
|
+
constructor (bufs?: number[][]) {
|
|
312
311
|
this.bufs = bufs || []
|
|
313
312
|
}
|
|
314
313
|
|
|
315
|
-
getLength(): number {
|
|
314
|
+
getLength (): number {
|
|
316
315
|
let len = 0
|
|
317
316
|
for (const buf of this.bufs) {
|
|
318
317
|
len = len + buf.length
|
|
@@ -320,23 +319,20 @@ export class Writer {
|
|
|
320
319
|
return len
|
|
321
320
|
}
|
|
322
321
|
|
|
323
|
-
toArray(): number[] {
|
|
322
|
+
toArray (): number[] {
|
|
324
323
|
let ret = []
|
|
325
324
|
for (const x of this.bufs) {
|
|
326
|
-
if (x.length < 65536)
|
|
327
|
-
ret.push(...x)
|
|
328
|
-
else
|
|
329
|
-
ret = ret.concat(x)
|
|
325
|
+
if (x.length < 65536) { ret.push(...x) } else { ret = ret.concat(x) }
|
|
330
326
|
}
|
|
331
327
|
return ret
|
|
332
328
|
}
|
|
333
329
|
|
|
334
|
-
write(buf: number[]): Writer {
|
|
330
|
+
write (buf: number[]): Writer {
|
|
335
331
|
this.bufs.push(buf)
|
|
336
332
|
return this
|
|
337
333
|
}
|
|
338
334
|
|
|
339
|
-
writeReverse(buf: number[]): Writer {
|
|
335
|
+
writeReverse (buf: number[]): Writer {
|
|
340
336
|
const buf2: number[] = new Array(buf.length)
|
|
341
337
|
for (let i = 0; i < buf2.length; i++) {
|
|
342
338
|
buf2[i] = buf[buf.length - 1 - i]
|
|
@@ -345,21 +341,21 @@ export class Writer {
|
|
|
345
341
|
return this
|
|
346
342
|
}
|
|
347
343
|
|
|
348
|
-
writeUInt8(n: number): Writer {
|
|
344
|
+
writeUInt8 (n: number): Writer {
|
|
349
345
|
const buf = new Array(1)
|
|
350
346
|
buf[0] = n
|
|
351
347
|
this.write(buf)
|
|
352
348
|
return this
|
|
353
349
|
}
|
|
354
350
|
|
|
355
|
-
writeInt8(n: number): Writer {
|
|
351
|
+
writeInt8 (n: number): Writer {
|
|
356
352
|
const buf = new Array(1)
|
|
357
353
|
buf[0] = n & 0xFF
|
|
358
354
|
this.write(buf)
|
|
359
355
|
return this
|
|
360
356
|
}
|
|
361
357
|
|
|
362
|
-
writeUInt16BE(n: number): Writer {
|
|
358
|
+
writeUInt16BE (n: number): Writer {
|
|
363
359
|
this.bufs.push([
|
|
364
360
|
(n >> 8) & 0xFF, // shift right 8 bits to get the high byte
|
|
365
361
|
n & 0xFF // low byte is just the last 8 bits
|
|
@@ -367,11 +363,11 @@ export class Writer {
|
|
|
367
363
|
return this
|
|
368
364
|
}
|
|
369
365
|
|
|
370
|
-
writeInt16BE(n: number): Writer {
|
|
366
|
+
writeInt16BE (n: number): Writer {
|
|
371
367
|
return this.writeUInt16BE(n & 0xFFFF) // Mask with 0xFFFF to get the lower 16 bits
|
|
372
368
|
}
|
|
373
369
|
|
|
374
|
-
writeUInt16LE(n: number): Writer {
|
|
370
|
+
writeUInt16LE (n: number): Writer {
|
|
375
371
|
this.bufs.push([
|
|
376
372
|
n & 0xFF, // low byte is just the last 8 bits
|
|
377
373
|
(n >> 8) & 0xFF // shift right 8 bits to get the high byte
|
|
@@ -379,11 +375,11 @@ export class Writer {
|
|
|
379
375
|
return this
|
|
380
376
|
}
|
|
381
377
|
|
|
382
|
-
writeInt16LE(n: number): Writer {
|
|
378
|
+
writeInt16LE (n: number): Writer {
|
|
383
379
|
return this.writeUInt16LE(n & 0xFFFF) // Mask with 0xFFFF to get the lower 16 bits
|
|
384
380
|
}
|
|
385
381
|
|
|
386
|
-
writeUInt32BE(n: number): Writer {
|
|
382
|
+
writeUInt32BE (n: number): Writer {
|
|
387
383
|
this.bufs.push([
|
|
388
384
|
(n >> 24) & 0xFF, // highest byte
|
|
389
385
|
(n >> 16) & 0xFF,
|
|
@@ -393,11 +389,11 @@ export class Writer {
|
|
|
393
389
|
return this
|
|
394
390
|
}
|
|
395
391
|
|
|
396
|
-
writeInt32BE(n: number): Writer {
|
|
392
|
+
writeInt32BE (n: number): Writer {
|
|
397
393
|
return this.writeUInt32BE(n >>> 0) // Using unsigned right shift to handle negative numbers
|
|
398
394
|
}
|
|
399
395
|
|
|
400
|
-
writeUInt32LE(n: number): Writer {
|
|
396
|
+
writeUInt32LE (n: number): Writer {
|
|
401
397
|
this.bufs.push([
|
|
402
398
|
n & 0xFF, // lowest byte
|
|
403
399
|
(n >> 8) & 0xFF,
|
|
@@ -407,41 +403,41 @@ export class Writer {
|
|
|
407
403
|
return this
|
|
408
404
|
}
|
|
409
405
|
|
|
410
|
-
writeInt32LE(n: number): Writer {
|
|
406
|
+
writeInt32LE (n: number): Writer {
|
|
411
407
|
return this.writeUInt32LE(n >>> 0) // Using unsigned right shift to handle negative numbers
|
|
412
408
|
}
|
|
413
409
|
|
|
414
|
-
writeUInt64BEBn(bn: BigNumber): Writer {
|
|
410
|
+
writeUInt64BEBn (bn: BigNumber): Writer {
|
|
415
411
|
const buf = bn.toArray('be', 8)
|
|
416
412
|
this.write(buf)
|
|
417
413
|
return this
|
|
418
414
|
}
|
|
419
415
|
|
|
420
|
-
writeUInt64LEBn(bn: BigNumber): Writer {
|
|
416
|
+
writeUInt64LEBn (bn: BigNumber): Writer {
|
|
421
417
|
const buf = bn.toArray('be', 8)
|
|
422
418
|
this.writeReverse(buf)
|
|
423
419
|
return this
|
|
424
420
|
}
|
|
425
421
|
|
|
426
|
-
writeUInt64LE(n: number): Writer {
|
|
422
|
+
writeUInt64LE (n: number): Writer {
|
|
427
423
|
const buf = new BigNumber(n).toArray('be', 8)
|
|
428
424
|
this.writeReverse(buf)
|
|
429
425
|
return this
|
|
430
426
|
}
|
|
431
427
|
|
|
432
|
-
writeVarIntNum(n: number): Writer {
|
|
428
|
+
writeVarIntNum (n: number): Writer {
|
|
433
429
|
const buf = Writer.varIntNum(n)
|
|
434
430
|
this.write(buf)
|
|
435
431
|
return this
|
|
436
432
|
}
|
|
437
433
|
|
|
438
|
-
writeVarIntBn(bn: BigNumber): Writer {
|
|
434
|
+
writeVarIntBn (bn: BigNumber): Writer {
|
|
439
435
|
const buf = Writer.varIntBn(bn)
|
|
440
436
|
this.write(buf)
|
|
441
437
|
return this
|
|
442
438
|
}
|
|
443
439
|
|
|
444
|
-
static varIntNum(n: number): number[] {
|
|
440
|
+
static varIntNum (n: number): number[] {
|
|
445
441
|
let buf: number[]
|
|
446
442
|
if (n < 253) {
|
|
447
443
|
buf = [n] // 1 byte
|
|
@@ -481,7 +477,7 @@ export class Writer {
|
|
|
481
477
|
return buf
|
|
482
478
|
}
|
|
483
479
|
|
|
484
|
-
static varIntBn(bn: BigNumber): number[] {
|
|
480
|
+
static varIntBn (bn: BigNumber): number[] {
|
|
485
481
|
let buf: number[]
|
|
486
482
|
if (bn.ltn(253)) {
|
|
487
483
|
const n = bn.toNumber()
|
|
@@ -509,22 +505,22 @@ export class Reader {
|
|
|
509
505
|
public bin: number[]
|
|
510
506
|
public pos: number
|
|
511
507
|
|
|
512
|
-
constructor(bin: number[] = [], pos: number = 0) {
|
|
508
|
+
constructor (bin: number[] = [], pos: number = 0) {
|
|
513
509
|
this.bin = bin
|
|
514
510
|
this.pos = pos
|
|
515
511
|
}
|
|
516
512
|
|
|
517
|
-
public eof(): boolean {
|
|
513
|
+
public eof (): boolean {
|
|
518
514
|
return this.pos >= this.bin.length
|
|
519
515
|
}
|
|
520
516
|
|
|
521
|
-
public read(len = this.bin.length): number[] {
|
|
517
|
+
public read (len = this.bin.length): number[] {
|
|
522
518
|
const bin = this.bin.slice(this.pos, this.pos + len)
|
|
523
519
|
this.pos = this.pos + len
|
|
524
520
|
return bin
|
|
525
521
|
}
|
|
526
522
|
|
|
527
|
-
public readReverse(len = this.bin.length): number[] {
|
|
523
|
+
public readReverse (len = this.bin.length): number[] {
|
|
528
524
|
const bin = this.bin.slice(this.pos, this.pos + len)
|
|
529
525
|
this.pos = this.pos + len
|
|
530
526
|
const buf2 = new Array(bin.length)
|
|
@@ -534,45 +530,45 @@ export class Reader {
|
|
|
534
530
|
return buf2
|
|
535
531
|
}
|
|
536
532
|
|
|
537
|
-
public readUInt8(): number {
|
|
533
|
+
public readUInt8 (): number {
|
|
538
534
|
const val = this.bin[this.pos]
|
|
539
535
|
this.pos += 1
|
|
540
536
|
return val
|
|
541
537
|
}
|
|
542
538
|
|
|
543
|
-
public readInt8(): number {
|
|
539
|
+
public readInt8 (): number {
|
|
544
540
|
const val = this.bin[this.pos]
|
|
545
541
|
this.pos += 1
|
|
546
542
|
// If the sign bit is set, convert to negative value
|
|
547
543
|
return (val & 0x80) !== 0 ? val - 0x100 : val
|
|
548
544
|
}
|
|
549
545
|
|
|
550
|
-
public readUInt16BE(): number {
|
|
546
|
+
public readUInt16BE (): number {
|
|
551
547
|
const val = (this.bin[this.pos] << 8) | this.bin[this.pos + 1]
|
|
552
548
|
this.pos += 2
|
|
553
549
|
return val
|
|
554
550
|
}
|
|
555
551
|
|
|
556
|
-
public readInt16BE(): number {
|
|
552
|
+
public readInt16BE (): number {
|
|
557
553
|
const val = this.readUInt16BE()
|
|
558
554
|
// If the sign bit is set, convert to negative value
|
|
559
555
|
return (val & 0x8000) !== 0 ? val - 0x10000 : val
|
|
560
556
|
}
|
|
561
557
|
|
|
562
|
-
public readUInt16LE(): number {
|
|
558
|
+
public readUInt16LE (): number {
|
|
563
559
|
const val = this.bin[this.pos] | (this.bin[this.pos + 1] << 8)
|
|
564
560
|
this.pos += 2
|
|
565
561
|
return val
|
|
566
562
|
}
|
|
567
563
|
|
|
568
|
-
public readInt16LE(): number {
|
|
564
|
+
public readInt16LE (): number {
|
|
569
565
|
const val = this.readUInt16LE()
|
|
570
566
|
// If the sign bit is set, convert to negative value
|
|
571
567
|
const x = (val & 0x8000) !== 0 ? val - 0x10000 : val
|
|
572
568
|
return x
|
|
573
569
|
}
|
|
574
570
|
|
|
575
|
-
public readUInt32BE(): number {
|
|
571
|
+
public readUInt32BE (): number {
|
|
576
572
|
const val =
|
|
577
573
|
(this.bin[this.pos] * 0x1000000) + // Shift the first byte by 24 bits
|
|
578
574
|
((this.bin[this.pos + 1] << 16) | // Shift the second byte by 16 bits
|
|
@@ -582,13 +578,13 @@ export class Reader {
|
|
|
582
578
|
return val
|
|
583
579
|
}
|
|
584
580
|
|
|
585
|
-
public readInt32BE(): number {
|
|
581
|
+
public readInt32BE (): number {
|
|
586
582
|
const val = this.readUInt32BE()
|
|
587
583
|
// If the sign bit is set, convert to negative value
|
|
588
584
|
return (val & 0x80000000) !== 0 ? val - 0x100000000 : val
|
|
589
585
|
}
|
|
590
586
|
|
|
591
|
-
public readUInt32LE(): number {
|
|
587
|
+
public readUInt32LE (): number {
|
|
592
588
|
const val =
|
|
593
589
|
(this.bin[this.pos] |
|
|
594
590
|
(this.bin[this.pos + 1] << 8) |
|
|
@@ -598,26 +594,26 @@ export class Reader {
|
|
|
598
594
|
return val
|
|
599
595
|
}
|
|
600
596
|
|
|
601
|
-
public readInt32LE(): number {
|
|
597
|
+
public readInt32LE (): number {
|
|
602
598
|
const val = this.readUInt32LE()
|
|
603
599
|
// Explicitly check if the sign bit is set and then convert to a negative value
|
|
604
600
|
return (val & 0x80000000) !== 0 ? val - 0x100000000 : val
|
|
605
601
|
}
|
|
606
602
|
|
|
607
|
-
public readUInt64BEBn(): BigNumber {
|
|
603
|
+
public readUInt64BEBn (): BigNumber {
|
|
608
604
|
const bin = this.bin.slice(this.pos, this.pos + 8)
|
|
609
605
|
const bn = new BigNumber(bin)
|
|
610
606
|
this.pos = this.pos + 8
|
|
611
607
|
return bn
|
|
612
608
|
}
|
|
613
609
|
|
|
614
|
-
public readUInt64LEBn(): BigNumber {
|
|
610
|
+
public readUInt64LEBn (): BigNumber {
|
|
615
611
|
const bin = this.readReverse(8)
|
|
616
612
|
const bn = new BigNumber(bin)
|
|
617
613
|
return bn
|
|
618
614
|
}
|
|
619
615
|
|
|
620
|
-
public readVarIntNum(): number {
|
|
616
|
+
public readVarIntNum (): number {
|
|
621
617
|
const first = this.readUInt8()
|
|
622
618
|
let bn: BigNumber
|
|
623
619
|
let n: number
|
|
@@ -638,7 +634,7 @@ export class Reader {
|
|
|
638
634
|
}
|
|
639
635
|
}
|
|
640
636
|
|
|
641
|
-
public readVarInt(): number[] {
|
|
637
|
+
public readVarInt (): number[] {
|
|
642
638
|
const first = this.bin[this.pos]
|
|
643
639
|
switch (first) {
|
|
644
640
|
case 0xfd:
|
|
@@ -652,7 +648,7 @@ export class Reader {
|
|
|
652
648
|
}
|
|
653
649
|
}
|
|
654
650
|
|
|
655
|
-
public readVarIntBn(): BigNumber {
|
|
651
|
+
public readVarIntBn (): BigNumber {
|
|
656
652
|
const first = this.readUInt8()
|
|
657
653
|
switch (first) {
|
|
658
654
|
case 0xfd:
|
|
@@ -665,4 +661,4 @@ export class Reader {
|
|
|
665
661
|
return new BigNumber(first)
|
|
666
662
|
}
|
|
667
663
|
}
|
|
668
|
-
}
|
|
664
|
+
}
|
package/src/script/Spend.ts
CHANGED
|
@@ -89,7 +89,7 @@ export default class Spend {
|
|
|
89
89
|
* inputSequence: 0xffffffff // inputSequence
|
|
90
90
|
* });
|
|
91
91
|
*/
|
|
92
|
-
constructor(params: {
|
|
92
|
+
constructor (params: {
|
|
93
93
|
sourceTXID: string
|
|
94
94
|
sourceOutputIndex: number
|
|
95
95
|
sourceSatoshis: number
|
|
@@ -116,7 +116,7 @@ export default class Spend {
|
|
|
116
116
|
this.reset()
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
reset(): void {
|
|
119
|
+
reset (): void {
|
|
120
120
|
this.context = 'UnlockingScript'
|
|
121
121
|
this.programCounter = 0
|
|
122
122
|
this.lastCodeSeparator = null
|
|
@@ -125,7 +125,7 @@ export default class Spend {
|
|
|
125
125
|
this.ifStack = []
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
step(): void {
|
|
128
|
+
step (): void {
|
|
129
129
|
// If the context is UnlockingScript and we have reached the end,
|
|
130
130
|
// set the context to LockingScript and zero the program counter
|
|
131
131
|
if (
|
|
@@ -1011,15 +1011,15 @@ export default class Spend {
|
|
|
1011
1011
|
}
|
|
1012
1012
|
buf = this.stacktop(-1)
|
|
1013
1013
|
if (currentOpcode === OP.OP_RIPEMD160) {
|
|
1014
|
-
bufHash = Hash.ripemd160(buf)
|
|
1014
|
+
bufHash = Hash.ripemd160(buf)
|
|
1015
1015
|
} else if (currentOpcode === OP.OP_SHA1) {
|
|
1016
|
-
bufHash = Hash.sha1(buf)
|
|
1016
|
+
bufHash = Hash.sha1(buf)
|
|
1017
1017
|
} else if (currentOpcode === OP.OP_SHA256) {
|
|
1018
|
-
bufHash = Hash.sha256(buf)
|
|
1018
|
+
bufHash = Hash.sha256(buf)
|
|
1019
1019
|
} else if (currentOpcode === OP.OP_HASH160) {
|
|
1020
|
-
bufHash = Hash.hash160(buf)
|
|
1020
|
+
bufHash = Hash.hash160(buf)
|
|
1021
1021
|
} else if (currentOpcode === OP.OP_HASH256) {
|
|
1022
|
-
bufHash = Hash.hash256(buf)
|
|
1022
|
+
bufHash = Hash.hash256(buf)
|
|
1023
1023
|
}
|
|
1024
1024
|
this.stack.pop()
|
|
1025
1025
|
this.stack.push(bufHash)
|
|
@@ -1330,7 +1330,7 @@ export default class Spend {
|
|
|
1330
1330
|
* console.log("Invalid spend!");
|
|
1331
1331
|
* }
|
|
1332
1332
|
*/
|
|
1333
|
-
validate(): boolean {
|
|
1333
|
+
validate (): boolean {
|
|
1334
1334
|
if (requirePushOnlyUnlockingScripts && !this.unlockingScript.isPushOnly()) {
|
|
1335
1335
|
this.scriptEvaluationError('Unlocking scripts can only contain push operations, and no other opcodes.')
|
|
1336
1336
|
}
|
|
@@ -1354,11 +1354,11 @@ export default class Spend {
|
|
|
1354
1354
|
return true
|
|
1355
1355
|
}
|
|
1356
1356
|
|
|
1357
|
-
private stacktop(i: number): number[] {
|
|
1357
|
+
private stacktop (i: number): number[] {
|
|
1358
1358
|
return this.stack[this.stack.length + i]
|
|
1359
1359
|
}
|
|
1360
1360
|
|
|
1361
|
-
private castToBool(val: number[]): boolean {
|
|
1361
|
+
private castToBool (val: number[]): boolean {
|
|
1362
1362
|
for (let i = 0; i < val.length; i++) {
|
|
1363
1363
|
if (val[i] !== 0) {
|
|
1364
1364
|
// can be negative zero
|
|
@@ -1371,7 +1371,7 @@ export default class Spend {
|
|
|
1371
1371
|
return false
|
|
1372
1372
|
}
|
|
1373
1373
|
|
|
1374
|
-
private scriptEvaluationError(str: string): void {
|
|
1374
|
+
private scriptEvaluationError (str: string): void {
|
|
1375
1375
|
throw new Error(`Script evaluation error: ${str}\n\nSource TXID: ${this.sourceTXID}\nSource output index: ${this.sourceOutputIndex}\nContext: ${this.context}\nProgram counter: ${this.programCounter}\nStack size: ${this.stack.length}\nAlt stack size: ${this.altStack.length}`)
|
|
1376
1376
|
}
|
|
1377
1377
|
}
|
package/src/script/index.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { default as LockingScript } from './LockingScript.js'
|
|
|
4
4
|
export { default as UnlockingScript } from './UnlockingScript.js'
|
|
5
5
|
export { default as Spend } from './Spend.js'
|
|
6
6
|
export type { default as ScriptTemplate } from './ScriptTemplate.js'
|
|
7
|
-
export * from './templates/index.js'
|
|
7
|
+
export * from './templates/index.js'
|
|
@@ -32,7 +32,7 @@ export default class P2PKH implements ScriptTemplate {
|
|
|
32
32
|
return new LockingScript([
|
|
33
33
|
{ op: OP.OP_DUP },
|
|
34
34
|
{ op: OP.OP_HASH160 },
|
|
35
|
-
{ op:
|
|
35
|
+
{ op: data.length, data },
|
|
36
36
|
{ op: OP.OP_EQUALVERIFY },
|
|
37
37
|
{ op: OP.OP_CHECKSIG }
|
|
38
38
|
])
|
|
@@ -97,7 +97,7 @@ export default class MerklePath {
|
|
|
97
97
|
this.path = path
|
|
98
98
|
|
|
99
99
|
// store all of the legal offsets which we expect given the txid indices.
|
|
100
|
-
|
|
100
|
+
const legalOffsets = Array(this.path.length).fill(0).map(() => new Set())
|
|
101
101
|
this.path.map((leaves, height) => {
|
|
102
102
|
if (leaves.length === 0) {
|
|
103
103
|
throw new Error(`Empty level at height: ${height}`)
|
|
@@ -107,7 +107,7 @@ export default class MerklePath {
|
|
|
107
107
|
if (offsetsAtThisHeight.has(leaf.offset)) throw new Error(`Duplicate offset: ${leaf.offset}, at height: ${height}`)
|
|
108
108
|
offsetsAtThisHeight.add(leaf.offset)
|
|
109
109
|
if (height === 0) {
|
|
110
|
-
if (leaf.duplicate
|
|
110
|
+
if (!leaf.duplicate) {
|
|
111
111
|
for (let h = 1; h < this.path.length; h++) {
|
|
112
112
|
legalOffsets[h].add(leaf.offset >> h ^ 1)
|
|
113
113
|
}
|
|
@@ -188,7 +188,7 @@ export default class MerklePath {
|
|
|
188
188
|
}
|
|
189
189
|
// Calculate the root using the index as a way to determine which direction to concatenate.
|
|
190
190
|
const hash = (m: string): string => toHex((
|
|
191
|
-
hash256(toArray(m, 'hex').reverse())
|
|
191
|
+
hash256(toArray(m, 'hex').reverse())
|
|
192
192
|
).reverse())
|
|
193
193
|
let workingHash = txid
|
|
194
194
|
for (let height = 0; height < this.path.length; height++) {
|