@teleportdao/bitcoin 1.7.21 → 1.8.4

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