From 566f8da8362e945e56405eb4351f1565bcf28018 Mon Sep 17 00:00:00 2001 From: Patrick Lodder Date: Fri, 24 Oct 2014 22:45:22 +0400 Subject: [PATCH] Allow for asynchronous signing Do not error when api version=2 and no pin is provided. Instead, return the object to sign. --- lib/block_io.js | 15 +++++++++------ test/api.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lib/block_io.js b/lib/block_io.js index 14a86b1..8dd791d 100644 --- a/lib/block_io.js +++ b/lib/block_io.js @@ -62,16 +62,16 @@ BlockIo.prototype._withdraw = function (method, path, args, cb) { if (!args || typeof(args) !== 'object') args = {}; // check for pin - var pin; + var pin = null; var pinSupplied = (typeof(args.pin) !== 'undefined'); - if (!pinSupplied && typeof(this.pin) === 'undefined') { - return cb(new Error('Missing "pin", please supply as argument or at initialization time.')); - } else { + + if (pinSupplied || typeof(this.pin) != 'undefined') pin = pinSupplied ? args.pin : this.pin; - } + // add pin to args for v1, remove for v2; - if (this.version === 1) { + if (this.version == 1) { + if (!pin) return cb(new Error('Missing "pin", please supply as argument or at initialization time.')); args.pin = pin; } else { delete args.pin; @@ -90,6 +90,9 @@ BlockIo.prototype._withdraw = function (method, path, args, cb) { if (!res.data.encrypted_passphrase || !res.data.encrypted_passphrase.passphrase) return cb(e, res); + // if no pin was supplied, return the response for signing asynchronously + if (!pin) return cb(e, res); + // If we get here, Block.io's asking us to provide some client-side signatures, let's get to it var encrypted_passphrase = res.data.encrypted_passphrase.passphrase; var aesKey = self.aesKey || helper.pinToKey(pin); diff --git a/test/api.js b/test/api.js index 8c8c452..03a6bdd 100644 --- a/test/api.js +++ b/test/api.js @@ -161,5 +161,44 @@ spec.addBatch({ ) }); + +if (VERSION == 1) spec.addBatch({ + "withdraw_from_address (without PIN, v1)": { + topic: function () { + client.pin = null; + client.aesKey = null; + client.withdraw_from_address({ + from_labels: cache.lazy('fromAddress'), + payment_address: cache.lazy('newAddress'), + amount: genericHelpers.calcWithdrawalAmount, + }, this.callback); + }, + "must return an error": function (err, res) { + assert.instanceOf(err, Error); + } + } +}); + +if (VERSION > 1) spec.addBatch({ + "withdraw_from_address (without PIN, > v1)": genericHelpers.makeMethodCase( + client, + 'withdraw_from_label', + { + from_labels: cache.lazy('fromLabel'), + payment_address: cache.lazy('newAddress'), + amount: genericHelpers.calcWithdrawalAmount, + }, + { + "must return a transaction to sign": function (err, res, r) { + assert.isObject(res); + assert.isObject(res.data); + assert.isString(res.data.reference_id) + assert.isObject(res.data.encrypted_passphrase) + assert.isArray(res.data.inputs); + } + } + ) +}); + if (genericHelpers.checkEnv()) spec.export(module);