Skip to content

Commit

Permalink
cleanup & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklodder committed Jan 24, 2015
1 parent 951679a commit 0e90bec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
59 changes: 39 additions & 20 deletions lib/block_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,43 @@ BlockIo.DEFAULT_PORT = '';
BlockIo.HOST = 'block.io';


// Error messages
var ERR_KEY_INV = 'Error occurred validating key';
var ERR_PIN_MIS = 'Missing "pin", please supply as argument';
var ERR_PIN_INV = 'Public key mismatch. Invalid Secret PIN detected.';
var ERR_PK_EXTR = 'Could not extract privkey';
var ERR_UNKNOWN = 'Unknown error occured';

// IF YOU EDIT THIS LIB, PLEASE BE A DARLING AND CHANGE THE USER AGENT :)
BlockIo.USER_AGENT = ['npm','block_io', pkgMeta.version].join(':');

// simple uniform methods that do not need special processing
BlockIo.PASSTHROUGH_METHODS = [
'get_balance', 'get_new_address', 'get_my_addresses', 'get_address_received',
'get_address_by_label', 'get_address_balance', 'create_user', 'get_users',
'get_user_balance', 'get_user_address', 'get_user_received', 'get_current_price',
'is_green_address', 'is_green_transaction', 'get_transactions', 'sign_and_finalize_withdrawal',
'get_new_dtrust_address', 'get_my_dtrust_addresses', 'get_dtrust_address_by_label', 'get_dtrust_transactions',
'get_dtrust_address_balance', 'get_network_fee_estimate', 'archive_address', 'unarchive_address',
'get_my_archived_addresses', 'archive_dtrust_address', 'unarchive_dtrust_address', 'get_my_archived_dtrust_addresses'
'get_user_balance', 'get_user_address', 'get_user_received',
'get_current_price', 'is_green_address', 'is_green_transaction',
'get_transactions', 'sign_and_finalize_withdrawal', 'get_new_dtrust_address',
'get_my_dtrust_addresses', 'get_dtrust_address_by_label',
'get_dtrust_transactions', 'get_dtrust_address_balance',
'get_network_fee_estimate', 'archive_address', 'unarchive_address',
'get_my_archived_addresses', 'archive_dtrust_address',
'unarchive_dtrust_address', 'get_my_archived_dtrust_addresses'
];

// withdrawal methods that need local signing
BlockIo.WITHDRAWAL_METHODS = [
'withdraw', 'withdraw_from_user', 'withdraw_from_label', 'withdraw_from_address', 'withdraw_from_labels',
'withdraw_from_addresses', 'withdraw_from_users', 'withdraw_from_dtrust_address', 'withdraw_from_dtrust_labels'
'withdraw', 'withdraw_from_user', 'withdraw_from_label',
'withdraw_from_address', 'withdraw_from_labels', 'withdraw_from_addresses',
'withdraw_from_users', 'withdraw_from_dtrust_address',
'withdraw_from_dtrust_labels'
];

BlockIo.prototype._withdraw = function (method, path, args, cb) {
if (!args || typeof(args) !== 'object') args = {};

// check for pin
var pin = null;
var pinError = new Error('Missing "pin", please supply as argument or at initialization time.');
var pinSupplied = (typeof(args.pin) !== 'undefined');

if (pinSupplied || typeof(this.pin) != 'undefined')
Expand All @@ -82,7 +95,7 @@ BlockIo.prototype._withdraw = function (method, path, args, cb) {

// add pin to args for v1, remove for v2;
if (this.version == 1) {
if (!pin) return cb(pinError);
if (!pin) return cb(new Error(ERR_PIN_MIS));
args.pin = pin;
} else {
delete args.pin;
Expand All @@ -98,32 +111,36 @@ BlockIo.prototype._withdraw = function (method, path, args, cb) {
!res.data.hasOwnProperty('reference_id')) return cb(e, res);

//if we're doing dtrust, return the intermediate object for manual signing.
if (!res.data.encrypted_passphrase || !res.data.encrypted_passphrase.passphrase)
return cb(e, res);
if (!res.data.encrypted_passphrase ||
!res.data.encrypted_passphrase.passphrase)
return cb(e, res);

// if no pin was supplied and allowNoPin flag was set,
// return the response for signing asynchronously,
// else return pin error
if (!pin) return (self.options.allowNoPin) ? cb(e, res) : cb(pinError);
if (!pin)
return (self.options.allowNoPin) ? cb(e, res) : cb(new Error(ERR_PIN_INV));

// If we get here, Block.io's asking us to provide some client-side signatures, let's get to it
// If we get here, Block.io's asking us to provide client-side signatures
var encrypted_passphrase = res.data.encrypted_passphrase.passphrase;
var aesKey = self.aesKey || helper.pinToKey(pin);
var privkey = helper.extractKey(encrypted_passphrase, aesKey);

if (!(privkey instanceof ECKey))
return cb(new Error('Could not extract privkey'));
return cb(new Error(ERR_PK_EXTR));

var pubkey = privkey.pub.toHex();
if (pubkey != res.data.encrypted_passphrase.signer_public_key)
return cb(new Error('Public key mismatch for requested signer and ourselves. Invalid Secret PIN detected.'));
return cb(new Error(ERR_PIN_INV));

res.data.inputs = helper.signInputs(privkey, res.data.inputs);

aesKey = '';
privkey = null;

self._request(method, 'sign_and_finalize_withdrawal', {signature_data: JSON.stringify(res.data)}, cb);
self._request(method, 'sign_and_finalize_withdrawal', {
signature_data: JSON.stringify(res.data)
}, cb);

});

Expand All @@ -136,7 +153,7 @@ BlockIo.prototype._constructURL = function (path, query) {
BlockIo.HOST, // block.io
(this.port) ? ':' : '', this.port, // eg: :80
'/api/v', (this.version).toString(10), '/', // eg: /api/v1/
path, // eg: withdraw_from_address
path, // eg: get_balance
query ? ['?', qs.stringify(query)].join('') : '' // eg: ?api_key=abc
].join('');
};
Expand Down Expand Up @@ -169,7 +186,7 @@ BlockIo.parseResponse = function (cb) {
if (err) return cb(err);

var errOut = null;
var errMsg = 'Unknown error occured';
var errMsg = ERR_UNKNOWN;
var data = null;

try {
Expand All @@ -180,7 +197,8 @@ BlockIo.parseResponse = function (cb) {

if (typeof(data) !== 'object' || data.status !== 'success') {
if (data.error_message) errMsg = data.error_message;
if (data.data && typeof(data.data) === 'object' && data.data.error_message) errMsg = data.data.error_message;
if (data.data && typeof(data.data) === 'object' &&
data.data.error_message) errMsg = data.data.error_message;
errOut = new Error(errMsg);
}

Expand All @@ -193,7 +211,8 @@ BlockIo.prototype.validate_key = function (cb) {
// Test if the key is valid by doing a simple balance check

this._request('GET', 'get_balance', {}, function (err, doc) {
var errOut = err || (doc.status === 'success') ? null : new Error('Error occurred validating key');
var errOut = err ||
(doc.status === 'success') ? null : new Error(ERR_KEY_INV);
return cb(errOut, (errOut === null));
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"pbkdf2-sha256" : "=1.1.0"
},
"devDependencies": {
"vows": "0.7.x"
"vows": "0.7.x"
},
"engines" : {
"node" : ">=0.8",
Expand Down

0 comments on commit 0e90bec

Please sign in to comment.