@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.
- package/dist/bitcoin-interface-ordinal.d.ts +108 -108
- package/dist/bitcoin-interface-ordinal.js +140 -140
- package/dist/bitcoin-interface-teleswap.d.ts +83 -101
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +119 -176
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +20 -20
- package/dist/bitcoin-interface-utils.js +45 -45
- package/dist/bitcoin-interface-wallet.d.ts +29 -28
- package/dist/bitcoin-interface-wallet.d.ts.map +1 -1
- package/dist/bitcoin-interface-wallet.js +126 -125
- package/dist/bitcoin-interface-wallet.js.map +1 -1
- package/dist/bitcoin-interface.d.ts +63 -66
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +112 -119
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +96 -96
- package/dist/bitcoin-utils.js +514 -514
- package/dist/bitcoin-wallet-base.d.ts +111 -111
- package/dist/bitcoin-wallet-base.d.ts.map +1 -1
- package/dist/bitcoin-wallet-base.js +258 -258
- package/dist/helper/brc20-helper.d.ts +42 -42
- package/dist/helper/brc20-helper.js +127 -127
- package/dist/helper/index.d.ts +3 -3
- package/dist/helper/index.js +29 -29
- package/dist/helper/ordinal-helper.d.ts +12 -12
- package/dist/helper/ordinal-helper.js +129 -129
- package/dist/helper/teleswap-helper.d.ts +95 -95
- package/dist/helper/teleswap-helper.js +186 -186
- package/dist/index.d.ts +12 -12
- package/dist/index.js +41 -41
- package/dist/ordinal-wallet.d.ts +492 -495
- package/dist/ordinal-wallet.d.ts.map +1 -1
- package/dist/ordinal-wallet.js +386 -386
- package/dist/ordinal-wallet.js.map +1 -1
- package/dist/sign/index.d.ts +1 -1
- package/dist/sign/index.js +8 -8
- package/dist/sign/sign-transaction.d.ts +12 -12
- package/dist/sign/sign-transaction.js +82 -82
- package/dist/teleswap-wallet.d.ts +45 -45
- package/dist/teleswap-wallet.js +68 -68
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/index.d.ts +3 -3
- package/dist/transaction-builder/index.js +19 -19
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
- package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
- package/dist/transaction-builder/transaction-builder.d.ts +223 -223
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +442 -447
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/type.d.ts +55 -61
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +2 -2
- package/dist/utils/networks.d.ts +5 -5
- package/dist/utils/networks.js +53 -53
- package/dist/utils/tools.d.ts +18 -18
- package/dist/utils/tools.js +74 -74
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -185
- package/src/bitcoin-interface-teleswap.ts +246 -251
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +112 -114
- package/src/bitcoin-interface.ts +146 -156
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -344
- package/src/helper/brc20-helper.ts +179 -179
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +654 -659
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -133
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
- package/src/transaction-builder/transaction-builder.ts +686 -690
- package/src/type.ts +64 -74
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +92 -92
- package/tsconfig.json +9 -9
- package/webpack.config.js +16 -16
- package/.tmp/block-parser.ts +0 -58
- package/.tmp/check.ts +0 -101
- package/.tmp/ordinal-helper.ts +0 -133
- package/.tmp/ordinal.ts +0 -25
- package/.tmp/psbt/sign-transaction.ts +0 -121
- package/.tmp/rbf.ts +0 -45
package/dist/ordinal-wallet.js
CHANGED
|
@@ -1,387 +1,387 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
16
|
-
const ordinal_transaction_builder_1 = require("./transaction-builder/ordinal-transaction-builder");
|
|
17
|
-
const sign_transaction_1 = __importDefault(require("./sign/sign-transaction"));
|
|
18
|
-
const bitcoin_interface_ordinal_1 = require("./bitcoin-interface-ordinal");
|
|
19
|
-
const brc20_helper_1 = require("./helper/brc20-helper");
|
|
20
|
-
const tools_1 = require("./utils/tools");
|
|
21
|
-
const bitcoin_wallet_base_1 = require("./bitcoin-wallet-base");
|
|
22
|
-
class OrdinalWallet extends bitcoin_wallet_base_1.BitcoinBaseWallet {
|
|
23
|
-
constructor(networkName, uniSatToken, connectionInfo) {
|
|
24
|
-
if (!connectionInfo.rpc) {
|
|
25
|
-
throw new Error("rpc is required");
|
|
26
|
-
}
|
|
27
|
-
super(networkName, connectionInfo);
|
|
28
|
-
this.transactionBuilder = new ordinal_transaction_builder_1.OrdinalTransactionBuilder(networkName, this.network, connectionInfo);
|
|
29
|
-
this.signer = new sign_transaction_1.default(this.network);
|
|
30
|
-
this.btcInterface = new bitcoin_interface_ordinal_1.BitcoinInterfaceOrdinal(connectionInfo, networkName, uniSatToken);
|
|
31
|
-
this.unisat = this.btcInterface.unisat;
|
|
32
|
-
}
|
|
33
|
-
static deployBRC20Data(tickName, max, limit) {
|
|
34
|
-
let data = {
|
|
35
|
-
p: "brc-20",
|
|
36
|
-
op: "deploy",
|
|
37
|
-
tick: tickName,
|
|
38
|
-
max: `${max}`,
|
|
39
|
-
lim: `${limit}`,
|
|
40
|
-
};
|
|
41
|
-
return {
|
|
42
|
-
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
43
|
-
type: "text/plain",
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
static mintBRC20Data(tickName, amount) {
|
|
47
|
-
if ((0, bignumber_js_1.default)(amount).isLessThanOrEqualTo(0))
|
|
48
|
-
throw new Error("amount should be greater than 0");
|
|
49
|
-
let data = {
|
|
50
|
-
p: "brc-20",
|
|
51
|
-
op: "mint",
|
|
52
|
-
tick: tickName,
|
|
53
|
-
amt: `${amount}`,
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
57
|
-
type: "text/plain",
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
static transferBRC20Data(tickName, amount) {
|
|
61
|
-
if ((0, bignumber_js_1.default)(amount).isLessThanOrEqualTo(0))
|
|
62
|
-
throw new Error("amount should be greater than 0");
|
|
63
|
-
let data = {
|
|
64
|
-
p: "brc-20",
|
|
65
|
-
op: "transfer",
|
|
66
|
-
tick: tickName,
|
|
67
|
-
amt: `${amount}`,
|
|
68
|
-
};
|
|
69
|
-
return {
|
|
70
|
-
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
71
|
-
type: "text/plain",
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
75
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
-
const ordinalSignerPublicKey = (ordinalSigner === null || ordinalSigner === void 0 ? void 0 : ordinalSigner.publicKey) || signer.publicKey;
|
|
77
|
-
const publicKey = Buffer.from(ordinalSignerPublicKey, "hex");
|
|
78
|
-
let transferOrdinal = this.transactionBuilder.createOrdinalAddress(file, publicKey);
|
|
79
|
-
const leafScript = transferOrdinal.redeem.output;
|
|
80
|
-
const { ordinalAddress } = transferOrdinal;
|
|
81
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
82
|
-
let extraFee = +(((400 + leafScript.length) / 4) * feeRate * 1.5).toFixed(0);
|
|
83
|
-
let ordinalAmount = 600;
|
|
84
|
-
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
85
|
-
let inscribeDepositUnsignedInfo = yield this.sendBTCUnsignedTx(ordinalAddress, ordinalAmount + extraFee, signer, feeRate, utxo1);
|
|
86
|
-
return {
|
|
87
|
-
inscribeDepositUnsignedInfo,
|
|
88
|
-
transferOrdinal,
|
|
89
|
-
};
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
93
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
const receiverAddress = (ordinalSigner === null || ordinalSigner === void 0 ? void 0 : ordinalSigner.address) || signer.address;
|
|
95
|
-
const { inscribeDepositUnsignedInfo, transferOrdinal } = yield this.inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee, extendedUtxo);
|
|
96
|
-
const { ordinalAddress } = transferOrdinal;
|
|
97
|
-
let ordinalAmount = 600;
|
|
98
|
-
if (!inscribeDepositUnsignedInfo.possibleTxId)
|
|
99
|
-
throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required. inscriber address type is not p2tr");
|
|
100
|
-
let inscribeDeposit = {
|
|
101
|
-
hash: inscribeDepositUnsignedInfo.possibleTxId,
|
|
102
|
-
value: inscribeDepositUnsignedInfo.outputs[0].value,
|
|
103
|
-
index: 0,
|
|
104
|
-
};
|
|
105
|
-
let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(transferOrdinal, inscribeDeposit, receiverAddress, ordinalAmount);
|
|
106
|
-
return {
|
|
107
|
-
inscribeDepositUnsignedInfo,
|
|
108
|
-
inscribeUnsignedInfo,
|
|
109
|
-
inscribeAddress: ordinalAddress,
|
|
110
|
-
receiverAddress,
|
|
111
|
-
};
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
inscribeOrdinal(file, fee = "normal", extendedUtxo, ordinalReceiverAddress) {
|
|
115
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
|
|
117
|
-
throw new Error("account not initialized");
|
|
118
|
-
}
|
|
119
|
-
const receiverAddress = ordinalReceiverAddress || this.bitcoinAddress;
|
|
120
|
-
const { inscribeDepositUnsignedInfo, transferOrdinal } = yield this.inscribeOrdinalDepositUnsigned(file, this.signerInfo, undefined, fee, extendedUtxo);
|
|
121
|
-
let ordinalUtxo = yield this.btcInterface.getBTCUtxo(transferOrdinal.ordinalAddress, this.signerInfo);
|
|
122
|
-
let inscribeDeposit;
|
|
123
|
-
if (ordinalUtxo.length > 1) {
|
|
124
|
-
throw new Error("multiple deposit found for this ordinal address");
|
|
125
|
-
}
|
|
126
|
-
if (ordinalUtxo.length === 0 ||
|
|
127
|
-
ordinalUtxo[0].value <
|
|
128
|
-
inscribeDepositUnsignedInfo.outputs[0].value -
|
|
129
|
-
inscribeDepositUnsignedInfo.outputs[0].value * 0.15) {
|
|
130
|
-
const signedPsbt1 = yield this.signer.signPsbt(inscribeDepositUnsignedInfo, this.privateKey);
|
|
131
|
-
console.log("inscribe deposit tx ...");
|
|
132
|
-
const inscribeDepositTxId = yield this.sendSignedPsbt(signedPsbt1);
|
|
133
|
-
console.log(`inscribe deposit txId : ${inscribeDepositTxId}`);
|
|
134
|
-
inscribeDeposit = {
|
|
135
|
-
hash: inscribeDepositTxId,
|
|
136
|
-
value: inscribeDepositUnsignedInfo.outputs[0].value,
|
|
137
|
-
index: 0,
|
|
138
|
-
inputs: inscribeDepositUnsignedInfo.inputs,
|
|
139
|
-
change: inscribeDepositUnsignedInfo.change,
|
|
140
|
-
changeIndex: inscribeDepositUnsignedInfo.change
|
|
141
|
-
? inscribeDepositUnsignedInfo.outputs.length
|
|
142
|
-
: undefined,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
inscribeDeposit = {
|
|
147
|
-
hash: ordinalUtxo[0].hash,
|
|
148
|
-
value: ordinalUtxo[0].value,
|
|
149
|
-
index: ordinalUtxo[0].index,
|
|
150
|
-
inputs: [],
|
|
151
|
-
};
|
|
152
|
-
console.log("no need to deposit", inscribeDeposit);
|
|
153
|
-
}
|
|
154
|
-
const ordinalAmount = 600;
|
|
155
|
-
let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(transferOrdinal, inscribeDeposit, receiverAddress, ordinalAmount);
|
|
156
|
-
const signedPsbt2 = yield this.signer.signPsbt(inscribeUnsignedInfo, this.privateKey, undefined, false);
|
|
157
|
-
console.log("inscribeTxId ...");
|
|
158
|
-
yield (0, tools_1.sleep)(10 * 1000);
|
|
159
|
-
let inscribeTxId = yield this.sendSignedPsbtWithRetry(signedPsbt2);
|
|
160
|
-
console.log("inscribeTxId", inscribeTxId);
|
|
161
|
-
return {
|
|
162
|
-
inscribeTx: { hash: inscribeTxId, index: 0, value: ordinalAmount },
|
|
163
|
-
inscribeDepositTx: inscribeDeposit,
|
|
164
|
-
inscribeAddress: transferOrdinal.ordinalAddress,
|
|
165
|
-
};
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
deployBrc20(brc, fee = "normal", extendedUtxo) {
|
|
169
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
let file = OrdinalWallet.deployBRC20Data(brc.tick, brc.max, brc.limit);
|
|
171
|
-
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
mintBrc20(brc, fee = "normal", extendedUtxo) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
let file = OrdinalWallet.mintBRC20Data(brc.tick, brc.amount);
|
|
177
|
-
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
inscribeBrc20Unsigned(brc, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
181
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
-
let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount);
|
|
183
|
-
return this.inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee, extendedUtxo);
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
inscribeBrc20(brc, fee = "normal", extendedUtxo) {
|
|
187
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
-
let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount);
|
|
189
|
-
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
transferBrc20Unsigned(receiver, brcInscribeUtxo, signer, ordinalSigner, fee = "normal", extendedUtxo, otherTargets) {
|
|
193
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
-
const ordinalSignerInfo = ordinalSigner || signer;
|
|
195
|
-
let utxo = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
196
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
197
|
-
let unsignedTx = yield this.transactionBuilder.createNftPsbt({
|
|
198
|
-
extendedUtxo: utxo,
|
|
199
|
-
nftExtendedUtxo: {
|
|
200
|
-
hash: brcInscribeUtxo.hash,
|
|
201
|
-
index: 0,
|
|
202
|
-
value: brcInscribeUtxo.value,
|
|
203
|
-
signerInfo: ordinalSignerInfo,
|
|
204
|
-
},
|
|
205
|
-
feeRate,
|
|
206
|
-
receiverAddress: receiver,
|
|
207
|
-
changeAddress: signer.address,
|
|
208
|
-
otherTargets,
|
|
209
|
-
});
|
|
210
|
-
return Object.assign(Object.assign({}, unsignedTx), { possibleTxId: this.transactionBuilder.getUnsignedPsbtTxId(unsignedTx.unsignedTransaction) });
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
transferBrc20(receiver, brcInscribeUtxo, fee = "normal", extendedUtxo, otherTargets) {
|
|
214
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
215
|
-
if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
|
|
216
|
-
throw new Error("account not initialized");
|
|
217
|
-
}
|
|
218
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
219
|
-
let unsignedTx = yield this.transactionBuilder.createNftPsbt({
|
|
220
|
-
extendedUtxo,
|
|
221
|
-
nftExtendedUtxo: {
|
|
222
|
-
hash: brcInscribeUtxo.hash,
|
|
223
|
-
index: 0,
|
|
224
|
-
value: brcInscribeUtxo.value,
|
|
225
|
-
signerInfo: this.signerInfo,
|
|
226
|
-
},
|
|
227
|
-
feeRate,
|
|
228
|
-
receiverAddress: receiver,
|
|
229
|
-
changeAddress: this.bitcoinAddress,
|
|
230
|
-
otherTargets,
|
|
231
|
-
});
|
|
232
|
-
let signedTx = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
233
|
-
let finalTransferTxId = yield this.sendSignedPsbtWithRetry(signedTx);
|
|
234
|
-
return {
|
|
235
|
-
hash: finalTransferTxId,
|
|
236
|
-
index: 0,
|
|
237
|
-
value: brcInscribeUtxo.value,
|
|
238
|
-
inputs: unsignedTx.inputs,
|
|
239
|
-
change: unsignedTx.change,
|
|
240
|
-
changeIndex: unsignedTx.change ? unsignedTx.outputs.length : undefined,
|
|
241
|
-
};
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
inscribeAndTransferBrc20Unsigned(receiver, brc, signer, ordinalSigner, otherTargets, fee = "normal", extendedUtxo) {
|
|
245
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
-
const ordinalSignerInfo = ordinalSigner || signer;
|
|
247
|
-
let brc20Balance = yield this.unisat.getBrc20AddressBalanceForTicker(signer.address, brc.tick);
|
|
248
|
-
if ((0, bignumber_js_1.default)(brc20Balance.transferableBalance).gte(brc.amount)) {
|
|
249
|
-
let transferrableInscription = brc20Balance.transferableInscriptions.find((insc) => insc.data.tick === brc.tick && (0, bignumber_js_1.default)(insc.data.amt).isEqualTo(brc.amount));
|
|
250
|
-
console.log("transferrableInscription", transferrableInscription);
|
|
251
|
-
}
|
|
252
|
-
if ((0, bignumber_js_1.default)(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
|
|
253
|
-
throw new Error("insufficient balance");
|
|
254
|
-
}
|
|
255
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
256
|
-
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
257
|
-
let utxo2 = [];
|
|
258
|
-
const { inscribeAddress, inscribeDepositUnsignedInfo, inscribeUnsignedInfo } = yield this.inscribeBrc20Unsigned(brc, signer, ordinalSignerInfo, feeRate, utxo1);
|
|
259
|
-
utxo2 = utxo1.filter((u) => inscribeDepositUnsignedInfo.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1);
|
|
260
|
-
if (inscribeDepositUnsignedInfo.change) {
|
|
261
|
-
if (!inscribeDepositUnsignedInfo.possibleTxId) {
|
|
262
|
-
throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required");
|
|
263
|
-
}
|
|
264
|
-
utxo2.push({
|
|
265
|
-
hash: inscribeDepositUnsignedInfo.possibleTxId,
|
|
266
|
-
index: inscribeDepositUnsignedInfo.outputs.length,
|
|
267
|
-
value: inscribeDepositUnsignedInfo.change.value,
|
|
268
|
-
signerInfo: signer,
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
let transferTxUnsignedInfo = yield this.transferBrc20Unsigned(receiver, {
|
|
272
|
-
hash: inscribeUnsignedInfo.possibleTxId,
|
|
273
|
-
value: inscribeUnsignedInfo.outputs[0].value,
|
|
274
|
-
index: 0,
|
|
275
|
-
}, signer, ordinalSignerInfo, feeRate, utxo2, otherTargets);
|
|
276
|
-
return {
|
|
277
|
-
inscribeDepositUnsignedInfo,
|
|
278
|
-
inscribeUnsignedInfo,
|
|
279
|
-
transferTxUnsignedInfo,
|
|
280
|
-
inscribeAddress,
|
|
281
|
-
};
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
inscribeAndTransferBrc20(receiver, brc, otherTargets, fee = "normal", extendedUtxo) {
|
|
285
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
-
let brc20Balance = yield this.unisat.getBrc20AddressBalanceForTicker(this.bitcoinAddress, brc.tick);
|
|
287
|
-
if ((0, bignumber_js_1.default)(brc20Balance.transferableBalance).gte(brc.amount)) {
|
|
288
|
-
let transferrableInscription = brc20Balance.transferableInscriptions.find((insc) => insc.data.tick === brc.tick && (0, bignumber_js_1.default)(insc.data.amt).isEqualTo(brc.amount));
|
|
289
|
-
console.log("transferrableInscription", transferrableInscription);
|
|
290
|
-
if (transferrableInscription) {
|
|
291
|
-
let ins = yield this.btcInterface.unisat.getInscriptionInfo(transferrableInscription.inscriptionId);
|
|
292
|
-
if (ins) {
|
|
293
|
-
let transferTx = yield this.transferBrc20(receiver, {
|
|
294
|
-
hash: ins.utxo.txid,
|
|
295
|
-
index: ins.utxo.vout,
|
|
296
|
-
value: ins.utxo.satoshi,
|
|
297
|
-
}, yield this.getFeeRate(fee), extendedUtxo ||
|
|
298
|
-
(yield this.btcInterface.getBTCUtxo(this.bitcoinAddress, this.signerInfo)), otherTargets);
|
|
299
|
-
return {
|
|
300
|
-
transferTx,
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if ((0, bignumber_js_1.default)(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
|
|
306
|
-
throw new Error(`insufficient balance ${brc20Balance.availableBalanceSafe} ${brc.amount}`);
|
|
307
|
-
}
|
|
308
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
309
|
-
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(this.bitcoinAddress, this.signerInfo));
|
|
310
|
-
let utxo2 = [];
|
|
311
|
-
const { inscribeAddress, inscribeDepositTx, inscribeTx } = yield this.inscribeBrc20(brc, feeRate, utxo1);
|
|
312
|
-
utxo2 = utxo1.filter((u) => inscribeDepositTx.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1);
|
|
313
|
-
if (inscribeDepositTx.change) {
|
|
314
|
-
utxo2.push({
|
|
315
|
-
hash: inscribeDepositTx.hash,
|
|
316
|
-
index: inscribeDepositTx.changeIndex,
|
|
317
|
-
value: inscribeDepositTx.change.value,
|
|
318
|
-
signerInfo: this.signerInfo,
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
yield (0, tools_1.sleep)(10 * 1000);
|
|
322
|
-
let transferTx = yield this.transferBrc20(receiver, inscribeTx, feeRate, utxo2, otherTargets);
|
|
323
|
-
return {
|
|
324
|
-
inscribeTx,
|
|
325
|
-
inscribeAddress,
|
|
326
|
-
inscribeDepositTx,
|
|
327
|
-
transferTx,
|
|
328
|
-
};
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
wrapBrc20Unsigned(recipientAddress, brc, brc20TokenId, signer, lockerAddress, exchange, ordinalSigner, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
332
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
-
const isExchange = !!exchange;
|
|
334
|
-
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
335
|
-
chainId,
|
|
336
|
-
appId,
|
|
337
|
-
brc20TokenId,
|
|
338
|
-
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
339
|
-
recipientAddress,
|
|
340
|
-
isExchange,
|
|
341
|
-
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
342
|
-
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
343
|
-
});
|
|
344
|
-
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
345
|
-
return this.inscribeAndTransferBrc20Unsigned(lockerAddress, brc, signer, ordinalSigner, [opTarget], fee, extendedUtxo);
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
wrapBrc20OnlyTransferUnsigned(recipientAddress, brcInscribeTx, brc, brc20TokenId, signer, lockerAddress, exchange, ordinalSigner, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
349
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
-
let utxo = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
351
|
-
const isExchange = !!exchange;
|
|
352
|
-
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
353
|
-
chainId,
|
|
354
|
-
appId,
|
|
355
|
-
brc20TokenId,
|
|
356
|
-
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
357
|
-
recipientAddress,
|
|
358
|
-
isExchange,
|
|
359
|
-
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
360
|
-
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
361
|
-
});
|
|
362
|
-
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
363
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
364
|
-
let transferTxUnsignedInfo = yield this.transferBrc20Unsigned(lockerAddress, brcInscribeTx, signer, ordinalSigner, feeRate, utxo, [opTarget]);
|
|
365
|
-
return transferTxUnsignedInfo;
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
wrapBrc20(recipientAddress, brc, brc20TokenId, lockerAddress, exchange, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
369
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
370
|
-
const isExchange = !!exchange;
|
|
371
|
-
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
372
|
-
chainId,
|
|
373
|
-
appId,
|
|
374
|
-
brc20TokenId,
|
|
375
|
-
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
376
|
-
recipientAddress,
|
|
377
|
-
isExchange,
|
|
378
|
-
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
379
|
-
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
380
|
-
});
|
|
381
|
-
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
382
|
-
return this.inscribeAndTransferBrc20(lockerAddress, brc, [opTarget], fee, extendedUtxo);
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
exports.default = OrdinalWallet;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
16
|
+
const ordinal_transaction_builder_1 = require("./transaction-builder/ordinal-transaction-builder");
|
|
17
|
+
const sign_transaction_1 = __importDefault(require("./sign/sign-transaction"));
|
|
18
|
+
const bitcoin_interface_ordinal_1 = require("./bitcoin-interface-ordinal");
|
|
19
|
+
const brc20_helper_1 = require("./helper/brc20-helper");
|
|
20
|
+
const tools_1 = require("./utils/tools");
|
|
21
|
+
const bitcoin_wallet_base_1 = require("./bitcoin-wallet-base");
|
|
22
|
+
class OrdinalWallet extends bitcoin_wallet_base_1.BitcoinBaseWallet {
|
|
23
|
+
constructor(networkName, uniSatToken, connectionInfo) {
|
|
24
|
+
if (!connectionInfo.rpc) {
|
|
25
|
+
throw new Error("rpc is required");
|
|
26
|
+
}
|
|
27
|
+
super(networkName, connectionInfo);
|
|
28
|
+
this.transactionBuilder = new ordinal_transaction_builder_1.OrdinalTransactionBuilder(networkName, this.network, connectionInfo);
|
|
29
|
+
this.signer = new sign_transaction_1.default(this.network);
|
|
30
|
+
this.btcInterface = new bitcoin_interface_ordinal_1.BitcoinInterfaceOrdinal(connectionInfo, networkName, uniSatToken);
|
|
31
|
+
this.unisat = this.btcInterface.unisat;
|
|
32
|
+
}
|
|
33
|
+
static deployBRC20Data(tickName, max, limit) {
|
|
34
|
+
let data = {
|
|
35
|
+
p: "brc-20",
|
|
36
|
+
op: "deploy",
|
|
37
|
+
tick: tickName,
|
|
38
|
+
max: `${max}`,
|
|
39
|
+
lim: `${limit}`,
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
43
|
+
type: "text/plain",
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
static mintBRC20Data(tickName, amount) {
|
|
47
|
+
if ((0, bignumber_js_1.default)(amount).isLessThanOrEqualTo(0))
|
|
48
|
+
throw new Error("amount should be greater than 0");
|
|
49
|
+
let data = {
|
|
50
|
+
p: "brc-20",
|
|
51
|
+
op: "mint",
|
|
52
|
+
tick: tickName,
|
|
53
|
+
amt: `${amount}`,
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
57
|
+
type: "text/plain",
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
static transferBRC20Data(tickName, amount) {
|
|
61
|
+
if ((0, bignumber_js_1.default)(amount).isLessThanOrEqualTo(0))
|
|
62
|
+
throw new Error("amount should be greater than 0");
|
|
63
|
+
let data = {
|
|
64
|
+
p: "brc-20",
|
|
65
|
+
op: "transfer",
|
|
66
|
+
tick: tickName,
|
|
67
|
+
amt: `${amount}`,
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
buffer: Buffer.from(JSON.stringify(data), "utf8"),
|
|
71
|
+
type: "text/plain",
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
const ordinalSignerPublicKey = (ordinalSigner === null || ordinalSigner === void 0 ? void 0 : ordinalSigner.publicKey) || signer.publicKey;
|
|
77
|
+
const publicKey = Buffer.from(ordinalSignerPublicKey, "hex");
|
|
78
|
+
let transferOrdinal = this.transactionBuilder.createOrdinalAddress(file, publicKey);
|
|
79
|
+
const leafScript = transferOrdinal.redeem.output;
|
|
80
|
+
const { ordinalAddress } = transferOrdinal;
|
|
81
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
82
|
+
let extraFee = +(((400 + leafScript.length) / 4) * feeRate * 1.5).toFixed(0);
|
|
83
|
+
let ordinalAmount = 600;
|
|
84
|
+
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
85
|
+
let inscribeDepositUnsignedInfo = yield this.sendBTCUnsignedTx(ordinalAddress, ordinalAmount + extraFee, signer, feeRate, utxo1);
|
|
86
|
+
return {
|
|
87
|
+
inscribeDepositUnsignedInfo,
|
|
88
|
+
transferOrdinal,
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const receiverAddress = (ordinalSigner === null || ordinalSigner === void 0 ? void 0 : ordinalSigner.address) || signer.address;
|
|
95
|
+
const { inscribeDepositUnsignedInfo, transferOrdinal } = yield this.inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee, extendedUtxo);
|
|
96
|
+
const { ordinalAddress } = transferOrdinal;
|
|
97
|
+
let ordinalAmount = 600;
|
|
98
|
+
if (!inscribeDepositUnsignedInfo.possibleTxId)
|
|
99
|
+
throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required. inscriber address type is not p2tr");
|
|
100
|
+
let inscribeDeposit = {
|
|
101
|
+
hash: inscribeDepositUnsignedInfo.possibleTxId,
|
|
102
|
+
value: inscribeDepositUnsignedInfo.outputs[0].value,
|
|
103
|
+
index: 0,
|
|
104
|
+
};
|
|
105
|
+
let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(transferOrdinal, inscribeDeposit, receiverAddress, ordinalAmount);
|
|
106
|
+
return {
|
|
107
|
+
inscribeDepositUnsignedInfo,
|
|
108
|
+
inscribeUnsignedInfo,
|
|
109
|
+
inscribeAddress: ordinalAddress,
|
|
110
|
+
receiverAddress,
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
inscribeOrdinal(file, fee = "normal", extendedUtxo, ordinalReceiverAddress) {
|
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
|
|
117
|
+
throw new Error("account not initialized");
|
|
118
|
+
}
|
|
119
|
+
const receiverAddress = ordinalReceiverAddress || this.bitcoinAddress;
|
|
120
|
+
const { inscribeDepositUnsignedInfo, transferOrdinal } = yield this.inscribeOrdinalDepositUnsigned(file, this.signerInfo, undefined, fee, extendedUtxo);
|
|
121
|
+
let ordinalUtxo = yield this.btcInterface.getBTCUtxo(transferOrdinal.ordinalAddress, this.signerInfo);
|
|
122
|
+
let inscribeDeposit;
|
|
123
|
+
if (ordinalUtxo.length > 1) {
|
|
124
|
+
throw new Error("multiple deposit found for this ordinal address");
|
|
125
|
+
}
|
|
126
|
+
if (ordinalUtxo.length === 0 ||
|
|
127
|
+
ordinalUtxo[0].value <
|
|
128
|
+
inscribeDepositUnsignedInfo.outputs[0].value -
|
|
129
|
+
inscribeDepositUnsignedInfo.outputs[0].value * 0.15) {
|
|
130
|
+
const signedPsbt1 = yield this.signer.signPsbt(inscribeDepositUnsignedInfo, this.privateKey);
|
|
131
|
+
console.log("inscribe deposit tx ...");
|
|
132
|
+
const inscribeDepositTxId = yield this.sendSignedPsbt(signedPsbt1);
|
|
133
|
+
console.log(`inscribe deposit txId : ${inscribeDepositTxId}`);
|
|
134
|
+
inscribeDeposit = {
|
|
135
|
+
hash: inscribeDepositTxId,
|
|
136
|
+
value: inscribeDepositUnsignedInfo.outputs[0].value,
|
|
137
|
+
index: 0,
|
|
138
|
+
inputs: inscribeDepositUnsignedInfo.inputs,
|
|
139
|
+
change: inscribeDepositUnsignedInfo.change,
|
|
140
|
+
changeIndex: inscribeDepositUnsignedInfo.change
|
|
141
|
+
? inscribeDepositUnsignedInfo.outputs.length
|
|
142
|
+
: undefined,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
inscribeDeposit = {
|
|
147
|
+
hash: ordinalUtxo[0].hash,
|
|
148
|
+
value: ordinalUtxo[0].value,
|
|
149
|
+
index: ordinalUtxo[0].index,
|
|
150
|
+
inputs: [],
|
|
151
|
+
};
|
|
152
|
+
console.log("no need to deposit", inscribeDeposit);
|
|
153
|
+
}
|
|
154
|
+
const ordinalAmount = 600;
|
|
155
|
+
let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(transferOrdinal, inscribeDeposit, receiverAddress, ordinalAmount);
|
|
156
|
+
const signedPsbt2 = yield this.signer.signPsbt(inscribeUnsignedInfo, this.privateKey, undefined, false);
|
|
157
|
+
console.log("inscribeTxId ...");
|
|
158
|
+
yield (0, tools_1.sleep)(10 * 1000);
|
|
159
|
+
let inscribeTxId = yield this.sendSignedPsbtWithRetry(signedPsbt2);
|
|
160
|
+
console.log("inscribeTxId", inscribeTxId);
|
|
161
|
+
return {
|
|
162
|
+
inscribeTx: { hash: inscribeTxId, index: 0, value: ordinalAmount },
|
|
163
|
+
inscribeDepositTx: inscribeDeposit,
|
|
164
|
+
inscribeAddress: transferOrdinal.ordinalAddress,
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
deployBrc20(brc, fee = "normal", extendedUtxo) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
let file = OrdinalWallet.deployBRC20Data(brc.tick, brc.max, brc.limit);
|
|
171
|
+
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
mintBrc20(brc, fee = "normal", extendedUtxo) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
let file = OrdinalWallet.mintBRC20Data(brc.tick, brc.amount);
|
|
177
|
+
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
inscribeBrc20Unsigned(brc, signer, ordinalSigner, fee = "normal", extendedUtxo) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount);
|
|
183
|
+
return this.inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee, extendedUtxo);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
inscribeBrc20(brc, fee = "normal", extendedUtxo) {
|
|
187
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount);
|
|
189
|
+
return this.inscribeOrdinal(file, fee, extendedUtxo);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
transferBrc20Unsigned(receiver, brcInscribeUtxo, signer, ordinalSigner, fee = "normal", extendedUtxo, otherTargets) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
const ordinalSignerInfo = ordinalSigner || signer;
|
|
195
|
+
let utxo = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
196
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
197
|
+
let unsignedTx = yield this.transactionBuilder.createNftPsbt({
|
|
198
|
+
extendedUtxo: utxo,
|
|
199
|
+
nftExtendedUtxo: {
|
|
200
|
+
hash: brcInscribeUtxo.hash,
|
|
201
|
+
index: 0,
|
|
202
|
+
value: brcInscribeUtxo.value,
|
|
203
|
+
signerInfo: ordinalSignerInfo,
|
|
204
|
+
},
|
|
205
|
+
feeRate,
|
|
206
|
+
receiverAddress: receiver,
|
|
207
|
+
changeAddress: signer.address,
|
|
208
|
+
otherTargets,
|
|
209
|
+
});
|
|
210
|
+
return Object.assign(Object.assign({}, unsignedTx), { possibleTxId: this.transactionBuilder.getUnsignedPsbtTxId(unsignedTx.unsignedTransaction) });
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
transferBrc20(receiver, brcInscribeUtxo, fee = "normal", extendedUtxo, otherTargets) {
|
|
214
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
215
|
+
if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
|
|
216
|
+
throw new Error("account not initialized");
|
|
217
|
+
}
|
|
218
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
219
|
+
let unsignedTx = yield this.transactionBuilder.createNftPsbt({
|
|
220
|
+
extendedUtxo,
|
|
221
|
+
nftExtendedUtxo: {
|
|
222
|
+
hash: brcInscribeUtxo.hash,
|
|
223
|
+
index: 0,
|
|
224
|
+
value: brcInscribeUtxo.value,
|
|
225
|
+
signerInfo: this.signerInfo,
|
|
226
|
+
},
|
|
227
|
+
feeRate,
|
|
228
|
+
receiverAddress: receiver,
|
|
229
|
+
changeAddress: this.bitcoinAddress,
|
|
230
|
+
otherTargets,
|
|
231
|
+
});
|
|
232
|
+
let signedTx = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
233
|
+
let finalTransferTxId = yield this.sendSignedPsbtWithRetry(signedTx);
|
|
234
|
+
return {
|
|
235
|
+
hash: finalTransferTxId,
|
|
236
|
+
index: 0,
|
|
237
|
+
value: brcInscribeUtxo.value,
|
|
238
|
+
inputs: unsignedTx.inputs,
|
|
239
|
+
change: unsignedTx.change,
|
|
240
|
+
changeIndex: unsignedTx.change ? unsignedTx.outputs.length : undefined,
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
inscribeAndTransferBrc20Unsigned(receiver, brc, signer, ordinalSigner, otherTargets, fee = "normal", extendedUtxo) {
|
|
245
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
+
const ordinalSignerInfo = ordinalSigner || signer;
|
|
247
|
+
let brc20Balance = yield this.unisat.getBrc20AddressBalanceForTicker(signer.address, brc.tick);
|
|
248
|
+
if ((0, bignumber_js_1.default)(brc20Balance.transferableBalance).gte(brc.amount)) {
|
|
249
|
+
let transferrableInscription = brc20Balance.transferableInscriptions.find((insc) => insc.data.tick === brc.tick && (0, bignumber_js_1.default)(insc.data.amt).isEqualTo(brc.amount));
|
|
250
|
+
console.log("transferrableInscription", transferrableInscription);
|
|
251
|
+
}
|
|
252
|
+
if ((0, bignumber_js_1.default)(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
|
|
253
|
+
throw new Error("insufficient balance");
|
|
254
|
+
}
|
|
255
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
256
|
+
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
257
|
+
let utxo2 = [];
|
|
258
|
+
const { inscribeAddress, inscribeDepositUnsignedInfo, inscribeUnsignedInfo } = yield this.inscribeBrc20Unsigned(brc, signer, ordinalSignerInfo, feeRate, utxo1);
|
|
259
|
+
utxo2 = utxo1.filter((u) => inscribeDepositUnsignedInfo.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1);
|
|
260
|
+
if (inscribeDepositUnsignedInfo.change) {
|
|
261
|
+
if (!inscribeDepositUnsignedInfo.possibleTxId) {
|
|
262
|
+
throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required");
|
|
263
|
+
}
|
|
264
|
+
utxo2.push({
|
|
265
|
+
hash: inscribeDepositUnsignedInfo.possibleTxId,
|
|
266
|
+
index: inscribeDepositUnsignedInfo.outputs.length,
|
|
267
|
+
value: inscribeDepositUnsignedInfo.change.value,
|
|
268
|
+
signerInfo: signer,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
let transferTxUnsignedInfo = yield this.transferBrc20Unsigned(receiver, {
|
|
272
|
+
hash: inscribeUnsignedInfo.possibleTxId,
|
|
273
|
+
value: inscribeUnsignedInfo.outputs[0].value,
|
|
274
|
+
index: 0,
|
|
275
|
+
}, signer, ordinalSignerInfo, feeRate, utxo2, otherTargets);
|
|
276
|
+
return {
|
|
277
|
+
inscribeDepositUnsignedInfo,
|
|
278
|
+
inscribeUnsignedInfo,
|
|
279
|
+
transferTxUnsignedInfo,
|
|
280
|
+
inscribeAddress,
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
inscribeAndTransferBrc20(receiver, brc, otherTargets, fee = "normal", extendedUtxo) {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
+
let brc20Balance = yield this.unisat.getBrc20AddressBalanceForTicker(this.bitcoinAddress, brc.tick);
|
|
287
|
+
if ((0, bignumber_js_1.default)(brc20Balance.transferableBalance).gte(brc.amount)) {
|
|
288
|
+
let transferrableInscription = brc20Balance.transferableInscriptions.find((insc) => insc.data.tick === brc.tick && (0, bignumber_js_1.default)(insc.data.amt).isEqualTo(brc.amount));
|
|
289
|
+
console.log("transferrableInscription", transferrableInscription);
|
|
290
|
+
if (transferrableInscription) {
|
|
291
|
+
let ins = yield this.btcInterface.unisat.getInscriptionInfo(transferrableInscription.inscriptionId);
|
|
292
|
+
if (ins) {
|
|
293
|
+
let transferTx = yield this.transferBrc20(receiver, {
|
|
294
|
+
hash: ins.utxo.txid,
|
|
295
|
+
index: ins.utxo.vout,
|
|
296
|
+
value: ins.utxo.satoshi,
|
|
297
|
+
}, yield this.getFeeRate(fee), extendedUtxo ||
|
|
298
|
+
(yield this.btcInterface.getBTCUtxo(this.bitcoinAddress, this.signerInfo)), otherTargets);
|
|
299
|
+
return {
|
|
300
|
+
transferTx,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if ((0, bignumber_js_1.default)(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
|
|
306
|
+
throw new Error(`insufficient balance ${brc20Balance.availableBalanceSafe} ${brc.amount}`);
|
|
307
|
+
}
|
|
308
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
309
|
+
let utxo1 = extendedUtxo || (yield this.btcInterface.getBTCUtxo(this.bitcoinAddress, this.signerInfo));
|
|
310
|
+
let utxo2 = [];
|
|
311
|
+
const { inscribeAddress, inscribeDepositTx, inscribeTx } = yield this.inscribeBrc20(brc, feeRate, utxo1);
|
|
312
|
+
utxo2 = utxo1.filter((u) => inscribeDepositTx.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1);
|
|
313
|
+
if (inscribeDepositTx.change) {
|
|
314
|
+
utxo2.push({
|
|
315
|
+
hash: inscribeDepositTx.hash,
|
|
316
|
+
index: inscribeDepositTx.changeIndex,
|
|
317
|
+
value: inscribeDepositTx.change.value,
|
|
318
|
+
signerInfo: this.signerInfo,
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
yield (0, tools_1.sleep)(10 * 1000);
|
|
322
|
+
let transferTx = yield this.transferBrc20(receiver, inscribeTx, feeRate, utxo2, otherTargets);
|
|
323
|
+
return {
|
|
324
|
+
inscribeTx,
|
|
325
|
+
inscribeAddress,
|
|
326
|
+
inscribeDepositTx,
|
|
327
|
+
transferTx,
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
wrapBrc20Unsigned(recipientAddress, brc, brc20TokenId, signer, lockerAddress, exchange, ordinalSigner, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
+
const isExchange = !!exchange;
|
|
334
|
+
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
335
|
+
chainId,
|
|
336
|
+
appId,
|
|
337
|
+
brc20TokenId,
|
|
338
|
+
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
339
|
+
recipientAddress,
|
|
340
|
+
isExchange,
|
|
341
|
+
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
342
|
+
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
343
|
+
});
|
|
344
|
+
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
345
|
+
return this.inscribeAndTransferBrc20Unsigned(lockerAddress, brc, signer, ordinalSigner, [opTarget], fee, extendedUtxo);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
wrapBrc20OnlyTransferUnsigned(recipientAddress, brcInscribeTx, brc, brc20TokenId, signer, lockerAddress, exchange, ordinalSigner, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
349
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
+
let utxo = extendedUtxo || (yield this.btcInterface.getBTCUtxo(signer.address, signer));
|
|
351
|
+
const isExchange = !!exchange;
|
|
352
|
+
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
353
|
+
chainId,
|
|
354
|
+
appId,
|
|
355
|
+
brc20TokenId,
|
|
356
|
+
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
357
|
+
recipientAddress,
|
|
358
|
+
isExchange,
|
|
359
|
+
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
360
|
+
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
361
|
+
});
|
|
362
|
+
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
363
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
364
|
+
let transferTxUnsignedInfo = yield this.transferBrc20Unsigned(lockerAddress, brcInscribeTx, signer, ordinalSigner, feeRate, utxo, [opTarget]);
|
|
365
|
+
return transferTxUnsignedInfo;
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
wrapBrc20(recipientAddress, brc, brc20TokenId, lockerAddress, exchange, fee = "normal", extendedUtxo, { chainId = 137, appId = exchange ? 1 : 0 } = {}) {
|
|
369
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
370
|
+
const isExchange = !!exchange;
|
|
371
|
+
let dataHex = (0, brc20_helper_1.generateBrc2OpReturn)({
|
|
372
|
+
chainId,
|
|
373
|
+
appId,
|
|
374
|
+
brc20TokenId,
|
|
375
|
+
inputAmount: (0, bignumber_js_1.default)(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
376
|
+
recipientAddress,
|
|
377
|
+
isExchange,
|
|
378
|
+
outputToken: exchange === null || exchange === void 0 ? void 0 : exchange.outputToken,
|
|
379
|
+
outputAmount: exchange === null || exchange === void 0 ? void 0 : exchange.outputAmount,
|
|
380
|
+
});
|
|
381
|
+
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex);
|
|
382
|
+
return this.inscribeAndTransferBrc20(lockerAddress, brc, [opTarget], fee, extendedUtxo);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
exports.default = OrdinalWallet;
|
|
387
387
|
//# sourceMappingURL=ordinal-wallet.js.map
|