Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Can not sign for input #0 with the key 03... #2081

Open
ambitious922 opened this issue Apr 19, 2024 · 1 comment
Open

Error: Can not sign for input #0 with the key 03... #2081

ambitious922 opened this issue Apr 19, 2024 · 1 comment

Comments

@ambitious922
Copy link

const expectedAddress =
  "tb1p2kclfdlyf35z2cz6r05ptx45utsypyjxzmj4ftm0tr2xq55yd47qtnh4j5";

bitcoin.initEccLib(ecc);
const bip32 = BIP32Factory(ecc);

// Your network (testnet in this case)
const network = bitcoin.networks.testnet;

const blockStreamApi = "https://blockstream.info/testnet/api";

// Load your private key (WIF)
const ECPair = ECPairFactory.ECPairFactory(ecc);
const privateKeyWIF = process.env.PRIVATE_KEY_WIF;
const keyPair = ECPair.fromWIF(privateKeyWIF, network);
// Replace with your private key
const internalPubkey = Buffer.from(keyPair.publicKey, "hex").slice(1, 33);
console.log(internalPubkey, "internalPubkey");
const mnemonic =
  "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const xprv =
  "xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu";
const path = `m/86'/0'/0'/0/0`; // Path to first child of receiving wallet on first account

const seed = await bip39.mnemonicToSeed(mnemonic);
const rootKey = bip32.fromSeed(seed);
assert.strictEqual(rootKey.toBase58(), xprv);
const childNode = rootKey.derivePath(path);

// Since internalKey is an xOnly pubkey, we drop the DER header byte
const childNodeXOnlyPubkey = toXOnly(internalPubkey);
assert.deepEqual(childNodeXOnlyPubkey, internalPubkey);

const { address, output } = bitcoin.payments.p2tr({
  internalPubkey,
  network,
});
assert(output);
assert.strictEqual(address, expectedAddress);
const tweakedChildNode = childNode.tweak(
  bitcoin.crypto.taggedHash("TapTweak", childNodeXOnlyPubkey)
);

console.log("address", address);

// const taprootP2TR = getTaprootP2TR(keyPair);
// const payment = taprootP2TR.payment;
// const taprootP2TR = getTaprootP2TR(keyPair);
// const signer = taprootP2TR.signer;

const utxos = [
  {
    hash: "4587ae424e038415dbf43103f096cc68e4615641063cb49ac2345e16675af3ed",
    index: 0,
    value: 546,
    // nonWitnessUtxo: Buffer.from(previousHex, "hex"),
  },
  {
    hash: "d4f0cf0b6642222da3e68491876d424f5d67b1697e8eae2357dbbb786e93f02d",
    index: 2,
    value: 99260,
    // nonWitnessUtxo: Buffer.from(previousHex, "hex"),
  },
];
const runeId = "2585883:3795";

const receiverAddress =
  "tb1pwzwkht8gzm39933ytfzf3uks89a5h4cvkfjwml3m5xceu4eshw3sz83wtw";

export async function createTransferTransaction() {
  // Fetch UTXOs (Unspent Transaction Outputs) for your address
  const utxosResponse = await fetch(
    `https://api.blockcypher.com/v1/btc/test3/addrs/${address}?unspentOnly=true`
  );
  const utxosData = await utxosResponse.json();
  const runeUtxosResponse = await fetch(
    `https://wallet-api-testnet.unisat.io/v5/runes/utxos?address=${address}&runeid=2585883:3795`
  );
  const runeUtxosData = await runeUtxosResponse.json();
  // console.log(runeUtxosData, "runeData");

  const outputScript = bitcoin.script.compile([
    internalPubkey,
    bitcoin.opcodes.OP_RETURN,
    bitcoin.opcodes.OP_13,
    [(runeId, 0, 80000000), (runeId, 1, 10000000)],
  ]);

  // Psbt class is used to do this.
  const txb = new bitcoin.Psbt({ network });

  for (let utxo of utxos) {
    txb.addInput({
      hash: utxo.hash,
      index: utxo.index,
      witnessUtxo: { script: output, value: utxo.value },
      tapInternalKey: toXOnly(internalPubkey),
    });
  }
  // add outputs as the buffer of receiver's address and the value with amount
  // of satoshis you're sending.
  txb.addOutput({
    script: outputScript,
    value: 0,
  });

  txb.addOutput({
    script: Buffer.from(address, "hex"),
    value: 546,
  });

  txb.addOutput({
    script: Buffer.from(receiverAddress, "hex"),
    value: 546,
  });

  txb.addOutput({
    script: Buffer.from(address, "hex"),
    value: 95000,
  });

  // sign with the generate keyPair and finalize the transansction
  txb.signInput(0, tweakedChildNode);
  txb.finalizeAllInputs();

Please help me with this error.

@jasonandjay
Copy link
Collaborator

The code you provided is incomplete and it is difficult for us to help you.

I think you can provide a complete and runnable example or a separate github repo for us to debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants