question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get error "Can not sign for this input with the key" when migrating from TransactionBuilder to Psbt

See original GitHub issue

Error: Can not sign for this input with the key

I got this error when tried to create a raw transaction with testnet, p2pkh address. Everything works fine if I use TransactionBuilder, except the warning message told me should use Psbt. When using signInput method, my pubkey seems not to match with utxo input.

This is my getPrivateKey function from mnemonic string, works fine with TransactionBuilder

const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed, list_networks['test']);
const child = root
    .deriveHardened(44)
    .deriveHardened(coin_type['test'].index)
    .deriveHardened(0)
    .derive(0)
    .derive(index = 0);

return bitcoin.ECPair.fromPrivateKey(child.privateKey, { network: list_networks['test'] });

And this is my new createRawTransaction

// Get Private Key From Mnemonic Word Lists 
const key = getPrivateKey(mnemonic);
// Create Psbt Instance
const psbt = new bitcoin.Psbt({ network: list_networks['test'] });
// Add Unspent transaction outputs
utxos.forEach(utxo => {
// this object is from getrawtransaction bitcoin-cli
  psbt.addInput({
      hash: utox.hash,
      index: utox.index,
      nonWitnessUtxo: Buffer.from(utox.hex, 'hex'),
  })
});
// Add Output To Destination address
psbt.addOutput({
        address: destination,
        value: amount,
});
// Sign utxos Input
for (let i = 0; i < utxos.length; i++) {
     psbt.signInput(i, key); // worked with TransactionBuilder.sign 
}

What’s missing? Shoud I stay with TransactionBuilder?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
HarryMarchcommented, Apr 4, 2020

I found out what I been missing. I passed a wrong utxo input index value. Sorry for wasting your time at the weekend. But I knew you’d show up as usual to help wandering people like me.

1reaction
HarryMarchcommented, Apr 4, 2020

I did a quick check, the private key is fine with TransactionBuilder

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support Request: Need help migrating from old Txb to Psbt ...
There shouldn't really be a use case for importing a completed raw transaction into PSBT, since you can't sign a completed transaction.
Read more >
Cannot sign txn with change address using bitcoinjs-lib psbt
I get Error: cannot sign for this input with the key <value> . The initial transaction works fine, where I generate a transaction...
Read more >
how do you create a new PSBT transaction in bitcoinjs-lib?
but I'm getting error: Can not sign for this input with the key 02c4ac... I've made sure that when I decode the tx...
Read more >
bitcoinjs-lib | Yarn - Package Manager
It is now fixed. 5.1.5 ... Psbt now has getFee(): number for use when all inputs are finalized. It returns the satoshi fee...
Read more >
How to use the bitcoinjs-lib.TransactionBuilder function in ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found