Skip to content

Instantly share code, notes, and snippets.

@abel30567
Created January 28, 2019 01:58
Show Gist options
  • Save abel30567/7c0e3c065a793cc6450e4010cc25cab4 to your computer and use it in GitHub Desktop.
Save abel30567/7c0e3c065a793cc6450e4010cc25cab4 to your computer and use it in GitHub Desktop.
const bitcore = require('bitcore-lib');
const Insight = require('bitcore-insight').Insight;
let insight = new Insight('testnet');
// Our private key and address
const wif = 'xBtatQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct';
const privateKey = new bitcore.PrivateKey(wif);
const myAddress = privateKey.toAddress();
// Address we are sending Bitcoin to
const addressTo = 'moCEHE5fJgb6yHtF9eLNnS52UQVUkHjnNm';
// Start the creating our transaction
const amount = 50000; // Sending amount must be in satoshis
const fee = 50000; // Fee is in satoshis
// Get the UTXOs of your Bitcoin address
insight.getUtxos(myAddress, (err, utxos) => {
if(err){
//Handle errors
return err;
}else {
// use the UTXOs to create transaction with bitcore Transaction object
let tx = bitcore.Transaction();
tx.from(utxos);
tx.to(addressTo, amount);
tx.change(myAddress);
tx.fee(fee);
tx.sign(privateKey);
tx.serialize();
// Broadcast your transaction to the Bitcoin network
insight.broadcast(tx.toString(), (error, txid) => {
if (error) {
return error;
} else {
// Your Transaction Id
console.log(txid)
}
})
}
});
@aman-singal
Copy link

Hey,
can you please tell how to convert it from testnet to mainnet,
additionally while running the script I have been receiving the below error:

e:\aman\WebD\bitcoin-demo\node_modules\bitcore-insight\node_modules\bitcore-lib\index.js:12
throw new Error(message);
^

Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.
at Object.bitcore.versionGuard (e:\aman\WebD\bitcoin-demo\node_modules\bitcore-insight\node_modules\bitcore-lib\index.js:12:11)
at Object. (e:\aman\WebD\bitcoin-demo\node_modules\bitcore-insight\node_modules\bitcore-lib\index.js:15:9)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (e:\aman\WebD\bitcoin-demo\node_modules\bitcore-insight\lib\models\addressinfo.js:3:15)

I usually try changing the first line to:
const bitcore = require('bitcore-explorers/node_modules/bitcore-lib');
which somehow works for me, but its kinda not working this time

Thanks in advance

@abel30567
Copy link
Author

Hi in order for this to work you will need to uninstall bitcore-lib and use the bitcore-lib that is configured in the bitcore-insight.

Your package.json file should only include bitcore-insight

@aman-singal
Copy link

thx a lot it worked, though it seems like bitpay is down or not accepting the request, any alternative?
Can you please tell how to convert it to mainnet?

@johnworthley
Copy link

@aman-singal It is not working for me either. Have you found any bitcoin node lightclient alternatives? I keep seeing Error: Cannot find module 'bitcore-lib' after installing bitcore-insight. Let's get this working

@aman-singal
Copy link

aman-singal commented Mar 8, 2020

@johnworthley as told by @abel30567 in order to fix the error you have to use bitcore-lib which is configured with bitcore-insight. As fas as alternative is concerned i havent found any. The only way i can think of broadcasting a btc transacion rn is by running a full node

@johnworthley
Copy link

johnworthley commented Mar 8, 2020

How are you importing bitcore? I am using const bitcore = require('../node_modules/bitcore-insight/node_modules/bitcore-lib') and seeing node_modules/bitcore-insight/node_modules/bitcore-lib/lib/util/preconditions.js:14 throw new errors.InvalidArgument(argumentName, message, docsPath); ^ Invalid Argument as an error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment