@teleportdao/bitcoin 1.3.2 → 1.4.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.
- package/package.json +3 -3
- package/src/bitcoin-interface.js +350 -350
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teleportdao/bitcoin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "teleswap bitcoin package",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@teleportdao/configs": "^1.
|
|
15
|
+
"@teleportdao/configs": "^1.4.0",
|
|
16
16
|
"@teleportdao/providers": "^1.2.4",
|
|
17
17
|
"axios": "^0.27.2",
|
|
18
18
|
"bignumber.js": "^9.1.1",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "c0c0001c9b78a18aff4545892adb60db8e4747f0"
|
|
35
35
|
}
|
package/src/bitcoin-interface.js
CHANGED
|
@@ -1,350 +1,350 @@
|
|
|
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
|
+
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
|