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

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