@teleportdao/bitcoin 1.9.0 → 2.0.2

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