@did-btcr2/bitcoin 0.3.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/cjs/client/http.js +9 -0
  2. package/dist/cjs/client/http.js.map +1 -0
  3. package/dist/cjs/client/rest/address.js +20 -28
  4. package/dist/cjs/client/rest/address.js.map +1 -1
  5. package/dist/cjs/client/rest/block.js +23 -22
  6. package/dist/cjs/client/rest/block.js.map +1 -1
  7. package/dist/cjs/client/rest/index.js +31 -84
  8. package/dist/cjs/client/rest/index.js.map +1 -1
  9. package/dist/cjs/client/rest/protocol.js +129 -0
  10. package/dist/cjs/client/rest/protocol.js.map +1 -0
  11. package/dist/cjs/client/rest/transaction.js +19 -21
  12. package/dist/cjs/client/rest/transaction.js.map +1 -1
  13. package/dist/cjs/client/rpc/index.js +79 -337
  14. package/dist/cjs/client/rpc/index.js.map +1 -1
  15. package/dist/cjs/client/rpc/interface.js +1 -6
  16. package/dist/cjs/client/rpc/interface.js.map +1 -1
  17. package/dist/cjs/client/rpc/json-rpc.js +40 -63
  18. package/dist/cjs/client/rpc/json-rpc.js.map +1 -1
  19. package/dist/cjs/client/rpc/protocol.js +134 -0
  20. package/dist/cjs/client/rpc/protocol.js.map +1 -0
  21. package/dist/cjs/connection.js +104 -0
  22. package/dist/cjs/connection.js.map +1 -0
  23. package/dist/cjs/constants.js +16 -5
  24. package/dist/cjs/constants.js.map +1 -1
  25. package/dist/cjs/errors.js.map +1 -1
  26. package/dist/cjs/index.js +21 -13
  27. package/dist/cjs/index.js.map +1 -1
  28. package/dist/cjs/types.js +5 -55
  29. package/dist/cjs/types.js.map +1 -1
  30. package/dist/esm/client/http.js +9 -0
  31. package/dist/esm/client/http.js.map +1 -0
  32. package/dist/esm/client/rest/address.js +20 -28
  33. package/dist/esm/client/rest/address.js.map +1 -1
  34. package/dist/esm/client/rest/block.js +23 -22
  35. package/dist/esm/client/rest/block.js.map +1 -1
  36. package/dist/esm/client/rest/index.js +31 -84
  37. package/dist/esm/client/rest/index.js.map +1 -1
  38. package/dist/esm/client/rest/protocol.js +129 -0
  39. package/dist/esm/client/rest/protocol.js.map +1 -0
  40. package/dist/esm/client/rest/transaction.js +19 -21
  41. package/dist/esm/client/rest/transaction.js.map +1 -1
  42. package/dist/esm/client/rpc/index.js +79 -337
  43. package/dist/esm/client/rpc/index.js.map +1 -1
  44. package/dist/esm/client/rpc/interface.js +1 -6
  45. package/dist/esm/client/rpc/interface.js.map +1 -1
  46. package/dist/esm/client/rpc/json-rpc.js +40 -63
  47. package/dist/esm/client/rpc/json-rpc.js.map +1 -1
  48. package/dist/esm/client/rpc/protocol.js +134 -0
  49. package/dist/esm/client/rpc/protocol.js.map +1 -0
  50. package/dist/esm/connection.js +104 -0
  51. package/dist/esm/connection.js.map +1 -0
  52. package/dist/esm/constants.js +16 -5
  53. package/dist/esm/constants.js.map +1 -1
  54. package/dist/esm/errors.js.map +1 -1
  55. package/dist/esm/index.js +21 -13
  56. package/dist/esm/index.js.map +1 -1
  57. package/dist/esm/types.js +5 -55
  58. package/dist/esm/types.js.map +1 -1
  59. package/dist/types/client/http.d.ts +22 -0
  60. package/dist/types/client/http.d.ts.map +1 -0
  61. package/dist/types/client/rest/address.d.ts +17 -19
  62. package/dist/types/client/rest/address.d.ts.map +1 -1
  63. package/dist/types/client/rest/block.d.ts +25 -16
  64. package/dist/types/client/rest/block.d.ts.map +1 -1
  65. package/dist/types/client/rest/index.d.ts +21 -51
  66. package/dist/types/client/rest/index.d.ts.map +1 -1
  67. package/dist/types/client/rest/protocol.d.ts +59 -0
  68. package/dist/types/client/rest/protocol.d.ts.map +1 -0
  69. package/dist/types/client/rest/transaction.d.ts +17 -17
  70. package/dist/types/client/rest/transaction.d.ts.map +1 -1
  71. package/dist/types/client/rpc/index.d.ts +112 -253
  72. package/dist/types/client/rpc/index.d.ts.map +1 -1
  73. package/dist/types/client/rpc/interface.d.ts +42 -6
  74. package/dist/types/client/rpc/interface.d.ts.map +1 -1
  75. package/dist/types/client/rpc/json-rpc.d.ts +21 -18
  76. package/dist/types/client/rpc/json-rpc.d.ts.map +1 -1
  77. package/dist/types/client/rpc/protocol.d.ts +83 -0
  78. package/dist/types/client/rpc/protocol.d.ts.map +1 -0
  79. package/dist/types/client/utils.d.ts +1 -0
  80. package/dist/types/connection.d.ts +82 -0
  81. package/dist/types/connection.d.ts.map +1 -0
  82. package/dist/types/constants.d.ts +17 -5
  83. package/dist/types/constants.d.ts.map +1 -1
  84. package/dist/types/errors.d.ts +5 -3
  85. package/dist/types/errors.d.ts.map +1 -1
  86. package/dist/types/index.d.ts +21 -13
  87. package/dist/types/index.d.ts.map +1 -1
  88. package/dist/types/network.d.ts +1 -0
  89. package/dist/types/types.d.ts +99 -696
  90. package/dist/types/types.d.ts.map +1 -1
  91. package/package.json +3 -3
  92. package/src/client/http.ts +28 -0
  93. package/src/client/rest/address.ts +24 -30
  94. package/src/client/rest/block.ts +29 -26
  95. package/src/client/rest/index.ts +38 -94
  96. package/src/client/rest/protocol.ts +153 -0
  97. package/src/client/rest/transaction.ts +23 -23
  98. package/src/client/rpc/index.ts +146 -379
  99. package/src/client/rpc/interface.ts +88 -53
  100. package/src/client/rpc/json-rpc.ts +53 -60
  101. package/src/client/rpc/protocol.ts +160 -0
  102. package/src/connection.ts +136 -0
  103. package/src/constants.ts +17 -5
  104. package/src/errors.ts +10 -3
  105. package/src/index.ts +81 -13
  106. package/src/types.ts +332 -972
  107. package/dist/cjs/bitcoin.js +0 -105
  108. package/dist/cjs/bitcoin.js.map +0 -1
  109. package/dist/esm/bitcoin.js +0 -105
  110. package/dist/esm/bitcoin.js.map +0 -1
  111. package/dist/types/bitcoin.d.ts +0 -60
  112. package/dist/types/bitcoin.d.ts.map +0 -1
  113. package/src/bitcoin.ts +0 -149
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Default executor backed by the global `fetch` function.
3
+ */
4
+ export const defaultHttpExecutor = (req) => fetch(req.url, {
5
+ method: req.method,
6
+ headers: req.headers,
7
+ body: req.body,
8
+ });
9
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/client/http.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAiB,CAAC,GAAgB,EAAqB,EAAE,CACvF,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;IACb,MAAM,EAAI,GAAG,CAAC,MAAM;IACpB,OAAO,EAAG,GAAG,CAAC,OAAO;IACrB,IAAI,EAAM,GAAG,CAAC,IAAI;CACnB,CAAC,CAAC"}
@@ -1,28 +1,27 @@
1
1
  /**
2
- * Implements a strongly-typed BitcoinRest to connect to remote bitcoin node via REST API for address-related operations.
3
- * @class BitcoinAddress
4
- * @type {BitcoinAddress}
2
+ * Address-related Esplora REST API operations.
5
3
  */
6
4
  export class BitcoinAddress {
7
- api;
8
- constructor(api) {
9
- this.api = api;
5
+ protocol;
6
+ exec;
7
+ constructor(protocol, exec) {
8
+ this.protocol = protocol;
9
+ this.exec = exec;
10
10
  }
11
11
  /**
12
12
  * Get transaction history for the specified address/scripthash, sorted with newest first.
13
13
  * Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
14
14
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-addressaddresstxs | Esplora GET /address/:address/txs } for details.
15
15
  * @param {string} addressOrScripthash The address or scripthash to check.
16
- * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
16
+ * @returns {Promise<Array<RawTransactionRest>>} Transaction history.
17
17
  */
18
18
  async getTxs(addressOrScripthash) {
19
- return await this.api({ path: `/address/${addressOrScripthash}/txs` });
19
+ return await this.exec(this.protocol.getAddressTxs(addressOrScripthash));
20
20
  }
21
21
  /**
22
- * Calls getAddressTxs and checks if any funds come back.
23
- * Toggle if those funds are confirmed.
22
+ * Checks if an address has any confirmed funded transactions.
24
23
  * @param {string} addressOrScripthash The address or scripthash to check.
25
- * @returns {Promise<boolean>} True if the address has any funds, false otherwise.
24
+ * @returns {Promise<boolean>} True if the address has confirmed funds.
26
25
  */
27
26
  async isFundedAddress(addressOrScripthash) {
28
27
  const txs = await this.getConfirmedTxs(addressOrScripthash);
@@ -33,44 +32,37 @@ export class BitcoinAddress {
33
32
  * Get unconfirmed transaction history for the specified address/scripthash.
34
33
  * Returns up to 50 transactions (no paging).
35
34
  * @param {string} addressOrScripthash The address or scripthash to check.
36
- * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
35
+ * @returns {Promise<Array<RawTransactionRest>>} Unconfirmed transactions.
37
36
  */
38
37
  async getTxsMempool(addressOrScripthash) {
39
- return await this.api({ path: `/address/${addressOrScripthash}/txs/mempool` });
38
+ return await this.exec(this.protocol.getAddressTxsMempool(addressOrScripthash));
40
39
  }
41
40
  /**
42
41
  * Get information about an address/scripthash.
43
- * Available fields: address/scripthash, chain_stats and mempool_stats.
44
- * {chain,mempool}_stats each contain an object with tx_count, funded_txo_count, funded_txo_sum, spent_txo_count and spent_txo_sum.
45
- * Elements-based chains don't have the {funded,spent}_txo_sum fields.
46
42
  * @param {string} addressOrScripthash The address or scripthash to check.
47
- * @returns {Promise<AddressInfo>} A promise resolving to an {@link AddressInfo} object.
43
+ * @returns {Promise<AddressInfo>} Address information.
48
44
  */
49
45
  async getInfo(addressOrScripthash) {
50
- return await this.api({ path: `/address/${addressOrScripthash}` });
46
+ return await this.exec(this.protocol.getAddressInfo(addressOrScripthash));
51
47
  }
52
48
  /**
53
49
  * Get confirmed transaction history for the specified address/scripthash, sorted with newest first.
54
- * Returns 25 transactions per page. More can be requested by specifying the last txid seen by the previous query.
50
+ * Returns 25 transactions per page.
55
51
  * @param {string} addressOrScripthash The address or scripthash to check.
56
- * @param lastSeenTxId The last transaction id seen by the previous query (optional).
57
- * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
52
+ * @param {string} [lastSeenTxId] The last transaction id seen by the previous query for pagination.
53
+ * @returns {Promise<Array<RawTransactionRest>>} Confirmed transactions.
58
54
  */
59
55
  async getConfirmedTxs(addressOrScripthash, lastSeenTxId) {
60
- return await this.api({
61
- path: lastSeenTxId
62
- ? `/address/${addressOrScripthash}/txs/chain/${lastSeenTxId}`
63
- : `/address/${addressOrScripthash}/txs/chain`
64
- });
56
+ return await this.exec(this.protocol.getAddressTxsChain(addressOrScripthash, lastSeenTxId));
65
57
  }
66
58
  /**
67
59
  * Get the list of unspent transaction outputs associated with the address/scripthash.
68
60
  * See {@link https://github.com/Blockstream/esplora/blob/master/API.md#get-addressaddressutxo | Esplora GET /address/:address/utxo } for details.
69
61
  * @param {string} addressOrScripthash The address or scripthash to check.
70
- * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
62
+ * @returns {Promise<Array<AddressUtxo>>} Unspent transaction outputs.
71
63
  */
72
64
  async getUtxos(addressOrScripthash) {
73
- return await this.api({ path: `/address/${addressOrScripthash}/utxo` });
65
+ return await this.exec(this.protocol.getAddressUtxos(addressOrScripthash));
74
66
  }
75
67
  }
76
68
  //# sourceMappingURL=address.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"address.js","sourceRoot":"","sources":["../../../../src/client/rest/address.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,cAAc;IACjB,GAAG,CAA8C;IAEzD,YAAY,GAAgD;QAC1D,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,MAAM,CAAC,mBAA2B;QAC7C,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,YAAY,mBAAmB,MAAM,EAAE,CAAC,CAAC;IACzE,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,eAAe,CAAC,mBAA2B;QACtD,MAAM,GAAG,GAAG,MAAM,IAAI,CA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AC,eAAe,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAC7E,CAAC;CACF"}
@@ -1,50 +1,51 @@
1
1
  import { BitcoinRestError } from '../../errors.js';
2
2
  /**
3
- * Implements a strongly-typed BitcoinRest to connect to remote bitcoin node via REST API for block-related operations.
4
- * @class BitcoinBlock
5
- * @type {BitcoinBlock}
3
+ * Block-related Esplora REST API operations.
4
+ *
5
+ * Note: The Esplora API always returns the same block format regardless
6
+ * of any "verbosity" setting (unlike Bitcoin Core RPC). Use the RPC
7
+ * client if you need verbosity-controlled block responses.
6
8
  */
7
9
  export class BitcoinBlock {
8
- api;
9
- constructor(api) {
10
- this.api = api;
10
+ protocol;
11
+ exec;
12
+ constructor(protocol, exec) {
13
+ this.protocol = protocol;
14
+ this.exec = exec;
11
15
  }
12
16
  /**
13
- * Returns the blockheight of the most-work fully-validated chain. The genesis block has height 0.
14
- * @returns {Blockheight} The number of the blockheight with the most-work of the fully-validated chain.
17
+ * Returns the blockheight of the most-work fully-validated chain.
18
+ * @returns {Promise<number>} The current block height.
15
19
  */
16
20
  async count() {
17
- return await this.api({ path: '/blocks/tip/height' });
21
+ return await this.exec(this.protocol.getBlockTipHeight());
18
22
  }
19
23
  /**
20
- * Returns the block data associated with a `blockhash` of a valid block.
21
- * @param {GetBlockParams} params See {@link GetBlockParams} for details.
22
- * @param {?string} params.blockhash The blockhash of the block to query.
23
- * @param {?number} params.height The block height of the block to query.
24
- * @returns {BlockResponse} A promise resolving to a {@link BlockResponse} formatted depending on `verbosity` level.
25
- * @throws {BitcoinRpcError} If neither `blockhash` nor `height` is provided.
24
+ * Returns the Esplora block data for a given blockhash or height.
25
+ * @param {object} params The block hash or height.
26
+ * @param {string} [params.blockhash] The blockhash of the block to query.
27
+ * @param {number} [params.height] The block height of the block to query.
28
+ * @returns {Promise<EsploraBlock | undefined>} The block data.
29
+ * @throws {BitcoinRestError} If neither blockhash nor height is provided.
26
30
  */
27
31
  async get({ blockhash, height }) {
28
- // Check if blockhash or height is provided, if neither throw an error
29
32
  if (!blockhash && height === undefined) {
30
- throw new BitcoinRestError('INVALID_PARAMS_GET_BLOCK: blockhash or height required', { blockhash, height });
33
+ throw new BitcoinRestError('blockhash or height required', { blockhash, height });
31
34
  }
32
- // If height is provided, get the blockhash
33
35
  blockhash ??= await this.getHash(height);
34
36
  if (!blockhash || typeof blockhash !== 'string') {
35
37
  return undefined;
36
38
  }
37
- // Get the block data
38
- return await this.api({ path: `/block/${blockhash}` });
39
+ return await this.exec(this.protocol.getBlock(blockhash));
39
40
  }
40
41
  /**
41
42
  * Get the block hash for a given block height.
42
43
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-block-heightheight | Esplora GET /block-height/:height } for details.
43
44
  * @param {number} height The block height (required).
44
- * @returns {Promise<string>} The hash of the block currently at height..
45
+ * @returns {Promise<string>} The hash of the block at the given height.
45
46
  */
46
47
  async getHash(height) {
47
- return await this.api({ path: `/block-height/${height}` });
48
+ return await this.exec(this.protocol.getBlockHeight(height));
48
49
  }
49
50
  }
50
51
  //# sourceMappingURL=block.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"block.js","sourceRoot":"","sources":["../../../../src/client/rest/block.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGnD;;;;GAIG;AACH,MAAM,OAAO,YAAY;IACf,GAAG,CAA8C;IAEzD,YAAY,GAAgD;QAC1D,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,KAAK;QAChB,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,oBAAoB,EAAE,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,GAAG,CAAC,EAAE,SAAS,EAAE,MAAM,EAAkB;QACpD,sEAAsE;QACtE,IAAG,CAAC,SAAS,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACtC,MAAM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EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;QACpF,CAAC;QAED,SAAS,KAAK,MAAM,IAAI,CAAC,OAAO,CAAC,MAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,SAAS,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;YAChD,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAiB,CAAC;IAC5E,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,OAAO,CAAC,MAAc;QACjC,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/D,CAAC;CACF"}
@@ -1,107 +1,54 @@
1
- import { JSONUtils, MethodError, StringUtils } from '@did-btcr2/common';
2
- import { DEFAULT_BITCOIN_NETWORK_CONFIG } from '../../constants.js';
1
+ import { MethodError } from '@did-btcr2/common';
3
2
  import { BitcoinAddress } from './address.js';
4
3
  import { BitcoinBlock } from './block.js';
5
4
  import { BitcoinTransaction } from './transaction.js';
6
- import { RestClientConfig } from '../../types.js';
5
+ import { EsploraProtocol } from './protocol.js';
6
+ import { defaultHttpExecutor } from '../http.js';
7
7
  /**
8
- * Implements a strongly-typed BitcoinRestClient to connect to remote bitcoin node via REST API.
9
- * @class BitcoinRestClient
10
- * @type {BitcoinRestClient}
8
+ * Esplora REST API client for Bitcoin.
9
+ *
10
+ * Wraps the sans-I/O {@link EsploraProtocol} with an {@link HttpExecutor}
11
+ * for convenience. Users who want full control over I/O can access the
12
+ * protocol layer directly via the {@link protocol} property.
11
13
  */
12
14
  export class BitcoinRestClient {
13
- /**
14
- * The encapsulated {@link RestClientConfig} object.
15
- * @private
16
- * @type {RestClientConfig}
17
- */
18
15
  _config;
19
16
  /**
20
- * The api calls related to bitcoin transactions.
21
- * @type {BitcoinTransaction}
17
+ * The sans-I/O protocol layer. Use this to build {@link HttpRequest}
18
+ * descriptors without performing any I/O.
22
19
  */
20
+ protocol;
21
+ executor;
22
+ /** Transaction-related API calls. */
23
23
  transaction;
24
- /**
25
- * The api calls related to bitcoin blocks.
26
- * @type {BitcoinBlock}
27
- */
24
+ /** Block-related API calls. */
28
25
  block;
29
- /**
30
- * The api calls related to bitcoin addresses.
31
- * @type {BitcoinAddress}
32
- */
26
+ /** Address-related API calls. */
33
27
  address;
34
- /**
35
- * The API call method that can be used to make requests to the REST API.
36
- * @returns {Promise<any>} A promise resolving to the response data.
37
- */
38
- api = this.call;
39
- constructor(config) {
40
- this._config = new RestClientConfig(config);
41
- this.api = this.call.bind(this);
42
- this.transaction = new BitcoinTransaction(this.api);
43
- this.block = new BitcoinBlock(this.api);
44
- this.address = new BitcoinAddress(this.api);
28
+ constructor(config, executor) {
29
+ this._config = config;
30
+ this.protocol = new EsploraProtocol(config);
31
+ this.executor = executor ?? defaultHttpExecutor;
32
+ const exec = this.executeRequest.bind(this);
33
+ this.transaction = new BitcoinTransaction(this.protocol, exec);
34
+ this.block = new BitcoinBlock(this.protocol, exec);
35
+ this.address = new BitcoinAddress(this.protocol, exec);
45
36
  }
46
- /**
47
- * Get the configuration object.
48
- * @private
49
- */
50
37
  get config() {
51
- const config = this._config;
52
- return config;
38
+ return this._config;
53
39
  }
54
40
  /**
55
- * Static method connects to a bitcoin node running a esplora REST API.
56
- *
57
- * @param {RestClientConfig} config The configuration object for the client (optional).
58
- * @returns {BitcoinRestClient} A new {@link BitcoinRestClient} instance.
59
- * @example
60
- * ```
61
- * const rest = BitcoinRestClient.connect();
62
- * ```
41
+ * Execute an {@link HttpRequest} built by the protocol layer,
42
+ * parse the response, and throw on HTTP errors.
63
43
  */
64
- static connect(config) {
65
- return new BitcoinRestClient(config ?? DEFAULT_BITCOIN_NETWORK_CONFIG.regtest.rest);
66
- }
67
- /**
68
- * Make a REST API call to the configured Bitcoin node.
69
- * @private
70
- * @param {RestApiCallParams} params The parameters for the API call. See {@link RestApiCallParams} for details.
71
- * @param {string} [params.path] The path to the API endpoint (required).
72
- * @param {string} [params.url] The full URL to the API endpoint (optional).
73
- * @param {string} [params.method] The HTTP method to use (default is 'GET').
74
- * @param {any} [params.body] The body of the request (optional).
75
- * @param {any} [params.headers] Additional headers to include in the request (optional).
76
- * @returns {Promise<any>} A promise resolving to the response data.
77
- */
78
- async call({ path, url, method, body, headers }) {
79
- // Construct the URL if not provided
80
- url ??= `${StringUtils.replaceEnd(this.config.host, '/')}${path}`;
81
- // Set the method to GET if not provided
82
- method ??= 'GET';
83
- // Construct the request options
84
- const requestInit = {
85
- method,
86
- headers: headers ?? {
87
- 'Content-Type': 'application/json',
88
- ...this.config.headers,
89
- }
90
- };
91
- // If the method is POST or PUT, add the body to the request
92
- if (body) {
93
- requestInit.body = JSONUtils.isObject(body) ? JSON.stringify(body) : body;
94
- requestInit.method = 'POST';
95
- }
96
- // Make the request
97
- const response = await fetch(url, requestInit);
98
- // Check if the response is a text/plain response
99
- const data = (response.headers.get('Content-Type') ?? 'json') === 'text/plain'
44
+ async executeRequest(request) {
45
+ const response = await this.executor(request);
46
+ const contentType = response.headers.get('Content-Type') ?? '';
47
+ const data = contentType.includes('text/plain')
100
48
  ? await response.text()
101
49
  : await response.json();
102
- // Check if the response is ok (status in the range 200-299)
103
50
  if (!response.ok) {
104
- throw new MethodError(`Request to ${url} failed: ${response.status} - ${response.statusText}`, 'FAILED_HTTP_REQUEST', { data, response });
51
+ throw new MethodError(`Request to ${request.url} failed: ${response.status} - ${response.statusText}`, 'FAILED_HTTP_REQUEST', { data });
105
52
  }
106
53
  return data;
107
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/client/rest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAE,8BAA8B,EAAE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IACtB,CAAC;IAED;;;OAGG;IACK,KAAK,CAAC,cAAc,CAAC,OAAoB;QAC/C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAE9C,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;QAC/D,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,CAAC,YAAY,CAAC;YAC7C,CAAC,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE;YACvB,CAAC,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAE1B,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,WAAW,CACnB,cAAc,OAAO,CAAC,GAAG,YAAY,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,UAAU,EAAE,EAC/E,qBAAqB,EACrB,EAAE,IAAI,EAAE,CACT,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,129 @@
1
+ import { StringUtils } from '@did-btcr2/common';
2
+ const HEX64_RE = /^[0-9a-f]{64}$/i;
3
+ /**
4
+ * Sans-I/O Esplora REST API protocol.
5
+ *
6
+ * Every method returns an {@link HttpRequest} descriptor — a plain object
7
+ * describing *what* to request — without performing any I/O. The caller
8
+ * is responsible for executing the request with an HTTP client of their
9
+ * choice and deserializing the response.
10
+ *
11
+ * This mirrors the pattern used by the Rust `esploda` crate where
12
+ * `Esplora` methods return `http::Request<()>` objects.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const protocol = new EsploraProtocol({ host: 'https://mempool.space/api' });
17
+ *
18
+ * // Build a request descriptor (no I/O)
19
+ * const req = protocol.getTx('abc123...');
20
+ *
21
+ * // Execute with any HTTP client
22
+ * const res = await fetch(req.url, req);
23
+ * const tx: RawTransactionRest = await res.json();
24
+ * ```
25
+ */
26
+ export class EsploraProtocol {
27
+ baseUrl;
28
+ defaultHeaders;
29
+ constructor(config) {
30
+ this.baseUrl = StringUtils.replaceEnd(config.host, '/');
31
+ this.defaultHeaders = {
32
+ 'Content-Type': 'application/json',
33
+ ...config.headers,
34
+ };
35
+ }
36
+ // ── validation ─────────────────────────────────────────────────────
37
+ static assertHex64(value, label) {
38
+ if (!HEX64_RE.test(value)) {
39
+ throw new Error(`Invalid ${label}: expected 64-char hex string`);
40
+ }
41
+ }
42
+ static assertAddress(value) {
43
+ if (!value || /[/\\.?#]/.test(value)) {
44
+ throw new Error('Invalid address: contains illegal characters');
45
+ }
46
+ }
47
+ // ── helpers ──────────────────────────────────────────────────────────
48
+ get(path) {
49
+ return {
50
+ url: `${this.baseUrl}${path}`,
51
+ method: 'GET',
52
+ headers: { ...this.defaultHeaders },
53
+ };
54
+ }
55
+ post(path, body, headers) {
56
+ return {
57
+ url: `${this.baseUrl}${path}`,
58
+ method: 'POST',
59
+ headers: headers ?? { ...this.defaultHeaders },
60
+ body,
61
+ };
62
+ }
63
+ // ── Transaction ──────────────────────────────────────────────────────
64
+ /** GET /tx/:txid */
65
+ getTx(txid) {
66
+ EsploraProtocol.assertHex64(txid, 'txid');
67
+ return this.get(`/tx/${txid}`);
68
+ }
69
+ /** GET /tx/:txid/hex */
70
+ getTxHex(txid) {
71
+ EsploraProtocol.assertHex64(txid, 'txid');
72
+ return this.get(`/tx/${txid}/hex`);
73
+ }
74
+ /** GET /tx/:txid/raw */
75
+ getTxRaw(txid) {
76
+ EsploraProtocol.assertHex64(txid, 'txid');
77
+ return this.get(`/tx/${txid}/raw`);
78
+ }
79
+ /** POST /tx */
80
+ postTx(hex) {
81
+ return this.post('/tx', hex, { 'Content-Type': 'text/plain' });
82
+ }
83
+ // ── Block ────────────────────────────────────────────────────────────
84
+ /** GET /blocks/tip/height */
85
+ getBlockTipHeight() {
86
+ return this.get('/blocks/tip/height');
87
+ }
88
+ /** GET /block/:blockhash */
89
+ getBlock(blockhash) {
90
+ EsploraProtocol.assertHex64(blockhash, 'blockhash');
91
+ return this.get(`/block/${blockhash}`);
92
+ }
93
+ /** GET /block-height/:height */
94
+ getBlockHeight(height) {
95
+ return this.get(`/block-height/${height}`);
96
+ }
97
+ // ── Address ──────────────────────────────────────────────────────────
98
+ /** GET /address/:address/txs */
99
+ getAddressTxs(address) {
100
+ EsploraProtocol.assertAddress(address);
101
+ return this.get(`/address/${address}/txs`);
102
+ }
103
+ /** GET /address/:address/txs/mempool */
104
+ getAddressTxsMempool(address) {
105
+ EsploraProtocol.assertAddress(address);
106
+ return this.get(`/address/${address}/txs/mempool`);
107
+ }
108
+ /** GET /address/:address/txs/chain[/:last_seen_txid] */
109
+ getAddressTxsChain(address, lastSeenTxId) {
110
+ EsploraProtocol.assertAddress(address);
111
+ if (lastSeenTxId)
112
+ EsploraProtocol.assertHex64(lastSeenTxId, 'lastSeenTxId');
113
+ const path = lastSeenTxId
114
+ ? `/address/${address}/txs/chain/${lastSeenTxId}`
115
+ : `/address/${address}/txs/chain`;
116
+ return this.get(path);
117
+ }
118
+ /** GET /address/:address */
119
+ getAddressInfo(address) {
120
+ EsploraProtocol.assertAddress(address);
121
+ return this.get(`/address/${address}`);
122
+ }
123
+ /** GET /address/:address/utxo */
124
+ getAddressUtxos(address) {
125
+ EsploraProtocol.assertAddress(address);
126
+ return this.get(`/address/${address}/utxo`);
127
+ }
128
+ }
129
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../../src/client/rest/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,MAAM,QAAQ,GAAG,iBAAiB,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,eAAe;IACT,OAAO,CAAS;IAChB,cAAc,CAAyB;IAExD,YAAY,MAAkB;QAC5B,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,UAAU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rD,CAAC;IAED,wDAAwD;IACxD,kBAAkB,CAAC,OAAe,EAAE,YAAqB;QACvD,eAAe,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACvC,IAAI,YAAY;YAAE,eAAe,CAAC,WAAW,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;QAC5E,MAAM,IAAI,GAAG,YAAY;YACvB,CAAC,CAAC,YAAY,OAAO,cAAc,YAAY,EAAE;YACjD,CAAC,CAAC,YAAY,OAAO,YAAY,CAAC;QACpC,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,4BAA4B;IAC5B,cAAc,CAAC,OAAe;QAC5B,eAAe,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,OAAO,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,iCAAiC;IACjC,eAAe,CAAC,OAAe;QAC7B,eAAe,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,OAAO,OAAO,CAAC,CAAC;IAC9C,CAAC;CACF"}
@@ -1,56 +1,54 @@
1
1
  export class BitcoinTransaction {
2
- api;
3
- constructor(api) {
4
- this.api = api;
2
+ protocol;
3
+ exec;
4
+ constructor(protocol, exec) {
5
+ this.protocol = protocol;
6
+ this.exec = exec;
5
7
  }
6
8
  /**
7
9
  * Returns the transaction in JSON format.
8
10
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
9
11
  * @param {string} txid The transaction id (required).
10
- * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
12
+ * @returns {Promise<RawTransactionRest>} A promise resolving to data about a transaction.
11
13
  */
12
14
  async get(txid) {
13
- return await this.api({ path: `/tx/${txid}` });
15
+ return await this.exec(this.protocol.getTx(txid));
14
16
  }
15
17
  /**
16
- * Returns the transaction in JSON format.
17
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
18
+ * Checks if a transaction is confirmed.
18
19
  * @param {string} txid The transaction id (required).
19
- * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
20
+ * @returns {Promise<boolean>} True if the transaction is confirmed.
20
21
  */
21
22
  async isConfirmed(txid) {
22
23
  const tx = await this.get(txid);
23
24
  return tx.status.confirmed;
24
25
  }
25
26
  /**
26
- * Returns the raw transaction in hex or as binary data.
27
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
28
- * {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
27
+ * Returns the raw transaction as a hex string.
28
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } for details.
29
29
  * @param {string} txid The transaction id (required).
30
- * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
30
+ * @returns {Promise<string>} A promise resolving to the raw transaction hex.
31
31
  */
32
32
  async getHex(txid) {
33
- return await this.api({ path: `/tx/${txid}/hex` });
33
+ return await this.exec(this.protocol.getTxHex(txid));
34
34
  }
35
35
  /**
36
- * Returns the raw transaction in hex or as binary data.
37
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
38
- * {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
36
+ * Returns the raw transaction as binary data.
37
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
39
38
  * @param {string} txid The transaction id (required).
40
- * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
39
+ * @returns {Promise<Bytes>} A promise resolving to the raw transaction bytes.
41
40
  */
42
41
  async getRaw(txid) {
43
- return await this.api({ path: `/tx/${txid}/raw` });
42
+ return await this.exec(this.protocol.getTxRaw(txid));
44
43
  }
45
44
  /**
46
- * Broadcast a raw transaction to the network. The transaction should be provided as hex in the request body. The txid
47
- * will be returned on success.
45
+ * Broadcast a raw transaction to the network.
48
46
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#post-tx | Esplora POST /tx } for details.
49
47
  * @param {string} tx The raw transaction in hex format (required).
50
48
  * @returns {Promise<string>} The transaction id of the broadcasted transaction.
51
49
  */
52
50
  async send(tx) {
53
- return await this.api({ path: '/tx', method: 'POST', body: tx, headers: { 'Content-Type': 'text/plain' } });
51
+ return await this.exec(this.protocol.postTx(tx));
54
52
  }
55
53
  }
56
54
  //# sourceMappingURL=transaction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../../../src/client/rest/transaction.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,kBAAkB;IACrB,GAAG,CAA8C;IAEzD,YAAY,GAAgD;QAC1D,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,GAAG,CAAC,IAAY;QAC3B,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,IAAI,EAAE,EAAE,CAAC,CAAC;IACjD,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,WAAW,CAAC,IAAY;QACnC,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAChC,OAAO,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,MAAM,CAAC,IAAY;QAC9B,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,IAAI,MAAM,EAAE,CAAC,CAAC;IACrD,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,MAAM,CAAC,IAAY;QAC9B,OAAO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