bitcoinjs / bitcoinjs-lib

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

How to Generate Litecoin Address from Ltub key ? #1339

Closed mchalise closed 5 years ago

mchalise commented 5 years ago

Lets say my Ltub key is

Ltub2Zn7k8vFeTjWP8aefAjLJfRRigihzTRjEFVuEBGpKiAVX15K2GVgr73cqLSYgGtKnHgnucwevUmUXZARKhwACGPCtmpcfS9ELhMyqNq2gwz

junderw commented 5 years ago

What kind of address?

What derivation path?

Did you search the other answers for alternative HDkeys? ie ypub etc.

mchalise commented 5 years ago

Public Address that my HD wallet uses(receiving address). Yup, I looked into old threads but all of them are Old ones and doesn't work at all. Could you please help me with this one. Thanks

junderw commented 5 years ago

Public Address that my HD wallet uses(receiving address).

Which one?

Please be specific, especially if you're asking someone to spend time to show you how to code for an unsupported currency.

mchalise commented 5 years ago

LTC receiving address. LXDzdci9cKGa5CGYuHZroR4sBBXptAuXfV like this one. you get it right?

junderw commented 5 years ago

Which HDpath?

Hard derivation?

P2PKH?

Are you trying to recreate a Trezor address or something?

Please be specific.

mchalise commented 5 years ago

Yes exactly. I want to pull out my LTC transaction history from HD wallets like Trezor, Exodus, so on. For that, I need addresses present in that wallet. correct me if I am wrong. Appreciate your help. Thanks

junderw commented 5 years ago

Each wallet has a different method.

Pick one.

Trezor?

mchalise commented 5 years ago

@junderw Ok Trezor. then

junderw commented 5 years ago

Here you go.

const bitcoinjs = require('bitcoinjs-lib')
const bip32 = bitcoinjs.bip32

const coininfo = require('coininfo')
const litecoin = coininfo.litecoin.main
const litecoinBitcoinJSLib = litecoin.toBitcoinJS()

const ltub = 'Ltub2Zn7k8vFeTjWP8aefAjLJfRRigihzTRjEFVuEBGpKiAVX15K2GVgr73cqLSYgGtKnHgnucwevUmUXZARKhwACGPCtmpcfS9ELhMyqNq2gwz'

const firstAddress = 'LRJrTmGRcknZkNcpZPJNskyzbhxwLCSyvj'

const accountNode = bip32.fromBase58(ltub, litecoinBitcoinJSLib)

function getAddress (node, network) {
  return bitcoinjs.payments.p2pkh({ pubkey: node.publicKey, network }).address
}

let firstTenReceiveAddress = []
let firstTenChangeAddress = []

const recvNode = accountNode.derive(0)
const changeNode = accountNode.derive(1)

for (let i = 0; i < 10; i++) {
  firstTenReceiveAddress.push(getAddress(recvNode.derive(i), litecoinBitcoinJSLib))
  firstTenChangeAddress.push(getAddress(changeNode.derive(i), litecoinBitcoinJSLib))
}

console.log(firstTenReceiveAddress)
console.log(firstTenChangeAddress)

console.log(firstAddress === firstTenReceiveAddress[0])
// true
mchalise commented 5 years ago

Huge Thanks @junderw . !!!!

mchalise commented 5 years ago

@junderw Quick question: It worked perfectly for Ltub but I also have like Mtub and xpub LTC keys. which is in BIP49, BIP44. Could you point me to some resource materials I can look into to generate for such keys also? Thanks. Appreciate All your Help and Time.

junderw commented 5 years ago

for non-standard xpub variants you need to base58check decode and replace the 4 version bytes with Ltub's etc. and then just do the same as above but with p2sh-p2wpkh.

mchalise commented 5 years ago

So what you are suggesting is Change every address like xpub, Mtub to Ltub and use above one with p2sh-p2wph right?

junderw commented 5 years ago

yes

mchalise commented 5 years ago

@junderw I tried your approach but it only worked for Ltub and Mtub. For xpub(Exodus wallet) its generating different address which is incorrect. For xpub and mtub I am using

const p2wpkh = bitcoinjs.payments.p2wpkh({ pubkey: node.publicKey, network }) return bitcoinjs.payments.p2sh({ redeem: p2wpkh, network }).address

Is there anything I am doing wrong ? thanks

junderw commented 5 years ago

could you give me an example wallet:

  1. xpub
  2. expected address
  3. code you used
  4. actual result address
mchalise commented 5 years ago

1.xpub6BurV3Ff8nB5CNM8MCkzJPV6rprymcbto7a7cesFWBorc1M4GqGkK8PkYoyBZN8RVSc1UwrEae4sYbmQZ9zvzZviL1Sv2GgCfDL8fdT8f28

  1. Expected Address: LXDzdci9cKGa5CGYuHZroR4sBBXptAuXfV
const bitcoinjs = require('bitcoinjs-lib')
const bs58check = require('bs58check')
const bip32 = bitcoinjs.bip32

const coininfo = require('coininfo')
const litecoin = coininfo.litecoin.main
const litecoinBitcoinJSLib = litecoin.toBitcoinJS()

const extended_key = 'xpub6BurV3Ff8nB5CNM8MCkzJPV6rprymcbto7a7cesFWBorc1M4GqGkK8PkYoyBZN8RVSc1UwrEae4sYbmQZ9zvzZviL1Sv2GgCfDL8fdT8f28'
const type = extended_key.substr(0,4)

/*
Litecoin  0x019da462 - Ltub 0x019d9cfe - Ltpv P2PKH or P2SH m/44'/2'
Litecoin  0x01b26ef6 - Mtub 0x01b26792 - Mtpv P2WPKH in P2SH  m/49'/1'
*/
function convertAnyAddressToLtub(extended_key, type) {
  if(type == "Ltub"){
    return extended_key
  }else{
    let data = bs58check.decode(extended_key)
    data = data.slice(4)
    data = Buffer.concat([Buffer.from('019da462','hex'), data])
    return bs58check.encode(data)
  }
}

const ltub = convertAnyAddressToLtub(extended_key, type)
const accountNode = bip32.fromBase58(ltub, litecoinBitcoinJSLib)

function getAddress (node, network, type) {
  if(type == "Ltub"){
    return bitcoinjs.payments.p2pkh({ pubkey: node.publicKey, network }).address
  }else{
    //segwit
    const p2wpkh = bitcoinjs.payments.p2wpkh({ pubkey: node.publicKey, network })
    return bitcoinjs.payments.p2sh({ redeem: p2wpkh, network }).address
  }
}

let firstFiveReceiveAddress = []
const recvNode = accountNode.derive(0) // derive(0): Receiving Node & derive(1): Change Node

for (let i = 0; i < 5; i++) {
  firstFiveReceiveAddress.push(getAddress(recvNode.derive(i), litecoinBitcoinJSLib, type))
}

console.log(firstFiveReceiveAddress)
  1. Actual result Address:

[ 'MMVeqbK6F4GcrHG27Pp3wpQURNYdJvgmey', 'MCY1f6Qn8mWYZuiXM8EG6d5w4ZtUGMs3JJ', 'MVTBQNQVw5f73rQurocogkrFCJyK3g1giR', 'MQ4ZnSZqftrytGZKubGmxevdB3CkUuT4Zp', 'MNXRPYQ4n4Ns8VY2s2UZMV1AcjoZbdS28e' ]

junderw commented 5 years ago

Could you try changing this? _2019-03-06_18-19-27

junderw commented 5 years ago

I got LXDzdci9cKGa5CGYuHZroR4sBBXptAuXfV

mchalise commented 5 years ago

Looks like both Ltub and xpub uses p2pkh. Got it. Thanks for all your help. Appreciate it.

VolckaertAchiel commented 3 years ago

@junderw excuse me for reviving a new thread, but can i use litecoinBitcoinJSLib to create transactions?