bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.6k stars 2.08k forks source link

Hello, I have generated the wallet address, how can I import it into other BTC wallet for transfer? #1107

Closed liuhuanhui closed 6 years ago

liuhuanhui commented 6 years ago

const bitcoin = require("bitcoinjs-lib"); const getRandomWallet = function () { const keyPair = bitcoin.ECPair.makeRandom(); return { address: keyPair.getAddress(), privKey: keyPair.toWIF(), }; };

Hello, I have generated the wallet address, how can I import it into other BTC wallet for transfer?

junderw commented 6 years ago

WIF keys are not well supported anymore...

Electrum has a recovery mode called "Import Bitcoin Addresses or private keys" _001

Paste only your WIF private key in there, and it will let you spend.

Please re-open if you need more help.

junderw commented 6 years ago

You can also do the below, and doing BIP39 restore in Electrum will give you the same first address, and it will be an HD wallet.

const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')

function generateNewKeys() {
  let phrase = bip39.generateMnemonic(null, null, bip39.wordlists.chinese_simplified)
  let seed = bip39.mnemonicToSeed(phrase)
  let HDRoot = bitcoin.HDNode.fromSeedBuffer(seed)
  let BIP44FirstKey = HDRoot.derivePath("m/44'/0'/0'/0/0")
  let address = BIP44FirstKey.getAddress()

  return {
    address: address,
    privatePhrase: phrase
  }
}

let result = generateNewKeys()
console.log(result)
// {
//   address: '1KuKX3WqrsgJPCqqCNMc5TsojCXCkjqvZL',
//   privatePhrase: '仇 衣 泡 晋 物 世 蒸 稍 馏 微 圈 缘'
// }
liuhuanhui commented 6 years ago

@junderw const bip39 = require('bip39') const bitcoin = require('bitcoinjs-lib')

function generateNewKeys() { let phrase = bip39.generateMnemonic(null, null, bip39.wordlists.chinese_simplified) let seed = bip39.mnemonicToSeed(phrase) let HDRoot = bitcoin.HDNode.fromSeedBuffer(seed) let BIP44FirstKey = HDRoot.derivePath("m/44'/0'/0'/0/0") let address = BIP44FirstKey.getAddress()

return { address: address, privatePhrase: phrase } }

let result = generateNewKeys() console.log(result) // { // address: '1KuKX3WqrsgJPCqqCNMc5TsojCXCkjqvZL', // privatePhrase: '仇 衣 泡 晋 物 世 蒸 稍 馏 微 圈 缘' // }

Thank you for your reply. I tried the code you sent to generate the address, and failed to import in Electrum.

liuhuanhui commented 6 years ago

image image

liuhuanhui commented 6 years ago
const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')

function generateNewKeys() {
  let phrase = bip39.generateMnemonic(null, null, bip39.wordlists.chinese_simplified)
  let seed = bip39.mnemonicToSeed(phrase)
  let HDRoot = bitcoin.HDNode.fromSeedBuffer(seed)
  let BIP44FirstKey = HDRoot.derivePath("m/44'/0'/0'/0/0")
  let address = BIP44FirstKey.getAddress()

  return {
    address: address,
    privatePhrase: phrase
  }
}

let result = generateNewKeys()
console.log(result)
// {
//   address: '1KuKX3WqrsgJPCqqCNMc5TsojCXCkjqvZL',
//   privatePhrase: '仇 衣 泡 晋 物 世 蒸 稍 馏 微 圈 缘'
// }

Will it happen to produce the same wallet?

junderw commented 6 years ago

If you look at the Addresses tab you will see the same address.

The warning will say "There is not money in this wallet" but that is OK.

Also, you must put space (' ') between each symbol, not a line break ('\n')

_001

junderw commented 6 years ago

You might need to show the "Addresses menu" with the "view" menu option at the top of the window.

dcousens commented 6 years ago

@junderw

WIF keys are not well supported anymore...

Interesting perspective. Understandable with BIP32's dominance, but, are we helping at all by still providing ECPair generally?

junderw commented 6 years ago

are we helping at all by still providing ECPair generally?

offering it is fine IMO, but I think we should move all tests and examples to only use HDNodes or their equivalent....

Also, I think that HDNodes should be written as a class extending ECPair so that anything taking an ECPair could take an HDNode...

HDNode would need to hold a path attribute and cache the current path's account level...

Perhaps a new instance could default to BIP49, which would grab the private info from xprv, derive m/49'/0'/0'/0 and m/49'/0'/0'/1 and cache them as "receiveNode" and "changeNode". Then when calling "getAddress(4) it will do p2sh(p2wpkh(hdnode.receiveNode.derive(4))) and return the address... etc etc...

Currently, ECPair looks like the main event to any first time programmers... perhaps this paradigm needs a shift...

Edit: However, I don't think internalizing BIP39 is a good idea... imo I kinda think BIP39 is a bad standard... LND's aezeed is pretty nice though...

But yeah, developers should be playing with xprv yprv zprv now, and not WIF imo.