Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var crypto = require('crypto');
var bitcoin = require("bitcoinjs-lib")
var helloblock = require('helloblock-js')({
network: 'testnet'
})
// placeholder! this is just some op_return sample code from helloblock-js
function done(err)
{
if(err) console.log("ERR",err);
process.exit(0);
}
var key = bitcoin.ECKey.fromWIF("L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy")
var address = key.pub.getAddress(bitcoin.networks.testnet).toString()
helloblock.faucet.withdraw(address, 2e4, function(err) {
if (err) return done(err)
helloblock.addresses.getUnspents(address, function(err, _, unspents) {
if (err) return done(err)
// filter small unspents
unspents = unspents.filter(function(unspent) { return unspent.value > 1e4 })
// use the oldest unspent
var unspent = unspents.pop();
// helloblock.transactions.get(unspent.txHash, function(err, res, transaction) {
// if (err) return done(err)
// console.log("unspent",unspent,transaction);
// });
/* jshint -W101 */
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
var bitcoin = require('bitcoinjs-lib');
var client = blocktrail.BlocktrailSDK({
apiKey : "YOUR_APIKEY_HERE",
apiSecret : "YOUR_APISECRET_HERE",
testnet : true
});
/*
* this example is for when you're storing the primary private and backup public key yourself
*/
var primaryPrivateKey = bitcoin.HDNode.fromBase58("tprv8ZgxMBicQKsPdMD2AYgpezVQZNi5kxsRJDpQWc5E9mxp747KgzekJbCkvhqv6sBTDErTjkWqZdY14rLP1YL3cJawEtEp2dufHxPhr1YUoeS", bitcoin.networks.testnet);
var backupPublicKey = bitcoin.HDNode.fromBase58("tpubD6NzVbkrYhZ4Y6Ny2VF2o5wkBGuZLQAsGPn88Y4JzKZH9siB85txQyYq3sDjRBFwnE1YhdthmHWWAurJu7EetmdeJH9M5jz3Chk7Ymw2oyf", bitcoin.networks.testnet);
var sendTransaction = function(wallet) {
wallet.getNewAddress(function(err, address, path) {
if (err) {
return console.log("getNewAddress ERR", err);
}
console.log('new address', address, path);
var pay = {};
pay[address] = blocktrail.toSatoshi(0.001);
wallet.pay(pay, function(err, result) {
if (err) {
return console.log("pay ERR", err);
constructor (opts = {}) {
super()
this.opts = opts
let network = opts.network || 'bitcoin'
// TODO: support regtest
this.bitcoinJsNetwork = bitcoin.networks[network]
this.params = params[network]
if (!this.bitcoinJsNetwork || !this.params) {
throw Error(`Unknown network "${network}"`)
}
this.error = this.error.bind(this)
this.scan = this.handleErrors(this.scan)
this.getUtxos = this.handleErrors(this.getUtxos)
// TODO: chain state persistence
this.chain = Blockchain({
indexed: true,
start: this.params.blockchain.checkpoints.slice(-1)[0],
allowMinDifficultyBlocks:
network === 'testnet' || network === 'regtest',
...opts.chainOpts
var expect = require('chai').expect
var bitcoin = require('bitcoinjs-lib')
var networks = bitcoin.networks
var coloredcoinlib = require('../src/index')
var AddressManager = coloredcoinlib.AddressManager
var AddressStore = coloredcoinlib.store.AddressStore
describe('AddressManager', function() {
var am, amStore
var seedHex = '00000000000000000000000000000000'
var masterKeyBase58 = 'xprv9s21ZrQH143K2JbpEjGU94NcdKSASB7LuXvJCTsxuENcGN1nVG7QjMnBZ6zZNcJaiJogsRaLaYFFjs48qt4Fg7y1GnmrchQt1zFNu6QVnta'
var address0 = '18KMigSHDPVFzsgWe1mcaPPA5wSY3Ur5wS'
beforeEach(function() {
amStore = new AddressStore()
am = new AddressManager(amStore)
getNetwork = () => {
if (BITCOIN_SETTINGS.network === "testnet") {
return bitcoin.networks.testnet;
} else if (BITCOIN_SETTINGS.network === "simnet") {
return SIMNET_NETWORK;
}
return bitcoin.networks.bitcoin;
};
const signMessage = (message, encodedPrivateKey) => {
const keyPair = bitcoin.ECPair.fromWIF(encodedPrivateKey, [bitcoin.networks.bitcoin, bitcoin.networks.testnet])
const privateKey = keyPair.d.toBuffer(32)
const signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)
return signature.toString('base64')
}
const signMessage = (message, encodedPrivateKey) => {
const keyPair = bitcoin.ECPair.fromWIF(encodedPrivateKey, [bitcoin.networks.bitcoin, bitcoin.networks.testnet])
const privateKey = keyPair.d.toBuffer(32)
const signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)
return signature.toString('base64')
}
static scriptPubKeyToAddress(scriptPubKey) {
const scriptPubKey2 = Buffer.from(scriptPubKey, 'hex');
let ret;
try {
ret = bitcoin.payments.p2wpkh({
output: scriptPubKey2,
network: bitcoin.networks.bitcoin,
}).address;
} catch (_) {
return false;
}
return ret;
}
}
Helpers.fromBitcoinCash = (address) => {
const { hash, version } = cashaddress.decode(address);
switch (version) {
case 'pubkeyhash':
return Bitcoin.address.toBase58Check(hash, Bitcoin.networks.bitcoin.pubKeyHash);
case 'scripthash':
return Bitcoin.address.toBase58Check(hash, Bitcoin.networks.bitcoin.scriptHash);
default:
throw new Error('fromBitcoinCash: Address type not supported');
}
};
'use strict'
let mongoBaseUri
const BitcoindClient = require('bitcoin-core');
const network = process.env.BITCOIND_NETWORK || 'testnet';
const bitcoinjs = require('bitcoinjs-lib')
const bitcoinjsNetwork = network === 'mainnet' ? bitcoinjs.networks.mainnet : bitcoinjs.networks.testnet;
if (process.env.MONGO_PORT_27017_TCP_ADDR){
mongoBaseUri = `mongodb://${process.env.MONGO_PORT_27017_TCP_ADDR}:27017/`
} else if (process.env.mongo) {
mongoBaseUri = `mongodb://${process.env.mongo}:27017/`
} else if(process.env.MONGO_URI ){
mongoBaseUri = `mongodb://${process.env.MONGO_URI}:27017/`
} else {
mongoBaseUri = `mongodb://localhost:27017/`
}
const mongoUri = mongoBaseUri + network
let TOKEN = "";
if (process.env.NODE_ENV === "development") {
TOKEN = process.env.DEVELOP_TOKEN ? process.env.DEVELOP_TOKEN : process.env.TOKEN;