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