@btc-vision/bitcoin 6.3.0 → 6.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/.babelrc +4 -0
  2. package/.gitattributes +2 -0
  3. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  4. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  5. package/.nyc_output/processinfo/index.json +1 -0
  6. package/.prettierrc.json +12 -0
  7. package/CHANGELOG.md +403 -0
  8. package/CONTRIBUTING.md +83 -0
  9. package/browser/address.d.ts +16 -0
  10. package/{src → browser}/bip66.d.ts +6 -7
  11. package/{src → browser}/block.d.ts +29 -30
  12. package/{src → browser}/bufferutils.d.ts +34 -54
  13. package/browser/crypto/crypto.d.ts +1 -0
  14. package/{src → browser}/crypto.d.ts +13 -18
  15. package/browser/ecc_lib.d.ts +3 -0
  16. package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
  17. package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
  18. package/browser/hooks/SignatureManager.d.ts +13 -0
  19. package/browser/index.d.ts +58 -0
  20. package/browser/index.js +2 -0
  21. package/browser/index.js.LICENSE.txt +14 -0
  22. package/browser/merkle.d.ts +1 -0
  23. package/browser/networks.d.ts +23 -0
  24. package/{src → browser}/ops.d.ts +126 -126
  25. package/browser/payments/bip341.d.ts +23 -0
  26. package/browser/payments/embed.d.ts +2 -0
  27. package/browser/payments/index.d.ts +41 -0
  28. package/{src → browser}/payments/lazy.d.ts +2 -2
  29. package/browser/payments/p2ms.d.ts +2 -0
  30. package/browser/payments/p2pk.d.ts +2 -0
  31. package/browser/payments/p2pkh.d.ts +2 -0
  32. package/browser/payments/p2sh.d.ts +2 -0
  33. package/browser/payments/p2tr.d.ts +2 -0
  34. package/browser/payments/p2wpkh.d.ts +2 -0
  35. package/browser/payments/p2wsh.d.ts +2 -0
  36. package/browser/psbt/bip371.d.ts +16 -0
  37. package/browser/psbt/psbtutils.d.ts +26 -0
  38. package/{src → browser}/psbt.d.ts +167 -235
  39. package/browser/push_data.d.ts +7 -0
  40. package/browser/script.d.ts +17 -0
  41. package/browser/script_number.d.ts +2 -0
  42. package/browser/script_signature.d.ts +7 -0
  43. package/{src → browser}/transaction.d.ts +48 -60
  44. package/{src → browser}/types.d.ts +37 -54
  45. package/build/address.d.ts +16 -0
  46. package/build/address.js +148 -0
  47. package/build/bip66.d.ts +6 -0
  48. package/build/bip66.js +99 -0
  49. package/build/block.d.ts +29 -0
  50. package/build/block.js +181 -0
  51. package/build/bufferutils.d.ts +34 -0
  52. package/build/bufferutils.js +141 -0
  53. package/build/crypto/crypto.d.ts +1 -0
  54. package/build/crypto/crypto.js +1 -0
  55. package/build/crypto.d.ts +13 -0
  56. package/build/crypto.js +87 -0
  57. package/build/ecc_lib.d.ts +3 -0
  58. package/build/ecc_lib.js +61 -0
  59. package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
  60. package/build/hooks/AdvancedSignatureManager.js +52 -0
  61. package/build/hooks/HookedSigner.d.ts +4 -0
  62. package/build/hooks/HookedSigner.js +64 -0
  63. package/build/hooks/SignatureManager.d.ts +13 -0
  64. package/build/hooks/SignatureManager.js +45 -0
  65. package/build/index.d.ts +58 -0
  66. package/build/index.js +32 -0
  67. package/build/merkle.d.ts +1 -0
  68. package/build/merkle.js +19 -0
  69. package/build/networks.d.ts +23 -0
  70. package/build/networks.js +121 -0
  71. package/build/ops.d.ts +126 -0
  72. package/{src → build}/ops.js +127 -131
  73. package/build/payments/bip341.d.ts +23 -0
  74. package/build/payments/bip341.js +82 -0
  75. package/build/payments/embed.d.ts +2 -0
  76. package/build/payments/embed.js +39 -0
  77. package/build/payments/index.d.ts +41 -0
  78. package/build/payments/index.js +10 -0
  79. package/build/payments/lazy.d.ts +2 -0
  80. package/{src → build}/payments/lazy.js +28 -32
  81. package/build/payments/p2ms.d.ts +2 -0
  82. package/{src → build}/payments/p2ms.js +128 -158
  83. package/build/payments/p2pk.d.ts +2 -0
  84. package/build/payments/p2pk.js +68 -0
  85. package/build/payments/p2pkh.d.ts +2 -0
  86. package/build/payments/p2pkh.js +135 -0
  87. package/build/payments/p2sh.d.ts +2 -0
  88. package/build/payments/p2sh.js +175 -0
  89. package/build/payments/p2tr.d.ts +2 -0
  90. package/build/payments/p2tr.js +254 -0
  91. package/build/payments/p2wpkh.d.ts +2 -0
  92. package/build/payments/p2wpkh.js +130 -0
  93. package/build/payments/p2wsh.d.ts +2 -0
  94. package/build/payments/p2wsh.js +180 -0
  95. package/build/psbt/bip371.d.ts +16 -0
  96. package/build/psbt/bip371.js +246 -0
  97. package/build/psbt/psbtutils.d.ts +26 -0
  98. package/build/psbt/psbtutils.js +170 -0
  99. package/build/psbt.d.ts +167 -0
  100. package/build/psbt.js +1305 -0
  101. package/build/push_data.d.ts +7 -0
  102. package/build/push_data.js +57 -0
  103. package/build/script.d.ts +17 -0
  104. package/build/script.js +167 -0
  105. package/build/script_number.d.ts +2 -0
  106. package/build/script_number.js +49 -0
  107. package/build/script_signature.d.ts +7 -0
  108. package/build/script_signature.js +49 -0
  109. package/build/transaction.d.ts +48 -0
  110. package/build/transaction.js +445 -0
  111. package/build/types.d.ts +37 -0
  112. package/build/types.js +73 -0
  113. package/cjs/package.json +3 -0
  114. package/eslint.config.js +56 -0
  115. package/gulpfile.js +42 -0
  116. package/package.json +105 -50
  117. package/src/{address.js → address.ts} +93 -73
  118. package/src/{bip66.js → bip66.ts} +23 -19
  119. package/src/{block.js → block.ts} +114 -105
  120. package/src/{bufferutils.js → bufferutils.ts} +65 -67
  121. package/src/crypto/crypto-browser.js +75 -0
  122. package/src/crypto/crypto.ts +1 -0
  123. package/src/crypto.ts +108 -0
  124. package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
  125. package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
  126. package/src/hooks/HookedSigner.ts +108 -0
  127. package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
  128. package/src/index.ts +86 -0
  129. package/src/{merkle.js → merkle.ts} +8 -7
  130. package/src/networks.ts +235 -0
  131. package/src/ops.ts +282 -0
  132. package/src/payments/bip341.ts +140 -0
  133. package/src/payments/embed.ts +55 -0
  134. package/src/payments/{index.d.ts → index.ts} +20 -10
  135. package/src/payments/lazy.ts +28 -0
  136. package/src/payments/p2ms.ts +150 -0
  137. package/src/payments/{p2pk.js → p2pk.ts} +32 -29
  138. package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
  139. package/src/payments/{p2sh.js → p2sh.ts} +72 -71
  140. package/src/payments/{p2tr.js → p2tr.ts} +114 -125
  141. package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
  142. package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
  143. package/src/psbt/{bip371.js → bip371.ts} +195 -178
  144. package/src/psbt/psbtutils.ts +299 -0
  145. package/src/{psbt.js → psbt.ts} +1048 -699
  146. package/src/{push_data.js → push_data.ts} +35 -21
  147. package/src/{script.js → script.ts} +93 -77
  148. package/src/{script_number.js → script_number.ts} +15 -21
  149. package/src/{script_signature.js → script_signature.ts} +26 -14
  150. package/src/{transaction.js → transaction.ts} +247 -167
  151. package/src/types.ts +122 -0
  152. package/test/address.spec.js +124 -0
  153. package/test/address.spec.ts +177 -0
  154. package/test/bitcoin.core.spec.js +170 -0
  155. package/test/bitcoin.core.spec.ts +234 -0
  156. package/test/block.spec.js +141 -0
  157. package/test/block.spec.ts +194 -0
  158. package/test/bufferutils.spec.js +427 -0
  159. package/test/bufferutils.spec.ts +513 -0
  160. package/test/crypto.spec.js +41 -0
  161. package/test/crypto.spec.ts +55 -0
  162. package/test/fixtures/address.json +329 -0
  163. package/test/fixtures/block.json +148 -0
  164. package/test/fixtures/bufferutils.json +102 -0
  165. package/test/fixtures/core/README.md +26 -0
  166. package/test/fixtures/core/base58_encode_decode.json +50 -0
  167. package/test/fixtures/core/base58_keys_invalid.json +152 -0
  168. package/test/fixtures/core/base58_keys_valid.json +452 -0
  169. package/test/fixtures/core/blocks.json +27 -0
  170. package/test/fixtures/core/sig_canonical.json +7 -0
  171. package/test/fixtures/core/sig_noncanonical.json +33 -0
  172. package/test/fixtures/core/sighash.json +3505 -0
  173. package/test/fixtures/core/tx_valid.json +2023 -0
  174. package/test/fixtures/crypto.json +43 -0
  175. package/test/fixtures/ecdsa.json +217 -0
  176. package/test/fixtures/ecpair.json +141 -0
  177. package/test/fixtures/embed.json +108 -0
  178. package/test/fixtures/p2ms.json +434 -0
  179. package/test/fixtures/p2pk.json +179 -0
  180. package/test/fixtures/p2pkh.json +276 -0
  181. package/test/fixtures/p2sh.json +508 -0
  182. package/test/fixtures/p2tr.json +1198 -0
  183. package/test/fixtures/p2wpkh.json +290 -0
  184. package/test/fixtures/p2wsh.json +489 -0
  185. package/test/fixtures/psbt.json +924 -0
  186. package/test/fixtures/script.json +465 -0
  187. package/test/fixtures/script_number.json +225 -0
  188. package/test/fixtures/signature.json +140 -0
  189. package/test/fixtures/transaction.json +916 -0
  190. package/test/integration/_regtest.js +7 -0
  191. package/test/integration/_regtest.ts +6 -0
  192. package/test/integration/addresses.spec.js +116 -0
  193. package/test/integration/addresses.spec.ts +154 -0
  194. package/test/integration/bip32.spec.js +85 -0
  195. package/test/integration/bip32.spec.ts +151 -0
  196. package/test/integration/blocks.spec.js +26 -0
  197. package/test/integration/blocks.spec.ts +28 -0
  198. package/test/integration/cltv.spec.js +199 -0
  199. package/test/integration/cltv.spec.ts +283 -0
  200. package/test/integration/csv.spec.js +362 -0
  201. package/test/integration/csv.spec.ts +527 -0
  202. package/test/integration/payments.spec.js +98 -0
  203. package/test/integration/payments.spec.ts +135 -0
  204. package/test/integration/taproot.spec.js +532 -0
  205. package/test/integration/taproot.spec.ts +707 -0
  206. package/test/integration/transactions.spec.js +561 -0
  207. package/test/integration/transactions.spec.ts +769 -0
  208. package/test/payments.spec.js +97 -0
  209. package/test/payments.spec.ts +125 -0
  210. package/test/payments.utils.js +190 -0
  211. package/test/payments.utils.ts +208 -0
  212. package/test/psbt.spec.js +1044 -0
  213. package/test/psbt.spec.ts +1414 -0
  214. package/test/script.spec.js +151 -0
  215. package/test/script.spec.ts +210 -0
  216. package/test/script_number.spec.js +24 -0
  217. package/test/script_number.spec.ts +29 -0
  218. package/test/script_signature.spec.js +52 -0
  219. package/test/script_signature.spec.ts +66 -0
  220. package/test/transaction.spec.js +269 -0
  221. package/test/transaction.spec.ts +387 -0
  222. package/test/ts-node-register.js +5 -0
  223. package/test/tsconfig.json +45 -0
  224. package/test/types.spec.js +46 -0
  225. package/test/types.spec.ts +58 -0
  226. package/tsconfig.base.json +27 -0
  227. package/tsconfig.json +19 -0
  228. package/tsconfig.webpack.json +18 -0
  229. package/webpack.config.js +79 -0
  230. package/src/address.d.ts +0 -42
  231. package/src/crypto.js +0 -128
  232. package/src/ecc_lib.d.ts +0 -17
  233. package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
  234. package/src/hooks/HookedSigner.js +0 -90
  235. package/src/hooks/SignatureManager.d.ts +0 -35
  236. package/src/index.d.ts +0 -42
  237. package/src/index.js +0 -87
  238. package/src/merkle.d.ts +0 -10
  239. package/src/networks.d.ts +0 -29
  240. package/src/networks.js +0 -71
  241. package/src/payments/bip341.d.ts +0 -49
  242. package/src/payments/bip341.js +0 -124
  243. package/src/payments/embed.d.ts +0 -9
  244. package/src/payments/embed.js +0 -54
  245. package/src/payments/index.js +0 -69
  246. package/src/payments/p2ms.d.ts +0 -9
  247. package/src/payments/p2pk.d.ts +0 -10
  248. package/src/payments/p2pkh.d.ts +0 -10
  249. package/src/payments/p2sh.d.ts +0 -10
  250. package/src/payments/p2tr.d.ts +0 -10
  251. package/src/payments/p2wpkh.d.ts +0 -10
  252. package/src/payments/p2wsh.d.ts +0 -10
  253. package/src/psbt/bip371.d.ts +0 -42
  254. package/src/psbt/psbtutils.d.ts +0 -64
  255. package/src/psbt/psbtutils.js +0 -191
  256. package/src/push_data.d.ts +0 -29
  257. package/src/script.d.ts +0 -42
  258. package/src/script_number.d.ts +0 -19
  259. package/src/script_signature.d.ts +0 -21
  260. package/src/types.js +0 -106
@@ -0,0 +1,7 @@
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 });
@@ -0,0 +1,6 @@
1
+ import { RegtestUtils } from 'regtest-client';
2
+
3
+ const APIPASS = process.env.APIPASS || 'satoshi';
4
+ const APIURL = process.env.APIURL || 'https://regtest.bitbank.cc/1';
5
+
6
+ export const regtestUtils = new RegtestUtils({ APIPASS, APIURL });
@@ -0,0 +1,116 @@
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
+ });
@@ -0,0 +1,154 @@
1
+ import * as assert from 'assert';
2
+ import ECPairFactory from 'ecpair';
3
+ import * as ecc from 'tiny-secp256k1';
4
+ import { describe, it } from 'mocha';
5
+ import * as bitcoin from '../..';
6
+ import { regtestUtils } from './_regtest';
7
+
8
+ const ECPair = ECPairFactory(ecc);
9
+ const dhttp = regtestUtils.dhttp;
10
+ const TESTNET = bitcoin.networks.testnet;
11
+
12
+ describe('bitcoinjs-lib (addresses)', () => {
13
+ it(
14
+ 'can generate a random address [and support the retrieval of ' +
15
+ 'transactions for that address (via 3PBP)]',
16
+ async () => {
17
+ const keyPair = ECPair.makeRandom();
18
+ const { address } = bitcoin.payments.p2pkh({
19
+ pubkey: keyPair.publicKey,
20
+ });
21
+
22
+ // bitcoin P2PKH addresses start with a '1'
23
+ assert.strictEqual(address!.startsWith('1'), true);
24
+
25
+ const result = await dhttp({
26
+ method: 'GET',
27
+ url: 'https://blockchain.info/rawaddr/' + address,
28
+ });
29
+
30
+ // random private keys [probably!] have no transactions
31
+ assert.strictEqual((result as any).n_tx, 0);
32
+ assert.strictEqual((result as any).total_received, 0);
33
+ assert.strictEqual((result as any).total_sent, 0);
34
+ },
35
+ );
36
+
37
+ it('can import an address via WIF', () => {
38
+ const keyPair = ECPair.fromWIF(
39
+ 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
40
+ );
41
+ const { address } = bitcoin.payments.p2pkh({
42
+ pubkey: keyPair.publicKey,
43
+ });
44
+
45
+ assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
46
+ });
47
+
48
+ it('can generate a P2SH, pay-to-multisig (2-of-3) address', () => {
49
+ const pubkeys = [
50
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
51
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
52
+ '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
53
+ ].map(hex => Buffer.from(hex, 'hex'));
54
+ const { address } = bitcoin.payments.p2sh({
55
+ redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
56
+ });
57
+
58
+ assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7');
59
+ });
60
+
61
+ it('can generate a SegWit address', () => {
62
+ const keyPair = ECPair.fromWIF(
63
+ 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
64
+ );
65
+ const { address } = bitcoin.payments.p2wpkh({
66
+ pubkey: keyPair.publicKey,
67
+ });
68
+
69
+ assert.strictEqual(
70
+ address,
71
+ 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
72
+ );
73
+ });
74
+
75
+ it('can generate a SegWit address (via P2SH)', () => {
76
+ const keyPair = ECPair.fromWIF(
77
+ 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
78
+ );
79
+ const { address } = bitcoin.payments.p2sh({
80
+ redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }),
81
+ });
82
+
83
+ assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN');
84
+ });
85
+
86
+ it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', () => {
87
+ const pubkeys = [
88
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
89
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
90
+ '023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
91
+ '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
92
+ ].map(hex => Buffer.from(hex, 'hex'));
93
+ const { address } = bitcoin.payments.p2wsh({
94
+ redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
95
+ });
96
+
97
+ assert.strictEqual(
98
+ address,
99
+ 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul',
100
+ );
101
+ });
102
+
103
+ it('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', () => {
104
+ const pubkeys = [
105
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
106
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
107
+ ].map(hex => Buffer.from(hex, 'hex'));
108
+ const { address } = bitcoin.payments.p2sh({
109
+ redeem: bitcoin.payments.p2wsh({
110
+ redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
111
+ }),
112
+ });
113
+
114
+ assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN');
115
+ });
116
+
117
+ // examples using other network information
118
+ it('can generate a Testnet address', () => {
119
+ const keyPair = ECPair.makeRandom({ network: TESTNET });
120
+ const { address } = bitcoin.payments.p2pkh({
121
+ pubkey: keyPair.publicKey,
122
+ network: TESTNET,
123
+ });
124
+
125
+ // bitcoin testnet P2PKH addresses start with a 'm' or 'n'
126
+ assert.strictEqual(
127
+ address!.startsWith('m') || address!.startsWith('n'),
128
+ true,
129
+ );
130
+ });
131
+
132
+ it('can generate a Litecoin address', () => {
133
+ // WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
134
+ const LITECOIN = {
135
+ messagePrefix: '\x19Litecoin Signed Message:\n',
136
+ bech32: 'ltc',
137
+ bip32: {
138
+ public: 0x019da462,
139
+ private: 0x019d9cfe,
140
+ },
141
+ pubKeyHash: 0x30,
142
+ scriptHash: 0x32,
143
+ wif: 0xb0,
144
+ };
145
+
146
+ const keyPair = ECPair.makeRandom({ network: LITECOIN });
147
+ const { address } = bitcoin.payments.p2pkh({
148
+ pubkey: keyPair.publicKey,
149
+ network: LITECOIN,
150
+ });
151
+
152
+ assert.strictEqual(address!.startsWith('L'), true);
153
+ });
154
+ });
@@ -0,0 +1,85 @@
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
+ });
@@ -0,0 +1,151 @@
1
+ import * as assert from 'assert';
2
+ import BIP32Factory from 'bip32';
3
+ import * as ecc from 'tiny-secp256k1';
4
+ import * as bip39 from 'bip39';
5
+ import { describe, it } from 'mocha';
6
+ import * as bitcoin from '../..';
7
+
8
+ const bip32 = BIP32Factory(ecc);
9
+
10
+ function getAddress(node: any, network?: any): string {
11
+ return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address!;
12
+ }
13
+
14
+ describe('bitcoinjs-lib (BIP32)', () => {
15
+ it('can import a BIP32 testnet xpriv and export to WIF', () => {
16
+ const xpriv =
17
+ 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK';
18
+ const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
19
+
20
+ assert.strictEqual(
21
+ node.toWIF(),
22
+ 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7',
23
+ );
24
+ });
25
+
26
+ it('can export a BIP32 xpriv, then import it', () => {
27
+ const mnemonic =
28
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
29
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
30
+ const node = bip32.fromSeed(seed);
31
+ const strng = node.toBase58();
32
+ const restored = bip32.fromBase58(strng);
33
+
34
+ assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
35
+ assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
36
+ });
37
+
38
+ it('can export a BIP32 xpub', () => {
39
+ const mnemonic =
40
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
41
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
42
+ const node = bip32.fromSeed(seed);
43
+ const strng = node.neutered().toBase58();
44
+
45
+ assert.strictEqual(
46
+ strng,
47
+ 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n',
48
+ );
49
+ });
50
+
51
+ it('can create a BIP32, bitcoin, account 0, external address', () => {
52
+ const path = "m/0'/0/0";
53
+ const root = bip32.fromSeed(
54
+ Buffer.from(
55
+ 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
56
+ 'hex',
57
+ ),
58
+ );
59
+
60
+ const child1 = root.derivePath(path);
61
+
62
+ // option 2, manually
63
+ const child1b = root.deriveHardened(0).derive(0).derive(0);
64
+
65
+ assert.strictEqual(
66
+ getAddress(child1),
67
+ '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
68
+ );
69
+ assert.strictEqual(
70
+ getAddress(child1b),
71
+ '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
72
+ );
73
+ });
74
+
75
+ it('can create a BIP44, bitcoin, account 0, external address', () => {
76
+ const root = bip32.fromSeed(
77
+ Buffer.from(
78
+ 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
79
+ 'hex',
80
+ ),
81
+ );
82
+
83
+ const child1 = root.derivePath("m/44'/0'/0'/0/0");
84
+
85
+ // option 2, manually
86
+ const child1b = root
87
+ .deriveHardened(44)
88
+ .deriveHardened(0)
89
+ .deriveHardened(0)
90
+ .derive(0)
91
+ .derive(0);
92
+
93
+ assert.strictEqual(
94
+ getAddress(child1),
95
+ '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
96
+ );
97
+ assert.strictEqual(
98
+ getAddress(child1b),
99
+ '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
100
+ );
101
+ });
102
+
103
+ it('can create a BIP49, bitcoin testnet, account 0, external address', () => {
104
+ const mnemonic =
105
+ 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
106
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
107
+ const root = bip32.fromSeed(seed);
108
+
109
+ const path = "m/49'/1'/0'/0/0";
110
+ const child = root.derivePath(path);
111
+
112
+ const { address } = bitcoin.payments.p2sh({
113
+ redeem: bitcoin.payments.p2wpkh({
114
+ pubkey: child.publicKey,
115
+ network: bitcoin.networks.testnet,
116
+ }),
117
+ network: bitcoin.networks.testnet,
118
+ });
119
+ assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
120
+ });
121
+
122
+ it('can use BIP39 to generate BIP32 addresses', () => {
123
+ // var mnemonic = bip39.generateMnemonic()
124
+ const mnemonic =
125
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
126
+ assert(bip39.validateMnemonic(mnemonic));
127
+
128
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
129
+ const root = bip32.fromSeed(seed);
130
+
131
+ // receive addresses
132
+ assert.strictEqual(
133
+ getAddress(root.derivePath("m/0'/0/0")),
134
+ '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC',
135
+ );
136
+ assert.strictEqual(
137
+ getAddress(root.derivePath("m/0'/0/1")),
138
+ '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL',
139
+ );
140
+
141
+ // change addresses
142
+ assert.strictEqual(
143
+ getAddress(root.derivePath("m/0'/1/0")),
144
+ '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn',
145
+ );
146
+ assert.strictEqual(
147
+ getAddress(root.derivePath("m/0'/1/1")),
148
+ '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6',
149
+ );
150
+ });
151
+ });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const assert = require("assert");
4
+ const mocha_1 = require("mocha");
5
+ const bitcoin = require("../..");
6
+ (0, mocha_1.describe)('bitcoinjs-lib (blocks)', () => {
7
+ (0, mocha_1.it)('can extract a height from a CoinBase transaction', () => {
8
+ // from 00000000000000000097669cdca131f24d40c4cc7d80eaa65967a2d09acf6ce6
9
+ const txHex = '010000000001010000000000000000000000000000000000000000000000000000000' +
10
+ '000000000ffffffff50037f9a07174d696e656420627920416e74506f6f6c685b205a' +
11
+ '2b1f7bfabe6d6d36afe1910eca9405b66f97750940a656e38e2c0312958190ff8e98f' +
12
+ 'd16761d220400000000000000aa340000d49f0000ffffffff02b07fc3660000000019' +
13
+ '76a9148349212dc27ce3ab4c5b29b85c4dec643d764b1788ac0000000000000000266' +
14
+ 'a24aa21a9ed72d9432948505e3d3062f1307a3f027a5dea846ff85e47159680919c12' +
15
+ 'bf1e40012000000000000000000000000000000000000000000000000000000000000' +
16
+ '0000000000000';
17
+ const tx = bitcoin.Transaction.fromHex(txHex);
18
+ assert.strictEqual(tx.ins.length, 1);
19
+ const script = tx.ins[0].script;
20
+ // bitcoin.script.decompile(script) // returns [] :(
21
+ assert.strictEqual(script[0], 0x03);
22
+ const heightBuffer = script.slice(1, 4);
23
+ const height = bitcoin.script.number.decode(heightBuffer);
24
+ assert.strictEqual(height, 498303);
25
+ });
26
+ });
@@ -0,0 +1,28 @@
1
+ import * as assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import * as bitcoin from '../..';
4
+
5
+ describe('bitcoinjs-lib (blocks)', () => {
6
+ it('can extract a height from a CoinBase transaction', () => {
7
+ // from 00000000000000000097669cdca131f24d40c4cc7d80eaa65967a2d09acf6ce6
8
+ const txHex =
9
+ '010000000001010000000000000000000000000000000000000000000000000000000' +
10
+ '000000000ffffffff50037f9a07174d696e656420627920416e74506f6f6c685b205a' +
11
+ '2b1f7bfabe6d6d36afe1910eca9405b66f97750940a656e38e2c0312958190ff8e98f' +
12
+ 'd16761d220400000000000000aa340000d49f0000ffffffff02b07fc3660000000019' +
13
+ '76a9148349212dc27ce3ab4c5b29b85c4dec643d764b1788ac0000000000000000266' +
14
+ 'a24aa21a9ed72d9432948505e3d3062f1307a3f027a5dea846ff85e47159680919c12' +
15
+ 'bf1e40012000000000000000000000000000000000000000000000000000000000000' +
16
+ '0000000000000';
17
+ const tx = bitcoin.Transaction.fromHex(txHex);
18
+
19
+ assert.strictEqual(tx.ins.length, 1);
20
+ const script = tx.ins[0].script;
21
+ // bitcoin.script.decompile(script) // returns [] :(
22
+
23
+ assert.strictEqual(script[0], 0x03);
24
+ const heightBuffer = script.slice(1, 4);
25
+ const height = bitcoin.script.number.decode(heightBuffer);
26
+ assert.strictEqual(height, 498303);
27
+ });
28
+ });