btcguide / btcguide.github.io

https://btcguide.github.io/
MIT License
90 stars 49 forks source link

Programmatic signing a transaction using Bip9 Seed words (like seedpicker paper wallet) #106

Closed dykstranet closed 2 years ago

dykstranet commented 2 years ago

My goal is to have a tails usb stick that can take seed words as input and sign a transaction all while offline. The backup will therefore remain a "cold" wallet.

I'm trying to use the paper wallet seed words to sign a multisig transaction.

import * as bitcoin from 'bitcoinjs-lib'
import * as bip39 from 'bip39'
import { BIP32Factory } from 'bip32'
import * as ecc from 'tiny-secp256k1'

const bip32 = BIP32Factory(ecc)
const transactionBase64 = "base64stuff"
const mnemonic = "abort abort"
const seed = bip39.mnemonicToSeedSync(mnemonic)
const node = bip32.fromSeed(seed)
const psbt = bitcoin.Psbt.fromBase64(transactionBase64)

console.log("Before:")
// console.log(psbt.toBase64())
console.log(psbt.txInputs)
console.log(psbt.txOutputs)
// psbt.signAllInputs(node)
psbt.signInput(0, node)
console.log("After:")
console.log(psbt.toBase64())

I got the following error:

Error: Can not sign for this input with the key 'xxx'

Looking for guidance and if I can get a working version, I'll document and make pull request. Hope I can help and thanks for your help!

mflaxman commented 2 years ago

I don't know bitcoinjs-lib well enough to debug, but you can use my open-source software buidl to accomplish this exact goal (and with 0 dependencies): https://twitter.com/mflaxman/status/1321503036724989952

I plan to update this guide with instructions for using many alternative Signers, with appropriate disclaimers. Coldcard and Keystone are still my default recommendations, but it is now possible to use many others. bitcoinjs-lib instructions/script would be welcomed.

dykstranet commented 2 years ago

Thankyou @mflaxman for the speedy reply. I'll have a look at buidl.

dykstranet commented 2 years ago

Here is the bitcoinjs-lib solution that works: https://github.com/bitcoinjs/bitcoinjs-lib/issues/1789#issuecomment-1092406302.

mflaxman commented 2 years ago

That is neat, I love to have more language libraries that can do this.

When I add more signers I'd like to add a link to this post, but from my quick reading of the code this is effectively blind-signing?

Even if it did display the outputs for verification prior to signing (would be easy enough to add), there's still no change validation, right? That's an essential feature in signing.