bitcoinjs / bitcoinjs-lib

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

Segwit bc1 address from private key #1795

Closed random9brat closed 2 years ago

random9brat commented 2 years ago

Still searching for the answer... right now im stuck with this code and I could really need some help... playing around and trying to learn more about btc dev.

let bitcoin = require('bitcoinjs-lib');
const ECPair = require('bitcoinjs-lib/src/ecpair');

const keyPair = ECPair.fromWIF('KxCb6hLLBUwxJdThXnSb37gJPKmN5BiibRiA7MXQouAWtRyRTQmo');
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });
console.log(address);

Im really new into this world of JS so be gentle, i guess i stuck with some imports etc but I really dont know what to do. I can feel that solution is one or two lines away but i really dont know what to do...

Am i wrong or it bc1 segwit can be generated just with private key??

junderw commented 2 years ago

From the README:

Screenshot_20220416_081020

From bip32 README: (ecpair is similar)

Screenshot_20220416_081219

Here's the answer:

const ecc = require('tiny-secp256k1');
const { ECPairFactory } = require('ecpair');
const ECPair = ECPairFactory(ecc);
random9brat commented 2 years ago

Hmmm...im still not doing something good, hope we can still communicate even if its closed:

const bitcoin = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1');
const { ECPairFactory } = require('ecpair');
const ECPair = ECPairFactory(ecc);

const keyPair = bitcoin.ECPair.fromWIF('KxCb6hLLBUwxJdThXnSb37gJPKmN5BiibRiA7MXQouAWtRyRTQmo');
var addr = keyPair.getAddress();

console.log(addr);

with this code im getting 13LzzJWrgRUM8Xtx9SnJa2Yn5aGruMQfEG not bc1.....

also when i try to run this im getting TypeError: Cannot read property 'p2wpkh' of undefined

const bitcoin = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1');
const { ECPairFactory } = require('ecpair');
const ECPair = ECPairFactory(ecc);

const keyPair = bitcoin.ECPair.fromWIF('KxCb6hLLBUwxJdThXnSb37gJPKmN5BiibRiA7MXQouAWtRyRTQmo');
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });

console.log(address);

I have a feeling that we are close :))) thanks for helping me tho, i really appreciate it

random9brat commented 2 years ago

Holly molly what a ride this was...after 15+ hours i finally did it... i was using bitcoinjs-lib@3.3.2 not @6.0.1...

i changed it and we are good to go :D.... posting this here so if anyone ever have the same problem...

thanks again for the help junderw, you are the man.