@btc-vision/wallet-sdk 1.0.2 → 1.0.5
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/es/bundle.js +1 -1
- package/es/bundle.js.LICENSE.txt +2 -2
- package/lib/address/index.js +50 -94
- package/lib/bitcoin-core.js +7 -47
- package/lib/constants.js +1 -4
- package/lib/error.js +6 -10
- package/lib/index.js +14 -53
- package/lib/keyring/hd-keyring.js +40 -89
- package/lib/keyring/index.js +4 -20
- package/lib/keyring/interfaces/SimpleKeyringOptions.js +16 -31
- package/lib/keyring/keystone-keyring.js +142 -211
- package/lib/keyring/simple-keyring.js +13 -18
- package/lib/message/bip322-simple.js +31 -81
- package/lib/message/deterministic-ecdsa.js +7 -43
- package/lib/message/ecdsa.js +10 -17
- package/lib/message/index.js +3 -19
- package/lib/network/index.js +12 -17
- package/lib/runes/index.js +1 -17
- package/lib/runes/rund_id.js +5 -10
- package/lib/runes/varint.js +6 -12
- package/lib/transaction/index.js +3 -19
- package/lib/transaction/inscription-utxo.js +9 -10
- package/lib/transaction/transaction.js +137 -142
- package/lib/transaction/utxo.js +13 -18
- package/lib/tx-helpers/index.js +8 -18
- package/lib/tx-helpers/send-atomicals-ft.js +45 -59
- package/lib/tx-helpers/send-atomicals-nft.js +29 -43
- package/lib/tx-helpers/send-btc.js +53 -70
- package/lib/tx-helpers/send-inscription.js +32 -46
- package/lib/tx-helpers/send-inscriptions.js +28 -42
- package/lib/tx-helpers/send-runes.js +83 -100
- package/lib/tx-helpers/split-inscription-utxo.js +39 -53
- package/lib/types.js +2 -5
- package/lib/utils.js +16 -29
- package/lib/wallet/abstract-wallet.js +1 -2
- package/lib/wallet/estimate-wallet.js +130 -147
- package/lib/wallet/index.js +3 -19
- package/lib/wallet/local-wallet.js +134 -150
- package/package.json +21 -12
- package/lib/address/index.d.ts +0 -36
- package/lib/bitcoin-core.d.ts +0 -5
- package/lib/constants.d.ts +0 -1
- package/lib/error.d.ts +0 -20
- package/lib/index.d.ts +0 -14
- package/lib/keyring/hd-keyring.d.ts +0 -50
- package/lib/keyring/index.d.ts +0 -4
- package/lib/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -52
- package/lib/keyring/keystone-keyring.d.ts +0 -82
- package/lib/keyring/simple-keyring.d.ts +0 -11
- package/lib/message/bip322-simple.d.ts +0 -19
- package/lib/message/deterministic-ecdsa.d.ts +0 -2
- package/lib/message/ecdsa.d.ts +0 -3
- package/lib/message/index.d.ts +0 -3
- package/lib/network/index.d.ts +0 -14
- package/lib/runes/index.d.ts +0 -1
- package/lib/runes/rund_id.d.ts +0 -11
- package/lib/runes/varint.d.ts +0 -14
- package/lib/src/address/index.d.ts +0 -36
- package/lib/src/bitcoin-core.d.ts +0 -5
- package/lib/src/constants.d.ts +0 -1
- package/lib/src/error.d.ts +0 -20
- package/lib/src/index.d.ts +0 -14
- package/lib/src/keyring/hd-keyring.d.ts +0 -50
- package/lib/src/keyring/index.d.ts +0 -4
- package/lib/src/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -52
- package/lib/src/keyring/keystone-keyring.d.ts +0 -82
- package/lib/src/keyring/simple-keyring.d.ts +0 -11
- package/lib/src/message/bip322-simple.d.ts +0 -19
- package/lib/src/message/deterministic-ecdsa.d.ts +0 -2
- package/lib/src/message/ecdsa.d.ts +0 -3
- package/lib/src/message/index.d.ts +0 -3
- package/lib/src/network/index.d.ts +0 -14
- package/lib/src/runes/index.d.ts +0 -1
- package/lib/src/runes/rund_id.d.ts +0 -11
- package/lib/src/runes/varint.d.ts +0 -14
- package/lib/src/transaction/index.d.ts +0 -3
- package/lib/src/transaction/inscription-utxo.d.ts +0 -33
- package/lib/src/transaction/transaction.d.ts +0 -51
- package/lib/src/transaction/utxo.d.ts +0 -35
- package/lib/src/tx-helpers/index.d.ts +0 -8
- package/lib/src/tx-helpers/send-atomicals-ft.d.ts +0 -16
- package/lib/src/tx-helpers/send-atomicals-nft.d.ts +0 -14
- package/lib/src/tx-helpers/send-btc.d.ts +0 -28
- package/lib/src/tx-helpers/send-inscription.d.ts +0 -16
- package/lib/src/tx-helpers/send-inscriptions.d.ts +0 -14
- package/lib/src/tx-helpers/send-runes.d.ts +0 -19
- package/lib/src/tx-helpers/split-inscription-utxo.d.ts +0 -15
- package/lib/src/types.d.ts +0 -59
- package/lib/src/utils.d.ts +0 -23
- package/lib/src/wallet/abstract-wallet.d.ts +0 -6
- package/lib/src/wallet/estimate-wallet.d.ts +0 -23
- package/lib/src/wallet/index.d.ts +0 -3
- package/lib/src/wallet/local-wallet.d.ts +0 -23
- package/lib/test/address/address.test.d.ts +0 -1
- package/lib/test/keyring/hd-keyring.test.d.ts +0 -1
- package/lib/test/keyring/keystone-keyring.test.d.ts +0 -1
- package/lib/test/keyring/simple-keyring.test.d.ts +0 -1
- package/lib/test/message/message.test.d.ts +0 -1
- package/lib/test/runes/varint.test.d.ts +0 -1
- package/lib/test/transaction/transaction.test.d.ts +0 -1
- package/lib/test/transaction/utxo.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-atomicals-ft.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-atomicals-nft.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-btc.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-inscription.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-inscriptions.test.d.ts +0 -1
- package/lib/test/tx-helpers/send-runes.test.d.ts +0 -1
- package/lib/test/tx-helpers/split-inscription-utxo.test.d.ts +0 -1
- package/lib/test/tx-helpers/utils.d.ts +0 -217
- package/lib/test/utils.d.ts +0 -4
- package/lib/test/wallet/local-wallet.test.d.ts +0 -1
- package/lib/transaction/index.d.ts +0 -3
- package/lib/transaction/inscription-utxo.d.ts +0 -33
- package/lib/transaction/transaction.d.ts +0 -51
- package/lib/transaction/utxo.d.ts +0 -35
- package/lib/tx-helpers/index.d.ts +0 -8
- package/lib/tx-helpers/send-atomicals-ft.d.ts +0 -16
- package/lib/tx-helpers/send-atomicals-nft.d.ts +0 -14
- package/lib/tx-helpers/send-btc.d.ts +0 -28
- package/lib/tx-helpers/send-inscription.d.ts +0 -16
- package/lib/tx-helpers/send-inscriptions.d.ts +0 -14
- package/lib/tx-helpers/send-runes.d.ts +0 -19
- package/lib/tx-helpers/split-inscription-utxo.d.ts +0 -15
- package/lib/types.d.ts +0 -59
- package/lib/utils.d.ts +0 -23
- package/lib/wallet/abstract-wallet.d.ts +0 -6
- package/lib/wallet/estimate-wallet.d.ts +0 -23
- package/lib/wallet/index.d.ts +0 -3
- package/lib/wallet/local-wallet.d.ts +0 -23
|
@@ -1,44 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
tx.setFeeRate(feeRate);
|
|
31
|
-
tx.setEnableRBF(enableRBF);
|
|
32
|
-
tx.setChangeAddress(changeAddress);
|
|
33
|
-
const toSignInputs = [];
|
|
34
|
-
// add asset
|
|
35
|
-
tx.addInput(assetUtxo);
|
|
36
|
-
toSignInputs.push({ index: 0, publicKey: assetUtxo.pubkey });
|
|
37
|
-
tx.addOutput(toAddress, assetUtxo.satoshis);
|
|
38
|
-
// add btc
|
|
39
|
-
const _toSignInputs = yield tx.addSufficientUtxosForFee(btcUtxos, true);
|
|
40
|
-
toSignInputs.push(..._toSignInputs);
|
|
41
|
-
const psbt = tx.toPsbt();
|
|
42
|
-
return { psbt, toSignInputs };
|
|
43
|
-
});
|
|
1
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
2
|
+
import { Transaction, utxoHelper } from '../transaction';
|
|
3
|
+
export async function sendAtomicalsNFT(params) {
|
|
4
|
+
const { assetUtxo, btcUtxos, toAddress, networkType, changeAddress, feeRate, enableRBF = true } = params;
|
|
5
|
+
// safe check
|
|
6
|
+
if (utxoHelper.hasAtomicalsFT([assetUtxo]) || utxoHelper.hasInscription([assetUtxo])) {
|
|
7
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
8
|
+
}
|
|
9
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
10
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
11
|
+
}
|
|
12
|
+
if (assetUtxo.atomicals.length !== 1) {
|
|
13
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
14
|
+
}
|
|
15
|
+
const tx = new Transaction();
|
|
16
|
+
tx.setNetworkType(networkType);
|
|
17
|
+
tx.setFeeRate(feeRate);
|
|
18
|
+
tx.setEnableRBF(enableRBF);
|
|
19
|
+
tx.setChangeAddress(changeAddress);
|
|
20
|
+
const toSignInputs = [];
|
|
21
|
+
// add asset
|
|
22
|
+
tx.addInput(assetUtxo);
|
|
23
|
+
toSignInputs.push({ index: 0, publicKey: assetUtxo.pubkey });
|
|
24
|
+
tx.addOutput(toAddress, assetUtxo.satoshis);
|
|
25
|
+
// add btc
|
|
26
|
+
const _toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos, true);
|
|
27
|
+
toSignInputs.push(..._toSignInputs);
|
|
28
|
+
const psbt = tx.toPsbt();
|
|
29
|
+
return { psbt, toSignInputs };
|
|
44
30
|
}
|
|
@@ -1,77 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { UTXO_DUST } from '../constants';
|
|
2
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
3
|
+
import { Transaction, utxoHelper } from '../transaction';
|
|
4
|
+
export async function sendBTC(params) {
|
|
5
|
+
const { btcUtxos, tos, networkType, changeAddress, feeRate, enableRBF = true, memo, memos } = params;
|
|
6
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
7
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
8
|
+
}
|
|
9
|
+
const tx = new Transaction();
|
|
10
|
+
tx.setNetworkType(networkType);
|
|
11
|
+
tx.setFeeRate(feeRate);
|
|
12
|
+
tx.setEnableRBF(enableRBF);
|
|
13
|
+
tx.setChangeAddress(changeAddress);
|
|
14
|
+
tos.forEach((v) => {
|
|
15
|
+
tx.addOutput(v.address, v.satoshis);
|
|
9
16
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.sendAllBTC = sendAllBTC;
|
|
14
|
-
const constants_1 = require("../constants");
|
|
15
|
-
const error_1 = require("../error");
|
|
16
|
-
const transaction_1 = require("../transaction");
|
|
17
|
-
function sendBTC(params) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
const { btcUtxos, tos, networkType, changeAddress, feeRate, enableRBF = true, memo, memos } = params;
|
|
20
|
-
if (transaction_1.utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
21
|
-
throw new error_1.WalletUtilsError(error_1.ErrorCodes.NOT_SAFE_UTXOS);
|
|
17
|
+
if (memo) {
|
|
18
|
+
if (Buffer.from(memo, 'hex').toString('hex') === memo) {
|
|
19
|
+
tx.addOpreturn([Buffer.from(memo, 'hex')]);
|
|
22
20
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
tx.setFeeRate(feeRate);
|
|
26
|
-
tx.setEnableRBF(enableRBF);
|
|
27
|
-
tx.setChangeAddress(changeAddress);
|
|
28
|
-
tos.forEach((v) => {
|
|
29
|
-
tx.addOutput(v.address, v.satoshis);
|
|
30
|
-
});
|
|
31
|
-
if (memo) {
|
|
32
|
-
if (Buffer.from(memo, 'hex').toString('hex') === memo) {
|
|
33
|
-
tx.addOpreturn([Buffer.from(memo, 'hex')]);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
tx.addOpreturn([Buffer.from(memo)]);
|
|
37
|
-
}
|
|
21
|
+
else {
|
|
22
|
+
tx.addOpreturn([Buffer.from(memo)]);
|
|
38
23
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else {
|
|
44
|
-
tx.addOpreturn(memos.map((memo) => Buffer.from(memo)));
|
|
45
|
-
}
|
|
24
|
+
}
|
|
25
|
+
else if (memos) {
|
|
26
|
+
if (Buffer.from(memos[0], 'hex').toString('hex') === memos[0]) {
|
|
27
|
+
tx.addOpreturn(memos.map((memo) => Buffer.from(memo, 'hex')));
|
|
46
28
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return { psbt, toSignInputs };
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function sendAllBTC(params) {
|
|
53
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
const { btcUtxos, toAddress, networkType, feeRate, enableRBF = true } = params;
|
|
55
|
-
if (transaction_1.utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
56
|
-
throw new error_1.WalletUtilsError(error_1.ErrorCodes.NOT_SAFE_UTXOS);
|
|
29
|
+
else {
|
|
30
|
+
tx.addOpreturn(memos.map((memo) => Buffer.from(memo)));
|
|
57
31
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
32
|
+
}
|
|
33
|
+
const toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos);
|
|
34
|
+
const psbt = tx.toPsbt();
|
|
35
|
+
return { psbt, toSignInputs };
|
|
36
|
+
}
|
|
37
|
+
export async function sendAllBTC(params) {
|
|
38
|
+
const { btcUtxos, toAddress, networkType, feeRate, enableRBF = true } = params;
|
|
39
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
40
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
41
|
+
}
|
|
42
|
+
const tx = new Transaction();
|
|
43
|
+
tx.setNetworkType(networkType);
|
|
44
|
+
tx.setFeeRate(feeRate);
|
|
45
|
+
tx.setEnableRBF(enableRBF);
|
|
46
|
+
tx.addOutput(toAddress, UTXO_DUST);
|
|
47
|
+
const toSignInputs = [];
|
|
48
|
+
btcUtxos.forEach((v, index) => {
|
|
49
|
+
tx.addInput(v);
|
|
50
|
+
toSignInputs.push({ index, publicKey: v.pubkey });
|
|
76
51
|
});
|
|
52
|
+
const fee = await tx.calNetworkFee();
|
|
53
|
+
const unspent = tx.getTotalInput() - fee;
|
|
54
|
+
if (unspent < UTXO_DUST) {
|
|
55
|
+
throw new WalletUtilsError(ErrorCodes.INSUFFICIENT_BTC_UTXO);
|
|
56
|
+
}
|
|
57
|
+
tx.outputs[0].value = unspent;
|
|
58
|
+
const psbt = tx.toPsbt();
|
|
59
|
+
return { psbt, toSignInputs };
|
|
77
60
|
}
|
|
@@ -1,48 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
throw new error_1.WalletUtilsError(error_1.ErrorCodes.ASSET_MAYBE_LOST);
|
|
32
|
-
}
|
|
33
|
-
const tx = new transaction_1.Transaction();
|
|
34
|
-
tx.setNetworkType(networkType);
|
|
35
|
-
tx.setFeeRate(feeRate);
|
|
36
|
-
tx.setEnableRBF(enableRBF);
|
|
37
|
-
tx.setChangeAddress(changeAddress);
|
|
38
|
-
tx.addInput(assetUtxo);
|
|
39
|
-
tx.addOutput(toAddress, outputValue);
|
|
40
|
-
const toSignInputs = yield tx.addSufficientUtxosForFee(btcUtxos);
|
|
41
|
-
toSignInputs.push({
|
|
42
|
-
index: 0,
|
|
43
|
-
publicKey: assetUtxo.pubkey
|
|
44
|
-
});
|
|
45
|
-
const psbt = tx.toPsbt();
|
|
46
|
-
return { psbt, toSignInputs };
|
|
1
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
2
|
+
import { Transaction } from '../transaction/transaction';
|
|
3
|
+
import { utxoHelper } from '../transaction/utxo';
|
|
4
|
+
export async function sendInscription({ assetUtxo, btcUtxos, toAddress, networkType, changeAddress, feeRate, outputValue, enableRBF = true, enableMixed = false }) {
|
|
5
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
6
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
7
|
+
}
|
|
8
|
+
if (utxoHelper.hasAtomicals([assetUtxo])) {
|
|
9
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
10
|
+
}
|
|
11
|
+
if (!enableMixed && assetUtxo.inscriptions.length !== 1) {
|
|
12
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
13
|
+
}
|
|
14
|
+
const maxOffset = assetUtxo.inscriptions.reduce((pre, cur) => {
|
|
15
|
+
return Math.max(pre, cur.offset);
|
|
16
|
+
}, 0);
|
|
17
|
+
if (outputValue - 1 < maxOffset) {
|
|
18
|
+
throw new WalletUtilsError(ErrorCodes.ASSET_MAYBE_LOST);
|
|
19
|
+
}
|
|
20
|
+
const tx = new Transaction();
|
|
21
|
+
tx.setNetworkType(networkType);
|
|
22
|
+
tx.setFeeRate(feeRate);
|
|
23
|
+
tx.setEnableRBF(enableRBF);
|
|
24
|
+
tx.setChangeAddress(changeAddress);
|
|
25
|
+
tx.addInput(assetUtxo);
|
|
26
|
+
tx.addOutput(toAddress, outputValue);
|
|
27
|
+
const toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos);
|
|
28
|
+
toSignInputs.push({
|
|
29
|
+
index: 0,
|
|
30
|
+
publicKey: assetUtxo.pubkey
|
|
47
31
|
});
|
|
32
|
+
const psbt = tx.toPsbt();
|
|
33
|
+
return { psbt, toSignInputs };
|
|
48
34
|
}
|
|
@@ -1,44 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
2
|
+
import { Transaction } from '../transaction/transaction';
|
|
3
|
+
import { utxoHelper } from '../transaction/utxo';
|
|
4
|
+
export async function sendInscriptions({ assetUtxos, btcUtxos, toAddress, networkType, changeAddress, feeRate, enableRBF = true }) {
|
|
5
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
6
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
7
|
+
}
|
|
8
|
+
if (utxoHelper.hasAtomicals(assetUtxos)) {
|
|
9
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
10
|
+
}
|
|
11
|
+
const tx = new Transaction();
|
|
12
|
+
tx.setNetworkType(networkType);
|
|
13
|
+
tx.setFeeRate(feeRate);
|
|
14
|
+
tx.setEnableRBF(enableRBF);
|
|
15
|
+
tx.setChangeAddress(changeAddress);
|
|
16
|
+
const toSignInputs = [];
|
|
17
|
+
for (let i = 0; i < assetUtxos.length; i++) {
|
|
18
|
+
const assetUtxo = assetUtxos[i];
|
|
19
|
+
if (assetUtxo.inscriptions.length > 1) {
|
|
20
|
+
throw new Error('Multiple inscriptions in one UTXO! Please split them first.');
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const toSignInputs = [];
|
|
30
|
-
for (let i = 0; i < assetUtxos.length; i++) {
|
|
31
|
-
const assetUtxo = assetUtxos[i];
|
|
32
|
-
if (assetUtxo.inscriptions.length > 1) {
|
|
33
|
-
throw new Error('Multiple inscriptions in one UTXO! Please split them first.');
|
|
34
|
-
}
|
|
35
|
-
tx.addInput(assetUtxo);
|
|
36
|
-
tx.addOutput(toAddress, assetUtxo.satoshis);
|
|
37
|
-
toSignInputs.push({ index: i, publicKey: assetUtxo.pubkey });
|
|
38
|
-
}
|
|
39
|
-
const _toSignInputs = yield tx.addSufficientUtxosForFee(btcUtxos);
|
|
40
|
-
toSignInputs.push(..._toSignInputs);
|
|
41
|
-
const psbt = tx.toPsbt();
|
|
42
|
-
return { psbt, toSignInputs };
|
|
43
|
-
});
|
|
22
|
+
tx.addInput(assetUtxo);
|
|
23
|
+
tx.addOutput(toAddress, assetUtxo.satoshis);
|
|
24
|
+
toSignInputs.push({ index: i, publicKey: assetUtxo.pubkey });
|
|
25
|
+
}
|
|
26
|
+
const _toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos);
|
|
27
|
+
toSignInputs.push(..._toSignInputs);
|
|
28
|
+
const psbt = tx.toPsbt();
|
|
29
|
+
return { psbt, toSignInputs };
|
|
44
30
|
}
|
|
@@ -1,104 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
exports.sendRunes = sendRunes;
|
|
16
|
-
const big_integer_1 = __importDefault(require("big-integer"));
|
|
17
|
-
const bitcoin_core_1 = require("../bitcoin-core");
|
|
18
|
-
const error_1 = require("../error");
|
|
19
|
-
const runes_1 = require("../runes");
|
|
20
|
-
const rund_id_1 = require("../runes/rund_id");
|
|
21
|
-
const transaction_1 = require("../transaction/transaction");
|
|
22
|
-
const utxo_1 = require("../transaction/utxo");
|
|
1
|
+
import bigInt from 'big-integer';
|
|
2
|
+
import { bitcoin } from '../bitcoin-core';
|
|
3
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
4
|
+
import { varint } from '../runes';
|
|
5
|
+
import { RuneId } from '../runes/rund_id';
|
|
6
|
+
import { Transaction } from '../transaction/transaction';
|
|
7
|
+
import { utxoHelper } from '../transaction/utxo';
|
|
23
8
|
// only one arc20 can be send
|
|
24
|
-
function sendRunes(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
if (Object.keys(runesMap).length > 1) {
|
|
58
|
-
hasMultipleRunes = true;
|
|
59
|
-
}
|
|
60
|
-
const changedRuneAmount = fromRuneAmount.minus((0, big_integer_1.default)(runeAmount));
|
|
61
|
-
if (changedRuneAmount.lt(0)) {
|
|
62
|
-
throw new error_1.WalletUtilsError(error_1.ErrorCodes.INSUFFICIENT_ASSET_UTXO);
|
|
63
|
-
}
|
|
64
|
-
let needChange = false;
|
|
65
|
-
if (hasMultipleRunes || changedRuneAmount.gt(0)) {
|
|
66
|
-
needChange = true;
|
|
67
|
-
}
|
|
68
|
-
let payload = [];
|
|
69
|
-
let runeId = rund_id_1.RuneId.fromString(runeid);
|
|
70
|
-
runes_1.varint.encodeToVec(0, payload);
|
|
71
|
-
// add send data
|
|
72
|
-
runes_1.varint.encodeToVec(runeId.block, payload);
|
|
73
|
-
runes_1.varint.encodeToVec(runeId.tx, payload);
|
|
74
|
-
runes_1.varint.encodeToVec(runeAmount, payload);
|
|
75
|
-
if (needChange) {
|
|
76
|
-
// 1 is to change
|
|
77
|
-
// 2 is to send
|
|
78
|
-
runes_1.varint.encodeToVec(2, payload);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
// 1 is to send
|
|
82
|
-
runes_1.varint.encodeToVec(1, payload);
|
|
83
|
-
}
|
|
84
|
-
// add op_return
|
|
85
|
-
tx.addScriptOutput(
|
|
86
|
-
// OUTPUT_0
|
|
87
|
-
bitcoin_core_1.bitcoin.script.compile([
|
|
88
|
-
bitcoin_core_1.bitcoin.opcodes.OP_RETURN,
|
|
89
|
-
bitcoin_core_1.bitcoin.opcodes.OP_13,
|
|
90
|
-
Buffer.from(new Uint8Array(payload))
|
|
91
|
-
]), 0);
|
|
92
|
-
if (needChange) {
|
|
93
|
-
// OUTPUT_1
|
|
94
|
-
// add change
|
|
95
|
-
tx.addOutput(assetAddress, outputValue);
|
|
9
|
+
export async function sendRunes({ assetUtxos, btcUtxos, assetAddress, btcAddress, toAddress, networkType, runeid, runeAmount, outputValue, feeRate, enableRBF = true }) {
|
|
10
|
+
// safe check
|
|
11
|
+
if (utxoHelper.hasAtomicalsNFT(assetUtxos) || utxoHelper.hasInscription(assetUtxos)) {
|
|
12
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
13
|
+
}
|
|
14
|
+
if (utxoHelper.hasAnyAssets(btcUtxos)) {
|
|
15
|
+
throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
|
|
16
|
+
}
|
|
17
|
+
const tx = new Transaction();
|
|
18
|
+
tx.setNetworkType(networkType);
|
|
19
|
+
tx.setFeeRate(feeRate);
|
|
20
|
+
tx.setEnableRBF(enableRBF);
|
|
21
|
+
tx.setChangeAddress(btcAddress);
|
|
22
|
+
const toSignInputs = [];
|
|
23
|
+
// add assets
|
|
24
|
+
assetUtxos.forEach((v, index) => {
|
|
25
|
+
tx.addInput(v);
|
|
26
|
+
toSignInputs.push({ index, publicKey: v.pubkey });
|
|
27
|
+
});
|
|
28
|
+
let fromRuneAmount = bigInt(0);
|
|
29
|
+
let hasMultipleRunes = false;
|
|
30
|
+
let runesMap = {};
|
|
31
|
+
assetUtxos.forEach((v) => {
|
|
32
|
+
if (v.runes) {
|
|
33
|
+
v.runes.forEach((w) => {
|
|
34
|
+
runesMap[w.runeid] = true;
|
|
35
|
+
if (w.runeid === runeid) {
|
|
36
|
+
fromRuneAmount = fromRuneAmount.plus(bigInt(w.amount));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
96
39
|
}
|
|
97
|
-
tx.addOutput(toAddress, outputValue);
|
|
98
|
-
// add btc
|
|
99
|
-
const _toSignInputs = yield tx.addSufficientUtxosForFee(btcUtxos, true);
|
|
100
|
-
toSignInputs.push(..._toSignInputs);
|
|
101
|
-
const psbt = tx.toPsbt();
|
|
102
|
-
return { psbt, toSignInputs };
|
|
103
40
|
});
|
|
41
|
+
if (Object.keys(runesMap).length > 1) {
|
|
42
|
+
hasMultipleRunes = true;
|
|
43
|
+
}
|
|
44
|
+
const changedRuneAmount = fromRuneAmount.minus(bigInt(runeAmount));
|
|
45
|
+
if (changedRuneAmount.lt(0)) {
|
|
46
|
+
throw new WalletUtilsError(ErrorCodes.INSUFFICIENT_ASSET_UTXO);
|
|
47
|
+
}
|
|
48
|
+
let needChange = false;
|
|
49
|
+
if (hasMultipleRunes || changedRuneAmount.gt(0)) {
|
|
50
|
+
needChange = true;
|
|
51
|
+
}
|
|
52
|
+
let payload = [];
|
|
53
|
+
let runeId = RuneId.fromString(runeid);
|
|
54
|
+
varint.encodeToVec(0, payload);
|
|
55
|
+
// add send data
|
|
56
|
+
varint.encodeToVec(runeId.block, payload);
|
|
57
|
+
varint.encodeToVec(runeId.tx, payload);
|
|
58
|
+
varint.encodeToVec(runeAmount, payload);
|
|
59
|
+
if (needChange) {
|
|
60
|
+
// 1 is to change
|
|
61
|
+
// 2 is to send
|
|
62
|
+
varint.encodeToVec(2, payload);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
// 1 is to send
|
|
66
|
+
varint.encodeToVec(1, payload);
|
|
67
|
+
}
|
|
68
|
+
// add op_return
|
|
69
|
+
tx.addScriptOutput(
|
|
70
|
+
// OUTPUT_0
|
|
71
|
+
bitcoin.script.compile([
|
|
72
|
+
bitcoin.opcodes.OP_RETURN,
|
|
73
|
+
bitcoin.opcodes.OP_13,
|
|
74
|
+
Buffer.from(new Uint8Array(payload))
|
|
75
|
+
]), 0);
|
|
76
|
+
if (needChange) {
|
|
77
|
+
// OUTPUT_1
|
|
78
|
+
// add change
|
|
79
|
+
tx.addOutput(assetAddress, outputValue);
|
|
80
|
+
}
|
|
81
|
+
tx.addOutput(toAddress, outputValue);
|
|
82
|
+
// add btc
|
|
83
|
+
const _toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos, true);
|
|
84
|
+
toSignInputs.push(..._toSignInputs);
|
|
85
|
+
const psbt = tx.toPsbt();
|
|
86
|
+
return { psbt, toSignInputs };
|
|
104
87
|
}
|