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