Friday, November 15, 2024
HomeBitcoinbitcoin core - Error: Cannot signal for enter #0 with the important...

bitcoin core – Error: Cannot signal for enter #0 with the important thing 03c3f3495867xxxx


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('Personal 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 personal 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.knowledge
  let availableUTXO = utxos.filter((okay: any) => (okay?.standing.confirmed && okay.worth > 546))
  availableUTXO = availableUTXO.kind((a: any, b: any) => b.worth - a.worth);
  return availableUTXO;
}

export async operate fundamental() {
  const private_key = 'L5Pe2yFCjshJrXoJHkXe8xEQofYHXvNKcxGS6XqNiiS7XzETRVPF' // setting personal 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 may get error

}

fundamental();

Then I received error: Error: Cannot signal for enter #0 with the important thing 03c3f349586717e8707ef8e0d74
I feel the potential cause is utxo vout is 0
The way to repair this? Thanks.

This may on-line edit : codesandbox on-line check

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments