@teleportdao/bitcoin 1.7.7 → 1.7.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 (39) hide show
  1. package/package.json +3 -3
  2. package/src/bitcoin-interface-ordinal.ts +181 -181
  3. package/src/bitcoin-interface-teleswap.ts +252 -252
  4. package/src/bitcoin-interface-utils.ts +59 -59
  5. package/src/bitcoin-interface.ts +247 -247
  6. package/src/bitcoin-utils.ts +591 -591
  7. package/src/bitcoin-wallet-base.ts +310 -310
  8. package/src/helper/brc20-helper.ts +181 -181
  9. package/src/helper/ordinal-helper.ts +118 -118
  10. package/src/index.ts +15 -15
  11. package/src/ordinal-wallet.ts +738 -738
  12. package/src/sign/index.ts +1 -1
  13. package/src/sign/sign-transaction.ts +108 -108
  14. package/src/teleswap-wallet.ts +152 -152
  15. package/src/transaction-builder/bitcoin-transaction-builder.ts +44 -44
  16. package/src/transaction-builder/index.ts +3 -3
  17. package/src/transaction-builder/ordinal-transaction-builder.ts +147 -147
  18. package/src/transaction-builder/transaction-builder.ts +706 -706
  19. package/src/type.ts +43 -43
  20. package/src/utils/networks.ts +33 -33
  21. package/src/utils/tools.ts +89 -89
  22. package/tsconfig.json +9 -9
  23. package/webpack.config.js +16 -16
  24. package/dist/bitcoin-base.d.ts +0 -93
  25. package/dist/bitcoin-base.d.ts.map +0 -1
  26. package/dist/bitcoin-base.js +0 -236
  27. package/dist/bitcoin-base.js.map +0 -1
  28. package/dist/helper/burn-request-helper.d.ts +0 -7
  29. package/dist/helper/burn-request-helper.d.ts.map +0 -1
  30. package/dist/helper/burn-request-helper.js +0 -26
  31. package/dist/helper/burn-request-helper.js.map +0 -1
  32. package/dist/helper/teleport-request-helper.d.ts +0 -47
  33. package/dist/helper/teleport-request-helper.d.ts.map +0 -1
  34. package/dist/helper/teleport-request-helper.js +0 -146
  35. package/dist/helper/teleport-request-helper.js.map +0 -1
  36. package/dist/teleport-dao-payments.d.ts +0 -76
  37. package/dist/teleport-dao-payments.d.ts.map +0 -1
  38. package/dist/teleport-dao-payments.js +0 -217
  39. package/dist/teleport-dao-payments.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teleportdao/bitcoin",
3
- "version": "1.7.7",
3
+ "version": "1.7.8",
4
4
  "description": "teleswap bitcoin package",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
16
  "@bitcoinerlab/secp256k1": "^1.0.5",
17
- "@teleportdao/configs": "^1.7.6",
17
+ "@teleportdao/configs": "^1.7.8",
18
18
  "@teleportdao/providers": "^1.7.7",
19
19
  "axios": "^0.27.2",
20
20
  "bignumber.js": "^9.1.1",
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "371f3ff04b9cfaa48fd3d3934b7bb783d957b620"
34
+ "gitHead": "2f6363569b5c5623f48ec2618b028b5353775ad7"
35
35
  }
@@ -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
+ }