@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,185 +1,185 @@
1
- import BigNumber from "bignumber.js"
2
- import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
- import { BitcoinInterface } from "./bitcoin-interface"
4
- import { Transaction, BitcoinTokenNodeConnectionInfo } from "./type"
5
- import type { SignerInfo } from "./transaction-builder"
6
-
7
- import { sleep } from "./utils/tools"
8
- import { checkAndParseBrc20Request } from "./helper/brc20-helper"
9
-
10
- export class BitcoinInterfaceOrdinal extends BitcoinInterface {
11
- unisat: bitcoinProviders.UniSat
12
- constructor(
13
- connectionInfo: BitcoinTokenNodeConnectionInfo,
14
- networkName: string,
15
- uniSatToken: string,
16
- ) {
17
- super(networkName, connectionInfo)
18
- this.unisat = new bitcoinProviders.UniSat(
19
- {
20
- token: uniSatToken,
21
- },
22
- networkName.includes("testnet"),
23
- )
24
- }
25
- async getInscriptionWithRetry(
26
- inscriptionId: string,
27
- { numberOfRetry = 5, sleepTime = 20000 } = {},
28
- ) {
29
- let count = 0
30
- while (count <= numberOfRetry) {
31
- try {
32
- let inscription = await this.unisat.getInscriptionInfo(inscriptionId)
33
- if (inscription?.brc20) {
34
- return inscription
35
- }
36
- } catch (e: any) {
37
- console.log(e.message)
38
- }
39
- await sleep(sleepTime + count * 5000)
40
- count += 1
41
- }
42
- return null
43
- }
44
-
45
- async getTransactionBrc20TransferInfo(transaction: {
46
- txId: string
47
- vout: {
48
- address: string | null
49
- script: string
50
- value: number
51
- }[]
52
- vin: {
53
- txId: string
54
- index: number
55
- address?: string | null
56
- script?: string | null
57
- value?: number | null
58
- }[]
59
- }) {
60
- let txBrc20s: {
61
- txId: string
62
- receiver: string
63
- index: number
64
- brc20: {
65
- op: string
66
- tick: string
67
- lim: string
68
- amt: string
69
- decimal: string
70
- }
71
- inscription: bitcoinProviders.UnisatInscription
72
- }[] = []
73
- for (let x = 0; x < transaction.vout.length - 1; x += 1) {
74
- if (!transaction.vout[x].address) {
75
- return []
76
- }
77
- let inscription = await this.unisat.getInscriptionInfo(
78
- `${transaction.vin[x].txId}i${transaction.vin[x].index}`,
79
- )
80
-
81
- const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
82
- if (brc20 && inscription) {
83
- txBrc20s.push({
84
- txId: transaction.txId,
85
- receiver: transaction.vout[x].address!,
86
- index: x,
87
- inscription,
88
- brc20,
89
- })
90
- } else {
91
- return []
92
- }
93
- }
94
-
95
- return txBrc20s
96
- }
97
-
98
- async convertToBrc20WrapRequest(transaction: Transaction, lockerAddress: string) {
99
- let { vout } = transaction
100
- let request = checkAndParseBrc20Request(vout, lockerAddress)
101
- let lockerLockingScript: string =
102
- transaction.addressScript ||
103
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
104
-
105
- return {
106
- transaction,
107
- request,
108
- lockerAddress,
109
- lockerLockingScript,
110
- }
111
- }
112
-
113
- async getBrc20WrapRequests(
114
- addresses: string[],
115
- startblockNumber: number,
116
- endBlockNumber: number,
117
- ) {
118
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
119
- let transactions: Transaction[] = await this.getMultipleBlocksTransactions(
120
- addresses,
121
- startblockNumber,
122
- endBlockNumber || (await this.getLatestBlockNumber()),
123
- )
124
-
125
- let requests = []
126
- let invalidRequests = []
127
-
128
- // eslint-disable-next-line no-restricted-syntax
129
- for (let inputTx of transactions) {
130
- let data = await this.convertToBrc20WrapRequest(inputTx, inputTx.address)
131
- if (data.request.status) {
132
- let inscription = await this.getInscriptionWithRetry(
133
- `${inputTx.vin[0].txId}i${inputTx.vin[0].index}`,
134
- )
135
- const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
136
-
137
- if (!brc20) {
138
- invalidRequests.push({
139
- ...data,
140
- message: `invalid brc20 -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}`,
141
- })
142
- } else if (
143
- !BigNumber(brc20.amt)
144
- .multipliedBy(1e18)
145
- .isEqualTo(data.request.data?.inputAmount || 0)
146
- ) {
147
- invalidRequests.push({
148
- ...data,
149
- message: `invalid brc20 amount -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}:${data.request.data?.inputAmount}`,
150
- })
151
- } else {
152
- requests.push({ ...data, brc20 })
153
- }
154
- // check brc20 info
155
- // this.unisat.getBrc20TickerTxHistory(data.request.data!.brc20!.ticker, data.transaction.txId)
156
- // ignore normal transaction
157
- } else if (data.request.code !== "NO_OP_RETURN") {
158
- invalidRequests.push(data)
159
- }
160
- }
161
- return { requests, invalidRequests }
162
- }
163
-
164
- async getBTCUtxo(address: string, signerInfo: SignerInfo) {
165
- return bitcoinProviders.UniSat.convertToNormalUtxo(
166
- (await this.unisat.getBTCUtxo(address))?.utxo || [],
167
- ).map(({ txId, index, value }) => ({
168
- hash: txId,
169
- index,
170
- value,
171
- signerInfo,
172
- }))
173
- }
174
-
175
- async getInscriptionUtxo(address: string, signerInfo: SignerInfo) {
176
- return bitcoinProviders.UniSat.convertToNormalUtxo(
177
- (await this.unisat.getInscriptionUtxo(address))?.utxo || [],
178
- ).map(({ txId, index, value }) => ({
179
- hash: txId,
180
- index,
181
- value,
182
- signerInfo,
183
- }))
184
- }
185
- }
1
+ import BigNumber from "bignumber.js"
2
+ import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
+ import { BitcoinInterface } from "./bitcoin-interface"
4
+ import { Transaction, BitcoinTokenNodeConnectionInfo } from "./type"
5
+ import type { SignerInfo } from "./transaction-builder"
6
+
7
+ import { sleep } from "./utils/tools"
8
+ import { checkAndParseBrc20Request } from "./helper/brc20-helper"
9
+
10
+ export class BitcoinInterfaceOrdinal extends BitcoinInterface {
11
+ unisat: bitcoinProviders.UniSat
12
+ constructor(
13
+ connectionInfo: BitcoinTokenNodeConnectionInfo,
14
+ networkName: string,
15
+ uniSatToken: string,
16
+ ) {
17
+ super(networkName, connectionInfo)
18
+ this.unisat = new bitcoinProviders.UniSat(
19
+ {
20
+ token: uniSatToken,
21
+ },
22
+ networkName.includes("testnet"),
23
+ )
24
+ }
25
+ async getInscriptionWithRetry(
26
+ inscriptionId: string,
27
+ { numberOfRetry = 5, sleepTime = 20000 } = {},
28
+ ) {
29
+ let count = 0
30
+ while (count <= numberOfRetry) {
31
+ try {
32
+ let inscription = await this.unisat.getInscriptionInfo(inscriptionId)
33
+ if (inscription?.brc20) {
34
+ return inscription
35
+ }
36
+ } catch (e: any) {
37
+ console.log(e.message)
38
+ }
39
+ await sleep(sleepTime + count * 5000)
40
+ count += 1
41
+ }
42
+ return null
43
+ }
44
+
45
+ async getTransactionBrc20TransferInfo(transaction: {
46
+ txId: string
47
+ vout: {
48
+ address: string | null
49
+ script: string
50
+ value: number
51
+ }[]
52
+ vin: {
53
+ txId: string
54
+ index: number
55
+ address?: string | null
56
+ script?: string | null
57
+ value?: number | null
58
+ }[]
59
+ }) {
60
+ let txBrc20s: {
61
+ txId: string
62
+ receiver: string
63
+ index: number
64
+ brc20: {
65
+ op: string
66
+ tick: string
67
+ lim: string
68
+ amt: string
69
+ decimal: string
70
+ }
71
+ inscription: bitcoinProviders.UnisatInscription
72
+ }[] = []
73
+ for (let x = 0; x < transaction.vout.length - 1; x += 1) {
74
+ if (!transaction.vout[x].address) {
75
+ return []
76
+ }
77
+ let inscription = await this.unisat.getInscriptionInfo(
78
+ `${transaction.vin[x].txId}i${transaction.vin[x].index}`,
79
+ )
80
+
81
+ const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
82
+ if (brc20 && inscription) {
83
+ txBrc20s.push({
84
+ txId: transaction.txId,
85
+ receiver: transaction.vout[x].address!,
86
+ index: x,
87
+ inscription,
88
+ brc20,
89
+ })
90
+ } else {
91
+ return []
92
+ }
93
+ }
94
+
95
+ return txBrc20s
96
+ }
97
+
98
+ async convertToBrc20WrapRequest(transaction: Transaction, lockerAddress: string) {
99
+ let { vout } = transaction
100
+ let request = checkAndParseBrc20Request(vout, lockerAddress)
101
+ let lockerLockingScript: string =
102
+ transaction.addressScript ||
103
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
104
+
105
+ return {
106
+ transaction,
107
+ request,
108
+ lockerAddress,
109
+ lockerLockingScript,
110
+ }
111
+ }
112
+
113
+ async getBrc20WrapRequests(
114
+ addresses: string[],
115
+ startblockNumber: number,
116
+ endBlockNumber: number,
117
+ ) {
118
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
119
+ let transactions: Transaction[] = await this.getMultipleBlocksTransactions(
120
+ addresses,
121
+ startblockNumber,
122
+ endBlockNumber || (await this.getLatestBlockNumber()),
123
+ )
124
+
125
+ let requests = []
126
+ let invalidRequests = []
127
+
128
+ // eslint-disable-next-line no-restricted-syntax
129
+ for (let inputTx of transactions) {
130
+ let data = await this.convertToBrc20WrapRequest(inputTx, inputTx.address)
131
+ if (data.request.status) {
132
+ let inscription = await this.getInscriptionWithRetry(
133
+ `${inputTx.vin[0].txId}i${inputTx.vin[0].index}`,
134
+ )
135
+ const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
136
+
137
+ if (!brc20) {
138
+ invalidRequests.push({
139
+ ...data,
140
+ message: `invalid brc20 -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}`,
141
+ })
142
+ } else if (
143
+ !BigNumber(brc20.amt)
144
+ .multipliedBy(1e18)
145
+ .isEqualTo(data.request.data?.inputAmount || 0)
146
+ ) {
147
+ invalidRequests.push({
148
+ ...data,
149
+ message: `invalid brc20 amount -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}:${data.request.data?.inputAmount}`,
150
+ })
151
+ } else {
152
+ requests.push({ ...data, brc20 })
153
+ }
154
+ // check brc20 info
155
+ // this.unisat.getBrc20TickerTxHistory(data.request.data!.brc20!.ticker, data.transaction.txId)
156
+ // ignore normal transaction
157
+ } else if (data.request.code !== "NO_OP_RETURN") {
158
+ invalidRequests.push(data)
159
+ }
160
+ }
161
+ return { requests, invalidRequests }
162
+ }
163
+
164
+ async getBTCUtxo(address: string, signerInfo: SignerInfo) {
165
+ return bitcoinProviders.UniSat.convertToNormalUtxo(
166
+ (await this.unisat.getBTCUtxo(address))?.utxo || [],
167
+ ).map(({ txId, index, value }) => ({
168
+ hash: txId,
169
+ index,
170
+ value,
171
+ signerInfo,
172
+ }))
173
+ }
174
+
175
+ async getInscriptionUtxo(address: string, signerInfo: SignerInfo) {
176
+ return bitcoinProviders.UniSat.convertToNormalUtxo(
177
+ (await this.unisat.getInscriptionUtxo(address))?.utxo || [],
178
+ ).map(({ txId, index, value }) => ({
179
+ hash: txId,
180
+ index,
181
+ value,
182
+ signerInfo,
183
+ }))
184
+ }
185
+ }