How can I use this lib to work with testnet?
As far as I know, it doesn't work with testnet. You'll want to use one of the forks: https://github.com/vbuterin/bitcoinjs-lib & https://github.com/kyledrake/bitcoinjs-lib or follow our effort over here: https://github.com/cryptocoinjs/cryptocoin
@jprichardson it should be as simple as changing the address prefix... need to verify that with others.
For generating testnet addresses, yes, just set an instance of Bitcoin.Address version field to 0x6f. That should do it.
I wrote http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript, scroll down to the part on Addresses.
edit: See below for up-to-date examples
From the integration tests directory
var testnet = bitcoin.networks.testnet var alice = bitcoin.ECPair.makeRandom({ network: testnet }) var bob = bitcoin.ECPair.makeRandom({ network: testnet }) var alicesAddress = alice.getAddress() var bobsAddress = bob.getAddress()
If you want to create the ECPair from a hash:
var hash = bitcoin.crypto.sha256('correct horse battery staple')
var d = bigi.fromBuffer(hash)
var keyPair = bitcoin.ECPair(d, null, {network: bitcoin.networks.testnet});
@mixdev this doesn't work anymore, see https://github.com/bitcoinjs/bitcoinjs-lib/pull/1085 and changelog:
ECPair.prototype.getAddress, use payments.p2pkh instead (#1085)However, payments.p2pkh doesn't return a testnet address (starting with m or n), but obviously a p2pkh address that isn't valid on the testnet address (correct me if I'm wrong). Is there still a way to get testnet addresses using bitcoinjs-lib?
pass network to p2pkh and all portions inside it.
@junderw is the .network information on ECPair still useful?
with the new payments API, no... not really.
pass network to p2pkh and all portions inside it.
@junderw Could you suggest how to pass network to p2pkh? This code still returns mainnet address:
const { address } = bitcoin.payments.p2pkh({ pubkey }, {network: bitcoin.networks.testnet});
const { address } = bitcoin.payments.p2pkh({ pubkey, network: bitcoin.networks.testnet})
this works.
one arg, not two.
I had to pass it directly as a parameter
https://github.com/bitcoinjs/bitcoinjs-lib/issues/1259
let script = bitcoin.address.toOutputScript(address, bitcoin.networks.testnet);
Most helpful comment
this works.
one arg, not two.