bitcoinjs / bip39

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
ISC License
1.09k stars 439 forks source link

to Wif? #129

Closed ekkis closed 4 years ago

ekkis commented 4 years ago

is the output of this module a Wif? if not, what is it and how can I convert it to a Wif?

junderw commented 4 years ago

Sorry, mixed up.

The output of this library is a seed Buffer.

Most commonly used with bip32 library (I thought this issue was on that library)

junderw commented 4 years ago

you can find the usage of bip39, bip32, etc. in the tests.

tests for bitcoinjs-lib have everything.

also, WIF is an outdated format. Unless you have a specific use case you should avoid using it

ekkis commented 4 years ago

perhaps you can point the better path then. this is what I currently do:

var seed = bip39.mnemonicToSeedSync(seedPhrase);
var root = bip32.fromSeed(seed);
var wif = root.toWIF();
var key = btc.ECPair.fromWIF(wif);
// ...
var tx = new btc.TransactionBuilder();
// ...
tx.sign(0, key);

if I don't use the WIF how do I get there?

junderw commented 4 years ago

you can pass root to sign directly

ekkis commented 4 years ago

awesome!

ekkis commented 4 years ago

actually, it doesn't work. if needs a WIF. I get the following spewage:

(node:3621) UnhandledPromiseRejectionWarning: TypeError: Inconsistent network at getSigningData (/Users/ekkis/dev/node_modules/bitcoinjs-lib/src/transaction_builder.js:1011:11) at TransactionBuilder.sign (/Users/ekkis/dev/node_modules/bitcoinjs-lib/src/transaction_builder.js:164:7) at Object.send (/Users/ekkis/dev/coinselect.js:32:12) at process.internalTickCallback (internal/process/next_tick.js:77:7) (node:3621) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:3621) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

junderw commented 4 years ago

pass fromSeed the same network you passed TransactionBuilder.

junderw commented 4 years ago

They should both default to bitcoin mainnet... but you are passing a network to one but not the other.

ekkis commented 4 years ago

I'm not following. I'm not passing a network to either (fromSeed() currently only receives the seed parameter, no network info, and TransactionBuilder() is called without specifying the network)... though I will need to for testing, which I don't know how to do

junderw commented 4 years ago

Show me root.network and tx.network right before tx.sign

junderw commented 4 years ago

Either way:

If you pass the same thing, this error will not occur.

ekkis commented 4 years ago

root.network is undefined and tx.network shows:

{ messagePrefix: '\u0018Bitcoin Signed Message:\n',
  bech32: 'bc',
  bip32: { public: 76067358, private: 76066276 },
  pubKeyHash: 0,
  scriptHash: 5,
  wif: 128 }
ekkis commented 4 years ago

from looking though https://github.com/bitcoinjs/bip32/blob/master/test/fixtures/index.json I can see that valid values for "network" would include "bitcoin" and "litecoin", but not a clue what the testnet would be... "testnet"? "testnet3"? nobody documents anything any more

ekkis commented 4 years ago

but apparently that's not a valid value either:

(node:4000) UnhandledPromiseRejectionWarning: Error: Expected Object, got String "bitcoin" at typeforce (/Users/ekkis/dev/node_modules/typeforce/index.js:235:11) at new BIP32 (/Users/ekkis/dev/node_modules/bip32/src/bip32.js:40:9) at fromPrivateKeyLocal (/Users/ekkis/dev/node_modules/bip32/src/bip32.js:261:12) at fromPrivateKey (/Users/ekkis/dev/node_modules/bip32/src/bip32.js:250:12) at Object.fromSeed (/Users/ekkis/dev/node_modules/bip32/src/bip32.js:288:12) at Object.root (/Users/ekkis/dev/coinselect.js:19:50) at Object.keys.forEach.env (/Users/ekkis/dev/coinselect.js:13:22) at Array.forEach () at Object.keys.forEach.dlt (/Users/ekkis/dev/coinselect.js:12:36)

ekkis commented 4 years ago

ah. found it in the bitcoin.networks object

ekkis commented 4 years ago

ok, you're correct in that if I pass in the network to both objects the error disappears. I also had the incorrect output value for root.network. it looks like this (when I don't supply it to the .fromSeed()):

{ wif: 128, bip32: { public: 76067358, private: 76066276 } }

which clearly differs from that shows in the transaction but I don't know why. the seed I pass to that method comes from:

var seed = bip39.mnemonicToSeedSync(mnem);
ekkis commented 4 years ago

for anyone reading this, the root can be passed to the .sign() function, as @junderw stated, but transactions produced must be signed by the same key owning the inputs. see: https://bitcoin.stackexchange.com/questions/92743/transaction-fails-to-validate-error-running-script?noredirect=1#comment106196_92743