@teleportdao/bitcoin 2.0.5 → 2.0.8

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