bitcoin-propagator
Advanced tools
Comparing version
module.exports = { | ||
Propagator: require('./src/propagator'), | ||
nodes: { | ||
Insight: require('./src/nodes/insight') | ||
} | ||
Node: require('./src/node') | ||
}; |
{ | ||
"name": "bitcoin-propagator", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Bitcoin transaction propagator and blockchain access", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,6 +21,6 @@ const request = require('request-promise'); | ||
else if (!network || network === 'testnet') | ||
else if (network === 'testnet') | ||
this.address = 'https://test-insight.bitpay.com/api'; | ||
else throw new Error('UnknownNetworkError', 'Unknown network'); | ||
else throw new UnknownNetworkError('Unknown network ' + network); | ||
} | ||
@@ -39,3 +39,3 @@ | ||
return request.post({ url: url, json: data, timeout: timeout }) | ||
.catch(onError('InvalidAddressError')); | ||
.catch(onError(InvalidAddressError)); | ||
} | ||
@@ -55,3 +55,3 @@ | ||
.then(data => data.txid) | ||
.catch(onError('TransactionBroadcastError')); | ||
.catch(onError(TransactionBroadcastError)); | ||
} | ||
@@ -79,5 +79,5 @@ | ||
if (error.name === 'StatusCodeError') | ||
throw new Error('TransactionNotFoundError', 'The transaction with ID \'' + transactionId + '\' wasn\'t found'); | ||
throw new TransactionNotFoundError('The transaction with ID \'' + transactionId + '\' wasn\'t found'); | ||
else | ||
onError('InvalidAddressError')(error); | ||
onError(InvalidTransactionError)(error); | ||
}); | ||
@@ -90,9 +90,9 @@ } | ||
function onError(type) | ||
function onError(errorClass) | ||
{ | ||
return error => { | ||
if (error.name === 'RequestError') | ||
throw new Error(error.name, error.message); | ||
throw error; | ||
else | ||
throw new Error(type, error.error); | ||
throw new errorClass(error.error); | ||
} | ||
@@ -131,2 +131,11 @@ } | ||
// Errors | ||
class UnknownNetworkError extends Error { constructor(message) { super('UnknownNetworkError', message); this.name = 'UnknownNetworkError'; } } | ||
class InvalidAddressError extends Error { constructor(message) { super('InvalidAddressError', message); } } | ||
class TransactionBroadcastError extends Error { constructor(message) { super('TransactionBroadcastError', message); } } | ||
class TransactionNotFoundError extends Error { constructor(message) { super('TransactionNotFoundError', message); } } | ||
class InvalidTransactionError extends Error { constructor(message) { super('InvalidTransactionError', message); } } | ||
module.exports = Insight; |
@@ -14,15 +14,20 @@ const Promise = require('bluebird'); | ||
* Propagator contructor. | ||
* @param {string} network The network to connect. | ||
* The first argument can be the network, testnet or mainnet (livenet too), | ||
* or the list of nodes instances. Each node must extend the Node class. | ||
* @param {string | Array} network The network to connect or an array of nodes. | ||
*/ | ||
constructor (network, nodes) { | ||
constructor (network) { | ||
super(); | ||
if (network !== undefined && network instanceof Array) | ||
if (typeof network === 'string') | ||
{ | ||
nodes = network; | ||
network = undefined; | ||
this.nodes = [ new Insight(network) ]; | ||
} | ||
const defaultNodes = [ new Insight(network) ]; | ||
this.nodes = nodes instanceof Array ? nodes : defaultNodes; | ||
else if (network instanceof Array) | ||
{ | ||
if (!network.every(node => node instanceof Node)) | ||
throw new TypeError('Every node must extend the class Node.'); | ||
this.nodes = network; | ||
} | ||
} | ||
@@ -82,6 +87,15 @@ | ||
} | ||
else return Promise.reject(new Error('ConnectionError', 'Could not connect to any propagator')); | ||
else return Promise.reject(new ConnectionError('Could not connect to any propagator')); | ||
} | ||
// Errors | ||
class ConnectionError extends Error { constructor(message) { super('ConnectionError', message); } } | ||
Propagator.nodes = [ Insight ]; | ||
Propagator.Insight = Insight; | ||
module.exports = Propagator; |
const bitcore = require('bitcore-lib'); | ||
const data = require('./data'); | ||
const Insight = require('../').nodes.Insight; | ||
const Insight = require('../').Propagator.Insight; | ||
@@ -16,8 +16,5 @@ | ||
insight.getUnspent('mfbb6ohg1SRnfDKGnS2AjVX2xEALfLHWs0') | ||
.then(data => { | ||
console.error('\t\tError, expected output', data); | ||
onError(); | ||
}) | ||
.then(data => onError('output', data)) | ||
.catch(error => { | ||
console.log('\t\tSuccess, error as expected', error); | ||
console.log('\tSuccess, error as expected', error.name + ' ' + error.message); | ||
@@ -29,8 +26,5 @@ console.log('\ngetUnspent with correct address'); | ||
.catch(error => { | ||
console.error('\t\tError, unexpected error', error); | ||
onError(); | ||
}) | ||
.catch(error => onError('error', error.name + ' ' + error.message)) | ||
.then(unspent => { | ||
console.log('\t\tSuccess, expected output', data); | ||
console.log('\tSuccess, expected output', unspent); | ||
@@ -51,8 +45,5 @@ console.log('\nbroadcast with valid data'); | ||
.catch(error => { | ||
console.error('\t\tError, unexpected error', error); | ||
onError(); | ||
}) | ||
.catch(error => onError('error', error.name + ' ' + error.message)) | ||
.then(data => { | ||
console.log('\t\tSuccess, expected output', data); | ||
console.log('\tSuccess, expected output', data); | ||
transactionId = data; | ||
@@ -65,8 +56,5 @@ | ||
.then(data => { | ||
console.error('\t\tError, expected output', data); | ||
onError(); | ||
}) | ||
.then(data => onError('output', data)) | ||
.catch(error => { | ||
console.log('\t\tSuccess, unexpected error', error); | ||
console.log('\tSuccess, expected error', error.name + ' ' + error.message); | ||
@@ -78,22 +66,19 @@ console.log('\nverify with valid transaction ID'); | ||
.catch(error => { | ||
console.error('\t\tError, unexpected', error); | ||
onError(); | ||
}) | ||
.catch(error => onError('error', error.name + ' ' + error.message)) | ||
.then(data => { | ||
if (data.data === 'test') | ||
console.log('\t\tSuccess, expected output', data); | ||
console.log('\tSuccess, expected output', data); | ||
else | ||
throw console.error('\t\tWrogn data', data); | ||
onError('output', data); | ||
}) | ||
.catch(onError) | ||
.then(() => console.log('\nSuccess!')); | ||
function onError() | ||
function onError(unexpected, message) | ||
{ | ||
console.error('\tError, unexpected', unexpected, message); | ||
console.error('\nError!'); | ||
process.exit(1); | ||
} |
@@ -1,8 +0,8 @@ | ||
const data = require('./data'); | ||
const Propagator = require('../src/propagator'); | ||
const Node = require('../src/node'); | ||
const Insight = require('../src/nodes/insight'); | ||
const data = require('./data'); | ||
const { Propagator, Node } = require('../'); | ||
class TestNode extends Node { | ||
class InvalidNode { } | ||
class ErrorNode extends Node { | ||
constructor () { super(); } | ||
@@ -15,3 +15,11 @@ | ||
class TestNode extends Node { | ||
constructor () { super(); } | ||
getUnspent (address) { | ||
return Promise.resolve([ 'unspent' ]); | ||
} | ||
} | ||
const network = 'testnet'; | ||
@@ -21,48 +29,55 @@ const privateKey = data.privateKey; | ||
let propagator = new Propagator( | ||
network, | ||
[ new TestNode() ] | ||
); | ||
var propagator; | ||
console.log('\npropagating through the test node') | ||
propagator.getUnspent(address) | ||
.then(data => { | ||
console.error('\t\tError, unexpect output', data); | ||
onError(); | ||
Promise.resolve() | ||
.then(() => { | ||
console.log('\npropagating through an invalid node') | ||
new Propagator([ new InvalidNode() ]); | ||
}) | ||
.then(data => onError('output', data)) | ||
.catch(error => { | ||
console.log('\t\tSuccess, expected error', error); | ||
propagator = new Propagator( | ||
network, | ||
[ new TestNode(), new Insight(network) ] | ||
); | ||
console.log('\tSuccess, expected error', error.name + ' ' + error.message); | ||
console.log('\npropagating through a valid node'); | ||
propagator = new Propagator([ new ErrorNode() ]); | ||
console.log('\npropagating through the error node') | ||
return propagator.getUnspent(address); | ||
}) | ||
.then(data => onError('output', data)) | ||
.catch(error => { | ||
console.error('\t\tError, unexpected error', error); | ||
onError(); | ||
console.log('\tSuccess, expected error', error.name + ' ' + error.message); | ||
propagator = new Propagator([ new ErrorNode(), new TestNode() ]); | ||
console.log('\npropagating through a valid node'); | ||
return propagator.getUnspent(address); | ||
}) | ||
.catch(error => onError('error', error.name + ' ' + error.message)) | ||
.then(data => { | ||
console.log('\t\tSuccess, expected ouput', data); | ||
console.log('\tSuccess, expected ouput', data); | ||
if (propagator.nodes[0] instanceof Insight) | ||
console.log('\nThe nodes were successfuly reordered'); | ||
else | ||
{ | ||
console.error('\nError: The nodes were not reordered'); | ||
onError(); | ||
} | ||
console.log('\nexpect reordering'); | ||
if (!(propagator.nodes[0] instanceof TestNode)) | ||
throw TypeError('The node does not extend TestNode'); | ||
}) | ||
.then(() => console.log('\nSuccess!')); | ||
.catch(error => onError('error', error.name + ' ' + error.message)) | ||
.then(() => { | ||
console.log('\tSuccess, the nodes were reordered'); | ||
console.log('\nSuccess!'); | ||
}); | ||
function onError() | ||
function onError(unexpected, message) | ||
{ | ||
console.error('\t\tError, unexpected', unexpected, message); | ||
console.error('\nError!'); | ||
process.exit(1); | ||
} |
15959
8.65%361
0.56%