bitpay / bitcore-lib

A pure and powerful JavaScript Bitcoin library
https://bitcore.io/
Other
611 stars 1.03k forks source link

TypeError: Address has mismatched network type. #247

Open saritvakrat opened 5 years ago

saritvakrat commented 5 years ago

Running insight to getUnspentUtxos for a testnet address shows the following error: throw new TypeError('Address has mismatched network type.'); ^TypeError: Address has mismatched network type.

Code: var Insight = require('bitcore-explorers').Insight; var insight = new Insight('testnet'); return insight.getUnspentUtxos(address, function (err, utxos) { if (err) { console.err(err) } console.log(utxos); }); Calling it with a testnet address

Code for testnet address: let privateKey = new bitcore.PrivateKey(), hexa = privateKey.toString(), publicKey = new bitcore.PublicKey(privateKey); address = new bitcore.Address(publicKey, Networks.testnet);

Divyesh-mi commented 5 years ago

Hello @ActoMatics, Please try below code for generate address

const bitcore = require('bitcore-lib');
let value = new Buffer('*Unique string.....');
                    let hash = bitcore.crypto.Hash.sha256(value);
                    let bn = bitcore.crypto.BN.fromBuffer(hash);
                    let privateKey = new bitcore.PrivateKey(bn,'testnet');
                    let address = privateKey.toAddress();                   
                    addressArr.address = address.toString();
                    resolve(addressArr);