@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,57 +1,43 @@
|
|
|
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
|
-
tx.setNetworkType(networkType);
|
|
26
|
-
tx.setFeeRate(feeRate);
|
|
27
|
-
tx.setEnableRBF(enableRBF);
|
|
28
|
-
tx.setChangeAddress(changeAddress);
|
|
29
|
-
const toSignInputs = [];
|
|
30
|
-
let lastUnit = null;
|
|
31
|
-
let splitedCount = 0;
|
|
32
|
-
const ordUtxo = new transaction_1.InscriptionUnspendOutput(assetUtxo, outputValue);
|
|
33
|
-
tx.addInput(ordUtxo.utxo);
|
|
34
|
-
toSignInputs.push({ index: 0, publicKey: ordUtxo.utxo.pubkey });
|
|
35
|
-
for (let j = 0; j < ordUtxo.inscriptionUnits.length; j++) {
|
|
36
|
-
const unit = ordUtxo.inscriptionUnits[j];
|
|
37
|
-
if (unit.hasInscriptions()) {
|
|
38
|
-
tx.addChangeOutput(unit.satoshis);
|
|
39
|
-
lastUnit = unit;
|
|
40
|
-
splitedCount++;
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
1
|
+
import { UTXO_DUST } from '../constants';
|
|
2
|
+
import { ErrorCodes, WalletUtilsError } from '../error';
|
|
3
|
+
import { InscriptionUnspendOutput, Transaction, utxoHelper } from '../transaction';
|
|
4
|
+
export async function splitInscriptionUtxo({ btcUtxos, assetUtxo, networkType, changeAddress, feeRate, enableRBF = true, outputValue = 546 }) {
|
|
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
|
+
const tx = new Transaction();
|
|
12
|
+
tx.setNetworkType(networkType);
|
|
13
|
+
tx.setFeeRate(feeRate || 1);
|
|
14
|
+
tx.setEnableRBF(enableRBF);
|
|
15
|
+
tx.setChangeAddress(changeAddress);
|
|
16
|
+
const toSignInputs = [];
|
|
17
|
+
let lastUnit = null;
|
|
18
|
+
let splitedCount = 0;
|
|
19
|
+
const ordUtxo = new InscriptionUnspendOutput(assetUtxo, outputValue);
|
|
20
|
+
tx.addInput(ordUtxo.utxo);
|
|
21
|
+
toSignInputs.push({ index: 0, publicKey: ordUtxo.utxo.pubkey });
|
|
22
|
+
for (let j = 0; j < ordUtxo.inscriptionUnits.length; j++) {
|
|
23
|
+
const unit = ordUtxo.inscriptionUnits[j];
|
|
24
|
+
if (unit.hasInscriptions()) {
|
|
43
25
|
tx.addChangeOutput(unit.satoshis);
|
|
44
26
|
lastUnit = unit;
|
|
27
|
+
splitedCount++;
|
|
28
|
+
continue;
|
|
45
29
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
30
|
+
tx.addChangeOutput(unit.satoshis);
|
|
31
|
+
lastUnit = unit;
|
|
32
|
+
}
|
|
33
|
+
if (!lastUnit.hasInscriptions()) {
|
|
34
|
+
tx.removeChangeOutput();
|
|
35
|
+
}
|
|
36
|
+
if (lastUnit.satoshis < UTXO_DUST) {
|
|
37
|
+
lastUnit.satoshis = UTXO_DUST;
|
|
38
|
+
}
|
|
39
|
+
const _toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos);
|
|
40
|
+
toSignInputs.push(..._toSignInputs);
|
|
41
|
+
const psbt = tx.toPsbt();
|
|
42
|
+
return { psbt, toSignInputs, splitedCount };
|
|
57
43
|
}
|
package/lib/types.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AddressType = void 0;
|
|
4
|
-
var AddressType;
|
|
1
|
+
export var AddressType;
|
|
5
2
|
(function (AddressType) {
|
|
6
3
|
AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
|
|
7
4
|
AddressType[AddressType["P2WPKH"] = 1] = "P2WPKH";
|
|
@@ -12,4 +9,4 @@ var AddressType;
|
|
|
12
9
|
AddressType[AddressType["P2WSH"] = 6] = "P2WSH";
|
|
13
10
|
AddressType[AddressType["P2SH"] = 7] = "P2SH";
|
|
14
11
|
AddressType[AddressType["UNKNOWN"] = 8] = "UNKNOWN";
|
|
15
|
-
})(AddressType || (
|
|
12
|
+
})(AddressType || (AddressType = {}));
|
package/lib/utils.js
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.schnorrValidator = exports.validator = exports.toXOnly = void 0;
|
|
7
|
-
exports.tweakSigner = tweakSigner;
|
|
8
|
-
exports.satoshisToAmount = satoshisToAmount;
|
|
9
|
-
exports.amountToSaothis = amountToSaothis;
|
|
10
|
-
exports.shortAddress = shortAddress;
|
|
11
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
12
|
-
const bitcoin_core_1 = require("./bitcoin-core");
|
|
13
|
-
const toXOnly = (pubKey) => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
|
|
14
|
-
exports.toXOnly = toXOnly;
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { bitcoin, ecc, ECPair } from './bitcoin-core';
|
|
3
|
+
export const toXOnly = (pubKey) => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
|
|
15
4
|
function tapTweakHash(pubKey, h) {
|
|
16
|
-
return
|
|
5
|
+
return bitcoin.crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]));
|
|
17
6
|
}
|
|
18
7
|
/**
|
|
19
8
|
* Transform raw private key to taproot address private key
|
|
20
9
|
*/
|
|
21
|
-
function tweakSigner(signer, opts = {}) {
|
|
10
|
+
export function tweakSigner(signer, opts = {}) {
|
|
22
11
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
23
12
|
// @ts-ignore
|
|
24
13
|
let privateKey = signer.privateKey;
|
|
@@ -26,43 +15,41 @@ function tweakSigner(signer, opts = {}) {
|
|
|
26
15
|
throw new Error('Private key is required for tweaking signer!');
|
|
27
16
|
}
|
|
28
17
|
if (signer.publicKey[0] === 3) {
|
|
29
|
-
privateKey =
|
|
18
|
+
privateKey = ecc.privateNegate(privateKey);
|
|
30
19
|
}
|
|
31
|
-
const tweakedPrivateKey =
|
|
20
|
+
const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash));
|
|
32
21
|
if (!tweakedPrivateKey) {
|
|
33
22
|
throw new Error('Invalid tweaked private key!');
|
|
34
23
|
}
|
|
35
|
-
return
|
|
24
|
+
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
36
25
|
network: opts.network
|
|
37
26
|
});
|
|
38
27
|
}
|
|
39
28
|
/**
|
|
40
29
|
* ECDSA signature validator
|
|
41
30
|
*/
|
|
42
|
-
const validator = (pubkey, msghash, signature) =>
|
|
43
|
-
exports.validator = validator;
|
|
31
|
+
export const validator = (pubkey, msghash, signature) => ECPair.fromPublicKey(pubkey).verify(msghash, signature);
|
|
44
32
|
/**
|
|
45
33
|
* Schnorr signature validator
|
|
46
34
|
*/
|
|
47
|
-
const schnorrValidator = (pubkey, msghash, signature) => {
|
|
48
|
-
return
|
|
35
|
+
export const schnorrValidator = (pubkey, msghash, signature) => {
|
|
36
|
+
return ECPair.fromPublicKey(pubkey).verifySchnorr(msghash, signature);
|
|
49
37
|
};
|
|
50
|
-
exports.schnorrValidator = schnorrValidator;
|
|
51
38
|
/**
|
|
52
39
|
* Transform satoshis to btc format
|
|
53
40
|
*/
|
|
54
|
-
function satoshisToAmount(val) {
|
|
55
|
-
const num = new
|
|
41
|
+
export function satoshisToAmount(val) {
|
|
42
|
+
const num = new BigNumber(val);
|
|
56
43
|
return num.dividedBy(100000000).toFixed(8);
|
|
57
44
|
}
|
|
58
45
|
/**
|
|
59
46
|
* Transform btc format to satoshis
|
|
60
47
|
*/
|
|
61
|
-
function amountToSaothis(val) {
|
|
62
|
-
const num = new
|
|
48
|
+
export function amountToSaothis(val) {
|
|
49
|
+
const num = new BigNumber(val);
|
|
63
50
|
return num.multipliedBy(100000000).toNumber();
|
|
64
51
|
}
|
|
65
|
-
function shortAddress(address, len = 5) {
|
|
52
|
+
export function shortAddress(address, len = 5) {
|
|
66
53
|
if (!address)
|
|
67
54
|
return '';
|
|
68
55
|
if (address.length <= len * 2)
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,174 +1,157 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.EstimateWallet = void 0;
|
|
13
|
-
const address_1 = require("../address");
|
|
14
|
-
const bitcoin_core_1 = require("../bitcoin-core");
|
|
15
|
-
const keyring_1 = require("../keyring");
|
|
16
|
-
const message_1 = require("../message");
|
|
17
|
-
const network_1 = require("../network");
|
|
18
|
-
const types_1 = require("../types");
|
|
19
|
-
const utils_1 = require("../utils");
|
|
1
|
+
import { publicKeyToAddress, scriptPkToAddress } from '../address';
|
|
2
|
+
import { bitcoin, ECPair } from '../bitcoin-core';
|
|
3
|
+
import { SimpleKeyring } from '../keyring';
|
|
4
|
+
import { signMessageOfBIP322Simple } from '../message';
|
|
5
|
+
import { NetworkType, toPsbtNetwork } from '../network';
|
|
6
|
+
import { AddressType } from '../types';
|
|
7
|
+
import { toXOnly } from '../utils';
|
|
20
8
|
/**
|
|
21
9
|
* EstimateWallet is a wallet that can be used to estimate the size of a transaction.
|
|
22
10
|
*/
|
|
23
|
-
class EstimateWallet {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
export class EstimateWallet {
|
|
12
|
+
keyring;
|
|
13
|
+
address;
|
|
14
|
+
pubkey;
|
|
15
|
+
network;
|
|
16
|
+
networkType;
|
|
17
|
+
addressType;
|
|
18
|
+
constructor(wif, networkType = NetworkType.MAINNET, addressType = AddressType.P2WPKH) {
|
|
19
|
+
const network = toPsbtNetwork(networkType);
|
|
20
|
+
const keyPair = ECPair.fromWIF(wif, network);
|
|
21
|
+
this.keyring = new SimpleKeyring({
|
|
28
22
|
privateKeys: [keyPair.privateKey.toString('hex')],
|
|
29
23
|
network: network
|
|
30
24
|
});
|
|
31
25
|
this.keyring.addAccounts(1);
|
|
32
26
|
this.pubkey = keyPair.publicKey.toString('hex');
|
|
33
|
-
this.address =
|
|
27
|
+
this.address = publicKeyToAddress(this.pubkey, addressType, networkType);
|
|
34
28
|
this.network = network;
|
|
35
29
|
this.networkType = networkType;
|
|
36
30
|
this.addressType = addressType;
|
|
37
31
|
}
|
|
38
|
-
static fromRandom(addressType =
|
|
39
|
-
const network =
|
|
40
|
-
const ecpair =
|
|
32
|
+
static fromRandom(addressType = AddressType.P2WPKH, networkType = NetworkType.MAINNET) {
|
|
33
|
+
const network = toPsbtNetwork(networkType);
|
|
34
|
+
const ecpair = ECPair.makeRandom({ network });
|
|
41
35
|
const wallet = new EstimateWallet(ecpair.toWIF(), networkType, addressType);
|
|
42
36
|
return wallet;
|
|
43
37
|
}
|
|
44
38
|
getNetworkType() {
|
|
45
39
|
return this.networkType;
|
|
46
40
|
}
|
|
47
|
-
signPsbt(psbt, opts) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
internalPubkey: tapInternalKey,
|
|
67
|
-
network: (0, network_1.toPsbtNetwork)(this.networkType)
|
|
68
|
-
});
|
|
69
|
-
if (((_a = v.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script.toString('hex')) == (output === null || output === void 0 ? void 0 : output.toString('hex'))) {
|
|
70
|
-
v.tapInternalKey = tapInternalKey;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
psbt = yield this.keyring.signTransaction(psbt, _inputs);
|
|
75
|
-
if (_opts.autoFinalized) {
|
|
76
|
-
_inputs.forEach((v) => {
|
|
77
|
-
// psbt.validateSignaturesOfInput(v.index, validator);
|
|
78
|
-
psbt.finalizeInput(v.index);
|
|
41
|
+
async signPsbt(psbt, opts) {
|
|
42
|
+
const _opts = opts || {
|
|
43
|
+
autoFinalized: true,
|
|
44
|
+
toSignInputs: []
|
|
45
|
+
};
|
|
46
|
+
let _inputs = await this.formatOptionsToSignInputs(psbt, opts);
|
|
47
|
+
if (_inputs.length == 0) {
|
|
48
|
+
throw new Error('no input to sign');
|
|
49
|
+
}
|
|
50
|
+
psbt.data.inputs.forEach((v) => {
|
|
51
|
+
const isNotSigned = !(v.finalScriptSig || v.finalScriptWitness);
|
|
52
|
+
const isP2TR = this.addressType === AddressType.P2TR || this.addressType === AddressType.M44_P2TR;
|
|
53
|
+
const lostInternalPubkey = !v.tapInternalKey;
|
|
54
|
+
// Special measures taken for compatibility with certain applications.
|
|
55
|
+
if (isNotSigned && isP2TR && lostInternalPubkey) {
|
|
56
|
+
const tapInternalKey = toXOnly(Buffer.from(this.pubkey, 'hex'));
|
|
57
|
+
const { output } = bitcoin.payments.p2tr({
|
|
58
|
+
internalPubkey: tapInternalKey,
|
|
59
|
+
network: toPsbtNetwork(this.networkType)
|
|
79
60
|
});
|
|
61
|
+
if (v.witnessUtxo?.script.toString('hex') == output?.toString('hex')) {
|
|
62
|
+
v.tapInternalKey = tapInternalKey;
|
|
63
|
+
}
|
|
80
64
|
}
|
|
81
|
-
return psbt;
|
|
82
65
|
});
|
|
66
|
+
psbt = await this.keyring.signTransaction(psbt, _inputs);
|
|
67
|
+
if (_opts.autoFinalized) {
|
|
68
|
+
_inputs.forEach((v) => {
|
|
69
|
+
// psbt.validateSignaturesOfInput(v.index, validator);
|
|
70
|
+
psbt.finalizeInput(v.index);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return psbt;
|
|
83
74
|
}
|
|
84
|
-
getPublicKey() {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return pubkeys[0];
|
|
88
|
-
});
|
|
75
|
+
async getPublicKey() {
|
|
76
|
+
const pubkeys = await this.keyring.getAccounts();
|
|
77
|
+
return pubkeys[0];
|
|
89
78
|
}
|
|
90
|
-
signMessage(text, type) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
});
|
|
79
|
+
async signMessage(text, type) {
|
|
80
|
+
if (type === 'bip322-simple') {
|
|
81
|
+
return await signMessageOfBIP322Simple({
|
|
82
|
+
message: text,
|
|
83
|
+
address: this.address,
|
|
84
|
+
networkType: this.networkType,
|
|
85
|
+
wallet: this
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const pubkey = await this.getPublicKey();
|
|
90
|
+
return await this.keyring.signMessage(pubkey, text);
|
|
91
|
+
}
|
|
105
92
|
}
|
|
106
|
-
formatOptionsToSignInputs(_psbt, options) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
sighashTypes: v.sighashType ? [v.sighashType] : undefined
|
|
165
|
-
});
|
|
166
|
-
}
|
|
93
|
+
async formatOptionsToSignInputs(_psbt, options) {
|
|
94
|
+
const accountAddress = this.address;
|
|
95
|
+
const accountPubkey = await this.getPublicKey();
|
|
96
|
+
let toSignInputs = [];
|
|
97
|
+
if (options && options.toSignInputs) {
|
|
98
|
+
// We expect userToSignInputs objects to be similar to ToSignInput interface,
|
|
99
|
+
// but we allow address to be specified in addition to publicKey for convenience.
|
|
100
|
+
toSignInputs = options.toSignInputs.map((input) => {
|
|
101
|
+
const index = Number(input.index);
|
|
102
|
+
if (isNaN(index))
|
|
103
|
+
throw new Error('invalid index in toSignInput');
|
|
104
|
+
if (!input.address && !input.publicKey) {
|
|
105
|
+
throw new Error('no address or public key in toSignInput');
|
|
106
|
+
}
|
|
107
|
+
if (input.address &&
|
|
108
|
+
input.address != accountAddress) {
|
|
109
|
+
throw new Error('invalid address in toSignInput');
|
|
110
|
+
}
|
|
111
|
+
if (input.publicKey &&
|
|
112
|
+
input.publicKey != accountPubkey) {
|
|
113
|
+
throw new Error('invalid public key in toSignInput');
|
|
114
|
+
}
|
|
115
|
+
const sighashTypes = input.sighashTypes?.map(Number);
|
|
116
|
+
if (sighashTypes?.some(isNaN))
|
|
117
|
+
throw new Error('invalid sighash type in toSignInput');
|
|
118
|
+
return {
|
|
119
|
+
index,
|
|
120
|
+
publicKey: accountPubkey,
|
|
121
|
+
sighashTypes,
|
|
122
|
+
disableTweakSigner: input.disableTweakSigner
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const networkType = this.getNetworkType();
|
|
128
|
+
const psbtNetwork = toPsbtNetwork(networkType);
|
|
129
|
+
const psbt = typeof _psbt === 'string'
|
|
130
|
+
? bitcoin.Psbt.fromHex(_psbt, { network: psbtNetwork })
|
|
131
|
+
: _psbt;
|
|
132
|
+
psbt.data.inputs.forEach((v, index) => {
|
|
133
|
+
let script = null;
|
|
134
|
+
if (v.witnessUtxo) {
|
|
135
|
+
script = v.witnessUtxo.script;
|
|
136
|
+
}
|
|
137
|
+
else if (v.nonWitnessUtxo) {
|
|
138
|
+
const tx = bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo);
|
|
139
|
+
const output = tx.outs[psbt.txInputs[index].index];
|
|
140
|
+
script = output.script;
|
|
141
|
+
}
|
|
142
|
+
const isSigned = v.finalScriptSig || v.finalScriptWitness;
|
|
143
|
+
if (script && !isSigned) {
|
|
144
|
+
const address = scriptPkToAddress(script, this.networkType);
|
|
145
|
+
if (accountAddress === address) {
|
|
146
|
+
toSignInputs.push({
|
|
147
|
+
index,
|
|
148
|
+
publicKey: accountPubkey,
|
|
149
|
+
sighashTypes: v.sighashType ? [v.sighashType] : undefined
|
|
150
|
+
});
|
|
167
151
|
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return toSignInputs;
|
|
172
156
|
}
|
|
173
157
|
}
|
|
174
|
-
exports.EstimateWallet = EstimateWallet;
|
package/lib/wallet/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./abstract-wallet"), exports);
|
|
18
|
-
__exportStar(require("./estimate-wallet"), exports);
|
|
19
|
-
__exportStar(require("./local-wallet"), exports);
|
|
1
|
+
export * from './abstract-wallet';
|
|
2
|
+
export * from './estimate-wallet';
|
|
3
|
+
export * from './local-wallet';
|