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