@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
@@ -1,28 +1,31 @@
1
1
  import { Bytes } from '@did-btcr2/common';
2
- import { RawTransactionRest, RestApiCallParams } from '../../types.js';
2
+ import { RawTransactionRest } from '../../types.js';
3
+ import { HttpRequest } from '../http.js';
4
+ import { EsploraProtocol } from './protocol.js';
3
5
 
4
6
  export class BitcoinTransaction {
5
- private api: (params: RestApiCallParams) => Promise<any>;
7
+ private readonly protocol: EsploraProtocol;
8
+ private readonly exec: (req: HttpRequest) => Promise<any>;
6
9
 
7
- constructor(api: (params: RestApiCallParams) => Promise<any>) {
8
- this.api = api;
10
+ constructor(protocol: EsploraProtocol, exec: (req: HttpRequest) => Promise<any>) {
11
+ this.protocol = protocol;
12
+ this.exec = exec;
9
13
  }
10
14
 
11
15
  /**
12
16
  * Returns the transaction in JSON format.
13
17
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
14
18
  * @param {string} txid The transaction id (required).
15
- * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
19
+ * @returns {Promise<RawTransactionRest>} A promise resolving to data about a transaction.
16
20
  */
17
21
  public async get(txid: string): Promise<RawTransactionRest> {
18
- return await this.api({ path: `/tx/${txid}` });
22
+ return await this.exec(this.protocol.getTx(txid));
19
23
  }
20
24
 
21
25
  /**
22
- * Returns the transaction in JSON format.
23
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
26
+ * Checks if a transaction is confirmed.
24
27
  * @param {string} txid The transaction id (required).
25
- * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
28
+ * @returns {Promise<boolean>} True if the transaction is confirmed.
26
29
  */
27
30
  public async isConfirmed(txid: string): Promise<boolean> {
28
31
  const tx = await this.get(txid);
@@ -30,35 +33,32 @@ export class BitcoinTransaction {
30
33
  }
31
34
 
32
35
  /**
33
- * Returns the raw transaction in hex or as binary data.
34
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
35
- * {@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 a hex string.
37
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } for details.
36
38
  * @param {string} txid The transaction id (required).
37
- * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
39
+ * @returns {Promise<string>} A promise resolving to the raw transaction hex.
38
40
  */
39
41
  public async getHex(txid: string): Promise<string> {
40
- return await this.api({ path: `/tx/${txid}/hex` });
42
+ return await this.exec(this.protocol.getTxHex(txid));
41
43
  }
42
44
 
43
45
  /**
44
- * Returns the raw transaction in hex or as binary data.
45
- * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
46
- * {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
46
+ * Returns the raw transaction as binary data.
47
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
47
48
  * @param {string} txid The transaction id (required).
48
- * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
49
+ * @returns {Promise<Bytes>} A promise resolving to the raw transaction bytes.
49
50
  */
50
51
  public async getRaw(txid: string): Promise<Bytes> {
51
- return await this.api({ path: `/tx/${txid}/raw` });
52
+ return await this.exec(this.protocol.getTxRaw(txid));
52
53
  }
53
54
 
54
55
  /**
55
- * Broadcast a raw transaction to the network. The transaction should be provided as hex in the request body. The txid
56
- * will be returned on success.
56
+ * Broadcast a raw transaction to the network.
57
57
  * See {@link https://github.com/blockstream/esplora/blob/master/API.md#post-tx | Esplora POST /tx } for details.
58
58
  * @param {string} tx The raw transaction in hex format (required).
59
59
  * @returns {Promise<string>} The transaction id of the broadcasted transaction.
60
60
  */
61
61
  public async send(tx: string): Promise<string> {
62
- return await this.api({ path: '/tx', method: 'POST', body: tx, headers: { 'Content-Type': 'text/plain' } });
62
+ return await this.exec(this.protocol.postTx(tx));
63
63
  }
64
- }
64
+ }