@teleportdao/bitcoin 1.6.1 → 1.7.1

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 (129) hide show
  1. package/.tmp/check.ts +101 -0
  2. package/.tmp/psbt/sign-transaction.ts +121 -0
  3. package/.tmp/rbf.ts +45 -0
  4. package/dist/bitcoin-interface-ordinal.d.ts +109 -0
  5. package/dist/bitcoin-interface-ordinal.d.ts.map +1 -0
  6. package/dist/bitcoin-interface-ordinal.js +141 -0
  7. package/dist/bitcoin-interface-ordinal.js.map +1 -0
  8. package/dist/bitcoin-interface-teleswap.d.ts +148 -0
  9. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -0
  10. package/dist/bitcoin-interface-teleswap.js +179 -0
  11. package/dist/bitcoin-interface-teleswap.js.map +1 -0
  12. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  13. package/dist/bitcoin-interface-utils.js.map +1 -1
  14. package/dist/bitcoin-interface.d.ts +45 -333
  15. package/dist/bitcoin-interface.d.ts.map +1 -1
  16. package/dist/bitcoin-interface.js +68 -202
  17. package/dist/bitcoin-interface.js.map +1 -1
  18. package/dist/bitcoin-utils.d.ts +12 -59
  19. package/dist/bitcoin-utils.d.ts.map +1 -1
  20. package/dist/bitcoin-utils.js +61 -61
  21. package/dist/bitcoin-utils.js.map +1 -1
  22. package/dist/{bitcoin-base.d.ts → bitcoin-wallet-base.d.ts} +9 -9
  23. package/dist/bitcoin-wallet-base.d.ts.map +1 -0
  24. package/dist/{bitcoin-base.js → bitcoin-wallet-base.js} +15 -14
  25. package/dist/bitcoin-wallet-base.js.map +1 -0
  26. package/dist/helper/brc20-helper.d.ts +43 -0
  27. package/dist/helper/brc20-helper.d.ts.map +1 -0
  28. package/dist/helper/brc20-helper.js +129 -0
  29. package/dist/helper/brc20-helper.js.map +1 -0
  30. package/dist/helper/index.d.ts +4 -0
  31. package/dist/helper/index.d.ts.map +1 -0
  32. package/dist/helper/index.js +30 -0
  33. package/dist/helper/index.js.map +1 -0
  34. package/dist/helper/ordinal-helper.d.ts +13 -0
  35. package/dist/helper/ordinal-helper.d.ts.map +1 -0
  36. package/dist/helper/ordinal-helper.js +127 -0
  37. package/dist/helper/ordinal-helper.js.map +1 -0
  38. package/dist/helper/teleswap-helper.d.ts +84 -0
  39. package/dist/helper/teleswap-helper.d.ts.map +1 -0
  40. package/dist/helper/teleswap-helper.js +181 -0
  41. package/dist/helper/teleswap-helper.js.map +1 -0
  42. package/dist/index.d.ts +8 -3
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +8 -3
  45. package/dist/index.js.map +1 -1
  46. package/dist/ordinal-wallet.d.ts +536 -0
  47. package/dist/ordinal-wallet.d.ts.map +1 -0
  48. package/dist/ordinal-wallet.js +448 -0
  49. package/dist/ordinal-wallet.js.map +1 -0
  50. package/dist/sign/sign-transaction.d.ts +2 -4
  51. package/dist/sign/sign-transaction.d.ts.map +1 -1
  52. package/dist/sign/sign-transaction.js +26 -22
  53. package/dist/sign/sign-transaction.js.map +1 -1
  54. package/dist/teleswap-wallet.d.ts +54 -0
  55. package/dist/teleswap-wallet.d.ts.map +1 -0
  56. package/dist/teleswap-wallet.js +87 -0
  57. package/dist/teleswap-wallet.js.map +1 -0
  58. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +2 -20
  59. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  60. package/dist/transaction-builder/bitcoin-transaction-builder.js +5 -4
  61. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  62. package/dist/transaction-builder/index.d.ts +2 -1
  63. package/dist/transaction-builder/index.d.ts.map +1 -1
  64. package/dist/transaction-builder/index.js +2 -6
  65. package/dist/transaction-builder/index.js.map +1 -1
  66. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -0
  67. package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -0
  68. package/dist/transaction-builder/ordinal-transaction-builder.js +131 -0
  69. package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -0
  70. package/dist/transaction-builder/transaction-builder.d.ts +10 -7
  71. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  72. package/dist/transaction-builder/transaction-builder.js +18 -9
  73. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  74. package/dist/type.d.ts +43 -0
  75. package/dist/type.d.ts.map +1 -0
  76. package/dist/type.js +3 -0
  77. package/dist/type.js.map +1 -0
  78. package/dist/utils/tools.d.ts +4 -4
  79. package/dist/utils/tools.d.ts.map +1 -1
  80. package/dist/utils/tools.js +8 -5
  81. package/dist/utils/tools.js.map +1 -1
  82. package/package.json +6 -9
  83. package/src/bitcoin-interface-ordinal.ts +181 -0
  84. package/src/bitcoin-interface-teleswap.ts +255 -0
  85. package/src/bitcoin-interface-utils.ts +1 -1
  86. package/src/bitcoin-interface.ts +99 -303
  87. package/src/bitcoin-utils.ts +65 -90
  88. package/src/{bitcoin-base.ts → bitcoin-wallet-base.ts} +24 -19
  89. package/src/helper/brc20-helper.ts +181 -0
  90. package/src/helper/index.ts +3 -0
  91. package/src/helper/ordinal-helper.ts +118 -0
  92. package/src/helper/teleswap-helper.ts +300 -0
  93. package/src/index.ts +11 -3
  94. package/src/ordinal-wallet.ts +738 -0
  95. package/src/sign/sign-transaction.ts +42 -33
  96. package/src/teleswap-wallet.ts +155 -0
  97. package/src/transaction-builder/bitcoin-transaction-builder.ts +7 -24
  98. package/src/transaction-builder/index.ts +2 -1
  99. package/src/transaction-builder/ordinal-transaction-builder.ts +147 -0
  100. package/src/transaction-builder/transaction-builder.ts +33 -15
  101. package/src/type.ts +43 -0
  102. package/src/utils/tools.ts +17 -11
  103. package/tsconfig.json +1 -2
  104. package/dist/bitcoin-base.d.ts.map +0 -1
  105. package/dist/bitcoin-base.js.map +0 -1
  106. package/dist/bitcoin-utils-2.d.ts +0 -2
  107. package/dist/bitcoin-utils-2.d.ts.map +0 -1
  108. package/dist/bitcoin-utils-2.js +0 -13
  109. package/dist/bitcoin-utils-2.js.map +0 -1
  110. package/dist/bundle.js +0 -17
  111. package/dist/helper/burn-request-helper.d.ts +0 -7
  112. package/dist/helper/burn-request-helper.d.ts.map +0 -1
  113. package/dist/helper/burn-request-helper.js +0 -26
  114. package/dist/helper/burn-request-helper.js.map +0 -1
  115. package/dist/helper/teleport-request-helper.d.ts +0 -47
  116. package/dist/helper/teleport-request-helper.d.ts.map +0 -1
  117. package/dist/helper/teleport-request-helper.js +0 -146
  118. package/dist/helper/teleport-request-helper.js.map +0 -1
  119. package/dist/mempool-space.d.ts +0 -69
  120. package/dist/mempool-space.d.ts.map +0 -1
  121. package/dist/mempool-space.js +0 -266
  122. package/dist/mempool-space.js.map +0 -1
  123. package/dist/teleport-dao-payments.d.ts +0 -76
  124. package/dist/teleport-dao-payments.d.ts.map +0 -1
  125. package/dist/teleport-dao-payments.js +0 -217
  126. package/dist/teleport-dao-payments.js.map +0 -1
  127. package/src/helper/burn-request-helper.js +0 -27
  128. package/src/helper/teleport-request-helper.js +0 -181
  129. package/src/teleport-dao-payments.ts +0 -347
@@ -0,0 +1,300 @@
1
+ import { teleswap } from "@teleportdao/configs"
2
+ import BigNumber from "bignumber.js"
3
+
4
+ type Vin = {
5
+ address: string
6
+ value: number
7
+ }
8
+
9
+ type Vout = {
10
+ address?: string
11
+ value: number
12
+ script: string
13
+ }
14
+
15
+ export type WrapOpReturn = {
16
+ chainId: number
17
+ appId: number
18
+ recipientAddress: string
19
+ percentageFee: number
20
+ speed: boolean
21
+ //
22
+ outputToken?: string
23
+ outputAmount?: string
24
+ deadline?: string
25
+ isFixedToken?: boolean
26
+ }
27
+
28
+ export type UnwrapInfo = {
29
+ receivers: (Vout & {
30
+ index: number
31
+ })[]
32
+ changes: (Vout & {
33
+ index: number
34
+ })[]
35
+ totalInputValue: number
36
+ lockerVin: {
37
+ vinIndex: number
38
+ address: string
39
+ value: number
40
+ }
41
+ }
42
+
43
+ export type WrapOpReturnWithType = WrapOpReturn & {
44
+ requestType: "wrap" | "wrapAndSwap"
45
+ }
46
+
47
+ const wrapOpReturnLength = {
48
+ chainId: 2,
49
+ appId: 4,
50
+ percentageFee: 4,
51
+ outputAmount: 56,
52
+ deadline: 8,
53
+ }
54
+
55
+ export function generateWrapOpReturn({
56
+ chainId,
57
+ appId,
58
+ recipientAddress, // 20 bytes
59
+ percentageFee, // 2 bytes in percent %
60
+ speed = false, // 1 byte
61
+ isExchange = false,
62
+ outputToken, // 20 bytes
63
+ outputAmount, // 28 bytes
64
+ deadline, // 4 bytes
65
+ isFixedToken = false, // 1 byte
66
+ }: WrapOpReturn & {
67
+ isExchange?: boolean
68
+ }) {
69
+ let data = ""
70
+ if (BigNumber(chainId).toString(16).length > wrapOpReturnLength.chainId) {
71
+ throw new Error("invalid chainId")
72
+ }
73
+ if (BigNumber(appId).toString(16).length > wrapOpReturnLength.appId) {
74
+ throw new Error("invalid appId")
75
+ }
76
+ if (BigNumber(percentageFee).toString(16).length > wrapOpReturnLength.percentageFee) {
77
+ throw new Error("invalid percentageFee")
78
+ }
79
+ data += BigNumber(chainId).toString(16).padStart(2, "0")
80
+ data += BigNumber(appId).toString(16).padStart(4, "0")
81
+ data += recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0")
82
+ data += BigNumber((percentageFee * 100).toFixed(0))
83
+ .toString(16)
84
+ .padStart(4, "0")
85
+ data += speed ? "01" : "00"
86
+ if (!isExchange) {
87
+ if (data.length !== 26 * 2) throw new Error("invalid data length")
88
+ return data
89
+ }
90
+ if (!outputToken) {
91
+ throw new Error("invalid outputToken")
92
+ }
93
+ if (
94
+ !outputAmount ||
95
+ BigNumber(outputAmount).toString(16).length > wrapOpReturnLength.outputAmount
96
+ ) {
97
+ throw new Error("invalid outputAmount")
98
+ }
99
+ if (!deadline || BigNumber(deadline).toString(16).length > wrapOpReturnLength.deadline) {
100
+ throw new Error("invalid deadline")
101
+ }
102
+
103
+ data += outputToken.replace("0x", "").toLowerCase().padStart(40, "0")
104
+ data += BigNumber(outputAmount).toString(16).padStart(56, "0")
105
+ data += BigNumber(deadline).toString(16).padStart(8, "0")
106
+ data += isFixedToken ? "01" : "00"
107
+ if (data.length !== 79 * 2) throw new Error("invalid data length")
108
+ return data
109
+ }
110
+
111
+ export function getBurnTransactionInfo(address: string, vin: Vin[] = [], vouts: Vout[] = []) {
112
+ let lockerVinIndex = vin.findIndex((vi) => vi.address === address)
113
+ if (lockerVinIndex >= 0) {
114
+ let lockerVin = { ...vin[lockerVinIndex], vinIndex: lockerVinIndex }
115
+ let totalInputValue = vin.reduce((acc, current) => acc + current.value, 0)
116
+ let receivers: (Vout & {
117
+ index: number
118
+ })[] = []
119
+ let changes: (Vout & {
120
+ index: number
121
+ })[] = []
122
+ vouts.forEach((vout, i) => {
123
+ let voutWithIndex = {
124
+ ...vout,
125
+ index: i,
126
+ }
127
+ if (voutWithIndex.address === address) {
128
+ changes.push(voutWithIndex)
129
+ } else {
130
+ receivers.push(voutWithIndex)
131
+ }
132
+ })
133
+ return { receivers, changes, totalInputValue, lockerVin }
134
+ }
135
+ return
136
+ }
137
+
138
+ //
139
+
140
+ function parseWrapRequest(data: string) {
141
+ let parsedData: any = {}
142
+ parsedData.requestType = "wrap"
143
+ let offset = 0
144
+ parsedData.chainId = BigNumber(`0x${data.slice(offset, (offset += 2))}`).toNumber() // 1 bytes
145
+ parsedData.appId = BigNumber(`0x${data.slice(offset, (offset += 4))}`).toNumber() // 2 bytes
146
+ parsedData.recipientAddress = `0x${data.slice(offset, (offset += 40))}` // 20 bytes
147
+ parsedData.percentageFee = BigNumber(`0x${data.slice(offset, (offset += 4))}`)
148
+ .dividedBy(100)
149
+ .toNumber() // 2 bytes
150
+ parsedData.speed = data.slice(offset, (offset += 2)) === "01" // 1 byte
151
+ if (data.length === offset) {
152
+ return {
153
+ status: true,
154
+ data: parsedData as WrapOpReturnWithType,
155
+ }
156
+ }
157
+
158
+ parsedData.requestType = "swapAndWrap"
159
+ parsedData.exchangeTokenAddress = `0x${data.slice(offset, (offset += 40))}` // 20 bytes
160
+ parsedData.outputAmount = new BigNumber(`0x${data.slice(offset, (offset += 56))}`).toFixed(0) // 28 bytes
161
+ parsedData.deadline = new BigNumber(`0x${data.slice(offset, (offset += 8))}`).toFixed(0) // 4 bytes
162
+ parsedData.isFixedToken = data.slice(offset, (offset += 2)) === "01" // 1 byte
163
+ if (data.length === offset) {
164
+ return {
165
+ status: true,
166
+ data: parsedData as WrapOpReturnWithType,
167
+ }
168
+ }
169
+
170
+ return {
171
+ status: false,
172
+ message: `invalid OP_RETURN data for requestType: 'transfer or exchange'. invalid data length : ${data.length} - valid length : ${offset}`,
173
+ code: "INVALID_OP_RETURN",
174
+ }
175
+ }
176
+
177
+ // function parseLendAndBorrowRequest(data: string) {
178
+ // let parsedData: any = {}
179
+ // parsedData.requestType = "lend"
180
+
181
+ // let offset = 0
182
+ // parsedData.chainId = BigNumber(`0x${data.slice(offset, (offset += 2))}`) // 1 bytes
183
+ // parsedData.appId = BigNumber(`0x${data.slice(offset, (offset += 4))}`) // 2 bytes
184
+ // parsedData.recipientAddress = `0x${data.slice(offset, (offset += 40))}` // 20 bytes
185
+ // parsedData.percentageFee = BigNumber(`0x${data.slice(offset, (offset += 4))}`) / 100 // 2 bytes
186
+ // parsedData.mode = BigNumber(`0x${data.slice(offset, (offset += 2))}`) // 1 byte
187
+ // if (data.length === offset) {
188
+ // return {
189
+ // status: true,
190
+ // data: parsedData,
191
+ // }
192
+ // }
193
+ // parsedData.requestType = "borrow"
194
+ // parsedData.tokenAddress = `0x${data.slice(offset, (offset += 40))}` // 20 bytes
195
+ // parsedData.borrowAmount = BigNumber(`0x${data.slice(offset, (offset += 56))}`) // 28 bytes
196
+ // if (data.length === offset) {
197
+ // return {
198
+ // status: true,
199
+ // data: parsedData,
200
+ // }
201
+ // }
202
+ // return {
203
+ // status: false,
204
+ // message: `invalid OP_RETURN data for requestType: 'lend or borrow'. invalid data length : ${data.length} - valid length : ${offset}`,
205
+ // code: "INVALID_OP_RETURN",
206
+ // }
207
+ // }
208
+
209
+ export function parseWrapRawRequest(opReturnData: string) {
210
+ let data = opReturnData.slice(2, 4) === "4c" ? opReturnData.slice(6) : opReturnData.slice(4)
211
+ let appIdHex = data.slice(2, 6) // 2 bytes
212
+ if (!appIdHex) {
213
+ return {
214
+ status: false,
215
+ message: `invalid OP_RETURN data : ${data}`,
216
+ code: "INVALID_APP_ID",
217
+ }
218
+ }
219
+
220
+ let appId = BigNumber(`0x${appIdHex}`).toNumber() // 2 bytes
221
+
222
+ // get type base on appId
223
+ let requestType = Object.keys(teleswap.requestAppId).find(
224
+ (key) =>
225
+ appId >= teleswap.requestAppId[key as keyof typeof teleswap.requestAppId].appIdRange[0] &&
226
+ appId <= teleswap.requestAppId[key as keyof typeof teleswap.requestAppId].appIdRange[1],
227
+ )
228
+
229
+ switch (requestType) {
230
+ case "wrap":
231
+ case "wrapAndSwap":
232
+ return parseWrapRequest(data)
233
+ default:
234
+ return {
235
+ status: false,
236
+ message: `invalid appId : ${appId}`,
237
+ code: "INVALID_OP_RETURN",
238
+ }
239
+ }
240
+ }
241
+
242
+ export function checkAndParseWrapRequest(
243
+ vouts: Vout[],
244
+ lockerAddress: string,
245
+ { minTeleporterFeeAmount = 0 } = {},
246
+ ) {
247
+ let requestOutputIndex = vouts.findIndex((vout_) => vout_.script.startsWith("6a"))
248
+ if (requestOutputIndex >= 0) {
249
+ let opReturnData = vouts[requestOutputIndex]?.script || null
250
+ if (!opReturnData) {
251
+ return {
252
+ status: false,
253
+ message: "no data to validate. it should not happen",
254
+ code: "INVALID_OP_RETURN",
255
+ }
256
+ }
257
+
258
+ let valueOutputIndex = vouts.findIndex((vout_) => vout_.address === lockerAddress)
259
+ let value = valueOutputIndex >= 0 ? vouts[valueOutputIndex].value || 0 : 0
260
+
261
+ let response = parseWrapRawRequest(opReturnData)
262
+
263
+ if (!response.status || !("data" in response) || !response.data) {
264
+ return response as {
265
+ status: boolean
266
+ message: string
267
+ code: string
268
+ }
269
+ }
270
+
271
+ const data = response.data
272
+
273
+ if (+data.percentageFee > 100) {
274
+ return {
275
+ status: false,
276
+ message: `percentageFee greater than 100 is invalid. percentageFee: ${data.percentageFee}`,
277
+ code: "INVALID_FEE",
278
+ }
279
+ }
280
+
281
+ if ((data.percentageFee / 100) * +value <= minTeleporterFeeAmount) {
282
+ return {
283
+ status: false,
284
+ message: `fee amount is less than or equal minimum teleporter fee amount.percentageFee: ${
285
+ data.percentageFee
286
+ } - value: ${value} - feeAmount ${((data.percentageFee / 100) * +value).toFixed(
287
+ 8,
288
+ )} - minimumFee: ${minTeleporterFeeAmount}`,
289
+ code: "NOT_ACCEPTED_BY_TELEPORTER",
290
+ }
291
+ }
292
+
293
+ return { status: response.status, data, value, valueOutputIndex }
294
+ }
295
+ return {
296
+ status: false,
297
+ message: "transaction outputs must include an OP_RETURN",
298
+ code: "NO_OP_RETURN",
299
+ }
300
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,15 @@
1
1
  export * as bitcoinUtils from "./bitcoin-utils"
2
- export * from "./teleport-dao-payments"
3
- export * from "./bitcoin-interface"
2
+ export * from "./helper"
3
+ export * from "./type"
4
+ //
5
+ export * from "./teleswap-wallet"
6
+ export * from "./ordinal-wallet"
7
+ export * from "./bitcoin-wallet-base"
8
+ //
4
9
  export * from "./bitcoin-interface-utils"
5
- export * from "./bitcoin-base"
10
+ export * from "./bitcoin-interface"
11
+ export * from "./bitcoin-interface-teleswap"
12
+ export * from "./bitcoin-interface-ordinal"
13
+ //
6
14
  export * from "./transaction-builder"
7
15
  export * from "./sign"