Can not sign for input #0 with the key
Hello!
Sorry for tag @junderw, I hope you are doing well!
Could you help with this error: Error: Can not sign for input #0 with the key 033f96f4eaa2349bbe53757a818dabf54e11d088181b83f716262cbb7c7141b417
Code:
import * as bitcoin from "bitcoinjs-lib";
import * as ecc from "tiny-secp256k1";
import { ECPairFactory } from "ecpair";
const ECPair = ECPairFactory(ecc);
bitcoin.initEccLib(ecc);
const wif: string = "KxBkq...";
const keyPair = ECPair.fromWIF(wif);
const psbtBase64: string =
"cHNidP8BAP0cAQIAAAAC+hBs7UC7UjB0bz0MwEZfBxfS9evbp6HZeb7PFJNZkWwAAAAAAP////+gJmTywISxZY3uz7M3U1k2lBHLXVc0voTz0yqdkRbJhAAAAAAA/////wVKAQAAAAAAACJRIAXGa/DSYHDuj8blBJiRkdFUIjIoCN7jEqLL4bzITDCVSgEAAAAAAAAiUSAKLQNgq0CRLk5Sw6ALtmzCk7OY5+on7B1Oz0EPhvWNEgAAAAAAAAAAC2pdCADEzzNUwAEBqGEAAAAAAAAiUSDm2OSmUFFMM473ax5bx8ZSgil520UmBaD+vgLnm2Q2+ZY9AAAAAAAAIlEgCi0DYKtAkS5OUsOgC7ZswpOzmOfqJ+wdTs9BD4b1jRIAAAAAAAEBK1DDAAAAAAAAIlEgCi0DYKtAkS5OUsOgC7ZswpOzmOfqJ+wdTs9BD4b1jRIBAwQBAAAAARcgP5b06qI0m75TdXqBjav1ThHQiBgbg/cWJiy7fHFBtBcAAQErSgEAAAAAAAAiUSAFxmvw0mBw7o/G5QSYkZHRVCIyKAje4xKiy+G8yEwwlQEDBAEAAAAAAAAAAAA=";
const network = bitcoin.networks.bitcoin;
const psbt = bitcoin.Psbt.fromBase64(psbtBase64, { network });
function toXOnly(pubKey: Buffer): Buffer {
return pubKey.length === 33 ? pubKey.slice(1, 33) : pubKey;
}
const xOnlyPubkey = toXOnly(keyPair.publicKey);
console.log("Yours x-only pub_key:", xOnlyPubkey.toString("hex"));
let tapInternalKey = psbt.data.inputs[0].tapInternalKey;
if (tapInternalKey) {
console.log("TapInternalKey from PSBT:", tapInternalKey.toString("hex"));
if (xOnlyPubkey.toString("hex") !== tapInternalKey.toString("hex")) {
console.error("x-only pub_key != TapInternalKey");
} else {
psbt.data.inputs[0].sighashType = bitcoin.Transaction.SIGHASH_DEFAULT;
psbt.signInput(0, keyPair);
psbt.signInput(1, keyPair);
psbt.finalizeAllInputs();
const signedPsbtBase64 = psbt.toBase64();
console.log(`Sign PSBT: ${signedPsbtBase64}`);
}
}
Inputs looks like:
Input 0: {
witnessUtxo: {
script: <Buffer 51 20 0a 2d 03 60 ab 40 91 2e 4e 52 c3 a0 0b b6 6c c2 93 b3 98 e7 ea 27 ec 1d 4e cf 41 0f 86 f5 8d 12>,
value: 50000
},
sighashType: 1,
tapInternalKey: <Buffer 3f 96 f4 ea a2 34 9b be 53 75 7a 81 8d ab f5 4e 11 d0 88 18 1b 83 f7 16 26 2c bb 7c 71 41 b4 17>
}
Input 1: {
witnessUtxo: {
script: <Buffer 51 20 05 c6 6b f0 d2 60 70 ee 8f c6 e5 04 98 91 91 d1 54 22 32 28 08 de e3 12 a2 cb e1 bc c8 4c 30 95>,
value: 330
},
sighashType: 1
}
I'm really can't understand what I'm doing wrong. Need I to sign some another way first input?
I'm already tried to do through psbt.signAllInputs(keyPair) and so on, but nothing works.
Thank you.
Trying to do this way:
import { ECPairFactory, ECPairInterface } from "ecpair";
import {
initEccLib,
networks,
payments,
Psbt,
Transaction,
address,
script,
opcodes,
crypto,
Network,
} from "bitcoinjs-lib";
import * as ecc from "tiny-secp256k1";
initEccLib(ecc);
const ECPair = ECPairFactory(ecc);
const network = networks.bitcoin;
export function tweakSign(keyPair: any) {
const childNodeXOnlyPubkeyFunding = toXOnly(keyPair.publicKey);
const tweakedChildNodeFunding = keyPair.tweak(
crypto.taggedHash("TapTweak", childNodeXOnlyPubkeyFunding)
);
return tweakedChildNodeFunding;
}
function toXOnly(pubkey: Buffer): Buffer {
return pubkey.slice(1, 33);
}
export async function main() {
const wif: string = "KxBkq...";
const keyPair = ECPair.fromWIF(wif, network);
const psbtBase64: string =
"cHNidP8BAP0cAQIAAAAC+hBs7UC7UjB0bz0MwEZfBxfS9evbp6HZeb7PFJNZkWwAAAAAAP////+gJmTywISxZY3uz7M3U1k2lBHLXVc0voTz0yqdkRbJhAAAAAAA/////wVKAQAAAAAAACJRIAXGa/DSYHDuj8blBJiRkdFUIjIoCN7jEqLL4bzITDCVSgEAAAAAAAAiUSAKLQNgq0CRLk5Sw6ALtmzCk7OY5+on7B1Oz0EPhvWNEgAAAAAAAAAAC2pdCADEzzNUwAEBqGEAAAAAAAAiUSDm2OSmUFFMM473ax5bx8ZSgil520UmBaD+vgLnm2Q2+ZY9AAAAAAAAIlEgCi0DYKtAkS5OUsOgC7ZswpOzmOfqJ+wdTs9BD4b1jRIAAAAAAAEBK1DDAAAAAAAAIlEgCi0DYKtAkS5OUsOgC7ZswpOzmOfqJ+wdTs9BD4b1jRIBAwQBAAAAARcgP5b06qI0m75TdXqBjav1ThHQiBgbg/cWJiy7fHFBtBcAAQErSgEAAAAAAAAiUSAFxmvw0mBw7o/G5QSYkZHRVCIyKAje4xKiy+G8yEwwlQEDBAEAAAAAAAAAAAA=";
const psbt = Psbt.fromBase64(psbtBase64, { network });
const tweakedSigner = tweakSign(keyPair);
psbt.data.inputs[0].sighashType = Transaction.SIGHASH_DEFAULT;
psbt.data.inputs[1].sighashType = Transaction.SIGHASH_DEFAULT;
psbt.signInput(0, tweakedSigner);
psbt.signInput(1, keyPair);
// Finalize inputs
psbt.finalizeAllInputs();
}
main();
Error: Can not sign for input #1 with the key 033f96f4eaa2349bbe53
Error: Can not sign for input #1 with the key 033f96f4eaa2349bbe53
This tells us that psbt.signInput(0, tweakedSigner); has succeeded. So we know that this is the correct key for signing with.
The input#1 script is different from both the tapInternalKey and script of Input#0... the "033f96f4eaa2349bbe53" from the error is the same pubkey as tapInternalKey.
You need to figure out how the Input#1 script was made. It looks like a completely different key.