@btc-vision/bitcoin 6.3.6 → 6.4.1

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 (71) hide show
  1. package/.mocharc.json +13 -0
  2. package/browser/address.d.ts +1 -1
  3. package/browser/index.js +1 -1
  4. package/browser/index.js.LICENSE.txt +3 -3
  5. package/browser/networks.d.ts +1 -0
  6. package/build/address.d.ts +2 -1
  7. package/build/address.js +68 -13
  8. package/build/block.js +2 -2
  9. package/build/bufferutils.js +5 -5
  10. package/build/networks.d.ts +1 -0
  11. package/build/networks.js +11 -0
  12. package/build/psbt/psbtutils.js +2 -2
  13. package/build/psbt.js +3 -7
  14. package/package.json +26 -26
  15. package/src/address.ts +91 -15
  16. package/src/block.ts +2 -2
  17. package/src/bufferutils.ts +15 -7
  18. package/src/index.ts +86 -86
  19. package/src/networks.ts +12 -0
  20. package/src/psbt/bip371.ts +441 -441
  21. package/src/psbt/psbtutils.ts +320 -319
  22. package/src/psbt.ts +8 -8
  23. package/test/address.spec.ts +55 -77
  24. package/test/bitcoin.core.spec.ts +47 -69
  25. package/test/block.spec.ts +23 -46
  26. package/test/bufferutils.spec.ts +32 -95
  27. package/test/crypto.spec.ts +9 -15
  28. package/test/fixtures/address.json +3 -3
  29. package/test/integration/addresses.spec.ts +12 -24
  30. package/test/integration/bip32.spec.ts +10 -31
  31. package/test/integration/blocks.spec.ts +2 -2
  32. package/test/integration/cltv.spec.ts +21 -63
  33. package/test/integration/csv.spec.ts +30 -105
  34. package/test/integration/payments.spec.ts +16 -41
  35. package/test/integration/taproot.spec.ts +31 -75
  36. package/test/integration/transactions.spec.ts +37 -138
  37. package/test/payments.spec.ts +95 -106
  38. package/test/payments.utils.ts +20 -63
  39. package/test/psbt.spec.ts +100 -229
  40. package/test/script.spec.ts +26 -50
  41. package/test/script_number.spec.ts +6 -9
  42. package/test/script_signature.spec.ts +7 -7
  43. package/test/transaction.spec.ts +46 -96
  44. package/test/ts-node-register.js +3 -1
  45. package/test/tsconfig.json +4 -1
  46. package/test/types.spec.ts +7 -12
  47. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  48. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/index.json +0 -1
  50. package/test/address.spec.js +0 -124
  51. package/test/bitcoin.core.spec.js +0 -170
  52. package/test/block.spec.js +0 -141
  53. package/test/bufferutils.spec.js +0 -427
  54. package/test/crypto.spec.js +0 -41
  55. package/test/integration/_regtest.js +0 -7
  56. package/test/integration/addresses.spec.js +0 -116
  57. package/test/integration/bip32.spec.js +0 -85
  58. package/test/integration/blocks.spec.js +0 -26
  59. package/test/integration/cltv.spec.js +0 -199
  60. package/test/integration/csv.spec.js +0 -362
  61. package/test/integration/payments.spec.js +0 -98
  62. package/test/integration/taproot.spec.js +0 -532
  63. package/test/integration/transactions.spec.js +0 -561
  64. package/test/payments.spec.js +0 -97
  65. package/test/payments.utils.js +0 -190
  66. package/test/psbt.spec.js +0 -1044
  67. package/test/script.spec.js +0 -151
  68. package/test/script_number.spec.js +0 -24
  69. package/test/script_signature.spec.js +0 -52
  70. package/test/transaction.spec.js +0 -269
  71. package/test/types.spec.js +0 -46
@@ -1,427 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const assert = require("assert");
4
- const mocha_1 = require("mocha");
5
- const bufferutils = require("../src/bufferutils");
6
- const bufferutils_1 = require("../src/bufferutils");
7
- const fixtures = require("./fixtures/bufferutils.json");
8
- const varuint = require('varuint-bitcoin');
9
- (0, mocha_1.describe)('bufferutils', () => {
10
- function concatToBuffer(values) {
11
- return Buffer.concat(values.map(data => Buffer.from(data)));
12
- }
13
- (0, mocha_1.describe)('readUInt64LE', () => {
14
- fixtures.valid.forEach(f => {
15
- (0, mocha_1.it)('decodes ' + f.hex, () => {
16
- const buffer = Buffer.from(f.hex, 'hex');
17
- const num = bufferutils.readUInt64LE(buffer, 0);
18
- assert.strictEqual(num, f.dec);
19
- });
20
- });
21
- fixtures.invalid.readUInt64LE.forEach(f => {
22
- (0, mocha_1.it)('throws on ' + f.description, () => {
23
- const buffer = Buffer.from(f.hex, 'hex');
24
- assert.throws(() => {
25
- bufferutils.readUInt64LE(buffer, 0);
26
- }, new RegExp(f.exception));
27
- });
28
- });
29
- });
30
- (0, mocha_1.describe)('writeUInt64LE', () => {
31
- fixtures.valid.forEach(f => {
32
- (0, mocha_1.it)('encodes ' + f.dec, () => {
33
- const buffer = Buffer.alloc(8, 0);
34
- bufferutils.writeUInt64LE(buffer, f.dec, 0);
35
- assert.strictEqual(buffer.toString('hex'), f.hex);
36
- });
37
- });
38
- fixtures.invalid.writeUInt64LE.forEach(f => {
39
- (0, mocha_1.it)('throws on ' + f.description, () => {
40
- const buffer = Buffer.alloc(8, 0);
41
- assert.throws(() => {
42
- bufferutils.writeUInt64LE(buffer, f.dec, 0);
43
- }, new RegExp(f.exception));
44
- });
45
- });
46
- });
47
- (0, mocha_1.describe)('BufferWriter', () => {
48
- function testBuffer(bufferWriter, expectedBuffer, expectedOffset = expectedBuffer.length) {
49
- assert.strictEqual(bufferWriter.offset, expectedOffset);
50
- assert.deepStrictEqual(bufferWriter.buffer.slice(0, expectedOffset), expectedBuffer.slice(0, expectedOffset));
51
- }
52
- (0, mocha_1.it)('withCapacity', () => {
53
- const expectedBuffer = Buffer.from('04030201', 'hex');
54
- const withCapacity = bufferutils_1.BufferWriter.withCapacity(4);
55
- withCapacity.writeInt32(0x01020304);
56
- testBuffer(withCapacity, expectedBuffer);
57
- });
58
- (0, mocha_1.it)('writeUint8', () => {
59
- const values = [0, 1, 254, 255];
60
- const expectedBuffer = Buffer.from([0, 1, 0xfe, 0xff]);
61
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
62
- values.forEach((v) => {
63
- const expectedOffset = bufferWriter.offset + 1;
64
- bufferWriter.writeUInt8(v);
65
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
66
- });
67
- testBuffer(bufferWriter, expectedBuffer);
68
- });
69
- (0, mocha_1.it)('writeInt32', () => {
70
- const values = [
71
- 0,
72
- 1,
73
- Math.pow(2, 31) - 2,
74
- Math.pow(2, 31) - 1,
75
- -1,
76
- -Math.pow(2, 31),
77
- ];
78
- const expectedBuffer = concatToBuffer([
79
- [0, 0, 0, 0],
80
- [1, 0, 0, 0],
81
- [0xfe, 0xff, 0xff, 0x7f],
82
- [0xff, 0xff, 0xff, 0x7f],
83
- [0xff, 0xff, 0xff, 0xff],
84
- [0x00, 0x00, 0x00, 0x80],
85
- ]);
86
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
87
- values.forEach((value) => {
88
- const expectedOffset = bufferWriter.offset + 4;
89
- bufferWriter.writeInt32(value);
90
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
91
- });
92
- testBuffer(bufferWriter, expectedBuffer);
93
- });
94
- (0, mocha_1.it)('writeUInt32', () => {
95
- const maxUInt32 = Math.pow(2, 32) - 1;
96
- const values = [0, 1, Math.pow(2, 16), maxUInt32];
97
- const expectedBuffer = concatToBuffer([
98
- [0, 0, 0, 0],
99
- [1, 0, 0, 0],
100
- [0, 0, 1, 0],
101
- [0xff, 0xff, 0xff, 0xff],
102
- ]);
103
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
104
- values.forEach((value) => {
105
- const expectedOffset = bufferWriter.offset + 4;
106
- bufferWriter.writeUInt32(value);
107
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
108
- });
109
- testBuffer(bufferWriter, expectedBuffer);
110
- });
111
- (0, mocha_1.it)('writeUInt64', () => {
112
- const values = [
113
- 0,
114
- 1,
115
- Math.pow(2, 32),
116
- Number.MAX_SAFE_INTEGER /* 2^53 - 1 */,
117
- ];
118
- const expectedBuffer = concatToBuffer([
119
- [0, 0, 0, 0, 0, 0, 0, 0],
120
- [1, 0, 0, 0, 0, 0, 0, 0],
121
- [0, 0, 0, 0, 1, 0, 0, 0],
122
- [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
123
- ]);
124
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
125
- values.forEach((value) => {
126
- const expectedOffset = bufferWriter.offset + 8;
127
- bufferWriter.writeUInt64(value);
128
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
129
- });
130
- testBuffer(bufferWriter, expectedBuffer);
131
- });
132
- (0, mocha_1.it)('writeVarInt', () => {
133
- const values = [
134
- 0,
135
- 1,
136
- 252,
137
- 253,
138
- 254,
139
- 255,
140
- 256,
141
- Math.pow(2, 16) - 2,
142
- Math.pow(2, 16) - 1,
143
- Math.pow(2, 16),
144
- Math.pow(2, 32) - 2,
145
- Math.pow(2, 32) - 1,
146
- Math.pow(2, 32),
147
- Number.MAX_SAFE_INTEGER,
148
- ];
149
- const expectedBuffer = concatToBuffer([
150
- [0x00],
151
- [0x01],
152
- [0xfc],
153
- [0xfd, 0xfd, 0x00],
154
- [0xfd, 0xfe, 0x00],
155
- [0xfd, 0xff, 0x00],
156
- [0xfd, 0x00, 0x01],
157
- [0xfd, 0xfe, 0xff],
158
- [0xfd, 0xff, 0xff],
159
- [0xfe, 0x00, 0x00, 0x01, 0x00],
160
- [0xfe, 0xfe, 0xff, 0xff, 0xff],
161
- [0xfe, 0xff, 0xff, 0xff, 0xff],
162
- [0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
163
- [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
164
- ]);
165
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
166
- values.forEach((value) => {
167
- const expectedOffset = bufferWriter.offset + varuint.encodingLength(value);
168
- bufferWriter.writeVarInt(value);
169
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
170
- });
171
- testBuffer(bufferWriter, expectedBuffer);
172
- });
173
- (0, mocha_1.it)('writeSlice', () => {
174
- const values = [[], [1], [1, 2, 3, 4], [254, 255]];
175
- const expectedBuffer = concatToBuffer(values);
176
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
177
- values.forEach((v) => {
178
- const expectedOffset = bufferWriter.offset + v.length;
179
- bufferWriter.writeSlice(Buffer.from(v));
180
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
181
- });
182
- testBuffer(bufferWriter, expectedBuffer);
183
- assert.throws(() => {
184
- bufferWriter.writeSlice(Buffer.from([0, 0]));
185
- }, /^Error: Cannot write slice out of bounds$/);
186
- });
187
- (0, mocha_1.it)('writeVarSlice', () => {
188
- const values = [
189
- Buffer.alloc(1, 1),
190
- Buffer.alloc(252, 2),
191
- Buffer.alloc(253, 3),
192
- ];
193
- const expectedBuffer = Buffer.concat([
194
- Buffer.from([0x01, 0x01]),
195
- Buffer.from([0xfc]),
196
- Buffer.alloc(252, 0x02),
197
- Buffer.from([0xfd, 0xfd, 0x00]),
198
- Buffer.alloc(253, 0x03),
199
- ]);
200
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
201
- values.forEach((value) => {
202
- const expectedOffset = bufferWriter.offset +
203
- varuint.encodingLength(value.length) +
204
- value.length;
205
- bufferWriter.writeVarSlice(value);
206
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
207
- });
208
- testBuffer(bufferWriter, expectedBuffer);
209
- });
210
- (0, mocha_1.it)('writeVector', () => {
211
- const values = [
212
- [Buffer.alloc(1, 4), Buffer.alloc(253, 5)],
213
- Array(253).fill(Buffer.alloc(1, 6)),
214
- ];
215
- const expectedBuffer = Buffer.concat([
216
- Buffer.from([0x02]),
217
- Buffer.from([0x01, 0x04]),
218
- Buffer.from([0xfd, 0xfd, 0x00]),
219
- Buffer.alloc(253, 5),
220
- Buffer.from([0xfd, 0xfd, 0x00]),
221
- Buffer.concat(Array(253)
222
- .fill(0)
223
- .map(() => Buffer.from([0x01, 0x06]))),
224
- ]);
225
- const bufferWriter = new bufferutils_1.BufferWriter(Buffer.allocUnsafe(expectedBuffer.length));
226
- values.forEach((value) => {
227
- const expectedOffset = bufferWriter.offset +
228
- varuint.encodingLength(value.length) +
229
- value.reduce((sum, v) => sum + varuint.encodingLength(v.length) + v.length, 0);
230
- bufferWriter.writeVector(value);
231
- testBuffer(bufferWriter, expectedBuffer, expectedOffset);
232
- });
233
- testBuffer(bufferWriter, expectedBuffer);
234
- });
235
- (0, mocha_1.it)('end', () => {
236
- const expected = Buffer.from('0403020108070605', 'hex');
237
- const bufferWriter = bufferutils_1.BufferWriter.withCapacity(8);
238
- bufferWriter.writeUInt32(0x01020304);
239
- bufferWriter.writeUInt32(0x05060708);
240
- const result = bufferWriter.end();
241
- testBuffer(bufferWriter, result);
242
- testBuffer(bufferWriter, expected);
243
- });
244
- });
245
- (0, mocha_1.describe)('BufferReader', () => {
246
- function testValue(bufferReader, value, expectedValue, expectedOffset = Buffer.isBuffer(expectedValue)
247
- ? expectedValue.length
248
- : 0) {
249
- assert.strictEqual(bufferReader.offset, expectedOffset);
250
- if (Buffer.isBuffer(expectedValue)) {
251
- assert.deepStrictEqual(value.slice(0, expectedOffset), expectedValue.slice(0, expectedOffset));
252
- }
253
- else {
254
- assert.strictEqual(value, expectedValue);
255
- }
256
- }
257
- (0, mocha_1.it)('readUInt8', () => {
258
- const values = [0, 1, 0xfe, 0xff];
259
- const buffer = Buffer.from([0, 1, 0xfe, 0xff]);
260
- const bufferReader = new bufferutils_1.BufferReader(buffer);
261
- values.forEach((v) => {
262
- const expectedOffset = bufferReader.offset + 1;
263
- const val = bufferReader.readUInt8();
264
- testValue(bufferReader, val, v, expectedOffset);
265
- });
266
- });
267
- (0, mocha_1.it)('readInt32', () => {
268
- const values = [
269
- 0,
270
- 1,
271
- Math.pow(2, 31) - 2,
272
- Math.pow(2, 31) - 1,
273
- -1,
274
- -Math.pow(2, 31),
275
- ];
276
- const buffer = concatToBuffer([
277
- [0, 0, 0, 0],
278
- [1, 0, 0, 0],
279
- [0xfe, 0xff, 0xff, 0x7f],
280
- [0xff, 0xff, 0xff, 0x7f],
281
- [0xff, 0xff, 0xff, 0xff],
282
- [0x00, 0x00, 0x00, 0x80],
283
- ]);
284
- const bufferReader = new bufferutils_1.BufferReader(buffer);
285
- values.forEach((value) => {
286
- const expectedOffset = bufferReader.offset + 4;
287
- const val = bufferReader.readInt32();
288
- testValue(bufferReader, val, value, expectedOffset);
289
- });
290
- });
291
- (0, mocha_1.it)('readUInt32', () => {
292
- const maxUInt32 = Math.pow(2, 32) - 1;
293
- const values = [0, 1, Math.pow(2, 16), maxUInt32];
294
- const buffer = concatToBuffer([
295
- [0, 0, 0, 0],
296
- [1, 0, 0, 0],
297
- [0, 0, 1, 0],
298
- [0xff, 0xff, 0xff, 0xff],
299
- ]);
300
- const bufferReader = new bufferutils_1.BufferReader(buffer);
301
- values.forEach((value) => {
302
- const expectedOffset = bufferReader.offset + 4;
303
- const val = bufferReader.readUInt32();
304
- testValue(bufferReader, val, value, expectedOffset);
305
- });
306
- });
307
- (0, mocha_1.it)('readUInt64', () => {
308
- const values = [
309
- 0,
310
- 1,
311
- Math.pow(2, 32),
312
- Number.MAX_SAFE_INTEGER /* 2^53 - 1 */,
313
- ];
314
- const buffer = concatToBuffer([
315
- [0, 0, 0, 0, 0, 0, 0, 0],
316
- [1, 0, 0, 0, 0, 0, 0, 0],
317
- [0, 0, 0, 0, 1, 0, 0, 0],
318
- [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
319
- ]);
320
- const bufferReader = new bufferutils_1.BufferReader(buffer);
321
- values.forEach((value) => {
322
- const expectedOffset = bufferReader.offset + 8;
323
- const val = bufferReader.readUInt64();
324
- testValue(bufferReader, val, value, expectedOffset);
325
- });
326
- });
327
- (0, mocha_1.it)('readVarInt', () => {
328
- const values = [
329
- 0,
330
- 1,
331
- 252,
332
- 253,
333
- 254,
334
- 255,
335
- 256,
336
- Math.pow(2, 16) - 2,
337
- Math.pow(2, 16) - 1,
338
- Math.pow(2, 16),
339
- Math.pow(2, 32) - 2,
340
- Math.pow(2, 32) - 1,
341
- Math.pow(2, 32),
342
- Number.MAX_SAFE_INTEGER,
343
- ];
344
- const buffer = concatToBuffer([
345
- [0x00],
346
- [0x01],
347
- [0xfc],
348
- [0xfd, 0xfd, 0x00],
349
- [0xfd, 0xfe, 0x00],
350
- [0xfd, 0xff, 0x00],
351
- [0xfd, 0x00, 0x01],
352
- [0xfd, 0xfe, 0xff],
353
- [0xfd, 0xff, 0xff],
354
- [0xfe, 0x00, 0x00, 0x01, 0x00],
355
- [0xfe, 0xfe, 0xff, 0xff, 0xff],
356
- [0xfe, 0xff, 0xff, 0xff, 0xff],
357
- [0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
358
- [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
359
- ]);
360
- const bufferReader = new bufferutils_1.BufferReader(buffer);
361
- values.forEach((value) => {
362
- const expectedOffset = bufferReader.offset + varuint.encodingLength(value);
363
- const val = bufferReader.readVarInt();
364
- testValue(bufferReader, val, value, expectedOffset);
365
- });
366
- });
367
- (0, mocha_1.it)('readSlice', () => {
368
- const values = [[1], [1, 2, 3, 4], [254, 255]];
369
- const buffer = concatToBuffer(values);
370
- const bufferReader = new bufferutils_1.BufferReader(buffer);
371
- values.forEach((v) => {
372
- const expectedOffset = bufferReader.offset + v.length;
373
- const val = bufferReader.readSlice(v.length);
374
- testValue(bufferReader, val, Buffer.from(v), expectedOffset);
375
- });
376
- assert.throws(() => {
377
- bufferReader.readSlice(2);
378
- }, /^Error: Cannot read slice out of bounds$/);
379
- });
380
- (0, mocha_1.it)('readVarSlice', () => {
381
- const values = [
382
- Buffer.alloc(1, 1),
383
- Buffer.alloc(252, 2),
384
- Buffer.alloc(253, 3),
385
- ];
386
- const buffer = Buffer.concat([
387
- Buffer.from([0x01, 0x01]),
388
- Buffer.from([0xfc]),
389
- Buffer.alloc(252, 0x02),
390
- Buffer.from([0xfd, 0xfd, 0x00]),
391
- Buffer.alloc(253, 0x03),
392
- ]);
393
- const bufferReader = new bufferutils_1.BufferReader(buffer);
394
- values.forEach((value) => {
395
- const expectedOffset = bufferReader.offset +
396
- varuint.encodingLength(value.length) +
397
- value.length;
398
- const val = bufferReader.readVarSlice();
399
- testValue(bufferReader, val, value, expectedOffset);
400
- });
401
- });
402
- (0, mocha_1.it)('readVector', () => {
403
- const values = [
404
- [Buffer.alloc(1, 4), Buffer.alloc(253, 5)],
405
- Array(253).fill(Buffer.alloc(1, 6)),
406
- ];
407
- const buffer = Buffer.concat([
408
- Buffer.from([0x02]),
409
- Buffer.from([0x01, 0x04]),
410
- Buffer.from([0xfd, 0xfd, 0x00]),
411
- Buffer.alloc(253, 5),
412
- Buffer.from([0xfd, 0xfd, 0x00]),
413
- Buffer.concat(Array(253)
414
- .fill(0)
415
- .map(() => Buffer.from([0x01, 0x06]))),
416
- ]);
417
- const bufferReader = new bufferutils_1.BufferReader(buffer);
418
- values.forEach((value) => {
419
- const expectedOffset = bufferReader.offset +
420
- varuint.encodingLength(value.length) +
421
- value.reduce((sum, v) => sum + varuint.encodingLength(v.length) + v.length, 0);
422
- const val = bufferReader.readVector();
423
- testValue(bufferReader, Buffer.concat(val), Buffer.concat(value), expectedOffset);
424
- });
425
- });
426
- });
427
- });
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const assert = require("assert");
4
- const mocha_1 = require("mocha");
5
- const __1 = require("..");
6
- const fixtures = require("./fixtures/crypto.json");
7
- const crypto_1 = require("../src/crypto");
8
- (0, mocha_1.describe)('crypto', () => {
9
- ['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(algorithm => {
10
- (0, mocha_1.describe)(algorithm, () => {
11
- fixtures.hashes.forEach(f => {
12
- const fn = __1.crypto[algorithm];
13
- const expected = f[algorithm];
14
- (0, mocha_1.it)('returns ' + expected + ' for ' + f.hex, () => {
15
- const data = Buffer.from(f.hex, 'hex');
16
- const actual = fn(data).toString('hex');
17
- assert.strictEqual(actual, expected);
18
- });
19
- });
20
- });
21
- });
22
- (0, mocha_1.describe)('taggedHash', () => {
23
- fixtures.taggedHash.forEach(f => {
24
- const bytes = Buffer.from(f.hex, 'hex');
25
- const expected = Buffer.from(f.result, 'hex');
26
- (0, mocha_1.it)(`returns ${f.result} for taggedHash "${f.tag}" of ${f.hex}`, () => {
27
- const actual = __1.crypto.taggedHash(f.tag, bytes);
28
- assert.strictEqual(actual.toString('hex'), expected.toString('hex'));
29
- });
30
- });
31
- });
32
- (0, mocha_1.describe)('TAGGED_HASH_PREFIXES', () => {
33
- const taggedHashPrefixes = Object.fromEntries(crypto_1.TAGS.map((tag) => {
34
- const tagHash = (0, crypto_1.sha256)(Buffer.from(tag));
35
- return [tag, Buffer.concat([tagHash, tagHash])];
36
- }));
37
- (0, mocha_1.it)('stored the result of operation', () => {
38
- assert.strictEqual(JSON.stringify(crypto_1.TAGGED_HASH_PREFIXES), JSON.stringify(taggedHashPrefixes));
39
- });
40
- });
41
- });
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.regtestUtils = void 0;
4
- const regtest_client_1 = require("regtest-client");
5
- const APIPASS = process.env.APIPASS || 'satoshi';
6
- const APIURL = process.env.APIURL || 'https://regtest.bitbank.cc/1';
7
- exports.regtestUtils = new regtest_client_1.RegtestUtils({ APIPASS, APIURL });
@@ -1,116 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const assert = require("assert");
4
- const ecpair_1 = require("ecpair");
5
- const ecc = require("tiny-secp256k1");
6
- const mocha_1 = require("mocha");
7
- const bitcoin = require("../..");
8
- const _regtest_1 = require("./_regtest");
9
- const ECPair = (0, ecpair_1.default)(ecc);
10
- const dhttp = _regtest_1.regtestUtils.dhttp;
11
- const TESTNET = bitcoin.networks.testnet;
12
- (0, mocha_1.describe)('bitcoinjs-lib (addresses)', () => {
13
- (0, mocha_1.it)('can generate a random address [and support the retrieval of ' +
14
- 'transactions for that address (via 3PBP)]', async () => {
15
- const keyPair = ECPair.makeRandom();
16
- const { address } = bitcoin.payments.p2pkh({
17
- pubkey: keyPair.publicKey,
18
- });
19
- // bitcoin P2PKH addresses start with a '1'
20
- assert.strictEqual(address.startsWith('1'), true);
21
- const result = await dhttp({
22
- method: 'GET',
23
- url: 'https://blockchain.info/rawaddr/' + address,
24
- });
25
- // random private keys [probably!] have no transactions
26
- assert.strictEqual(result.n_tx, 0);
27
- assert.strictEqual(result.total_received, 0);
28
- assert.strictEqual(result.total_sent, 0);
29
- });
30
- (0, mocha_1.it)('can import an address via WIF', () => {
31
- const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
32
- const { address } = bitcoin.payments.p2pkh({
33
- pubkey: keyPair.publicKey,
34
- });
35
- assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
36
- });
37
- (0, mocha_1.it)('can generate a P2SH, pay-to-multisig (2-of-3) address', () => {
38
- const pubkeys = [
39
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
40
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
41
- '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
42
- ].map(hex => Buffer.from(hex, 'hex'));
43
- const { address } = bitcoin.payments.p2sh({
44
- redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
45
- });
46
- assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7');
47
- });
48
- (0, mocha_1.it)('can generate a SegWit address', () => {
49
- const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
50
- const { address } = bitcoin.payments.p2wpkh({
51
- pubkey: keyPair.publicKey,
52
- });
53
- assert.strictEqual(address, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
54
- });
55
- (0, mocha_1.it)('can generate a SegWit address (via P2SH)', () => {
56
- const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
57
- const { address } = bitcoin.payments.p2sh({
58
- redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }),
59
- });
60
- assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN');
61
- });
62
- (0, mocha_1.it)('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', () => {
63
- const pubkeys = [
64
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
65
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
66
- '023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
67
- '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
68
- ].map(hex => Buffer.from(hex, 'hex'));
69
- const { address } = bitcoin.payments.p2wsh({
70
- redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
71
- });
72
- assert.strictEqual(address, 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul');
73
- });
74
- (0, mocha_1.it)('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', () => {
75
- const pubkeys = [
76
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
77
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
78
- ].map(hex => Buffer.from(hex, 'hex'));
79
- const { address } = bitcoin.payments.p2sh({
80
- redeem: bitcoin.payments.p2wsh({
81
- redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
82
- }),
83
- });
84
- assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN');
85
- });
86
- // examples using other network information
87
- (0, mocha_1.it)('can generate a Testnet address', () => {
88
- const keyPair = ECPair.makeRandom({ network: TESTNET });
89
- const { address } = bitcoin.payments.p2pkh({
90
- pubkey: keyPair.publicKey,
91
- network: TESTNET,
92
- });
93
- // bitcoin testnet P2PKH addresses start with a 'm' or 'n'
94
- assert.strictEqual(address.startsWith('m') || address.startsWith('n'), true);
95
- });
96
- (0, mocha_1.it)('can generate a Litecoin address', () => {
97
- // WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
98
- const LITECOIN = {
99
- messagePrefix: '\x19Litecoin Signed Message:\n',
100
- bech32: 'ltc',
101
- bip32: {
102
- public: 0x019da462,
103
- private: 0x019d9cfe,
104
- },
105
- pubKeyHash: 0x30,
106
- scriptHash: 0x32,
107
- wif: 0xb0,
108
- };
109
- const keyPair = ECPair.makeRandom({ network: LITECOIN });
110
- const { address } = bitcoin.payments.p2pkh({
111
- pubkey: keyPair.publicKey,
112
- network: LITECOIN,
113
- });
114
- assert.strictEqual(address.startsWith('L'), true);
115
- });
116
- });
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const assert = require("assert");
4
- const bip32_1 = require("bip32");
5
- const ecc = require("tiny-secp256k1");
6
- const bip39 = require("bip39");
7
- const mocha_1 = require("mocha");
8
- const bitcoin = require("../..");
9
- const bip32 = (0, bip32_1.default)(ecc);
10
- function getAddress(node, network) {
11
- return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address;
12
- }
13
- (0, mocha_1.describe)('bitcoinjs-lib (BIP32)', () => {
14
- (0, mocha_1.it)('can import a BIP32 testnet xpriv and export to WIF', () => {
15
- const xpriv = 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK';
16
- const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
17
- assert.strictEqual(node.toWIF(), 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7');
18
- });
19
- (0, mocha_1.it)('can export a BIP32 xpriv, then import it', () => {
20
- const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
21
- const seed = bip39.mnemonicToSeedSync(mnemonic);
22
- const node = bip32.fromSeed(seed);
23
- const strng = node.toBase58();
24
- const restored = bip32.fromBase58(strng);
25
- assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
26
- assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
27
- });
28
- (0, mocha_1.it)('can export a BIP32 xpub', () => {
29
- const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
30
- const seed = bip39.mnemonicToSeedSync(mnemonic);
31
- const node = bip32.fromSeed(seed);
32
- const strng = node.neutered().toBase58();
33
- assert.strictEqual(strng, 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n');
34
- });
35
- (0, mocha_1.it)('can create a BIP32, bitcoin, account 0, external address', () => {
36
- const path = "m/0'/0/0";
37
- const root = bip32.fromSeed(Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'));
38
- const child1 = root.derivePath(path);
39
- // option 2, manually
40
- const child1b = root.deriveHardened(0).derive(0).derive(0);
41
- assert.strictEqual(getAddress(child1), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7');
42
- assert.strictEqual(getAddress(child1b), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7');
43
- });
44
- (0, mocha_1.it)('can create a BIP44, bitcoin, account 0, external address', () => {
45
- const root = bip32.fromSeed(Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'));
46
- const child1 = root.derivePath("m/44'/0'/0'/0/0");
47
- // option 2, manually
48
- const child1b = root
49
- .deriveHardened(44)
50
- .deriveHardened(0)
51
- .deriveHardened(0)
52
- .derive(0)
53
- .derive(0);
54
- assert.strictEqual(getAddress(child1), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
55
- assert.strictEqual(getAddress(child1b), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
56
- });
57
- (0, mocha_1.it)('can create a BIP49, bitcoin testnet, account 0, external address', () => {
58
- const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
59
- const seed = bip39.mnemonicToSeedSync(mnemonic);
60
- const root = bip32.fromSeed(seed);
61
- const path = "m/49'/1'/0'/0/0";
62
- const child = root.derivePath(path);
63
- const { address } = bitcoin.payments.p2sh({
64
- redeem: bitcoin.payments.p2wpkh({
65
- pubkey: child.publicKey,
66
- network: bitcoin.networks.testnet,
67
- }),
68
- network: bitcoin.networks.testnet,
69
- });
70
- assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
71
- });
72
- (0, mocha_1.it)('can use BIP39 to generate BIP32 addresses', () => {
73
- // var mnemonic = bip39.generateMnemonic()
74
- const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
75
- assert(bip39.validateMnemonic(mnemonic));
76
- const seed = bip39.mnemonicToSeedSync(mnemonic);
77
- const root = bip32.fromSeed(seed);
78
- // receive addresses
79
- assert.strictEqual(getAddress(root.derivePath("m/0'/0/0")), '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC');
80
- assert.strictEqual(getAddress(root.derivePath("m/0'/0/1")), '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL');
81
- // change addresses
82
- assert.strictEqual(getAddress(root.derivePath("m/0'/1/0")), '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn');
83
- assert.strictEqual(getAddress(root.derivePath("m/0'/1/1")), '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6');
84
- });
85
- });