@teleportdao/bitcoin 1.7.2 → 1.7.6

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 (52) hide show
  1. package/dist/bitcoin-base.d.ts +93 -0
  2. package/dist/bitcoin-base.d.ts.map +1 -0
  3. package/dist/bitcoin-base.js +236 -0
  4. package/dist/bitcoin-base.js.map +1 -0
  5. package/dist/bitcoin-interface-teleswap.d.ts +6 -1
  6. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-teleswap.js +2 -4
  8. package/dist/bitcoin-interface-teleswap.js.map +1 -1
  9. package/dist/helper/burn-request-helper.d.ts +7 -0
  10. package/dist/helper/burn-request-helper.d.ts.map +1 -0
  11. package/dist/helper/burn-request-helper.js +26 -0
  12. package/dist/helper/burn-request-helper.js.map +1 -0
  13. package/dist/helper/teleport-request-helper.d.ts +47 -0
  14. package/dist/helper/teleport-request-helper.d.ts.map +1 -0
  15. package/dist/helper/teleport-request-helper.js +146 -0
  16. package/dist/helper/teleport-request-helper.js.map +1 -0
  17. package/dist/helper/teleswap-helper.d.ts +20 -8
  18. package/dist/helper/teleswap-helper.d.ts.map +1 -1
  19. package/dist/helper/teleswap-helper.js +55 -50
  20. package/dist/helper/teleswap-helper.js.map +1 -1
  21. package/dist/teleport-dao-payments.d.ts +76 -0
  22. package/dist/teleport-dao-payments.d.ts.map +1 -0
  23. package/dist/teleport-dao-payments.js +217 -0
  24. package/dist/teleport-dao-payments.js.map +1 -0
  25. package/dist/teleswap-wallet.d.ts +4 -6
  26. package/dist/teleswap-wallet.d.ts.map +1 -1
  27. package/dist/teleswap-wallet.js +7 -8
  28. package/dist/teleswap-wallet.js.map +1 -1
  29. package/package.json +5 -5
  30. package/src/bitcoin-interface-ordinal.ts +181 -181
  31. package/src/bitcoin-interface-teleswap.ts +252 -255
  32. package/src/bitcoin-interface-utils.ts +59 -59
  33. package/src/bitcoin-interface.ts +247 -247
  34. package/src/bitcoin-utils.ts +591 -591
  35. package/src/bitcoin-wallet-base.ts +314 -314
  36. package/src/helper/brc20-helper.ts +181 -181
  37. package/src/helper/ordinal-helper.ts +118 -118
  38. package/src/helper/teleswap-helper.ts +77 -101
  39. package/src/index.ts +15 -15
  40. package/src/ordinal-wallet.ts +738 -738
  41. package/src/sign/index.ts +1 -1
  42. package/src/sign/sign-transaction.ts +108 -108
  43. package/src/teleswap-wallet.ts +152 -155
  44. package/src/transaction-builder/bitcoin-transaction-builder.ts +44 -44
  45. package/src/transaction-builder/index.ts +3 -3
  46. package/src/transaction-builder/ordinal-transaction-builder.ts +147 -147
  47. package/src/transaction-builder/transaction-builder.ts +705 -705
  48. package/src/type.ts +43 -43
  49. package/src/utils/networks.ts +33 -33
  50. package/src/utils/tools.ts +89 -89
  51. package/tsconfig.json +9 -9
  52. package/webpack.config.js +16 -16
@@ -1,247 +1,247 @@
1
- import { bitcoin as bitcoinProvider } from "@teleportdao/providers"
2
- import { runWithRetries } from "./utils/tools"
3
- import {
4
- parseRawTransaction,
5
- calculateMerkleProof,
6
- extractTransactionsAndBlockInfoFromRawBlock,
7
- } from "./bitcoin-utils"
8
- import { BitcoinInterfaceUtils } from "./bitcoin-interface-utils"
9
- import type { BitcoinConnectionInfo } from "./type"
10
- import type { SignerInfo } from "./transaction-builder/transaction-builder"
11
-
12
- export class BitcoinInterface extends BitcoinInterfaceUtils {
13
- rpcProvider?: bitcoinProvider.BitcoinRPC
14
- utxoProvider: bitcoinProvider.Types.UtxoProvider
15
- apiProvider: bitcoinProvider.MempoolSpace
16
-
17
- constructor(connectionInfo: BitcoinConnectionInfo, networkName: string) {
18
- super(networkName)
19
- if (connectionInfo.api.provider === "BlockStream") {
20
- this.apiProvider = new bitcoinProvider.BlockStream(this.testnet)
21
- } else {
22
- this.apiProvider = new bitcoinProvider.MempoolSpace(this.testnet)
23
- }
24
- if (connectionInfo.rpc?.enabled) {
25
- this.rpcProvider = bitcoinProvider.getRpcProvider(connectionInfo.rpc)
26
- }
27
- this.utxoProvider = bitcoinProvider.getUtxoProvider(connectionInfo.api as any, networkName)!
28
- }
29
-
30
- async getFeeRate(speed?: "normal" | "slow" | "fast") {
31
- return new bitcoinProvider.MempoolSpace(this.testnet).getRecommendedFeeRate(speed)
32
- }
33
-
34
- async getLatestBlockNumber(): Promise<number> {
35
- let latestHeight = await (this.rpcProvider || this.apiProvider)!.getLatestBlockNumber()
36
- return latestHeight
37
- }
38
-
39
- async getBlockHash(blockNumber: number): Promise<string> {
40
- let headerHash = await runWithRetries(() =>
41
- (this.rpcProvider || this.apiProvider)!.getBlockHash(blockNumber),
42
- )
43
- return headerHash
44
- }
45
-
46
- async getBlockHeaderHex(blockNumber: number): Promise<string> {
47
- let headerHex = await runWithRetries(() =>
48
- (this.rpcProvider || this.apiProvider)!.getBlockHeaderHex(blockNumber),
49
- )
50
- return headerHex
51
- }
52
-
53
- async getTransaction(txId: string) {
54
- return (this.rpcProvider || this.apiProvider)!.getTransaction(txId)
55
- }
56
-
57
- async getRawTransaction(txId: string) {
58
- return (this.rpcProvider || this.apiProvider)!.getRawTransaction(txId)
59
- }
60
-
61
- async getMerkleProof(
62
- txId: string,
63
- blockHash: string,
64
- ): Promise<{
65
- intermediateNodes: string
66
- transactionIndex: number
67
- }> {
68
- if (!this.rpcProvider) {
69
- return this.apiProvider.getMerkleProof(txId)
70
- }
71
- let txIds = await runWithRetries(() => this.rpcProvider!.getBlockTransactionIds(blockHash), {
72
- maxTries: 10,
73
- retrySleep: 2000,
74
- })
75
- let proof = calculateMerkleProof(txIds, txId)
76
- return proof
77
- }
78
-
79
- async getRequestProof(transaction: {
80
- txId: string
81
- hex?: string
82
- blockHash?: string
83
- blockNumber?: number
84
- merkleProof?: {
85
- intermediateNodes: string
86
- transactionIndex: number
87
- }
88
- }) {
89
- let transactionHex = transaction.hex || (await this.getRawTransaction(transaction.txId))
90
-
91
- let blockHash = transaction.blockHash
92
- let blockNumber = transaction.blockNumber
93
- if (!(transaction.blockHash && transaction.blockNumber)) {
94
- let txInfo = await this.getTransaction(transaction.txId)
95
- blockHash = txInfo.blockHash
96
- blockNumber = txInfo.blockNumber
97
- }
98
- let parsedTx = parseRawTransaction(transactionHex)
99
- let merkleProof =
100
- transaction.merkleProof || (await this.getMerkleProof(transaction.txId, blockHash!))
101
-
102
- return {
103
- parsedTx,
104
- merkleProof,
105
- blockNumber: blockNumber!,
106
- blockHash: blockHash!,
107
- }
108
- }
109
-
110
- async getUtxo(address: string) {
111
- if (!this.apiProvider) {
112
- throw new Error("api provider not set")
113
- }
114
- return this.apiProvider.getUtxos(address)
115
- }
116
-
117
- async getExtendedUtxo(signerInfo: SignerInfo) {
118
- if (!this.apiProvider) {
119
- throw new Error("api provider not set")
120
- }
121
- let utxos = await this.apiProvider.getUtxos(signerInfo.address)
122
- return utxos.map((tx: any) => ({
123
- hash: tx.txId,
124
- value: tx.value,
125
- index: tx.index,
126
- signerInfo,
127
- }))
128
- }
129
-
130
- async getAddressesUtxo(allAddresses: string[]) {
131
- if (!this.apiProvider) {
132
- throw new Error("api provider not set")
133
- }
134
- const chunkOfAddresses = []
135
- const chunkLength = 20
136
- for (let i = 0; i < allAddresses.length; i += chunkLength) {
137
- const tmp = allAddresses.slice(i, i + chunkLength)
138
- chunkOfAddresses.push(tmp)
139
- }
140
-
141
- let results = []
142
-
143
- for (let addresses of chunkOfAddresses) {
144
- const allPromises = []
145
- for (let address of addresses) {
146
- let promise = await this.getUtxo(address)
147
- allPromises.push(promise)
148
- }
149
- let result = await Promise.all(allPromises)
150
- if (result.flat(1).length === 0) {
151
- break
152
- }
153
- results.push(result.flat(1))
154
- }
155
- return results.flat(1)
156
- }
157
-
158
- async getAddressesExtendedUtxo(signerInfos: SignerInfo[]) {
159
- if (!this.apiProvider) {
160
- throw new Error("api provider not set")
161
- }
162
- const chunkOfAddresses = []
163
- const chunkLength = 20
164
- for (let i = 0; i < signerInfos.length; i += chunkLength) {
165
- const tmp = signerInfos.slice(i, i + chunkLength)
166
- chunkOfAddresses.push(tmp)
167
- }
168
-
169
- let results = []
170
-
171
- for (let addresses of chunkOfAddresses) {
172
- const allPromises = []
173
- for (let signerInfo of addresses) {
174
- let promise = await this.getExtendedUtxo(signerInfo)
175
- allPromises.push(promise)
176
- }
177
- let result = await Promise.all(allPromises)
178
- if (result.flat(1).length === 0) {
179
- break
180
- }
181
- results.push(result.flat(1))
182
- }
183
- return results.flat(1)
184
- }
185
-
186
- async getBalance(address: string) {
187
- if (!this.apiProvider) {
188
- throw new Error("this function need an api provider")
189
- }
190
- return this.apiProvider.getBalance(address)
191
- }
192
-
193
- async getBlockTransactions(
194
- addresses: string[],
195
- blockNumber: number,
196
- inputTxIds: {
197
- txId: string
198
- index: number
199
- address: string
200
- script?: string | undefined
201
- value?: number | undefined
202
- }[] = [],
203
- ) {
204
- if (!this.rpcProvider) {
205
- throw new Error("RPC provider not set")
206
- }
207
- let rawBlockHex = await runWithRetries(
208
- () => this.rpcProvider!.getBlockByBlockNumber(blockNumber, 0),
209
- {
210
- maxTries: 10,
211
- retrySleep: 2000,
212
- },
213
- )
214
- return extractTransactionsAndBlockInfoFromRawBlock(
215
- rawBlockHex,
216
- blockNumber,
217
- addresses,
218
- inputTxIds,
219
- this.network,
220
- )
221
- }
222
-
223
- // from start+1 to end
224
- async getMultipleBlocksTransactions(
225
- addresses: string[],
226
- startBlockNumber: number,
227
- endBlockNumber: number,
228
- inputTxIds: {
229
- txId: string
230
- index: number
231
- address: string
232
- script?: string | undefined
233
- value?: number | undefined
234
- }[] = [],
235
- ) {
236
- let blockTxs = []
237
- for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
238
- console.log(blockNumber)
239
- const response = await this.getBlockTransactions(addresses, blockNumber, inputTxIds)
240
- blockTxs.push(response.withdrawTxs, response.depositTxs)
241
- }
242
- blockTxs = await Promise.all(blockTxs)
243
- return blockTxs.flat(1)
244
- }
245
- }
246
-
247
- export default BitcoinInterface
1
+ import { bitcoin as bitcoinProvider } from "@teleportdao/providers"
2
+ import { runWithRetries } from "./utils/tools"
3
+ import {
4
+ parseRawTransaction,
5
+ calculateMerkleProof,
6
+ extractTransactionsAndBlockInfoFromRawBlock,
7
+ } from "./bitcoin-utils"
8
+ import { BitcoinInterfaceUtils } from "./bitcoin-interface-utils"
9
+ import type { BitcoinConnectionInfo } from "./type"
10
+ import type { SignerInfo } from "./transaction-builder/transaction-builder"
11
+
12
+ export class BitcoinInterface extends BitcoinInterfaceUtils {
13
+ rpcProvider?: bitcoinProvider.BitcoinRPC
14
+ utxoProvider: bitcoinProvider.Types.UtxoProvider
15
+ apiProvider: bitcoinProvider.MempoolSpace
16
+
17
+ constructor(connectionInfo: BitcoinConnectionInfo, networkName: string) {
18
+ super(networkName)
19
+ if (connectionInfo.api.provider === "BlockStream") {
20
+ this.apiProvider = new bitcoinProvider.BlockStream(this.testnet)
21
+ } else {
22
+ this.apiProvider = new bitcoinProvider.MempoolSpace(this.testnet)
23
+ }
24
+ if (connectionInfo.rpc?.enabled) {
25
+ this.rpcProvider = bitcoinProvider.getRpcProvider(connectionInfo.rpc)
26
+ }
27
+ this.utxoProvider = bitcoinProvider.getUtxoProvider(connectionInfo.api as any, networkName)!
28
+ }
29
+
30
+ async getFeeRate(speed?: "normal" | "slow" | "fast") {
31
+ return new bitcoinProvider.MempoolSpace(this.testnet).getRecommendedFeeRate(speed)
32
+ }
33
+
34
+ async getLatestBlockNumber(): Promise<number> {
35
+ let latestHeight = await (this.rpcProvider || this.apiProvider)!.getLatestBlockNumber()
36
+ return latestHeight
37
+ }
38
+
39
+ async getBlockHash(blockNumber: number): Promise<string> {
40
+ let headerHash = await runWithRetries(() =>
41
+ (this.rpcProvider || this.apiProvider)!.getBlockHash(blockNumber),
42
+ )
43
+ return headerHash
44
+ }
45
+
46
+ async getBlockHeaderHex(blockNumber: number): Promise<string> {
47
+ let headerHex = await runWithRetries(() =>
48
+ (this.rpcProvider || this.apiProvider)!.getBlockHeaderHex(blockNumber),
49
+ )
50
+ return headerHex
51
+ }
52
+
53
+ async getTransaction(txId: string) {
54
+ return (this.rpcProvider || this.apiProvider)!.getTransaction(txId)
55
+ }
56
+
57
+ async getRawTransaction(txId: string) {
58
+ return (this.rpcProvider || this.apiProvider)!.getRawTransaction(txId)
59
+ }
60
+
61
+ async getMerkleProof(
62
+ txId: string,
63
+ blockHash: string,
64
+ ): Promise<{
65
+ intermediateNodes: string
66
+ transactionIndex: number
67
+ }> {
68
+ if (!this.rpcProvider) {
69
+ return this.apiProvider.getMerkleProof(txId)
70
+ }
71
+ let txIds = await runWithRetries(() => this.rpcProvider!.getBlockTransactionIds(blockHash), {
72
+ maxTries: 10,
73
+ retrySleep: 2000,
74
+ })
75
+ let proof = calculateMerkleProof(txIds, txId)
76
+ return proof
77
+ }
78
+
79
+ async getRequestProof(transaction: {
80
+ txId: string
81
+ hex?: string
82
+ blockHash?: string
83
+ blockNumber?: number
84
+ merkleProof?: {
85
+ intermediateNodes: string
86
+ transactionIndex: number
87
+ }
88
+ }) {
89
+ let transactionHex = transaction.hex || (await this.getRawTransaction(transaction.txId))
90
+
91
+ let blockHash = transaction.blockHash
92
+ let blockNumber = transaction.blockNumber
93
+ if (!(transaction.blockHash && transaction.blockNumber)) {
94
+ let txInfo = await this.getTransaction(transaction.txId)
95
+ blockHash = txInfo.blockHash
96
+ blockNumber = txInfo.blockNumber
97
+ }
98
+ let parsedTx = parseRawTransaction(transactionHex)
99
+ let merkleProof =
100
+ transaction.merkleProof || (await this.getMerkleProof(transaction.txId, blockHash!))
101
+
102
+ return {
103
+ parsedTx,
104
+ merkleProof,
105
+ blockNumber: blockNumber!,
106
+ blockHash: blockHash!,
107
+ }
108
+ }
109
+
110
+ async getUtxo(address: string) {
111
+ if (!this.apiProvider) {
112
+ throw new Error("api provider not set")
113
+ }
114
+ return this.apiProvider.getUtxos(address)
115
+ }
116
+
117
+ async getExtendedUtxo(signerInfo: SignerInfo) {
118
+ if (!this.apiProvider) {
119
+ throw new Error("api provider not set")
120
+ }
121
+ let utxos = await this.apiProvider.getUtxos(signerInfo.address)
122
+ return utxos.map((tx: any) => ({
123
+ hash: tx.txId,
124
+ value: tx.value,
125
+ index: tx.index,
126
+ signerInfo,
127
+ }))
128
+ }
129
+
130
+ async getAddressesUtxo(allAddresses: string[]) {
131
+ if (!this.apiProvider) {
132
+ throw new Error("api provider not set")
133
+ }
134
+ const chunkOfAddresses = []
135
+ const chunkLength = 20
136
+ for (let i = 0; i < allAddresses.length; i += chunkLength) {
137
+ const tmp = allAddresses.slice(i, i + chunkLength)
138
+ chunkOfAddresses.push(tmp)
139
+ }
140
+
141
+ let results = []
142
+
143
+ for (let addresses of chunkOfAddresses) {
144
+ const allPromises = []
145
+ for (let address of addresses) {
146
+ let promise = await this.getUtxo(address)
147
+ allPromises.push(promise)
148
+ }
149
+ let result = await Promise.all(allPromises)
150
+ if (result.flat(1).length === 0) {
151
+ break
152
+ }
153
+ results.push(result.flat(1))
154
+ }
155
+ return results.flat(1)
156
+ }
157
+
158
+ async getAddressesExtendedUtxo(signerInfos: SignerInfo[]) {
159
+ if (!this.apiProvider) {
160
+ throw new Error("api provider not set")
161
+ }
162
+ const chunkOfAddresses = []
163
+ const chunkLength = 20
164
+ for (let i = 0; i < signerInfos.length; i += chunkLength) {
165
+ const tmp = signerInfos.slice(i, i + chunkLength)
166
+ chunkOfAddresses.push(tmp)
167
+ }
168
+
169
+ let results = []
170
+
171
+ for (let addresses of chunkOfAddresses) {
172
+ const allPromises = []
173
+ for (let signerInfo of addresses) {
174
+ let promise = await this.getExtendedUtxo(signerInfo)
175
+ allPromises.push(promise)
176
+ }
177
+ let result = await Promise.all(allPromises)
178
+ if (result.flat(1).length === 0) {
179
+ break
180
+ }
181
+ results.push(result.flat(1))
182
+ }
183
+ return results.flat(1)
184
+ }
185
+
186
+ async getBalance(address: string) {
187
+ if (!this.apiProvider) {
188
+ throw new Error("this function need an api provider")
189
+ }
190
+ return this.apiProvider.getBalance(address)
191
+ }
192
+
193
+ async getBlockTransactions(
194
+ addresses: string[],
195
+ blockNumber: number,
196
+ inputTxIds: {
197
+ txId: string
198
+ index: number
199
+ address: string
200
+ script?: string | undefined
201
+ value?: number | undefined
202
+ }[] = [],
203
+ ) {
204
+ if (!this.rpcProvider) {
205
+ throw new Error("RPC provider not set")
206
+ }
207
+ let rawBlockHex = await runWithRetries(
208
+ () => this.rpcProvider!.getBlockByBlockNumber(blockNumber, 0),
209
+ {
210
+ maxTries: 10,
211
+ retrySleep: 2000,
212
+ },
213
+ )
214
+ return extractTransactionsAndBlockInfoFromRawBlock(
215
+ rawBlockHex,
216
+ blockNumber,
217
+ addresses,
218
+ inputTxIds,
219
+ this.network,
220
+ )
221
+ }
222
+
223
+ // from start+1 to end
224
+ async getMultipleBlocksTransactions(
225
+ addresses: string[],
226
+ startBlockNumber: number,
227
+ endBlockNumber: number,
228
+ inputTxIds: {
229
+ txId: string
230
+ index: number
231
+ address: string
232
+ script?: string | undefined
233
+ value?: number | undefined
234
+ }[] = [],
235
+ ) {
236
+ let blockTxs = []
237
+ for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
238
+ console.log(blockNumber)
239
+ const response = await this.getBlockTransactions(addresses, blockNumber, inputTxIds)
240
+ blockTxs.push(response.withdrawTxs, response.depositTxs)
241
+ }
242
+ blockTxs = await Promise.all(blockTxs)
243
+ return blockTxs.flat(1)
244
+ }
245
+ }
246
+
247
+ export default BitcoinInterface