@teleportdao/bitcoin 1.4.3 → 1.4.6

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 (69) hide show
  1. package/dist/bitcoin-base.d.ts +66 -53
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +47 -38
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-interface-utils.d.ts +12 -10
  6. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-utils.js +16 -10
  8. package/dist/bitcoin-interface-utils.js.map +1 -1
  9. package/dist/bitcoin-interface.d.ts +206 -50
  10. package/dist/bitcoin-interface.d.ts.map +1 -1
  11. package/dist/bitcoin-interface.js +42 -27
  12. package/dist/bitcoin-interface.js.map +1 -1
  13. package/dist/bitcoin-utils.d.ts +111 -41
  14. package/dist/bitcoin-utils.d.ts.map +1 -1
  15. package/dist/bitcoin-utils.js +215 -156
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -0
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +32 -12
  21. package/dist/index.js.map +1 -1
  22. package/dist/sign/sign-transaction.d.ts +9 -5
  23. package/dist/sign/sign-transaction.d.ts.map +1 -1
  24. package/dist/sign/sign-transaction.js +14 -11
  25. package/dist/sign/sign-transaction.js.map +1 -1
  26. package/dist/teleport-dao-payments.d.ts +68 -89
  27. package/dist/teleport-dao-payments.d.ts.map +1 -1
  28. package/dist/teleport-dao-payments.js +16 -4
  29. package/dist/teleport-dao-payments.js.map +1 -1
  30. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
  31. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
  33. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  34. package/dist/transaction-builder/transaction-builder.d.ts +198 -9
  35. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  36. package/dist/transaction-builder/transaction-builder.js +291 -38
  37. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  38. package/dist/utils/networks.d.ts +5 -35
  39. package/dist/utils/networks.d.ts.map +1 -1
  40. package/dist/utils/networks.js +26 -2
  41. package/dist/utils/networks.js.map +1 -1
  42. package/dist/utils/tools.d.ts +15 -9
  43. package/dist/utils/tools.d.ts.map +1 -1
  44. package/dist/utils/tools.js +14 -11
  45. package/dist/utils/tools.js.map +1 -1
  46. package/package.json +8 -6
  47. package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
  48. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  49. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
  50. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
  51. package/src/helper/teleport-request-helper.js +179 -179
  52. package/src/index.ts +5 -0
  53. package/src/sign/sign-transaction.ts +96 -0
  54. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
  55. package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
  56. package/src/transaction-builder/transaction-builder.ts +567 -0
  57. package/src/utils/{networks.js → networks.ts} +33 -31
  58. package/src/utils/{tools.js → tools.ts} +80 -72
  59. package/tsconfig.json +10 -9
  60. package/webpack.config.js +16 -0
  61. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  62. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  63. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  64. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
  65. package/src/index.js +0 -15
  66. package/src/sign/sign-transaction.js +0 -36
  67. package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
  68. package/src/transaction-builder/transaction-builder-common.js +0 -236
  69. package/src/transaction-builder/transaction-builder.js +0 -159
@@ -1,280 +1,341 @@
1
- const BitcoinBase = require("./bitcoin-base")
2
-
3
- class TeleportDaoPayment extends BitcoinBase {
4
- // payment
5
- async payBurnRequest(receivers, feeSpeed = "normal") {
6
- let extendedUtxo = await this.transactionBuilder.getExtendedUtxo({
7
- address: this.currentAccount,
8
- addressType: this.currentAccountType,
9
- publicKey: this.publicKey.toString("hex"),
10
- })
11
-
12
- let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
13
- let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
14
- extendedUtxo,
15
- targets: receivers,
16
- changeAddress: this.currentAccount,
17
- feeRate,
18
- fullAmount: false,
19
- })
20
- let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
21
- let signedTx = this.signer.finalizePsbts([signedPsbt])
22
- let txId = await this.transactionBuilder.sendTx(signedTx)
23
- return txId
24
- }
25
-
26
- // send
27
- async transferBitcoinToEth({
28
- lockerAddress,
29
- amount,
30
- fullAmount = false,
31
- //-----------
32
- chainId,
33
- appId,
34
- recipientAddress, // 20 bytes
35
- percentageFee, // 2 bytes in satoshi
36
- speed = 0, // 1 byte
37
- isExchange = false,
38
- exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
39
- outputAmount = 0, // 28 bytes
40
- deadline, // 4 bytes
41
- isFixedToken = false, // 1 byte
42
- feeSpeed = "normal",
43
- }) {
44
- let extendedUtxo = await this.getExtendedUtxo({
45
- address: this.currentAccount,
46
- addressType: this.currentAccountType,
47
- publicKey: this.publicKey.toString("hex"),
48
- })
49
- let unsignedTx = await this.getBitcoinToEthUnsignedPsbt({
50
- changeAddress: this.currentAccount,
51
- extendedUtxo,
52
- lockerAddress,
53
- amount,
54
- fullAmount,
55
- //-----------
56
- chainId,
57
- appId,
58
- recipientAddress,
59
- percentageFee,
60
- speed,
61
- isExchange,
62
- exchangeTokenAddress,
63
- outputAmount,
64
- deadline,
65
- isFixedToken,
66
- feeSpeed,
67
- })
68
- let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
69
- let txId = await this.sendSignedPsbt(signedPsbt)
70
- return txId
71
- }
72
-
73
- // get
74
- async getBitcoinToEthTargetOutputs({
75
- lockerAddress,
76
- amount,
77
- fullAmount = false,
78
- //-----------
79
- chainId,
80
- appId,
81
- recipientAddress, // 20 bytes
82
- percentageFee, // 2 bytes in satoshi
83
- speed = 0, // 1 byte
84
- isExchange = false,
85
- exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
86
- outputAmount = 0, // 28 bytes
87
- deadline = 0, // 4 bytes
88
- isFixedToken = false, // 1 byte
89
- }) {
90
- let dataHex = TeleportDaoPayment.getTransferOpReturnData({
91
- chainId,
92
- appId,
93
- recipientAddress,
94
- percentageFee,
95
- speed,
96
- isExchange,
97
- exchangeTokenAddress,
98
- outputAmount,
99
- deadline,
100
- isFixedToken,
101
- })
102
- let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
103
- return fullAmount
104
- ? [opTarget]
105
- : [
106
- {
107
- address: lockerAddress,
108
- value: amount,
109
- },
110
- opTarget,
111
- ]
112
- }
113
-
114
- async getBitcoinToEthUnsignedPsbt({
115
- changeAddress,
116
- extendedUtxo,
117
- lockerAddress,
118
- amount,
119
- fullAmount = false,
120
- //-----------
121
- chainId,
122
- appId,
123
- recipientAddress, // 20 bytes
124
- percentageFee, // 2 bytes in satoshi
125
- speed = 0, // 1 byte
126
- isExchange = false,
127
- exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
128
- outputAmount = 0, // 28 bytes
129
- deadline = 0, // 4 bytes
130
- isFixedToken = false, // 1 byte
131
- feeSpeed = "normal",
132
- }) {
133
- let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
134
- let targets = await this.getBitcoinToEthTargetOutputs({
135
- lockerAddress,
136
- amount,
137
- fullAmount,
138
- chainId,
139
- appId,
140
- recipientAddress,
141
- percentageFee,
142
- speed,
143
- isExchange,
144
- exchangeTokenAddress,
145
- outputAmount,
146
- deadline,
147
- isFixedToken,
148
- })
149
- let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
150
- extendedUtxo,
151
- targets,
152
- changeAddress: fullAmount ? lockerAddress : changeAddress,
153
- feeRate,
154
- fullAmount,
155
- })
156
- return unsignedTx
157
- }
158
-
159
- // send
160
- async bitcoinToEthLend({
161
- lockerAddress,
162
- amount,
163
- //-----------
164
- chainId,
165
- appId,
166
- recipientAddress, // 20 bytes
167
- percentageFee, // 2 bytes in satoshi
168
- mode = 0, // 1 byte
169
- isBorrow = false,
170
- tokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
171
- borrowAmount = 0, // 28 bytes
172
- }) {
173
- let dataHex = TeleportDaoPayment.getLendingOpReturnData({
174
- chainId,
175
- appId,
176
- recipientAddress,
177
- percentageFee,
178
- mode,
179
- isBorrow,
180
- tokenAddress,
181
- borrowAmount,
182
- })
183
- let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
184
-
185
- let extendedUtxo = await this.transactionBuilder.getExtendedUtxo({
186
- address: this.currentAccount,
187
- addressType: this.currentAccountType,
188
- publicKey: this.publicKey.toString("hex"),
189
- })
190
- let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
191
- extendedUtxo,
192
- targets: [
193
- {
194
- address: lockerAddress,
195
- value: amount,
196
- },
197
- opTarget,
198
- ],
199
- changeAddress: this.currentAccount,
200
- feeRate: 1,
201
- fullAmount: false,
202
- })
203
- let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
204
- let signedTx = this.signer.finalizePsbts([signedPsbt])
205
- let txId = await this.transactionBuilder.sendTx(signedTx)
206
- return txId
207
- }
208
-
209
- static getTransferOpReturnData({
210
- chainId,
211
- appId,
212
- recipientAddress, // 20 bytes
213
- percentageFee, // 2 bytes in satoshi
214
- speed = 0, // 1 byte
215
- isExchange = false,
216
- exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
217
- outputAmount = 0, // 28 bytes
218
- deadline, // 4 bytes
219
- isFixedToken = false, // 1 byte
220
- }) {
221
- let chainIdHex = Number(chainId).toString(16).padStart(2, "0")
222
- let appIdHex = Number(appId).toString(16).padStart(4, "0")
223
- let recipientAddressHex = recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0")
224
- let percentageFeeHex = Number((percentageFee * 100).toFixed(0))
225
- .toString(16)
226
- .padStart(4, "0")
227
- let speedHex = speed ? "01" : "00"
228
- let dataHex = chainIdHex + appIdHex + recipientAddressHex + percentageFeeHex + speedHex
229
-
230
- if (!isExchange) {
231
- if (dataHex.length !== 26 * 2) throw new Error("invalid data length")
232
- return dataHex
233
- }
234
-
235
- let exchangeTokenAddressHex = exchangeTokenAddress
236
- .replace("0x", "")
237
- .toLowerCase()
238
- .padStart(40, "0")
239
- let outputAmountHex = Number(outputAmount).toString(16).padStart(56, "0")
240
- let deadlineHex = Number(deadline).toString(16).padStart(8, "0")
241
- let isFixedTokenHex = isFixedToken ? "01" : "00"
242
-
243
- dataHex = dataHex + exchangeTokenAddressHex + outputAmountHex + deadlineHex + isFixedTokenHex
244
- if (dataHex.length !== 79 * 2) throw new Error("invalid data length")
245
- return dataHex
246
- }
247
-
248
- static getLendingOpReturnData({
249
- chainId, // 1 byte
250
- appId, // 1 byte
251
- recipientAddress, // 20 byte
252
- percentageFee, // 2 byte
253
- mode, // 1 byte
254
- // ------
255
- isBorrow = false,
256
- tokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
257
- borrowAmount = 0, // 28 bytes
258
- }) {
259
- let chainIdHex = Number(chainId).toString(16).padStart(2, "0")
260
- let appIdHex = Number(appId).toString(16).padStart(4, "0")
261
- let recipientAddressHex = recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0")
262
- let percentageFeeHex = Number((percentageFee * 100).toFixed(0))
263
- .toString(16)
264
- .padStart(4, "0")
265
- let modeHex = Number(mode).toString(16).padStart(2, "0")
266
- let dataHex = chainIdHex + appIdHex + recipientAddressHex + percentageFeeHex + modeHex
267
- if (!isBorrow) {
268
- if (dataHex.length !== 26 * 2) throw new Error("invalid data length")
269
- return dataHex
270
- }
271
-
272
- let tokenAddressHex = tokenAddress.replace("0x", "").toLowerCase().padStart(40, "0")
273
- let borrowAmountHex = Number(borrowAmount).toString(16).padStart(56, "0")
274
- dataHex = dataHex + tokenAddressHex + borrowAmountHex
275
- if (dataHex.length !== 74 * 2) throw new Error("invalid data length")
276
- return dataHex
277
- }
278
- }
279
-
280
- module.exports = TeleportDaoPayment
1
+ import type { ExtendedUtxo, Target } from "./transaction-builder/transaction-builder"
2
+ import { BitcoinBase } from "./bitcoin-base"
3
+
4
+ export type TransferRequest = {
5
+ changeAddress?: string
6
+ lockerAddress: string
7
+ amount: number
8
+ fullAmount?: boolean
9
+ //-----------
10
+ chainId: number
11
+ appId: number
12
+ recipientAddress: string // 20 bytes
13
+ percentageFee: number // 2 bytes in satoshi
14
+ speed?: number | boolean // 1 byte
15
+ isExchange?: boolean
16
+ exchangeTokenAddress?: string // 20 bytes
17
+ outputAmount?: number // 28 bytes
18
+ deadline?: number // 4 bytes
19
+ isFixedToken?: boolean // 1 byte
20
+ feeSpeed?: "normal" | "fast" | "slow"
21
+ }
22
+ export class TeleportDaoPayment extends BitcoinBase {
23
+ // payment
24
+ async payBurnRequest(receivers: Target[], feeSpeed: "normal" | "fast" | "slow" = "normal") {
25
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
26
+ throw new Error("account not initialized")
27
+ }
28
+ let extendedUtxo = await this.getExtendedUtxo({
29
+ address: this.currentAccount,
30
+ addressType: this.currentAccountType,
31
+ publicKey: this.publicKey.toString("hex"),
32
+ })
33
+
34
+ let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
35
+ let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
36
+ extendedUtxo,
37
+ targets: receivers,
38
+ changeAddress: this.currentAccount,
39
+ feeRate,
40
+ fullAmount: false,
41
+ })
42
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
43
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
44
+ let txId = await this.transactionBuilder.sendTx(signedTx)
45
+ return txId
46
+ }
47
+
48
+ // send
49
+ async transferBitcoinToEth({
50
+ lockerAddress,
51
+ amount,
52
+ fullAmount = false,
53
+ //-----------
54
+ chainId,
55
+ appId,
56
+ recipientAddress, // 20 bytes
57
+ percentageFee, // 2 bytes in satoshi
58
+ speed = 0, // 1 byte
59
+ isExchange = false,
60
+ exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
61
+ outputAmount = 0, // 28 bytes
62
+ deadline, // 4 bytes
63
+ isFixedToken = false, // 1 byte
64
+ feeSpeed = "normal",
65
+ }: TransferRequest) {
66
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
67
+ throw new Error("account not initialized")
68
+ }
69
+ let extendedUtxo = await this.getExtendedUtxo({
70
+ address: this.currentAccount,
71
+ addressType: this.currentAccountType,
72
+ publicKey: this.publicKey.toString("hex"),
73
+ })
74
+ let unsignedTx = await this.getBitcoinToEthUnsignedPsbt({
75
+ changeAddress: this.currentAccount,
76
+ extendedUtxo,
77
+ lockerAddress,
78
+ amount,
79
+ fullAmount,
80
+ //-----------
81
+ chainId,
82
+ appId,
83
+ recipientAddress,
84
+ percentageFee,
85
+ speed,
86
+ isExchange,
87
+ exchangeTokenAddress,
88
+ outputAmount,
89
+ deadline,
90
+ isFixedToken,
91
+ feeSpeed,
92
+ })
93
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
94
+ let txId = await this.sendSignedPsbt(signedPsbt)
95
+ return txId
96
+ }
97
+
98
+ // get
99
+ async getBitcoinToEthTargetOutputs({
100
+ lockerAddress,
101
+ amount,
102
+ fullAmount = false,
103
+ //-----------
104
+ chainId,
105
+ appId,
106
+ recipientAddress, // 20 bytes
107
+ percentageFee, // 2 bytes in satoshi
108
+ speed = 0, // 1 byte
109
+ isExchange = false,
110
+ exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
111
+ outputAmount = 0, // 28 bytes
112
+ deadline = 0, // 4 bytes
113
+ isFixedToken = false, // 1 byte
114
+ }: TransferRequest): Promise<Target[]> {
115
+ let dataHex = TeleportDaoPayment.getTransferOpReturnData({
116
+ chainId,
117
+ appId,
118
+ recipientAddress,
119
+ percentageFee,
120
+ speed,
121
+ isExchange,
122
+ exchangeTokenAddress,
123
+ outputAmount,
124
+ deadline,
125
+ isFixedToken,
126
+ })
127
+ let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
128
+ return fullAmount
129
+ ? [opTarget]
130
+ : [
131
+ {
132
+ address: lockerAddress,
133
+ value: amount,
134
+ },
135
+ opTarget,
136
+ ]
137
+ }
138
+
139
+ async getBitcoinToEthUnsignedPsbt({
140
+ changeAddress,
141
+ extendedUtxo,
142
+ lockerAddress,
143
+ amount,
144
+ fullAmount = false,
145
+ //-----------
146
+ chainId,
147
+ appId,
148
+ recipientAddress, // 20 bytes
149
+ percentageFee, // 2 bytes in satoshi
150
+ speed = 0, // 1 byte
151
+ isExchange = false,
152
+ exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
153
+ outputAmount = 0, // 28 bytes
154
+ deadline = 0, // 4 bytes
155
+ isFixedToken = false, // 1 byte
156
+ feeSpeed = "normal",
157
+ }: TransferRequest & {
158
+ extendedUtxo: ExtendedUtxo[]
159
+ }) {
160
+ let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
161
+ let targets = await this.getBitcoinToEthTargetOutputs({
162
+ lockerAddress,
163
+ amount,
164
+ fullAmount,
165
+ chainId,
166
+ appId,
167
+ recipientAddress,
168
+ percentageFee,
169
+ speed,
170
+ isExchange,
171
+ exchangeTokenAddress,
172
+ outputAmount,
173
+ deadline,
174
+ isFixedToken,
175
+ })
176
+ let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
177
+ extendedUtxo,
178
+ targets,
179
+ changeAddress: fullAmount ? lockerAddress : changeAddress,
180
+ feeRate,
181
+ fullAmount,
182
+ })
183
+ return unsignedTx
184
+ }
185
+
186
+ // send
187
+ async bitcoinToEthLend({
188
+ lockerAddress,
189
+ amount,
190
+ //-----------
191
+ chainId,
192
+ appId,
193
+ recipientAddress, // 20 bytes
194
+ percentageFee, // 2 bytes in satoshi
195
+ mode = 0, // 1 byte
196
+ isBorrow = false,
197
+ tokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
198
+ borrowAmount = 0, // 28 bytes
199
+ }: {
200
+ lockerAddress: string
201
+ amount: number
202
+ chainId: number
203
+ appId: number
204
+ recipientAddress: string // 20 bytes
205
+ percentageFee: number // 2 bytes in satoshi
206
+ mode?: 0 | 1 | boolean // 1 byte
207
+ isBorrow?: boolean
208
+ tokenAddress?: string // 20 bytes
209
+ borrowAmount?: number // 28 bytes
210
+ }) {
211
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
212
+ throw new Error("account not initialized")
213
+ }
214
+
215
+ let dataHex = TeleportDaoPayment.getLendingOpReturnData({
216
+ chainId,
217
+ appId,
218
+ recipientAddress,
219
+ percentageFee,
220
+ mode,
221
+ isBorrow,
222
+ tokenAddress,
223
+ borrowAmount,
224
+ })
225
+ let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
226
+
227
+ let extendedUtxo = await this.transactionBuilder.getExtendedUtxo({
228
+ address: this.currentAccount,
229
+ addressType: this.currentAccountType,
230
+ publicKey: this.publicKey.toString("hex"),
231
+ })
232
+ let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
233
+ extendedUtxo,
234
+ targets: [
235
+ {
236
+ address: lockerAddress,
237
+ value: amount,
238
+ },
239
+ opTarget,
240
+ ],
241
+ changeAddress: this.currentAccount,
242
+ feeRate: 1,
243
+ fullAmount: false,
244
+ })
245
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
246
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
247
+ let txId = await this.transactionBuilder.sendTx(signedTx)
248
+ return txId
249
+ }
250
+
251
+ static getTransferOpReturnData({
252
+ chainId,
253
+ appId,
254
+ recipientAddress, // 20 bytes
255
+ percentageFee, // 2 bytes in satoshi
256
+ speed = 0, // 1 byte
257
+ isExchange = false,
258
+ exchangeTokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
259
+ outputAmount = 0, // 28 bytes
260
+ deadline, // 4 bytes
261
+ isFixedToken = false, // 1 byte
262
+ }: {
263
+ chainId: number
264
+ appId: number
265
+ recipientAddress: string // 20 bytes
266
+ percentageFee: number // 2 bytes in satoshi
267
+ deadline: number // 4 bytes
268
+ speed?: number | boolean // 1 byte
269
+ isExchange?: boolean
270
+ exchangeTokenAddress?: string // 20 bytes
271
+ outputAmount?: number // 28 bytes
272
+ isFixedToken?: boolean // 1 byte
273
+ }) {
274
+ let chainIdHex = Number(chainId).toString(16).padStart(2, "0")
275
+ let appIdHex = Number(appId).toString(16).padStart(4, "0")
276
+ let recipientAddressHex = recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0")
277
+ let percentageFeeHex = Number((percentageFee * 100).toFixed(0))
278
+ .toString(16)
279
+ .padStart(4, "0")
280
+ let speedHex = speed ? "01" : "00"
281
+ let dataHex = chainIdHex + appIdHex + recipientAddressHex + percentageFeeHex + speedHex
282
+
283
+ if (!isExchange) {
284
+ if (dataHex.length !== 26 * 2) throw new Error("invalid data length")
285
+ return dataHex
286
+ }
287
+
288
+ let exchangeTokenAddressHex = exchangeTokenAddress
289
+ .replace("0x", "")
290
+ .toLowerCase()
291
+ .padStart(40, "0")
292
+ let outputAmountHex = Number(outputAmount).toString(16).padStart(56, "0")
293
+ let deadlineHex = Number(deadline).toString(16).padStart(8, "0")
294
+ let isFixedTokenHex = isFixedToken ? "01" : "00"
295
+
296
+ dataHex = dataHex + exchangeTokenAddressHex + outputAmountHex + deadlineHex + isFixedTokenHex
297
+ if (dataHex.length !== 79 * 2) throw new Error("invalid data length")
298
+ return dataHex
299
+ }
300
+
301
+ static getLendingOpReturnData({
302
+ chainId, // 1 byte
303
+ appId, // 1 byte
304
+ recipientAddress, // 20 byte
305
+ percentageFee, // 2 byte
306
+ mode, // 1 byte
307
+ // ------
308
+ isBorrow = false,
309
+ tokenAddress = "0x0000000000000000000000000000000000000000", // 20 bytes
310
+ borrowAmount = 0, // 28 bytes
311
+ }: {
312
+ chainId: number
313
+ appId: number
314
+ recipientAddress: string // 20 bytes
315
+ percentageFee: number // 2 bytes in satoshi
316
+ mode?: 0 | 1 | boolean // 1 byte
317
+ isBorrow?: boolean
318
+ tokenAddress?: string // 20 bytes
319
+ borrowAmount?: number // 28 bytes
320
+ }) {
321
+ let chainIdHex = Number(chainId).toString(16).padStart(2, "0")
322
+ let appIdHex = Number(appId).toString(16).padStart(4, "0")
323
+ let recipientAddressHex = recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0")
324
+ let percentageFeeHex = Number((percentageFee * 100).toFixed(0))
325
+ .toString(16)
326
+ .padStart(4, "0")
327
+ let modeHex = Number(mode).toString(16).padStart(2, "0")
328
+ let dataHex = chainIdHex + appIdHex + recipientAddressHex + percentageFeeHex + modeHex
329
+ if (!isBorrow) {
330
+ if (dataHex.length !== 26 * 2) throw new Error("invalid data length")
331
+ return dataHex
332
+ }
333
+
334
+ let tokenAddressHex = tokenAddress.replace("0x", "").toLowerCase().padStart(40, "0")
335
+ let borrowAmountHex = Number(borrowAmount).toString(16).padStart(56, "0")
336
+ dataHex = dataHex + tokenAddressHex + borrowAmountHex
337
+ if (dataHex.length !== 74 * 2) throw new Error("invalid data length")
338
+ return dataHex
339
+ }
340
+ }
341
+ export default TeleportDaoPayment