chain-node
Advanced tools
Comparing version 2.0.0 to 3.0.0-pre0
334
index.js
@@ -1,193 +0,149 @@ | ||
var request = require('request'); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var bitcoin = require('bitcoinjs-lib'); | ||
var HttpUtility = require('./http-utility'); | ||
var Signer = require('./signer'); | ||
var util = require('util'); | ||
var URL = "https://api.chain.com"; | ||
var PEM = fs.readFileSync(path.join(__dirname, "./chain.pem")); | ||
module.exports = Chain; | ||
module.exports = { | ||
getKey: function() { | ||
return this.key || 'GUEST-TOKEN'; | ||
}, | ||
getApiKeyId: function() { | ||
return this.apiKeyId || this.getKey(); | ||
}, | ||
getApiKeySecret: function() { | ||
return this.apiKeySecret; | ||
}, | ||
getAuth: function() { | ||
return {user: this.getApiKeyId(), pass: this.getApiKeySecret()}; | ||
}, | ||
getVersion: function() { | ||
return this.version || 'v2'; | ||
}, | ||
getBlockChain: function() { | ||
return this.blockChain || 'bitcoin'; | ||
}, | ||
getBaseURL: function() { | ||
return URL + '/' + this.getVersion() + '/' + this.getBlockChain(); | ||
}, | ||
getNotificationsURL: function() { | ||
return URL + '/' + this.getVersion() + '/notifications'; | ||
}, | ||
getAddress: function(address, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/addresses/' + address, | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getAddresses: function(addresses, cb) { | ||
this.getAddress(addresses.join(','), cb); | ||
}, | ||
getAddressTransactions: function(address, options, cb) { | ||
options = options || {}; | ||
if (typeof(options) == 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/addresses/' + address + '/transactions', | ||
qs: options, | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getAddressesTransactions: function(addresses, options, cb) { | ||
this.getAddressTransactions(addresses.join(','), options, cb); | ||
}, | ||
getAddressUnspents: function(address, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/addresses/' + address + '/unspents', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getAddressesUnspents: function(addresses, cb) { | ||
this.getAddressUnspents(addresses.join(','), cb); | ||
}, | ||
getAddressOpReturns: function(address, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/addresses/' + address + '/op-returns', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getTransaction: function(hash, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/transactions/' + hash, | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getTransactionOpReturn: function(hash, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/transactions/' + hash + '/op-return', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
sendTransaction: function(hex, cb) { | ||
request({ | ||
method: 'PUT', | ||
uri: this.getBaseURL() + '/transactions', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
json: {hex: hex}, | ||
}, function(err, msg, resp) { | ||
cb(err, resp); | ||
}); | ||
}, | ||
getBlock: function(hashOrHeight, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/blocks/' + hashOrHeight, | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getLatestBlock: function(cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/blocks/latest', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
getBlockOpReturns: function(hashOrHeight, cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getBaseURL() + '/blocks/' + hashOrHeight + '/op-returns', | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
createNotification: function(args, cb) { | ||
request({ | ||
method: 'POST', | ||
uri: this.getNotificationsURL(), | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
json: args, | ||
}, function(err, msg, resp) { | ||
cb(err, resp); | ||
}); | ||
}, | ||
listNotifications: function(cb) { | ||
request({ | ||
method: 'GET', | ||
uri: this.getNotificationsURL(), | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth(), | ||
}, function(err, msg, resp) { | ||
cb(err, JSON.parse(resp)); | ||
}); | ||
}, | ||
deleteNotification: function(id, cb) { | ||
request({ | ||
method: 'DELETE', | ||
uri: this.getNotificationsURL() + '/' + id, | ||
strictSSL: true, | ||
cert: PEM, | ||
auth: this.getAuth() | ||
}, function(err, msg, resp) { | ||
cb(err, resp); | ||
}); | ||
function Chain(c) { | ||
if(c.keyId == null) { | ||
c.keyId = "GUEST-TOKEN"; | ||
}; | ||
this.auth = {user: c.keyId, pass: c.keySecret}; | ||
if(c.url == null) { | ||
c.url = 'https://api.chain.com'; | ||
}; | ||
if(c.apiVersion == null) { | ||
c.apiVersion = 'v2'; | ||
}; | ||
if(c.blockChain == null) { | ||
c.blockChain = 'bitcoin'; | ||
}; | ||
if(c.blockChain == 'bitcoin') { | ||
this.blockChainConfig = bitcoin.networks.bitcoin; | ||
} else { | ||
this.blockChainConfig = bitcoin.networks.testnet; | ||
} | ||
var baseurl = c.url + '/' + c.apiVersion; | ||
this.dataApi = new HttpUtility({ | ||
url: baseurl + '/' + c.blockChain, | ||
auth: this.auth | ||
}); | ||
this.notifApi = new HttpUtility({ | ||
url: baseurl + '/notifications', | ||
auth: this.auth | ||
}); | ||
} | ||
Chain.prototype.getAddress = function(address, cb) { | ||
this.dataApi.get('/addresses/' + address, cb); | ||
}; | ||
Chain.prototype.getAddresses = function(addresses, cb) { | ||
this.getAddress(addresses.join(','), cb); | ||
}; | ||
Chain.prototype.getAddressTransactions = function(address, options, cb) { | ||
options = options || {}; | ||
if (typeof(options) == 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
this.dataApi.get('/addresses/' + address + '/transactions', cb); | ||
}; | ||
Chain.prototype.getAddressesTransactions = function(addresses, options, cb) { | ||
this.getAddressTransactions(addresses.join(','), options, cb); | ||
}; | ||
Chain.prototype.getAddressUnspents = function(address, cb) { | ||
this.dataApi.get('/addresses/' + address + '/unspents', cb); | ||
}; | ||
Chain.prototype.getAddressesUnspents = function(addresses, cb) { | ||
this.getAddressUnspents(addresses.join(','), cb); | ||
}; | ||
Chain.prototype.getAddressOpReturns = function(address, cb) { | ||
this.dataApi.get('/addresses/' + address + '/op-returns', cb); | ||
}; | ||
Chain.prototype.getTransaction = function(hash, cb) { | ||
this.dataApi.get('/transactions/' + hash, cb); | ||
}; | ||
Chain.prototype.getTransactionOpReturn = function(hash, cb) { | ||
this.dataApi.get('/transactions/' + hash + '/op-return', cb); | ||
}; | ||
Chain.prototype.sendTransaction = function(hex, cb) { | ||
this.dataApi.post('/transactions', {hex: hex}, cb); | ||
}; | ||
Chain.prototype.getBlock = function(hashOrHeight, cb) { | ||
this.dataApi.get('/blocks/' + hashOrHeight, cb); | ||
}; | ||
Chain.prototype.getLatestBlock = function(cb) { | ||
this.getBlock('latest', cb); | ||
}; | ||
Chain.prototype.getBlockOpReturns = function(hashOrHeight, cb) { | ||
this.dataApi.get('/blocks/' + hashOrHeight + '/op-returns', cb); | ||
}; | ||
Chain.prototype.createNotification = function(args, cb) { | ||
this.notifApi.post('/', args, cb); | ||
}; | ||
Chain.prototype.listNotifications = function(cb) { | ||
this.notifApi.get('/', cb); | ||
}; | ||
Chain.prototype.deleteNotification = function(id, cb) { | ||
this.notifApi.delete('/' + id, cb); | ||
}; | ||
Chain.prototype.signTemplate = function(template, keys, cb) { | ||
return Signer(this.blockChainConfig, template, keys); | ||
}; | ||
Chain.prototype.sendTransaction = function(template, cb) { | ||
this.dataApi.post('/transactions/send', template, cb); | ||
}; | ||
Chain.prototype.buildTransaction = function(args, cb) { | ||
var buildRequest = { | ||
inputs: args.inputs, | ||
outputs: args.outputs, | ||
miner_fee_rate: args.miner_fee_rate, | ||
change_address: args.change_address, | ||
min_confirmations: args.min_confirmations | ||
}; | ||
this.dataApi.post('/transactions/build', buildRequest, cb); | ||
}; | ||
Chain.prototype.transact = function(args, cb) { | ||
var that = this; | ||
var blockChainConfig = this.blockChainConfig; | ||
var keys = args.inputs.map(function(input) { | ||
return bitcoin.ECKey.fromWIF(input.private_key); | ||
}); | ||
args.inputs = keys.map(function(key) { | ||
return {address: key.pub.getAddress(blockChainConfig).toString()} | ||
}); | ||
this.buildTransaction(args, function(err, resp) { | ||
if(err == null) { | ||
var signedTemplate = that.signTemplate(resp, keys); | ||
that.sendTransaction(signedTemplate, cb); | ||
} else { | ||
cb(err, null); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "chain-node", | ||
"version": "2.0.0", | ||
"version": "3.0.0-pre0", | ||
"description": "The Official Node.js SDK for Chain's Bitcoin API", | ||
@@ -24,3 +24,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"request": "2.36.0" | ||
"request": "2.36.0", | ||
"bitcoinjs-lib": "1.2.0" | ||
}, | ||
@@ -27,0 +28,0 @@ "files": [ |
@@ -14,3 +14,5 @@ # chain-node | ||
```js | ||
var chain = require('chain-node'); | ||
var Chain = require('chain-node'); | ||
var chain = new Chain(); | ||
chain.getAddress('17x23dNjXJLzGMev6R63uyRhMWP1VHawKc', function(err, resp) { | ||
@@ -17,0 +19,0 @@ console.log('balance='+resp['balance']); |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
33
0
10325
2
121
3
1
+ Addedbitcoinjs-lib@1.2.0
+ Addedasn1.js@4.10.1(transitive)
+ Addedbigi@1.4.2(transitive)
+ Addedbitcoinjs-lib@1.2.0(transitive)
+ Addedbn.js@4.12.05.2.1(transitive)
+ Addedbrorand@1.1.0(transitive)
+ Addedbrowserify-aes@1.2.0(transitive)
+ Addedbrowserify-cipher@1.0.1(transitive)
+ Addedbrowserify-des@1.0.2(transitive)
+ Addedbrowserify-rsa@4.1.0(transitive)
+ Addedbrowserify-sign@4.2.3(transitive)
+ Addedbs58@2.0.1(transitive)
+ Addedbs58check@1.0.3(transitive)
+ Addedbuffer-xor@1.0.3(transitive)
+ Addedcipher-base@1.0.4(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcreate-ecdh@4.0.4(transitive)
+ Addedcreate-hash@1.2.0(transitive)
+ Addedcreate-hmac@1.1.7(transitive)
+ Addedcrypto-browserify@3.12.0(transitive)
+ Addeddes.js@1.1.0(transitive)
+ Addeddiffie-hellman@5.0.3(transitive)
+ Addedecurve@1.0.0(transitive)
+ Addedelliptic@6.5.5(transitive)
+ Addedevp_bytestokey@1.0.3(transitive)
+ Addedhash-base@3.0.4(transitive)
+ Addedhash.js@1.1.7(transitive)
+ Addedhmac-drbg@1.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedmd5.js@1.3.5(transitive)
+ Addedmiller-rabin@4.0.1(transitive)
+ Addedminimalistic-assert@1.0.1(transitive)
+ Addedminimalistic-crypto-utils@1.0.1(transitive)
+ Addedparse-asn1@5.1.7(transitive)
+ Addedpbkdf2@3.1.2(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpublic-encrypt@4.0.3(transitive)
+ Addedrandombytes@2.1.0(transitive)
+ Addedrandomfill@1.0.4(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedripemd160@2.0.2(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsha.js@2.4.11(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)