@teleportdao/bitcoin 1.4.4 → 1.4.7

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 (58) hide show
  1. package/dist/bitcoin-base.d.ts +62 -50
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +55 -28
  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 -159
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -4
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +31 -13
  21. package/dist/index.js.map +1 -1
  22. package/dist/teleport-dao-payments.d.ts +60 -92
  23. package/dist/teleport-dao-payments.d.ts.map +1 -1
  24. package/dist/teleport-dao-payments.js +12 -1
  25. package/dist/teleport-dao-payments.js.map +1 -1
  26. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +8 -4
  27. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  28. package/dist/transaction-builder/bitcoin-transaction-builder.js +3 -3
  29. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  30. package/dist/transaction-builder/transaction-builder.d.ts +63 -13
  31. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/transaction-builder.js +72 -19
  33. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  34. package/dist/utils/networks.d.ts +5 -35
  35. package/dist/utils/networks.d.ts.map +1 -1
  36. package/dist/utils/networks.js +26 -2
  37. package/dist/utils/networks.js.map +1 -1
  38. package/dist/utils/tools.d.ts +15 -9
  39. package/dist/utils/tools.d.ts.map +1 -1
  40. package/dist/utils/tools.js +14 -11
  41. package/dist/utils/tools.js.map +1 -1
  42. package/package.json +6 -6
  43. package/src/{bitcoin-base.js → bitcoin-base.ts} +97 -69
  44. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  45. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +115 -45
  46. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +337 -216
  47. package/src/index.ts +5 -8
  48. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +67 -6
  49. package/src/transaction-builder/bitcoin-transaction-builder.ts +8 -4
  50. package/src/transaction-builder/transaction-builder.ts +102 -25
  51. package/src/utils/{networks.js → networks.ts} +33 -31
  52. package/src/utils/{tools.js → tools.ts} +80 -72
  53. package/tsconfig.json +10 -9
  54. package/webpack.config.js +16 -0
  55. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  56. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  57. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  58. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
@@ -1,27 +1,39 @@
1
- const { getRpcProvider, getApiProvider } = require("@teleportdao/providers").bitcoin
2
- const { runWithRetries, sleep } = require("./utils/tools")
3
- const {
1
+ import { bitcoin as bitcoinProvider } from "@teleportdao/providers"
2
+ import { runWithRetries, sleep } from "./utils/tools"
3
+ import {
4
4
  parseRawTransaction,
5
5
  calculateMerkleProof,
6
6
  parseBlockHeader,
7
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 }) {
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
+ ) {
15
27
  super(networkName)
16
28
  if (connectionInfo.rpc?.enabled) {
17
- this.rpcProvider = getRpcProvider(connectionInfo.rpc)
29
+ this.rpcProvider = bitcoinProvider.getRpcProvider(connectionInfo.rpc)
18
30
  } else if (connectionInfo.api.provider !== "BlockStream") {
19
31
  throw new Error("if rpc is disabled, we just support BlockStream as api provider")
20
32
  }
21
33
 
22
34
  if (connectionInfo.api.enabled) {
23
35
  this.apiProviderName = connectionInfo.api.provider
24
- this.apiProvider = getApiProvider(connectionInfo.api, networkName)
36
+ this.apiProvider = bitcoinProvider.getApiProvider(connectionInfo.api as any, networkName)
25
37
  }
26
38
 
27
39
  this.minTeleporterFeeAmount = config.minTeleporterFeeAmount
@@ -36,22 +48,22 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
36
48
  return latestHeight
37
49
  }
38
50
 
39
- async getBlockHash(blockNumber) {
51
+ async getBlockHash(blockNumber: number) {
40
52
  let headerHash = await runWithRetries(() => this.provider.getBlockHash(blockNumber))
41
53
  return headerHash
42
54
  }
43
55
 
44
- async getBlockHeaderHex(blockNumber) {
56
+ async getBlockHeaderHex(blockNumber: number) {
45
57
  let headerHex = await runWithRetries(() => this.provider.getBlockHeaderHex(blockNumber))
46
58
  return headerHex
47
59
  }
48
60
 
49
- async getTransaction(txId) {
61
+ async getTransaction(txId: string) {
50
62
  return this.provider.getTransaction(txId)
51
63
  }
52
64
 
53
65
  // speed : low normal fast
54
- async getFeeRate(speed = "normal") {
66
+ async getFeeRate(speed: "normal" | "slow" | "fast" = "normal") {
55
67
  if (!(speed === "normal" || speed === "slow" || speed === "fast")) {
56
68
  throw new Error("incorrect speed")
57
69
  }
@@ -62,7 +74,7 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
62
74
  // ----------- specific
63
75
 
64
76
  // relayer
65
- async getHexBlockHeaders(startBlockNumber, endBlockNumber) {
77
+ async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
66
78
  const blockHeaders = []
67
79
  let difficulty = null
68
80
  let hexBlockHeaders = ""
@@ -98,16 +110,35 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
98
110
  return blockHeaders
99
111
  }
100
112
 
101
- async getRequestProof(transaction) {
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
+ }) {
102
123
  let transactionHex =
103
124
  transaction.hex || (await this.provider.getRawTransaction(transaction.txId))
104
125
 
105
- let txInfo
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
106
137
  if (!(transaction.blockHash && transaction.blockNumber)) {
107
138
  txInfo = await this.provider.getTransaction(transaction.txId)
108
139
  }
109
- let blockHash = transaction.blockHash || txInfo.blockHash
110
- let blockNumber = transaction.blockNumber || txInfo.blockNumber
140
+ let blockHash: string = transaction.blockHash || txInfo?.blockHash
141
+ let blockNumber: number = transaction.blockNumber || txInfo?.blockNumber
111
142
  let parsedTx = parseRawTransaction(transactionHex)
112
143
  let merkleProof =
113
144
  transaction.merkleProof || (await this.getMerkleProof(transaction.txId, blockHash))
@@ -120,8 +151,11 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
120
151
  }
121
152
  }
122
153
 
123
- async getMerkleProof(txId, blockHash) {
124
- let txIds = await this.provider.getBlockTransactionIds(blockHash)
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)
125
159
  // let a = await this.provider.getMerkleProof(txId)
126
160
  let proof = calculateMerkleProof(txIds, txId)
127
161
  // console.log(a.intermediateNodes === proof.intermediateNodes)
@@ -131,9 +165,9 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
131
165
  // ------------------ utxo providers --------------------------------
132
166
 
133
167
  // teleporter + locker
134
- async getAddressesUtxo(allAddresses) {
168
+ async getAddressesUtxo(allAddresses: string[]) {
135
169
  if (!this.apiProvider) {
136
- throw new Error("this function need an api provider")
170
+ throw new Error("api provider not set")
137
171
  }
138
172
  const chunkOfAddresses = []
139
173
  const chunkLength = 20
@@ -159,7 +193,7 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
159
193
  return results.flat(1)
160
194
  }
161
195
 
162
- async getAddressesExtendedUtxo(allAddressesInfo) {
196
+ async getAddressesExtendedUtxo(allAddressesInfo: SignerInfo[]) {
163
197
  if (!this.apiProvider) {
164
198
  throw new Error("this function need an api provider")
165
199
  }
@@ -189,7 +223,6 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
189
223
  hash: tx.txId,
190
224
  value: tx.value,
191
225
  index: tx.index,
192
- ...addressesInfo[i],
193
226
  signerInfo: addressesInfo[i],
194
227
  })),
195
228
  )
@@ -199,7 +232,7 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
199
232
  return results.flat(1)
200
233
  }
201
234
 
202
- async getBalance(address) {
235
+ async getBalance(address: string) {
203
236
  if (!this.apiProvider) {
204
237
  throw new Error("this function need an api provider")
205
238
  }
@@ -209,7 +242,10 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
209
242
 
210
243
  // ------------------ utxo provider + rpc or blockstream----------------
211
244
  // teleporter
212
- async getBlockTransactions(addresses, blockNumber, inputTxIds = []) {
245
+ async getBlockTransactions(addresses: string[], blockNumber: number, inputTxIds = []) {
246
+ if (!this.rpcProvider) {
247
+ throw new Error("RPC provider not set")
248
+ }
213
249
  let rawBlockHex = await this.rpcProvider.getBlockByBlockNumber(blockNumber, 0)
214
250
  let { withdrawTxs, depositTxs } = extractTransactionsAndBlockInfoFromRawBlock(
215
251
  rawBlockHex,
@@ -223,9 +259,9 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
223
259
 
224
260
  // teleporter
225
261
  async getMultipleBlocksTransactions(
226
- addresses,
227
- startBlockNumber,
228
- endBlockNumber,
262
+ addresses: string[],
263
+ startBlockNumber: number,
264
+ endBlockNumber: number,
229
265
  inputTxIds = [],
230
266
  ) {
231
267
  if (endBlockNumber - startBlockNumber > 20) {
@@ -242,7 +278,11 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
242
278
  }
243
279
 
244
280
  // teleporter
245
- async getTransactionHistory(addresses, startBlockNumber, endBlockNumber) {
281
+ async getTransactionHistory(
282
+ addresses: string[],
283
+ startBlockNumber: number,
284
+ endBlockNumber: number,
285
+ ) {
246
286
  if (this.rpcProvider) {
247
287
  let endBlock = endBlockNumber || (await this.getLatestBlockNumber())
248
288
  let startBlock = Math.max(+startBlockNumber, +endBlock - 20)
@@ -259,7 +299,7 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
259
299
  }
260
300
 
261
301
  // ------------------just blockstream----------------------
262
- async getMempoolTransactionHistory(addresses) {
302
+ async getMempoolTransactionHistory(addresses: string[]) {
263
303
  if (this.apiProviderName !== "BlockStream") {
264
304
  throw new Error("teleporter just support BlockStream as api provider")
265
305
  }
@@ -267,7 +307,12 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
267
307
  return txs.flat(1)
268
308
  }
269
309
 
270
- async getTeleporterRequests(addresses, startblockNumber, endBlockNumber, mempool = false) {
310
+ async getTeleporterRequests(
311
+ addresses: string[],
312
+ startblockNumber: number,
313
+ endBlockNumber: number,
314
+ mempool = false,
315
+ ) {
271
316
  // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
272
317
  let transactions = mempool
273
318
  ? await this.getMempoolTransactionHistory(addresses)
@@ -288,18 +333,31 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
288
333
  return { requests, invalidRequests }
289
334
  }
290
335
 
291
- async getTeleportRequestByTx(inputTransaction, lockerAddress) {
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
+ ) {
292
349
  let transaction = inputTransaction
293
350
  if (!transaction.txId) throw new Error("txId not exist")
294
- if (!transaction.vout) {
351
+ if (!transaction?.vout) {
295
352
  transaction = await this.getTransaction(transaction.txId)
296
353
  }
297
354
  let vout = transaction.vout
298
355
  let request = checkAndParseProtocolRequest(vout, lockerAddress, {
299
356
  minTeleporterFeeAmount: this.minTeleporterFeeAmount,
300
357
  })
301
- let lockerLockingScript =
302
- transaction.addressScript || this.convertAddressToScript(lockerAddress).script.toString("hex")
358
+ let lockerLockingScript: string =
359
+ transaction.addressScript ||
360
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
303
361
 
304
362
  return {
305
363
  transaction,
@@ -309,7 +367,12 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
309
367
  }
310
368
  }
311
369
 
312
- async getLockersBurnTransactions(addresses, startBlockNumber, endBlockNumber, mempool = false) {
370
+ async getLockersBurnTransactions(
371
+ addresses: string[],
372
+ startBlockNumber: number,
373
+ endBlockNumber: number,
374
+ mempool = false,
375
+ ) {
313
376
  let transactions = mempool
314
377
  ? await this.getMempoolTransactionHistory(addresses)
315
378
  : await this.getTransactionHistory(addresses, startBlockNumber, endBlockNumber)
@@ -332,13 +395,22 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
332
395
  return validTxs
333
396
  }
334
397
 
335
- async getTransactionBurnInfoByTx(transaction, lockerAddress) {
398
+ async getTransactionBurnInfoByTx(
399
+ transaction: {
400
+ txId: any
401
+ vout?: any
402
+ vin?: any
403
+ addressScript?: any
404
+ },
405
+ lockerAddress: string,
406
+ ) {
336
407
  if (!transaction.txId) throw new Error("txId not exist")
337
408
  let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
338
409
  let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
339
410
  if (!burnInfo) return null
340
- let lockerLockingScript =
341
- transaction.addressScript || this.convertAddressToScript(lockerAddress).script.toString("hex")
411
+ let lockerLockingScript: string =
412
+ transaction.addressScript ||
413
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
342
414
  return {
343
415
  burnInfo,
344
416
  lockerAddress,
@@ -346,5 +418,3 @@ class BitcoinInterface extends BitcoinInterfaceUtils {
346
418
  }
347
419
  }
348
420
  }
349
-
350
- module.exports = BitcoinInterface