Merge pull request #18 from matiu/feat/client-filestorage
add FileStorage + refactor
This commit is contained in:
commit
d96634f951
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var Client = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -11,14 +11,8 @@ program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
var args = program.args;
|
var args = program.args;
|
||||||
var cli = new Client({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
client.createAddress(function(err, x) {
|
||||||
});
|
utils.die(err);
|
||||||
|
|
||||||
cli.createAddress(function(err, x) {
|
|
||||||
common.die(err);
|
|
||||||
console.log('* New Address %s ', x.address);
|
console.log('* New Address %s ', x.address);
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', x); //TODO
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
var Client = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -12,18 +12,12 @@ program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
var args = program.args;
|
var args = program.args;
|
||||||
var cli = new Client({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
client.getAddresses(function(err, x) {
|
||||||
});
|
utils.die(err);
|
||||||
|
|
||||||
cli.getAddresses(function(err, x) {
|
|
||||||
common.die(err);
|
|
||||||
|
|
||||||
console.log('* Addresses:');
|
console.log('* Addresses:');
|
||||||
_.each(x, function(a){
|
_.each(x, function(a){
|
||||||
console.log(' ', a.address);
|
console.log(' ', a.address);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', x); //TODO
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var Client = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -11,14 +11,9 @@ program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
var args = program.args;
|
var args = program.args;
|
||||||
var cli = new Client({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
|
||||||
});
|
|
||||||
|
|
||||||
cli.getBalance(function(err, x) {
|
client.getBalance(function(err, x) {
|
||||||
common.die(err);
|
utils.die(err);
|
||||||
console.log('* Wallet balance', x);
|
console.log('* Wallet balance', x);
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', x); //TODO
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var ClientLib = require('../lib/client');
|
var ClientLib = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -19,13 +19,11 @@ var walletName = args[0];
|
||||||
var copayerName = args[2] || process.env.USER;
|
var copayerName = args[2] || process.env.USER;
|
||||||
var network = program.network;
|
var network = program.network;
|
||||||
|
|
||||||
var mn = common.parseMN(args[1]);
|
var mn = utils.parseMN(args[1]);
|
||||||
|
|
||||||
var cli = new ClientLib({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
|
||||||
});
|
utils.die(err);
|
||||||
cli.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
|
|
||||||
common.die(err);
|
|
||||||
console.log(' * Wallet Created.');
|
console.log(' * Wallet Created.');
|
||||||
console.log(' - Secret to share:\n\t' + secret);
|
console.log(' - Secret to share:\n\t' + secret);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var ClientLib = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -17,11 +17,8 @@ if (!args[0])
|
||||||
var secret = args[0];
|
var secret = args[0];
|
||||||
var copayerName = args[1] || process.env.USER;
|
var copayerName = args[1] || process.env.USER;
|
||||||
|
|
||||||
var cli = new ClientLib({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
|
||||||
});
|
|
||||||
|
|
||||||
cli.joinWallet(secret, copayerName, function(err, xx) {
|
cli.joinWallet(secret, copayerName, function(err, xx) {
|
||||||
common.die(err);
|
utils.die(err);
|
||||||
console.log(' * Wallet Joined.', xx || '');
|
console.log(' * Wallet Joined.', xx || '');
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var Client = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -17,25 +17,14 @@ if (!args[0])
|
||||||
program.help();
|
program.help();
|
||||||
var txpid = args[0];
|
var txpid = args[0];
|
||||||
var reason = args[1] || '';
|
var reason = args[1] || '';
|
||||||
|
var client = utils.getClient(program);
|
||||||
|
|
||||||
var cli = new Client({
|
client.getTxProposals({}, function(err, txps) {
|
||||||
filename: program.config
|
utils.die(err);
|
||||||
});
|
|
||||||
|
|
||||||
cli.getTxProposals({}, function(err, txps) {
|
|
||||||
common.die(err);
|
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', txps); //TODO
|
|
||||||
|
|
||||||
var txp = common.findOneTxProposal(txps, txpid);
|
|
||||||
|
|
||||||
cli.rejectTxProposal(txp, reason, function(err, tx) {
|
|
||||||
common.die(err);
|
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', tx); //TODO
|
|
||||||
|
|
||||||
|
var txp = utils.findOneTxProposal(txps, txpid);
|
||||||
|
client.rejectTxProposal(txp, reason, function(err, tx) {
|
||||||
|
utils.die(err);
|
||||||
console.log('Transaction rejected.');
|
console.log('Transaction rejected.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var Client = require('../lib/client');
|
var Client = require('../lib/client');
|
||||||
var common = require('./common');
|
var utils = require('./cli-utils');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -19,15 +19,14 @@ if (!args[0] || !args[1] || !args[2])
|
||||||
var amount = args[1];
|
var amount = args[1];
|
||||||
var message = args[2];
|
var message = args[2];
|
||||||
|
|
||||||
var cli = new Client({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
|
||||||
});
|
|
||||||
|
|
||||||
cli.sendTxProposal({toAddress: address, amount: amount, message:message}, function(err, x) {
|
client.sendTxProposal({
|
||||||
common.die(err);
|
toAddress: address,
|
||||||
|
amount: amount,
|
||||||
|
message: message
|
||||||
|
}, function(err, x) {
|
||||||
|
utils.die(err);
|
||||||
console.log(' * Tx created: ID %s [%s] RequiredSignatures:',
|
console.log(' * Tx created: ID %s [%s] RequiredSignatures:',
|
||||||
x.id, x.status, x.requiredSignatures);
|
x.id, x.status, x.requiredSignatures);
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', x); //TODO
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var ClientLib = require('../lib/client');
|
var utils = require('./cli-utils');
|
||||||
var common = require('./common');
|
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -18,24 +17,13 @@ if (!args[0])
|
||||||
|
|
||||||
var txpid = args[0];
|
var txpid = args[0];
|
||||||
|
|
||||||
var cli = new ClientLib({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
client.getTxProposals({}, function(err, txps) {
|
||||||
});
|
utils.die(err);
|
||||||
|
|
||||||
cli.getTxProposals({}, function(err, txps) {
|
|
||||||
common.die(err);
|
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', txps); //TODO
|
|
||||||
|
|
||||||
var txp = common.findOneTxProposal(txps, txpid);
|
|
||||||
|
|
||||||
cli.signTxProposal(txp, function(err, tx) {
|
|
||||||
common.die(err);
|
|
||||||
|
|
||||||
if (program.verbose)
|
|
||||||
console.log('* Raw Server Response:\n', tx); //TODO
|
|
||||||
|
|
||||||
|
var txp = utils.findOneTxProposal(txps, txpid);
|
||||||
|
client.signTxProposal(txp, function(err, x) {
|
||||||
|
utils.die(err);
|
||||||
console.log('Transaction signed.');
|
console.log('Transaction signed.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
|
var utils = require('./cli-utils');
|
||||||
var Client = require('../lib/client');
|
|
||||||
var common = require('./common');
|
|
||||||
|
|
||||||
program
|
program
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
|
@ -13,12 +11,10 @@ program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
var args = program.args;
|
var args = program.args;
|
||||||
var cli = new Client({
|
var client = utils.getClient(program);
|
||||||
filename: program.config
|
|
||||||
});
|
|
||||||
|
|
||||||
cli.getStatus(function(err, res) {
|
client.getStatus(function(err, res) {
|
||||||
common.die(err);
|
utils.die(err);
|
||||||
|
|
||||||
var x = res.wallet;
|
var x = res.wallet;
|
||||||
console.log('* Wallet %s [%s]: %d-%d %s ', x.name, x.isTestnet ? 'testnet' : 'livenet', x.m, x.n, x.status);
|
console.log('* Wallet %s [%s]: %d-%d %s ', x.name, x.isTestnet ? 'testnet' : 'livenet', x.m, x.n, x.status);
|
||||||
|
@ -29,7 +25,7 @@ cli.getStatus(function(err, res) {
|
||||||
if (!_.isEmpty(res.pendingTxps)) {
|
if (!_.isEmpty(res.pendingTxps)) {
|
||||||
console.log("* TX Proposals:")
|
console.log("* TX Proposals:")
|
||||||
_.each(res.pendingTxps, function(x) {
|
_.each(res.pendingTxps, function(x) {
|
||||||
console.log("\t%s [%s by %s] %dSAT => %s", common.shortID(x.id), x.message, x.creatorName, x.amount, x.toAddress);
|
console.log("\t%s [%s by %s] %dSAT => %s", utils.shortID(x.id), x.message, x.creatorName, x.amount, x.toAddress);
|
||||||
|
|
||||||
if (!_.isEmpty(x.actions)) {
|
if (!_.isEmpty(x.actions)) {
|
||||||
console.log('\t\t * Actions');
|
console.log('\t\t * Actions');
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
|
||||||
|
var _ = require('lodash');
|
||||||
|
var Client = require('../lib/client');
|
||||||
|
|
||||||
|
var lib = function() {};
|
||||||
|
|
||||||
|
var die = lib.die = function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
lib.parseMN = function(MN) {
|
||||||
|
if (!MN)
|
||||||
|
die('No m-n parameter');
|
||||||
|
var mn = MN.split('-');
|
||||||
|
|
||||||
|
var m = parseInt(mn[0]);
|
||||||
|
var n = parseInt(mn[1]);
|
||||||
|
|
||||||
|
if (!m || ! n) {
|
||||||
|
die('Bad m-n parameter');
|
||||||
|
}
|
||||||
|
|
||||||
|
return [m, n];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
lib.shortID = function(id) {
|
||||||
|
return id.substr(id.length - 4);
|
||||||
|
};
|
||||||
|
|
||||||
|
lib.getClient = function(args) {
|
||||||
|
var storage = new Client.FileStorage({
|
||||||
|
filename: args.config
|
||||||
|
});
|
||||||
|
return new Client({
|
||||||
|
storage: storage,
|
||||||
|
verbose: args.verbose
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
lib.findOneTxProposal = function(txps, id) {
|
||||||
|
var matches = _.filter(txps, function(tx) {
|
||||||
|
return _.endsWith(lib.shortID(tx.id), id);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!matches.length)
|
||||||
|
lib.die('Could not find TX Proposal:' + id);
|
||||||
|
|
||||||
|
if (matches.length > 1)
|
||||||
|
lib.die('More than one TX Proposals match:' + id + ' : ' + _.map(matches, function(tx) {
|
||||||
|
return tx.id;
|
||||||
|
}).join(' '));;
|
||||||
|
|
||||||
|
return matches[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = lib;
|
|
@ -1,51 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
var _ = require('lodash');
|
|
||||||
|
|
||||||
var common = function() {};
|
|
||||||
|
|
||||||
|
|
||||||
var die = common.die = function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.error(err);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
common.parseMN = function(MN) {
|
|
||||||
if (!MN)
|
|
||||||
die('No m-n parameter');
|
|
||||||
var mn = MN.split('-');
|
|
||||||
|
|
||||||
var m = parseInt(mn[0]);
|
|
||||||
var n = parseInt(mn[1]);
|
|
||||||
|
|
||||||
if (!m || !n) {
|
|
||||||
die('Bad m-n parameter');
|
|
||||||
}
|
|
||||||
|
|
||||||
return [m, n];
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
common.shortID = function(id) {
|
|
||||||
return id.substr(id.length - 4);
|
|
||||||
};
|
|
||||||
|
|
||||||
common.findOneTxProposal = function(txps, id) {
|
|
||||||
var matches = _.filter(txps, function(tx) {
|
|
||||||
return _.endsWith(common.shortID(tx.id), id);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!matches.length)
|
|
||||||
common.die('Could not find TX Proposal:' + id);
|
|
||||||
|
|
||||||
if (matches.length > 1)
|
|
||||||
common.die('More than one TX Proposals match:' + id + ' : ' + _.map(matches, function(tx) {
|
|
||||||
return tx.id;
|
|
||||||
}).join(' '));;
|
|
||||||
|
|
||||||
return matches[0];
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = common;
|
|
|
@ -1,12 +1,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
|
var util = require('util');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var log = require('npmlog');
|
var log = require('npmlog');
|
||||||
var request = require('request')
|
var request = require('request')
|
||||||
log.debug = log.verbose;
|
log.debug = log.verbose;
|
||||||
log.level = 'debug';
|
|
||||||
var fs = require('fs')
|
|
||||||
|
|
||||||
var Bitcore = require('bitcore')
|
var Bitcore = require('bitcore')
|
||||||
var SignUtils = require('../signutils');
|
var SignUtils = require('../signutils');
|
||||||
|
@ -48,24 +47,19 @@ function _createXPrivKey(network) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function API(opts) {
|
function API(opts) {
|
||||||
if (!opts.filename) {
|
if (!opts.storage) {
|
||||||
throw new Error('Please set the config filename');
|
throw new Error('Must provide storage option');
|
||||||
|
}
|
||||||
|
this.storage = opts.storage;
|
||||||
|
this.verbose = !!opts.verbose;
|
||||||
|
if (this.verbose) {
|
||||||
|
log.level = 'debug';
|
||||||
}
|
}
|
||||||
this.filename = opts.filename;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype._save = function(data) {
|
|
||||||
fs.writeFileSync(this.filename, JSON.stringify(data));
|
|
||||||
};
|
|
||||||
|
|
||||||
API.prototype._load = function() {
|
|
||||||
try {
|
|
||||||
return JSON.parse(fs.readFileSync(this.filename));
|
|
||||||
} catch (ex) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
API.prototype._loadAndCheck = function() {
|
API.prototype._loadAndCheck = function() {
|
||||||
var data = this._load();
|
var data = this.storage.load();
|
||||||
if (!data) {
|
if (!data) {
|
||||||
log.error('Wallet file not found.');
|
log.error('Wallet file not found.');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -87,7 +81,7 @@ API.prototype._loadAndCheck = function() {
|
||||||
API.prototype._doRequest = function(method, url, args, data, cb) {
|
API.prototype._doRequest = function(method, url, args, data, cb) {
|
||||||
var reqSignature = _signRequest(method, url, args, data.signingPrivKey);
|
var reqSignature = _signRequest(method, url, args, data.signingPrivKey);
|
||||||
var absUrl = _getUrl(url);
|
var absUrl = _getUrl(url);
|
||||||
request({
|
var args = {
|
||||||
headers: {
|
headers: {
|
||||||
'x-identity': data.copayerId,
|
'x-identity': data.copayerId,
|
||||||
'x-signature': reqSignature,
|
'x-signature': reqSignature,
|
||||||
|
@ -96,7 +90,11 @@ API.prototype._doRequest = function(method, url, args, data, cb) {
|
||||||
url: absUrl,
|
url: absUrl,
|
||||||
body: args,
|
body: args,
|
||||||
json: true,
|
json: true,
|
||||||
}, function(err, res, body) {
|
};
|
||||||
|
log.verbose('Request Args', util.inspect(args));
|
||||||
|
request(args, function(err, res, body) {
|
||||||
|
log.verbose('Response:', err, body);
|
||||||
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (res.statusCode != 200) {
|
if (res.statusCode != 200) {
|
||||||
_parseError(body);
|
_parseError(body);
|
||||||
|
@ -123,7 +121,7 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb
|
||||||
if (!_.contains(['testnet', 'livenet'], network))
|
if (!_.contains(['testnet', 'livenet'], network))
|
||||||
return cb('Invalid network');
|
return cb('Invalid network');
|
||||||
|
|
||||||
var data = this._load();
|
var data = this.storage.load();
|
||||||
if (data) return cb('File ' + this.filename + ' already contains a wallet');
|
if (data) return cb('File ' + this.filename + ' already contains a wallet');
|
||||||
|
|
||||||
// Generate wallet key pair to verify copayers
|
// Generate wallet key pair to verify copayers
|
||||||
|
@ -152,7 +150,7 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb
|
||||||
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L');
|
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L');
|
||||||
data.secret = secret;
|
data.secret = secret;
|
||||||
|
|
||||||
self._save(data);
|
self.storage.save(data);
|
||||||
self._joinWallet(data, secret, copayerName, function(err) {
|
self._joinWallet(data, secret, copayerName, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
@ -192,7 +190,7 @@ API.prototype._joinWallet = function(data, secret, copayerName, cb) {
|
||||||
data.m = wallet.m;
|
data.m = wallet.m;
|
||||||
data.n = wallet.n;
|
data.n = wallet.n;
|
||||||
data.publicKeyRing = wallet.publicKeyRing;
|
data.publicKeyRing = wallet.publicKeyRing;
|
||||||
self._save(data);
|
self.storage.save(data);
|
||||||
|
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
|
@ -201,7 +199,7 @@ API.prototype._joinWallet = function(data, secret, copayerName, cb) {
|
||||||
API.prototype.joinWallet = function(secret, copayerName, cb) {
|
API.prototype.joinWallet = function(secret, copayerName, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var data = this._load();
|
var data = this.storage.load();
|
||||||
if (data) return cb('File ' + this.filename + ' already contains a wallet');
|
if (data) return cb('File ' + this.filename + ' already contains a wallet');
|
||||||
|
|
||||||
self._joinWallet(data, secret, copayerName, cb);
|
self._joinWallet(data, secret, copayerName, cb);
|
||||||
|
@ -236,7 +234,7 @@ API.prototype.getStatus = function(cb) {
|
||||||
} else {
|
} else {
|
||||||
data.verified = 'ok';
|
data.verified = 'ok';
|
||||||
}
|
}
|
||||||
self._save(data);
|
self.storage.save(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(null, wallet);
|
return cb(null, wallet);
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
var fs = require('fs')
|
||||||
|
|
||||||
|
function FileStorage(opts) {
|
||||||
|
if (!opts.filename) {
|
||||||
|
throw new Error('Please set the config filename');
|
||||||
|
}
|
||||||
|
this.filename = opts.filename;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
FileStorage.prototype.save = function(data) {
|
||||||
|
fs.writeFileSync(this.filename, JSON.stringify(data));
|
||||||
|
};
|
||||||
|
|
||||||
|
FileStorage.prototype.load = function() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(fs.readFileSync(this.filename));
|
||||||
|
} catch (ex) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = FileStorage;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//var client = ;
|
//var client = ;
|
||||||
|
|
||||||
module.exports = require('./API');
|
var client = module.exports = require('./API');
|
||||||
//client.verificator = require('./API');
|
client.FileStorage = require('./FileStorage');
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
Loading…
Reference in New Issue