@teleportdao/bitcoin 1.7.1 → 1.7.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.
@@ -1,255 +1,255 @@
1
- import { bitcoin } from "@teleportdao/providers"
2
- import { parseBlockHeader } from "./bitcoin-utils"
3
- import {
4
- checkAndParseWrapRequest,
5
- getBurnTransactionInfo,
6
- UnwrapInfo,
7
- WrapOpReturnWithType,
8
- } from "./helper/teleswap-helper"
9
- import BitcoinInterface from "./bitcoin-interface"
10
- import { Transaction } from "./type"
11
-
12
- export type ValidWrapRequest = {
13
- transaction: bitcoin.Types.ConfirmedTransaction & {
14
- address?: string
15
- addressScript?: string
16
- merkleProof?: Transaction["merkleProof"]
17
- }
18
- request: {
19
- status: boolean
20
- data: WrapOpReturnWithType
21
- value: number
22
- valueOutputIndex: number
23
- }
24
- lockerAddress: string
25
- lockerLockingScript: string
26
- }
27
-
28
- export type InValidWrapRequest = {
29
- transaction: bitcoin.Types.ConfirmedTransaction & {
30
- address?: string
31
- addressScript?: string
32
- merkleProof?: Transaction["merkleProof"]
33
- }
34
- request: {
35
- status: boolean
36
- message: string
37
- code: string
38
- }
39
- lockerAddress: string
40
- lockerLockingScript: string
41
- }
42
-
43
- export type UnwrapRequest = {
44
- burnInfo: UnwrapInfo | undefined
45
- lockerAddress: string
46
- lockerLockingScript: string
47
- }
48
-
49
- export class BitcoinInterfaceTeleswap extends BitcoinInterface {
50
- // relayer
51
- async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
52
- const blockHeaders = []
53
- let difficulty = null
54
- let hexBlockHeaders = ""
55
-
56
- let fromBlockNumber = startBlockNumber
57
- for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
58
- let blockHeader = await this.getBlockHeaderHex(blockNumber)
59
- console.log("block", blockNumber)
60
- let parsedBlockHeader = parseBlockHeader(blockHeader)
61
- if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
62
- blockHeaders.push({
63
- hexBlockHeaders,
64
- fromBlockNumber,
65
- toBlockNumber: blockNumber - 1,
66
- difficulty,
67
- })
68
- hexBlockHeaders = blockHeader
69
- fromBlockNumber = blockNumber
70
- } else {
71
- hexBlockHeaders += blockHeader
72
- }
73
- difficulty = parsedBlockHeader.difficulty
74
- }
75
- if (hexBlockHeaders) {
76
- blockHeaders.push({
77
- hexBlockHeaders,
78
- fromBlockNumber,
79
- toBlockNumber: endBlockNumber,
80
- difficulty,
81
- })
82
- }
83
-
84
- return blockHeaders
85
- }
86
-
87
- async getWrapRequests(addresses: string[], startblockNumber: number, endBlockNumber: number) {
88
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
89
- let transactions = await this.getMultipleBlocksTransactions(
90
- addresses,
91
- startblockNumber,
92
- endBlockNumber,
93
- )
94
-
95
- let requests: ValidWrapRequest[] = []
96
- let invalidRequests: InValidWrapRequest[] = []
97
-
98
- for (let inputTx of transactions) {
99
- let { transaction, request, lockerAddress, lockerLockingScript } =
100
- await this.getWrapRequestByTx(inputTx, inputTx.address)
101
- if (request.status && "data" in request && request.data) {
102
- requests.push({
103
- transaction,
104
- request,
105
- lockerAddress,
106
- lockerLockingScript,
107
- })
108
- } else if (request.code !== "NO_OP_RETURN") {
109
- invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
110
- }
111
- }
112
- return { requests, invalidRequests }
113
- }
114
-
115
- async getMempoolWrapRequests(addresses: string[]) {
116
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
117
- let transactions = await this.apiProvider.getMempoolTransactionHistoryForMultipleAddresses(
118
- addresses,
119
- )
120
-
121
- let requests = []
122
- let invalidRequests = []
123
-
124
- for (let inputTx of transactions) {
125
- let { transaction, request, lockerAddress, lockerLockingScript } =
126
- await this.getWrapRequestByTx(inputTx, inputTx.address)
127
- if (request.status) {
128
- requests.push({ transaction, request, lockerAddress, lockerLockingScript })
129
- } else if (request.code !== "NO_OP_RETURN") {
130
- invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
131
- }
132
- }
133
- return { requests, invalidRequests }
134
- }
135
-
136
- async getWrapRequestByTx(
137
- inputTransaction:
138
- | { txId: string }
139
- | (bitcoin.Types.ConfirmedTransaction & {
140
- address?: string
141
- addressScript?: string
142
- merkleProof?: Transaction["merkleProof"]
143
- }),
144
- lockerAddress: string,
145
- minTeleporterFeeAmount = 0,
146
- ) {
147
- let transaction: bitcoin.Types.ConfirmedTransaction & {
148
- address?: string
149
- addressScript?: string
150
- merkleProof?: Transaction["merkleProof"]
151
- }
152
- if ("vout" in inputTransaction && "blockNumber" in inputTransaction) {
153
- transaction = inputTransaction
154
- } else {
155
- if (!inputTransaction.txId) throw new Error("txId not exist")
156
- transaction = await this.getTransaction(inputTransaction.txId)
157
- }
158
-
159
- let vout = transaction.vout
160
- let request = checkAndParseWrapRequest(vout!, lockerAddress, {
161
- minTeleporterFeeAmount,
162
- })
163
- let lockerLockingScript: string =
164
- transaction.addressScript ||
165
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
166
-
167
- return {
168
- transaction,
169
- request,
170
- lockerAddress,
171
- lockerLockingScript,
172
- }
173
- }
174
-
175
- async getLockersUnWrapTransactions(
176
- addresses: string[],
177
- startBlockNumber: number,
178
- endBlockNumber: number,
179
- ) {
180
- let transactions = await this.getMultipleBlocksTransactions(
181
- addresses,
182
- startBlockNumber,
183
- endBlockNumber,
184
- )
185
-
186
- let validTxs = []
187
- for (let transaction of transactions) {
188
- let address = transaction.address
189
- // check if its a transaction to spend btc
190
- let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
191
- if (txBurnInfo) {
192
- const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
193
- validTxs.push({
194
- transaction,
195
- burnInfo,
196
- lockerAddress,
197
- lockerLockingScript,
198
- })
199
- }
200
- }
201
- return validTxs
202
- }
203
-
204
- async getLockersUnwrapMEmpoolTransactions(
205
- addresses: string[],
206
- startBlockNumber: number,
207
- endBlockNumber: number,
208
- ) {
209
- let transactions = await this.getMultipleBlocksTransactions(
210
- addresses,
211
- startBlockNumber,
212
- endBlockNumber,
213
- )
214
-
215
- let validTxs = []
216
- for (let transaction of transactions) {
217
- let address = transaction.address
218
- // check if its a transaction to spend btc
219
- let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
220
- if (txBurnInfo) {
221
- const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
222
- validTxs.push({
223
- transaction,
224
- burnInfo,
225
- lockerAddress,
226
- lockerLockingScript,
227
- })
228
- }
229
- }
230
- return validTxs
231
- }
232
-
233
- async getTransactionUnwrapInfoByTx(
234
- transaction: {
235
- txId: any
236
- vout?: any
237
- vin?: any
238
- addressScript?: any
239
- },
240
- lockerAddress: string,
241
- ) {
242
- if (!transaction.txId) throw new Error("txId not exist")
243
- let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
244
- let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
245
- if (!burnInfo) return
246
- let lockerLockingScript: string =
247
- transaction.addressScript ||
248
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
249
- return {
250
- burnInfo,
251
- lockerAddress,
252
- lockerLockingScript,
253
- } as UnwrapRequest
254
- }
255
- }
1
+ import { bitcoin } from "@teleportdao/providers"
2
+ import { parseBlockHeader } from "./bitcoin-utils"
3
+ import {
4
+ checkAndParseWrapRequest,
5
+ getBurnTransactionInfo,
6
+ UnwrapInfo,
7
+ WrapOpReturnWithType,
8
+ } from "./helper/teleswap-helper"
9
+ import BitcoinInterface from "./bitcoin-interface"
10
+ import { Transaction } from "./type"
11
+
12
+ export type ValidWrapRequest = {
13
+ transaction: bitcoin.Types.ConfirmedTransaction & {
14
+ address?: string
15
+ addressScript?: string
16
+ merkleProof?: Transaction["merkleProof"]
17
+ }
18
+ request: {
19
+ status: boolean
20
+ data: WrapOpReturnWithType
21
+ value: number
22
+ valueOutputIndex: number
23
+ }
24
+ lockerAddress: string
25
+ lockerLockingScript: string
26
+ }
27
+
28
+ export type InValidWrapRequest = {
29
+ transaction: bitcoin.Types.ConfirmedTransaction & {
30
+ address?: string
31
+ addressScript?: string
32
+ merkleProof?: Transaction["merkleProof"]
33
+ }
34
+ request: {
35
+ status: boolean
36
+ message: string
37
+ code: string
38
+ }
39
+ lockerAddress: string
40
+ lockerLockingScript: string
41
+ }
42
+
43
+ export type UnwrapRequest = {
44
+ burnInfo: UnwrapInfo | undefined
45
+ lockerAddress: string
46
+ lockerLockingScript: string
47
+ }
48
+
49
+ export class BitcoinInterfaceTeleswap extends BitcoinInterface {
50
+ // relayer
51
+ async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
52
+ const blockHeaders = []
53
+ let difficulty = null
54
+ let hexBlockHeaders = ""
55
+
56
+ let fromBlockNumber = startBlockNumber
57
+ for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
58
+ let blockHeader = await this.getBlockHeaderHex(blockNumber)
59
+ console.log("block", blockNumber)
60
+ let parsedBlockHeader = parseBlockHeader(blockHeader)
61
+ if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
62
+ blockHeaders.push({
63
+ hexBlockHeaders,
64
+ fromBlockNumber,
65
+ toBlockNumber: blockNumber - 1,
66
+ difficulty,
67
+ })
68
+ hexBlockHeaders = blockHeader
69
+ fromBlockNumber = blockNumber
70
+ } else {
71
+ hexBlockHeaders += blockHeader
72
+ }
73
+ difficulty = parsedBlockHeader.difficulty
74
+ }
75
+ if (hexBlockHeaders) {
76
+ blockHeaders.push({
77
+ hexBlockHeaders,
78
+ fromBlockNumber,
79
+ toBlockNumber: endBlockNumber,
80
+ difficulty,
81
+ })
82
+ }
83
+
84
+ return blockHeaders
85
+ }
86
+
87
+ async getWrapRequests(addresses: string[], startblockNumber: number, endBlockNumber: number) {
88
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
89
+ let transactions = await this.getMultipleBlocksTransactions(
90
+ addresses,
91
+ startblockNumber,
92
+ endBlockNumber,
93
+ )
94
+
95
+ let requests: ValidWrapRequest[] = []
96
+ let invalidRequests: InValidWrapRequest[] = []
97
+
98
+ for (let inputTx of transactions) {
99
+ let { transaction, request, lockerAddress, lockerLockingScript } =
100
+ await this.getWrapRequestByTx(inputTx, inputTx.address)
101
+ if (request.status && "data" in request && request.data) {
102
+ requests.push({
103
+ transaction,
104
+ request,
105
+ lockerAddress,
106
+ lockerLockingScript,
107
+ })
108
+ } else if (request.code !== "NO_OP_RETURN") {
109
+ invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
110
+ }
111
+ }
112
+ return { requests, invalidRequests }
113
+ }
114
+
115
+ async getMempoolWrapRequests(addresses: string[]) {
116
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
117
+ let transactions = await this.apiProvider.getMempoolTransactionHistoryForMultipleAddresses(
118
+ addresses,
119
+ )
120
+
121
+ let requests = []
122
+ let invalidRequests = []
123
+
124
+ for (let inputTx of transactions) {
125
+ let { transaction, request, lockerAddress, lockerLockingScript } =
126
+ await this.getWrapRequestByTx(inputTx, inputTx.address)
127
+ if (request.status) {
128
+ requests.push({ transaction, request, lockerAddress, lockerLockingScript })
129
+ } else if (request.code !== "NO_OP_RETURN") {
130
+ invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
131
+ }
132
+ }
133
+ return { requests, invalidRequests }
134
+ }
135
+
136
+ async getWrapRequestByTx(
137
+ inputTransaction:
138
+ | { txId: string }
139
+ | (bitcoin.Types.ConfirmedTransaction & {
140
+ address?: string
141
+ addressScript?: string
142
+ merkleProof?: Transaction["merkleProof"]
143
+ }),
144
+ lockerAddress: string,
145
+ minTeleporterFeeAmount = 0,
146
+ ) {
147
+ let transaction: bitcoin.Types.ConfirmedTransaction & {
148
+ address?: string
149
+ addressScript?: string
150
+ merkleProof?: Transaction["merkleProof"]
151
+ }
152
+ if ("vout" in inputTransaction && "blockNumber" in inputTransaction) {
153
+ transaction = inputTransaction
154
+ } else {
155
+ if (!inputTransaction.txId) throw new Error("txId not exist")
156
+ transaction = await this.getTransaction(inputTransaction.txId)
157
+ }
158
+
159
+ let vout = transaction.vout
160
+ let request = checkAndParseWrapRequest(vout!, lockerAddress, {
161
+ minTeleporterFeeAmount,
162
+ })
163
+ let lockerLockingScript: string =
164
+ transaction.addressScript ||
165
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
166
+
167
+ return {
168
+ transaction,
169
+ request,
170
+ lockerAddress,
171
+ lockerLockingScript,
172
+ }
173
+ }
174
+
175
+ async getLockersUnWrapTransactions(
176
+ addresses: string[],
177
+ startBlockNumber: number,
178
+ endBlockNumber: number,
179
+ ) {
180
+ let transactions = await this.getMultipleBlocksTransactions(
181
+ addresses,
182
+ startBlockNumber,
183
+ endBlockNumber,
184
+ )
185
+
186
+ let validTxs = []
187
+ for (let transaction of transactions) {
188
+ let address = transaction.address
189
+ // check if its a transaction to spend btc
190
+ let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
191
+ if (txBurnInfo) {
192
+ const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
193
+ validTxs.push({
194
+ transaction,
195
+ burnInfo,
196
+ lockerAddress,
197
+ lockerLockingScript,
198
+ })
199
+ }
200
+ }
201
+ return validTxs
202
+ }
203
+
204
+ async getLockersUnwrapMEmpoolTransactions(
205
+ addresses: string[],
206
+ startBlockNumber: number,
207
+ endBlockNumber: number,
208
+ ) {
209
+ let transactions = await this.getMultipleBlocksTransactions(
210
+ addresses,
211
+ startBlockNumber,
212
+ endBlockNumber,
213
+ )
214
+
215
+ let validTxs = []
216
+ for (let transaction of transactions) {
217
+ let address = transaction.address
218
+ // check if its a transaction to spend btc
219
+ let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
220
+ if (txBurnInfo) {
221
+ const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
222
+ validTxs.push({
223
+ transaction,
224
+ burnInfo,
225
+ lockerAddress,
226
+ lockerLockingScript,
227
+ })
228
+ }
229
+ }
230
+ return validTxs
231
+ }
232
+
233
+ async getTransactionUnwrapInfoByTx(
234
+ transaction: {
235
+ txId: any
236
+ vout?: any
237
+ vin?: any
238
+ addressScript?: any
239
+ },
240
+ lockerAddress: string,
241
+ ) {
242
+ if (!transaction.txId) throw new Error("txId not exist")
243
+ let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
244
+ let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
245
+ if (!burnInfo) return
246
+ let lockerLockingScript: string =
247
+ transaction.addressScript ||
248
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
249
+ return {
250
+ burnInfo,
251
+ lockerAddress,
252
+ lockerLockingScript,
253
+ } as UnwrapRequest
254
+ }
255
+ }
@@ -1,59 +1,59 @@
1
- import type { Network } from "bitcoinjs-lib"
2
- import networks from "./utils/networks"
3
- import {
4
- createAddressObjectByHash,
5
- createAddressObjectByAddress,
6
- createAddressObjectByPublicKey,
7
- createAddressObjectByScript,
8
- } from "./bitcoin-utils"
9
-
10
- export class BitcoinInterfaceUtils {
11
- testnet: boolean
12
- network: Network
13
- constructor(networkName: string) {
14
- this.testnet = networkName.includes("_testnet")
15
- this.network = networks[networkName]
16
- }
17
-
18
- convertHashToAddress(hashHex: string, addressType: string) {
19
- let addressObj = createAddressObjectByHash(
20
- { addressType, hash: Buffer.from(hashHex, "hex") },
21
- this.network,
22
- )
23
- if (!addressObj.address) throw new Error("incorrect input")
24
- return addressObj.address
25
- }
26
-
27
- convertScriptToAddress(scriptHex: string, addressType: string) {
28
- let addressObj = createAddressObjectByScript(
29
- { addressType, script: Buffer.from(scriptHex, "hex") },
30
- this.network,
31
- )
32
- if (!addressObj.address) throw new Error("incorrect input")
33
- return addressObj.address
34
- }
35
-
36
- convertAddressToScript(address: string) {
37
- let { addressObject, addressType } = createAddressObjectByAddress(address, this.network)
38
- return {
39
- script: addressObject.output,
40
- hash: addressObject.hash,
41
- addressType,
42
- }
43
- }
44
-
45
- convertAddressToObject(address: string) {
46
- let addObj = createAddressObjectByAddress(address, this.network)
47
- return addObj
48
- }
49
-
50
- createAddressObjectByPublicKey(publicKey: string, addressType: string) {
51
- let addObj = createAddressObjectByPublicKey(
52
- { publicKey: Buffer.from(publicKey, "hex"), addressType },
53
- this.network,
54
- )
55
- return addObj
56
- }
57
- }
58
-
59
- export default BitcoinInterfaceUtils
1
+ import type { Network } from "bitcoinjs-lib"
2
+ import networks from "./utils/networks"
3
+ import {
4
+ createAddressObjectByHash,
5
+ createAddressObjectByAddress,
6
+ createAddressObjectByPublicKey,
7
+ createAddressObjectByScript,
8
+ } from "./bitcoin-utils"
9
+
10
+ export class BitcoinInterfaceUtils {
11
+ testnet: boolean
12
+ network: Network
13
+ constructor(networkName: string) {
14
+ this.testnet = networkName.includes("_testnet")
15
+ this.network = networks[networkName]
16
+ }
17
+
18
+ convertHashToAddress(hashHex: string, addressType: string) {
19
+ let addressObj = createAddressObjectByHash(
20
+ { addressType, hash: Buffer.from(hashHex, "hex") },
21
+ this.network,
22
+ )
23
+ if (!addressObj.address) throw new Error("incorrect input")
24
+ return addressObj.address
25
+ }
26
+
27
+ convertScriptToAddress(scriptHex: string, addressType: string) {
28
+ let addressObj = createAddressObjectByScript(
29
+ { addressType, script: Buffer.from(scriptHex, "hex") },
30
+ this.network,
31
+ )
32
+ if (!addressObj.address) throw new Error("incorrect input")
33
+ return addressObj.address
34
+ }
35
+
36
+ convertAddressToScript(address: string) {
37
+ let { addressObject, addressType } = createAddressObjectByAddress(address, this.network)
38
+ return {
39
+ script: addressObject.output,
40
+ hash: addressObject.hash,
41
+ addressType,
42
+ }
43
+ }
44
+
45
+ convertAddressToObject(address: string) {
46
+ let addObj = createAddressObjectByAddress(address, this.network)
47
+ return addObj
48
+ }
49
+
50
+ createAddressObjectByPublicKey(publicKey: string, addressType: string) {
51
+ let addObj = createAddressObjectByPublicKey(
52
+ { publicKey: Buffer.from(publicKey, "hex"), addressType },
53
+ this.network,
54
+ )
55
+ return addObj
56
+ }
57
+ }
58
+
59
+ export default BitcoinInterfaceUtils