blockcypher-unofficial
Advanced tools
Comparing version 0.0.1 to 1.0.0
157
index.js
@@ -1,152 +0,23 @@ | ||
var request = require('request'); | ||
var Addresses = require('./lib/addresses.js'); | ||
var Transactions = require('./lib/transactions.js'); | ||
var Blocks = require('./lib/blocks.js'); | ||
function calculateFee(inputs, outputs){ | ||
var totalIn = 0; | ||
var totalOut = 0; | ||
for(var i = 0; i < inputs.length; i++){ | ||
totalIn += inputs[i].output_value; | ||
} | ||
for(var i = 0; i < outputs.length; i++){ | ||
totalOut += outputs[i].value; | ||
} | ||
return(totalIn - totalOut); | ||
} | ||
//config variables for the module. (only network for now) | ||
//"testnet" for testnet and anything else for mainnet | ||
//returns the correct url endpoint based on network | ||
function getBaseURL(network){ | ||
var baseURL; | ||
if (network === "testnet"){ | ||
baseURL = "https://api.blockcypher.com/v1/btc/test3"; | ||
} else { | ||
baseURL = "https://api.blockcypher.com/v1/btc/main"; | ||
function BlockCypher(opts) { | ||
if (!(this instanceof BlockCypher)) return new BlockCypher(opts); | ||
if(!opts.network){ | ||
console.log("please specify a blockchain. (defaults to mainnet)"); | ||
} | ||
return baseURL; | ||
} | ||
//abstracted json get request caller method. | ||
function getFromURL(url, callback){ | ||
request.get(url, function (err, response, body) { | ||
if (err) { | ||
console.log("error fetching info from blockcypher " + err); | ||
callback(err, null); | ||
} else { | ||
try { | ||
callback(false, JSON.parse(body)); | ||
} | ||
catch(err) { | ||
callback(err, null); | ||
} | ||
} | ||
}); | ||
BlockCypher.prototype.Addresses = Addresses(opts); | ||
BlockCypher.prototype.Transactions = Transactions(opts); | ||
BlockCypher.prototype.Blocks = Blocks(opts); | ||
} | ||
//returns wallet info in standard format to callback. | ||
function getWalletInfo(options, callback){ | ||
var response = {}; | ||
var url = getBaseURL(options.network) + "/addrs/" + options.address; | ||
getFromURL(url, function (err, resp){ | ||
if(err){ | ||
callback(err, null); | ||
} | ||
else{ | ||
response.balance = resp.balance; | ||
response.totalReceived = resp.total_received; | ||
response.totalSent = resp.total_sent; | ||
response.txCount = resp.n_tx; | ||
callback(false, response); | ||
} | ||
}); | ||
} | ||
//returns unspents for options.address in standard format to callback. | ||
function getUnspentOutputs(options, callback){ | ||
var url = getBaseURL(options.network) + "/addrs/" + options.address + "?unspentOnly=true"; | ||
var response = {"unspents" : []}; | ||
getFromURL(url, function (err, resp){ | ||
if(err){ | ||
callback(err, null); | ||
} else { | ||
resp.txrefs.forEach(function (unspent){ | ||
var utxo = {}; | ||
utxo.txid = unspent.tx_hash; | ||
utxo.vout = unspent.tx_output_n; | ||
utxo.address = options.address; | ||
utxo.scriptPubKey = "not provided"; | ||
utxo.amount = unspent.value; | ||
utxo.confirmations = unspent.confirmations; | ||
response.unspents.push(utxo); | ||
}); | ||
callback(err, response); | ||
} | ||
}); | ||
} | ||
//returns transaction information for options.txid in standard format to callback. | ||
function getTransaction(options, callback){ | ||
var url = getBaseURL(options.network) + "/txs/" + options.txid; | ||
var response = {}; | ||
getFromURL(url, function (err, resp){ | ||
if(err){ | ||
callback(err, null); | ||
} else { | ||
response.hex = "not provided"; | ||
response.txid = resp.hash; | ||
response.version = resp.ver; | ||
response.locktime = resp.lock_time; | ||
response.fee = calculateFee(resp.inputs, resp.outputs); | ||
response.vin = []; | ||
response.vout = []; | ||
resp.inputs.forEach(function (input){ | ||
response.vin.push({txid: input.prev_hash, vout: input.output_index, | ||
scriptSig: {asm: "not provided", hex: input.script}, sequence: input.sequence}); | ||
}); | ||
resp.outputs.forEach(function (output){ | ||
response.vout.push({value: output.value, index: "not provided", spentTxid: "not provided", | ||
scriptPubKey : {asm: "not provided", hex: output.script, reqSigs: output.addresses.length, | ||
type: output.script_type, addresses: output.addresses}}); | ||
}); | ||
response.blockhash = resp.block_hash; | ||
response.blockindex = "not provided" | ||
response.blocktime = new Date(resp.confirmed).getTime(); | ||
response.confirmations = resp.confirmations; | ||
response.timeReceived = new Date(resp.received).getTime(); | ||
callback(err, response); | ||
} | ||
}); | ||
} | ||
//pushes options.transaction in hex to network and returns the resultant txid to callback. | ||
function pushTransaction(options, callback){ | ||
var postBody = {"tx": options.transaction}; | ||
var url = getBaseURL(options.network) + "/txs/push"; | ||
request({ | ||
url: url, //URL to hit | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(postBody) | ||
}, function(err, response, body){ | ||
if(err) { | ||
console.log("error pushing raw txhex to blockcypher" + err); | ||
callback(err, null); | ||
} else { | ||
try { | ||
callback(false, JSON.parse(body).tx.hash); | ||
} | ||
catch(err) { | ||
callback(err, null); | ||
} | ||
} | ||
}); | ||
} | ||
module.exports.getWalletInfo = getWalletInfo; | ||
module.exports.getUnspentOutputs = getUnspentOutputs; | ||
module.exports.getTransaction = getTransaction; | ||
module.exports.pushTransaction = pushTransaction; | ||
module.exports = BlockCypher; |
{ | ||
"name": "blockcypher-unofficial", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "wrapper around blockcypher's api. Conforms to a standard derived from bitcoind", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"Andrew Malta <andrew.malta@yale.edu>", | ||
"Howard Wu <howardWu@berkely.edu>" | ||
"Howard Wu <howardwu@berkeley.edu>" | ||
], | ||
@@ -30,0 +30,0 @@ "bugs": { |
@@ -5,2 +5,61 @@ # blockcypher-unofficial | ||
you can install the npm moudle <a href="">here</a> | ||
you can install the npm module <a href="https:www.npmjs.com/package/blockcypher-unofficial">here</a> | ||
for a guide to the standard that this module follows please check out <a href="https:github.com/blockai/abstract-common-blockchain/blob/master/README.md">here</a> | ||
Also check out the comments above each function in lib if you want a deeper understanding of what each function expects and returns. | ||
here is a quick example call to each function in the library | ||
##Usage | ||
//simply require the npm module at the top of the file you are using it on. | ||
var blockcypher = require('blockcypher-unoffical'); | ||
## Addresses | ||
//for more information about the arguments, check the comment stubs above each function in addresses.js in lib. | ||
blockcypher({network: "mainnet"}).Addresses.Summary(addresses, callback); | ||
blockcypher({network: "testnet"}).Addresses.Summary(addresses, callback); | ||
blockcypher({network: "mainnet"}).Addresses.Unspents(addresses, callback); | ||
blockcypher({network: "testnet"}).Addresses.Unspents(addresses, callback); | ||
blockcypher({network: "mainnet"}).Addresses.Transactions(addresses, callback); | ||
blockcypher({network: "testnet"}).Addresses.Transactions(addresses, callback); | ||
## Blocks | ||
//for more information about the arguments, check the comment stubs above each function in blocks.js in lib. | ||
blockcypher({network: "mainnet"}).Blocks.Get(blockids, callback); | ||
blockcypher({network: "testnet"}).Blocks.Get(blockids, callback); | ||
blockcypher({network: "mainnet"}).Blocks.Latest(callback); | ||
blockcypher({network: "testnet"}).Blocks.Latest(callback); | ||
blockcypher({network: "mainnet"}).Blocks.Propogate(blockhex, callback); | ||
blockcypher({network: "testnet"}).Blocks.Propogate(blockhex, callback); | ||
blockcypher({network: "mainnet"}).Blocks.Transactions(blockids, callback); | ||
blockcypher({network: "testnet"}).Blocks.Transactions(blockids, callback); | ||
## Transactions | ||
//for more information about the arguments, check the comment stubs above each function in blocks.js in lib. | ||
blockcypher({network: "mainnet"}).Transactions.Get(txids, callback); | ||
blockcypher({network: "testnet"}).Transactions.Get(txids, callback); | ||
blockcypher({network: "mainnet"}).Transactions.Latest(callback); | ||
blockcypher({network: "testnet"}).Transactions.Latest(callback); | ||
blockcypher({network: "mainnet"}).Transactions.Outputs(outputs, callback); | ||
blockcypher({network: "testnet"}).Transactions.Outputs(outputs, callback); | ||
blockcypher({network: "mainnet"}).Transactions.Status(txids, callback); | ||
blockcypher({network: "testnet"}).Transactions.Status(txids, callback); | ||
blockcypher({network: "mainnet"}).Transactions.Propogate(transactionHex, callback); | ||
blockcypher({network: "testnet"}).Transactions.Propogate(transactionHex, callback); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
35196
9
579
1
64
1