@teleportdao/bitcoin 1.4.3 → 1.4.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 (69) hide show
  1. package/dist/bitcoin-base.d.ts +66 -53
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +47 -38
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-interface-utils.d.ts +12 -10
  6. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-utils.js +16 -10
  8. package/dist/bitcoin-interface-utils.js.map +1 -1
  9. package/dist/bitcoin-interface.d.ts +206 -50
  10. package/dist/bitcoin-interface.d.ts.map +1 -1
  11. package/dist/bitcoin-interface.js +42 -27
  12. package/dist/bitcoin-interface.js.map +1 -1
  13. package/dist/bitcoin-utils.d.ts +111 -41
  14. package/dist/bitcoin-utils.d.ts.map +1 -1
  15. package/dist/bitcoin-utils.js +215 -156
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -0
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +32 -12
  21. package/dist/index.js.map +1 -1
  22. package/dist/sign/sign-transaction.d.ts +9 -5
  23. package/dist/sign/sign-transaction.d.ts.map +1 -1
  24. package/dist/sign/sign-transaction.js +14 -11
  25. package/dist/sign/sign-transaction.js.map +1 -1
  26. package/dist/teleport-dao-payments.d.ts +68 -89
  27. package/dist/teleport-dao-payments.d.ts.map +1 -1
  28. package/dist/teleport-dao-payments.js +16 -4
  29. package/dist/teleport-dao-payments.js.map +1 -1
  30. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
  31. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
  33. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  34. package/dist/transaction-builder/transaction-builder.d.ts +198 -9
  35. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  36. package/dist/transaction-builder/transaction-builder.js +291 -38
  37. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  38. package/dist/utils/networks.d.ts +5 -35
  39. package/dist/utils/networks.d.ts.map +1 -1
  40. package/dist/utils/networks.js +26 -2
  41. package/dist/utils/networks.js.map +1 -1
  42. package/dist/utils/tools.d.ts +15 -9
  43. package/dist/utils/tools.d.ts.map +1 -1
  44. package/dist/utils/tools.js +14 -11
  45. package/dist/utils/tools.js.map +1 -1
  46. package/package.json +8 -6
  47. package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
  48. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  49. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
  50. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
  51. package/src/helper/teleport-request-helper.js +179 -179
  52. package/src/index.ts +5 -0
  53. package/src/sign/sign-transaction.ts +96 -0
  54. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
  55. package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
  56. package/src/transaction-builder/transaction-builder.ts +567 -0
  57. package/src/utils/{networks.js → networks.ts} +33 -31
  58. package/src/utils/{tools.js → tools.ts} +80 -72
  59. package/tsconfig.json +10 -9
  60. package/webpack.config.js +16 -0
  61. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  62. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  63. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  64. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
  65. package/src/index.js +0 -15
  66. package/src/sign/sign-transaction.js +0 -36
  67. package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
  68. package/src/transaction-builder/transaction-builder-common.js +0 -236
  69. package/src/transaction-builder/transaction-builder.js +0 -159
@@ -1,350 +1,420 @@
1
- const { getRpcProvider, getApiProvider } = require("@teleportdao/providers").bitcoin
2
- const { runWithRetries, sleep } = require("./utils/tools")
3
- const {
4
- parseRawTransaction,
5
- calculateMerkleProof,
6
- parseBlockHeader,
7
- extractTransactionsAndBlockInfoFromRawBlock,
8
- } = require("./bitcoin-utils")
9
- const { checkAndParseProtocolRequest } = require("./helper/teleport-request-helper")
10
- const { getBurnTransactionInfo } = require("./helper/burn-request-helper")
11
- const BitcoinInterfaceUtils = require("./bitcoin-interface-utils")
12
-
13
- class BitcoinInterface extends BitcoinInterfaceUtils {
14
- constructor(connectionInfo, networkName, config = { minTeleporterFeeAmount: 0 }) {
15
- super(networkName)
16
- if (connectionInfo.rpc?.enabled) {
17
- this.rpcProvider = getRpcProvider(connectionInfo.rpc)
18
- } else if (connectionInfo.api.provider !== "BlockStream") {
19
- throw new Error("if rpc is disabled, we just support BlockStream as api provider")
20
- }
21
-
22
- if (connectionInfo.api.enabled) {
23
- this.apiProviderName = connectionInfo.api.provider
24
- this.apiProvider = getApiProvider(connectionInfo.api, networkName)
25
- }
26
-
27
- this.minTeleporterFeeAmount = config.minTeleporterFeeAmount
28
- this.provider = this.rpcProvider || this.apiProvider
29
- }
30
-
31
- // rpc + api
32
-
33
- // general
34
- async getLatestBlockNumber() {
35
- let latestHeight = await this.provider.getLatestBlockNumber()
36
- return latestHeight
37
- }
38
-
39
- async getBlockHash(blockNumber) {
40
- let headerHash = await runWithRetries(() => this.provider.getBlockHash(blockNumber))
41
- return headerHash
42
- }
43
-
44
- async getBlockHeaderHex(blockNumber) {
45
- let headerHex = await runWithRetries(() => this.provider.getBlockHeaderHex(blockNumber))
46
- return headerHex
47
- }
48
-
49
- async getTransaction(txId) {
50
- return this.provider.getTransaction(txId)
51
- }
52
-
53
- // speed : low normal fast
54
- async getFeeRate(speed = "normal") {
55
- if (!(speed === "normal" || speed === "slow" || speed === "fast")) {
56
- throw new Error("incorrect speed")
57
- }
58
- let fee = await this.provider.getFeeRate(speed)
59
- return +fee
60
- }
61
-
62
- // ----------- specific
63
-
64
- // relayer
65
- async getHexBlockHeaders(startBlockNumber, endBlockNumber) {
66
- const blockHeaders = []
67
- let difficulty = null
68
- let hexBlockHeaders = ""
69
-
70
- let fromBlockNumber = startBlockNumber
71
- for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
72
- let blockHeader = await this.getBlockHeaderHex(blockNumber)
73
- console.log("block", blockNumber)
74
- let parsedBlockHeader = parseBlockHeader(blockHeader)
75
- if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
76
- blockHeaders.push({
77
- hexBlockHeaders,
78
- fromBlockNumber,
79
- toBlockNumber: blockNumber - 1,
80
- difficulty,
81
- })
82
- hexBlockHeaders = blockHeader
83
- fromBlockNumber = blockNumber
84
- } else {
85
- hexBlockHeaders += blockHeader
86
- }
87
- difficulty = parsedBlockHeader.difficulty
88
- }
89
- if (hexBlockHeaders) {
90
- blockHeaders.push({
91
- hexBlockHeaders,
92
- fromBlockNumber,
93
- toBlockNumber: endBlockNumber,
94
- difficulty,
95
- })
96
- }
97
-
98
- return blockHeaders
99
- }
100
-
101
- async getRequestProof(transaction) {
102
- let transactionHex =
103
- transaction.hex || (await this.provider.getRawTransaction(transaction.txId))
104
-
105
- let txInfo
106
- if (!(transaction.blockHash && transaction.blockNumber)) {
107
- txInfo = await this.provider.getTransaction(transaction.txId)
108
- }
109
- let blockHash = transaction.blockHash || txInfo.blockHash
110
- let blockNumber = transaction.blockNumber || txInfo.blockNumber
111
- let parsedTx = parseRawTransaction(transactionHex)
112
- let merkleProof =
113
- transaction.merkleProof || (await this.getMerkleProof(transaction.txId, blockHash))
114
-
115
- return {
116
- parsedTx,
117
- merkleProof,
118
- blockNumber,
119
- blockHash,
120
- }
121
- }
122
-
123
- async getMerkleProof(txId, blockHash) {
124
- let txIds = await this.provider.getBlockTransactionIds(blockHash)
125
- // let a = await this.provider.getMerkleProof(txId)
126
- let proof = calculateMerkleProof(txIds, txId)
127
- // console.log(a.intermediateNodes === proof.intermediateNodes)
128
- return proof
129
- }
130
-
131
- // ------------------ utxo providers --------------------------------
132
-
133
- // teleporter + locker
134
- async getAddressesUtxo(allAddresses) {
135
- if (!this.apiProvider) {
136
- throw new Error("this function need an api provider")
137
- }
138
- const chunkOfAddresses = []
139
- const chunkLength = 20
140
- for (let i = 0; i < allAddresses.length; i += chunkLength) {
141
- const tmp = allAddresses.slice(i, i + chunkLength)
142
- chunkOfAddresses.push(tmp)
143
- }
144
-
145
- let results = []
146
-
147
- for (let addresses of chunkOfAddresses) {
148
- const allPromises = []
149
- for (let address of addresses) {
150
- let promise = await this.apiProvider.getUtxos(address)
151
- allPromises.push(promise)
152
- }
153
- let result = await Promise.all(allPromises)
154
- if (result.flat(1).length === 0) {
155
- break
156
- }
157
- results.push(result.flat(1))
158
- }
159
- return results.flat(1)
160
- }
161
-
162
- async getAddressesExtendedUtxo(allAddressesInfo) {
163
- if (!this.apiProvider) {
164
- throw new Error("this function need an api provider")
165
- }
166
- const chunkOfAddressesInfo = []
167
- const chunkLength = 20
168
- for (let i = 0; i < allAddressesInfo.length; i += chunkLength) {
169
- const tmp = allAddressesInfo.slice(i, i + chunkLength)
170
- chunkOfAddressesInfo.push(tmp)
171
- }
172
-
173
- let results = []
174
-
175
- for (let addressesInfo of chunkOfAddressesInfo) {
176
- const allPromises = []
177
- for (let info of addressesInfo) {
178
- let promise = this.apiProvider.getUtxos(info.address)
179
- allPromises.push(promise)
180
- }
181
- let result = await Promise.all(allPromises)
182
- if (result.flat(1).length === 0) {
183
- break
184
- }
185
- results.push(
186
- result
187
- .map((utxos, i) =>
188
- utxos.map((tx) => ({
189
- hash: tx.txId,
190
- value: tx.value,
191
- index: tx.index,
192
- ...addressesInfo[i],
193
- signerInfo: addressesInfo[i],
194
- })),
195
- )
196
- .flat(1),
197
- )
198
- }
199
- return results.flat(1)
200
- }
201
-
202
- async getBalance(address) {
203
- if (!this.apiProvider) {
204
- throw new Error("this function need an api provider")
205
- }
206
- let utxos = (await this.apiProvider.getUtxos(address)) ?? []
207
- return utxos.reduce((a, tx) => a + Number(tx.value), 0)
208
- }
209
-
210
- // ------------------ utxo provider + rpc or blockstream----------------
211
- // teleporter
212
- async getBlockTransactions(addresses, blockNumber, inputTxIds = []) {
213
- let rawBlockHex = await this.rpcProvider.getBlockByBlockNumber(blockNumber, 0)
214
- let { withdrawTxs, depositTxs } = extractTransactionsAndBlockInfoFromRawBlock(
215
- rawBlockHex,
216
- blockNumber,
217
- addresses,
218
- inputTxIds,
219
- this.network,
220
- )
221
- return depositTxs.concat(withdrawTxs)
222
- }
223
-
224
- // teleporter
225
- async getMultipleBlocksTransactions(
226
- addresses,
227
- startBlockNumber,
228
- endBlockNumber,
229
- inputTxIds = [],
230
- ) {
231
- if (endBlockNumber - startBlockNumber > 20) {
232
- throw new Error("cant get more than 20 block per function call")
233
- }
234
- let blockTxs = []
235
- for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
236
- const response = this.getBlockTransactions(addresses, blockNumber, inputTxIds)
237
- blockTxs.push(response)
238
- await sleep(200)
239
- }
240
- blockTxs = await Promise.all(blockTxs)
241
- return blockTxs.flat(1)
242
- }
243
-
244
- // teleporter
245
- async getTransactionHistory(addresses, startBlockNumber, endBlockNumber) {
246
- if (this.rpcProvider) {
247
- let endBlock = endBlockNumber || (await this.getLatestBlockNumber())
248
- let startBlock = Math.max(+startBlockNumber, +endBlock - 20)
249
- return this.getMultipleBlocksTransactions(addresses, startBlock, endBlock)
250
- }
251
- if (this.apiProviderName !== "BlockStream") {
252
- throw new Error("just support BlockStream as api provider for this function")
253
- }
254
- let txs = await this.apiProvider.getTransactionHistoryForMultipleAddresses(
255
- addresses,
256
- startBlockNumber,
257
- )
258
- return txs.flat(1)
259
- }
260
-
261
- // ------------------just blockstream----------------------
262
- async getMempoolTransactionHistory(addresses) {
263
- if (this.apiProviderName !== "BlockStream") {
264
- throw new Error("teleporter just support BlockStream as api provider")
265
- }
266
- let txs = await this.apiProvider.getMempoolTransactionHistoryForMultipleAddresses(addresses)
267
- return txs.flat(1)
268
- }
269
-
270
- async getTeleporterRequests(addresses, startblockNumber, endBlockNumber, mempool = false) {
271
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
272
- let transactions = mempool
273
- ? await this.getMempoolTransactionHistory(addresses)
274
- : await this.getTransactionHistory(addresses, startblockNumber, endBlockNumber)
275
-
276
- let requests = []
277
- let invalidRequests = []
278
-
279
- for (let inputTx of transactions) {
280
- let { transaction, request, lockerAddress, lockerLockingScript } =
281
- await this.getTeleportRequestByTx(inputTx, inputTx.address)
282
- if (request.status) {
283
- requests.push({ transaction, request, lockerAddress, lockerLockingScript })
284
- } else if (request.code !== "NO_OP_RETURN") {
285
- invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
286
- }
287
- }
288
- return { requests, invalidRequests }
289
- }
290
-
291
- async getTeleportRequestByTx(inputTransaction, lockerAddress) {
292
- let transaction = inputTransaction
293
- if (!transaction.txId) throw new Error("txId not exist")
294
- if (!transaction.vout) {
295
- transaction = await this.getTransaction(transaction.txId)
296
- }
297
- let vout = transaction.vout
298
- let request = checkAndParseProtocolRequest(vout, lockerAddress, {
299
- minTeleporterFeeAmount: this.minTeleporterFeeAmount,
300
- })
301
- let lockerLockingScript =
302
- transaction.addressScript || this.convertAddressToScript(lockerAddress).script.toString("hex")
303
-
304
- return {
305
- transaction,
306
- request,
307
- lockerAddress,
308
- lockerLockingScript,
309
- }
310
- }
311
-
312
- async getLockersBurnTransactions(addresses, startBlockNumber, endBlockNumber, mempool = false) {
313
- let transactions = mempool
314
- ? await this.getMempoolTransactionHistory(addresses)
315
- : await this.getTransactionHistory(addresses, startBlockNumber, endBlockNumber)
316
-
317
- let validTxs = []
318
- for (let transaction of transactions) {
319
- let address = transaction.address
320
- // check if its a transaction to spend btc
321
- let txBurnInfo = await this.getTransactionBurnInfoByTx(transaction, address)
322
- if (txBurnInfo) {
323
- const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
324
- validTxs.push({
325
- transaction,
326
- burnInfo,
327
- lockerAddress,
328
- lockerLockingScript,
329
- })
330
- }
331
- }
332
- return validTxs
333
- }
334
-
335
- async getTransactionBurnInfoByTx(transaction, lockerAddress) {
336
- if (!transaction.txId) throw new Error("txId not exist")
337
- let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
338
- let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
339
- if (!burnInfo) return null
340
- let lockerLockingScript =
341
- transaction.addressScript || this.convertAddressToScript(lockerAddress).script.toString("hex")
342
- return {
343
- burnInfo,
344
- lockerAddress,
345
- lockerLockingScript,
346
- }
347
- }
348
- }
349
-
350
- module.exports = BitcoinInterface
1
+ import { bitcoin as bitcoinProvider } from "@teleportdao/providers"
2
+ import { runWithRetries, sleep } from "./utils/tools"
3
+ import {
4
+ parseRawTransaction,
5
+ calculateMerkleProof,
6
+ parseBlockHeader,
7
+ extractTransactionsAndBlockInfoFromRawBlock,
8
+ } from "./bitcoin-utils"
9
+ import { checkAndParseProtocolRequest } from "./helper/teleport-request-helper"
10
+ import { getBurnTransactionInfo } from "./helper/burn-request-helper"
11
+ import { BitcoinInterfaceUtils } from "./bitcoin-interface-utils"
12
+ import type { BitcoinConnectionInfo } from "./transaction-builder/bitcoin-transaction-builder"
13
+ import type { SignerInfo } from "./transaction-builder/transaction-builder"
14
+
15
+ export class BitcoinInterface extends BitcoinInterfaceUtils {
16
+ rpcProvider?: bitcoinProvider.RPC
17
+ apiProviderName!: string
18
+ // todo : add provider type
19
+ minTeleporterFeeAmount: number
20
+ provider: bitcoinProvider.ApiProviders.BlockStream | bitcoinProvider.RPC
21
+ apiProvider!: bitcoinProvider.ApiProviders.BlockStream
22
+ constructor(
23
+ connectionInfo: BitcoinConnectionInfo,
24
+ networkName: string,
25
+ config = { minTeleporterFeeAmount: 0 },
26
+ ) {
27
+ super(networkName)
28
+ if (connectionInfo.rpc?.enabled) {
29
+ this.rpcProvider = bitcoinProvider.getRpcProvider(connectionInfo.rpc)
30
+ } else if (connectionInfo.api.provider !== "BlockStream") {
31
+ throw new Error("if rpc is disabled, we just support BlockStream as api provider")
32
+ }
33
+
34
+ if (connectionInfo.api.enabled) {
35
+ this.apiProviderName = connectionInfo.api.provider
36
+ this.apiProvider = bitcoinProvider.getApiProvider(connectionInfo.api as any, networkName)
37
+ }
38
+
39
+ this.minTeleporterFeeAmount = config.minTeleporterFeeAmount
40
+ this.provider = this.rpcProvider || this.apiProvider
41
+ }
42
+
43
+ // rpc + api
44
+
45
+ // general
46
+ async getLatestBlockNumber() {
47
+ let latestHeight = await this.provider.getLatestBlockNumber()
48
+ return latestHeight
49
+ }
50
+
51
+ async getBlockHash(blockNumber: number) {
52
+ let headerHash = await runWithRetries(() => this.provider.getBlockHash(blockNumber))
53
+ return headerHash
54
+ }
55
+
56
+ async getBlockHeaderHex(blockNumber: number) {
57
+ let headerHex = await runWithRetries(() => this.provider.getBlockHeaderHex(blockNumber))
58
+ return headerHex
59
+ }
60
+
61
+ async getTransaction(txId: string) {
62
+ return this.provider.getTransaction(txId)
63
+ }
64
+
65
+ // speed : low normal fast
66
+ async getFeeRate(speed: "normal" | "slow" | "fast" = "normal") {
67
+ if (!(speed === "normal" || speed === "slow" || speed === "fast")) {
68
+ throw new Error("incorrect speed")
69
+ }
70
+ let fee = await this.provider.getFeeRate(speed)
71
+ return +fee
72
+ }
73
+
74
+ // ----------- specific
75
+
76
+ // relayer
77
+ async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
78
+ const blockHeaders = []
79
+ let difficulty = null
80
+ let hexBlockHeaders = ""
81
+
82
+ let fromBlockNumber = startBlockNumber
83
+ for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
84
+ let blockHeader = await this.getBlockHeaderHex(blockNumber)
85
+ console.log("block", blockNumber)
86
+ let parsedBlockHeader = parseBlockHeader(blockHeader)
87
+ if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
88
+ blockHeaders.push({
89
+ hexBlockHeaders,
90
+ fromBlockNumber,
91
+ toBlockNumber: blockNumber - 1,
92
+ difficulty,
93
+ })
94
+ hexBlockHeaders = blockHeader
95
+ fromBlockNumber = blockNumber
96
+ } else {
97
+ hexBlockHeaders += blockHeader
98
+ }
99
+ difficulty = parsedBlockHeader.difficulty
100
+ }
101
+ if (hexBlockHeaders) {
102
+ blockHeaders.push({
103
+ hexBlockHeaders,
104
+ fromBlockNumber,
105
+ toBlockNumber: endBlockNumber,
106
+ difficulty,
107
+ })
108
+ }
109
+
110
+ return blockHeaders
111
+ }
112
+
113
+ async getRequestProof(transaction: {
114
+ txId: string
115
+ hex?: string
116
+ blockHash?: string
117
+ blockNumber?: number
118
+ merkleProof?: {
119
+ intermediateNodes: string
120
+ transactionIndex: number
121
+ }
122
+ }) {
123
+ let transactionHex =
124
+ transaction.hex || (await this.provider.getRawTransaction(transaction.txId))
125
+
126
+ let txInfo:
127
+ | {
128
+ txId: any
129
+ version: any
130
+ locktime: any
131
+ blockNumber: any
132
+ blockHash: any
133
+ vout: any
134
+ vin: any
135
+ }
136
+ | undefined
137
+ if (!(transaction.blockHash && transaction.blockNumber)) {
138
+ txInfo = await this.provider.getTransaction(transaction.txId)
139
+ }
140
+ let blockHash: string = transaction.blockHash || txInfo?.blockHash
141
+ let blockNumber: number = transaction.blockNumber || txInfo?.blockNumber
142
+ let parsedTx = parseRawTransaction(transactionHex)
143
+ let merkleProof =
144
+ transaction.merkleProof || (await this.getMerkleProof(transaction.txId, blockHash))
145
+
146
+ return {
147
+ parsedTx,
148
+ merkleProof,
149
+ blockNumber,
150
+ blockHash,
151
+ }
152
+ }
153
+
154
+ async getMerkleProof(txId: string, blockHash: string) {
155
+ if (!this.rpcProvider) {
156
+ throw new Error("RPC provider not set")
157
+ }
158
+ let txIds = await this.rpcProvider.getBlockTransactionIds(blockHash)
159
+ // let a = await this.provider.getMerkleProof(txId)
160
+ let proof = calculateMerkleProof(txIds, txId)
161
+ // console.log(a.intermediateNodes === proof.intermediateNodes)
162
+ return proof
163
+ }
164
+
165
+ // ------------------ utxo providers --------------------------------
166
+
167
+ // teleporter + locker
168
+ async getAddressesUtxo(allAddresses: string[]) {
169
+ if (!this.apiProvider) {
170
+ throw new Error("api provider not set")
171
+ }
172
+ const chunkOfAddresses = []
173
+ const chunkLength = 20
174
+ for (let i = 0; i < allAddresses.length; i += chunkLength) {
175
+ const tmp = allAddresses.slice(i, i + chunkLength)
176
+ chunkOfAddresses.push(tmp)
177
+ }
178
+
179
+ let results = []
180
+
181
+ for (let addresses of chunkOfAddresses) {
182
+ const allPromises = []
183
+ for (let address of addresses) {
184
+ let promise = await this.apiProvider.getUtxos(address)
185
+ allPromises.push(promise)
186
+ }
187
+ let result = await Promise.all(allPromises)
188
+ if (result.flat(1).length === 0) {
189
+ break
190
+ }
191
+ results.push(result.flat(1))
192
+ }
193
+ return results.flat(1)
194
+ }
195
+
196
+ async getAddressesExtendedUtxo(allAddressesInfo: SignerInfo[]) {
197
+ if (!this.apiProvider) {
198
+ throw new Error("this function need an api provider")
199
+ }
200
+ const chunkOfAddressesInfo = []
201
+ const chunkLength = 20
202
+ for (let i = 0; i < allAddressesInfo.length; i += chunkLength) {
203
+ const tmp = allAddressesInfo.slice(i, i + chunkLength)
204
+ chunkOfAddressesInfo.push(tmp)
205
+ }
206
+
207
+ let results = []
208
+
209
+ for (let addressesInfo of chunkOfAddressesInfo) {
210
+ const allPromises = []
211
+ for (let info of addressesInfo) {
212
+ let promise = this.apiProvider.getUtxos(info.address)
213
+ allPromises.push(promise)
214
+ }
215
+ let result = await Promise.all(allPromises)
216
+ if (result.flat(1).length === 0) {
217
+ break
218
+ }
219
+ results.push(
220
+ result
221
+ .map((utxos, i) =>
222
+ utxos.map((tx) => ({
223
+ hash: tx.txId,
224
+ value: tx.value,
225
+ index: tx.index,
226
+ signerInfo: addressesInfo[i],
227
+ })),
228
+ )
229
+ .flat(1),
230
+ )
231
+ }
232
+ return results.flat(1)
233
+ }
234
+
235
+ async getBalance(address: string) {
236
+ if (!this.apiProvider) {
237
+ throw new Error("this function need an api provider")
238
+ }
239
+ let utxos = (await this.apiProvider.getUtxos(address)) ?? []
240
+ return utxos.reduce((a, tx) => a + Number(tx.value), 0)
241
+ }
242
+
243
+ // ------------------ utxo provider + rpc or blockstream----------------
244
+ // teleporter
245
+ async getBlockTransactions(addresses: string[], blockNumber: number, inputTxIds = []) {
246
+ if (!this.rpcProvider) {
247
+ throw new Error("RPC provider not set")
248
+ }
249
+ let rawBlockHex = await this.rpcProvider.getBlockByBlockNumber(blockNumber, 0)
250
+ let { withdrawTxs, depositTxs } = extractTransactionsAndBlockInfoFromRawBlock(
251
+ rawBlockHex,
252
+ blockNumber,
253
+ addresses,
254
+ inputTxIds,
255
+ this.network,
256
+ )
257
+ return depositTxs.concat(withdrawTxs)
258
+ }
259
+
260
+ // teleporter
261
+ async getMultipleBlocksTransactions(
262
+ addresses: string[],
263
+ startBlockNumber: number,
264
+ endBlockNumber: number,
265
+ inputTxIds = [],
266
+ ) {
267
+ if (endBlockNumber - startBlockNumber > 20) {
268
+ throw new Error("cant get more than 20 block per function call")
269
+ }
270
+ let blockTxs = []
271
+ for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
272
+ const response = this.getBlockTransactions(addresses, blockNumber, inputTxIds)
273
+ blockTxs.push(response)
274
+ await sleep(200)
275
+ }
276
+ blockTxs = await Promise.all(blockTxs)
277
+ return blockTxs.flat(1)
278
+ }
279
+
280
+ // teleporter
281
+ async getTransactionHistory(
282
+ addresses: string[],
283
+ startBlockNumber: number,
284
+ endBlockNumber: number,
285
+ ) {
286
+ if (this.rpcProvider) {
287
+ let endBlock = endBlockNumber || (await this.getLatestBlockNumber())
288
+ let startBlock = Math.max(+startBlockNumber, +endBlock - 20)
289
+ return this.getMultipleBlocksTransactions(addresses, startBlock, endBlock)
290
+ }
291
+ if (this.apiProviderName !== "BlockStream") {
292
+ throw new Error("just support BlockStream as api provider for this function")
293
+ }
294
+ let txs = await this.apiProvider.getTransactionHistoryForMultipleAddresses(
295
+ addresses,
296
+ startBlockNumber,
297
+ )
298
+ return txs.flat(1)
299
+ }
300
+
301
+ // ------------------just blockstream----------------------
302
+ async getMempoolTransactionHistory(addresses: string[]) {
303
+ if (this.apiProviderName !== "BlockStream") {
304
+ throw new Error("teleporter just support BlockStream as api provider")
305
+ }
306
+ let txs = await this.apiProvider.getMempoolTransactionHistoryForMultipleAddresses(addresses)
307
+ return txs.flat(1)
308
+ }
309
+
310
+ async getTeleporterRequests(
311
+ addresses: string[],
312
+ startblockNumber: number,
313
+ endBlockNumber: number,
314
+ mempool = false,
315
+ ) {
316
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
317
+ let transactions = mempool
318
+ ? await this.getMempoolTransactionHistory(addresses)
319
+ : await this.getTransactionHistory(addresses, startblockNumber, endBlockNumber)
320
+
321
+ let requests = []
322
+ let invalidRequests = []
323
+
324
+ for (let inputTx of transactions) {
325
+ let { transaction, request, lockerAddress, lockerLockingScript } =
326
+ await this.getTeleportRequestByTx(inputTx, inputTx.address)
327
+ if (request.status) {
328
+ requests.push({ transaction, request, lockerAddress, lockerLockingScript })
329
+ } else if (request.code !== "NO_OP_RETURN") {
330
+ invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
331
+ }
332
+ }
333
+ return { requests, invalidRequests }
334
+ }
335
+
336
+ async getTeleportRequestByTx(
337
+ inputTransaction: {
338
+ txId: any
339
+ version?: any
340
+ locktime?: any
341
+ blockNumber?: any
342
+ blockHash?: any
343
+ vout?: any
344
+ vin?: any
345
+ addressScript?: any
346
+ },
347
+ lockerAddress: string,
348
+ ) {
349
+ let transaction = inputTransaction
350
+ if (!transaction.txId) throw new Error("txId not exist")
351
+ if (!transaction?.vout) {
352
+ transaction = await this.getTransaction(transaction.txId)
353
+ }
354
+ let vout = transaction.vout
355
+ let request = checkAndParseProtocolRequest(vout, lockerAddress, {
356
+ minTeleporterFeeAmount: this.minTeleporterFeeAmount,
357
+ })
358
+ let lockerLockingScript: string =
359
+ transaction.addressScript ||
360
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
361
+
362
+ return {
363
+ transaction,
364
+ request,
365
+ lockerAddress,
366
+ lockerLockingScript,
367
+ }
368
+ }
369
+
370
+ async getLockersBurnTransactions(
371
+ addresses: string[],
372
+ startBlockNumber: number,
373
+ endBlockNumber: number,
374
+ mempool = false,
375
+ ) {
376
+ let transactions = mempool
377
+ ? await this.getMempoolTransactionHistory(addresses)
378
+ : await this.getTransactionHistory(addresses, startBlockNumber, endBlockNumber)
379
+
380
+ let validTxs = []
381
+ for (let transaction of transactions) {
382
+ let address = transaction.address
383
+ // check if its a transaction to spend btc
384
+ let txBurnInfo = await this.getTransactionBurnInfoByTx(transaction, address)
385
+ if (txBurnInfo) {
386
+ const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
387
+ validTxs.push({
388
+ transaction,
389
+ burnInfo,
390
+ lockerAddress,
391
+ lockerLockingScript,
392
+ })
393
+ }
394
+ }
395
+ return validTxs
396
+ }
397
+
398
+ async getTransactionBurnInfoByTx(
399
+ transaction: {
400
+ txId: any
401
+ vout?: any
402
+ vin?: any
403
+ addressScript?: any
404
+ },
405
+ lockerAddress: string,
406
+ ) {
407
+ if (!transaction.txId) throw new Error("txId not exist")
408
+ let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
409
+ let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
410
+ if (!burnInfo) return null
411
+ let lockerLockingScript: string =
412
+ transaction.addressScript ||
413
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
414
+ return {
415
+ burnInfo,
416
+ lockerAddress,
417
+ lockerLockingScript,
418
+ }
419
+ }
420
+ }