import { ECPairFactory, ECPairInterface } from 'ecpair';
import { initEccLib, networks, funds, Psbt, Transaction, deal with, script, opcodes, crypto, Community } from 'bitcoinjs-lib';
import * as ecc from 'tiny-secp256k1';
import axios from 'axios';
initEccLib(ecc);
const ECPair = ECPairFactory(ecc);
const community = networks.bitcoin
operate tweakSigner(signer: ECPairInterface, opts: any = {}): any {
let privateKey: Buffer | undefined = signer.privateKey;
if (!privateKey) {
throw new Error('Non-public key's required for tweaking signer');
}
const publicKey = toXOnly(signer.publicKey);
const tweakedPrivateKey = ecc.privateAdd(
privateKey,
tapTweakHash(publicKey, opts.tweakHash)
);
if (!tweakedPrivateKey) {
throw new Error('Invalid tweaked non-public key');
}
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
community: opts.community,
});
}
operate tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {
return crypto.taggedHash(
'TapTweak',
Buffer.concat(h ? [pubKey, h] : [pubKey])
);
}
operate toXOnly(pubkey: Buffer): Buffer {
return pubkey.slice(1, 33);
}
async operate getUTXOs(deal with: string) {
const response = await axios.get(`https://mempool-testnet.fractalbitcoin.io/api/deal with/${deal with}/utxo`);
const utxos = response.information
let availableUTXO = utxos.filter((ok: any) => (ok?.standing.confirmed && ok.worth > 546))
availableUTXO = availableUTXO.type((a: any, b: any) => b.worth - a.worth);
return availableUTXO;
}
export async operate most important() {
const private_key = 'L5Pe2yFCjshJrXoJHkXe8xEQofYHXvNKcxGS6XqNiiS7XzETRVPF' // setting non-public key
const keyPair = ECPair.fromWIF(private_key, community);
const mainWallet = funds.p2tr({
internalPubkey: toXOnly(keyPair.publicKey),
community: community
});
const utxos = await getUTXOs(String(mainWallet.deal with))
console.log(utxos)
const utxo = utxos[0]
const finalPsbt = new Psbt({ community });
const tweakedSigner = tweakSigner(keyPair, { community });
finalPsbt.addInput({
hash: utxo.txid,
index: utxo.vout,
witnessUtxo: {
script: mainWallet.output!,
worth: utxo.worth,
},
tapInternalKey: toXOnly(keyPair.publicKey)
});
finalPsbt.addOutput({
deal with: mainWallet.deal with!,
worth: 10000,
});
finalPsbt.signInput(0, tweakedSigner); // this can get error
}
most important();
Then I obtained error: Error: Can't signal for enter #0 with the important thing 03c3f349586717e8707ef8e0d74
I feel the attainable cause is utxo
vout is 0
Learn how to repair this? Thanks.
This could on-line edit : codesandbox on-line check