bit-create & bit-join

This commit is contained in:
Ivan Socolsky 2015-03-01 15:43:37 -03:00
parent 5b5aca9970
commit 6dcd387461
5 changed files with 100 additions and 50 deletions

View File

@ -27,10 +27,14 @@ try {
utils.die(ex);
}
var client = utils.getClient(program);
client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
utils.die(err);
console.log(' * ' + _.capitalize(network) + ' Wallet Created.');
if (secret)
console.log(' - Secret to share:\n\t' + secret);
utils.getClient(program, function (client) {
client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
utils.die(err);
console.log(' * ' + _.capitalize(network) + ' Wallet Created.');
utils.saveClient(program, client, function () {
if (secret) {
console.log(' - Secret to share:\n\t' + secret);
}
});
});
});

View File

@ -17,8 +17,10 @@ if (!args[0])
var secret = args[0];
var copayerName = args[1] || process.env.USER;
var client = utils.getClient(program);
client.joinWallet(secret, copayerName, function(err, xx) {
utils.die(err);
console.log(' * Wallet Joined.', xx || '');
utils.getClient(program, function (client) {
client.joinWallet(secret, copayerName, function(err, wallet) {
utils.die(err);
console.log(' * Wallet Joined.', wallet.name);
utils.saveClient(program, client, function () {});
});
});

View File

@ -1,5 +1,6 @@
var _ = require('lodash');
var Client = require('../lib/client');
var FileStorage = require('./filestorage');
var read = require('read')
var Utils = function() {};
@ -39,56 +40,67 @@ Utils.confirmationId = function(copayer) {
return parseInt(copayer.xPubKeySignature.substr(-4), 16).toString().substr(-4);
}
Utils.getClient = function(args) {
var storage = new Client.FileStorage({
Utils.getClient = function(args, cb) {
var storage = new FileStorage({
filename: args.file || process.env['BIT_FILE'],
});
var c = new Client({
storage: storage,
var client = new Client({
baseUrl: args.host || process.env['BIT_HOST'],
verbose: args.verbose,
});
if (args.nopasswd)
c.setNopasswdAccess(args.nopasswd);
var setPassword;
c.on('needPassword', function(cb) {
if (args.password) {
return cb(args.password);
} else {
if (setPassword)
return cb(setPassword);
read({
prompt: 'Password for ' + args.file + ' : ',
silent: true
}, function(er, password) {
setPassword = password;
return cb(password);
})
storage.load(function(err, walletData) {
if (err && err.code != 'ENOENT') die(err);
if (walletData) {
client.import(walletData);
}
return cb(client);
});
};
c.on('needNewPassword', function(cb) {
if (args.password) {
return cb(args.password);
} else {
read({
prompt: 'New Password: ',
silent: true
}, function(er, password) {
return cb(password);
})
}
Utils.saveClient = function(args, client, cb) {
var storage = new FileStorage({
filename: args.file || process.env['BIT_FILE'],
});
var str = client.export();
storage.save(str, function(err) {
die(err);
return cb();
});
};
// var setPassword;
// c.on('needPassword', function(cb) {
// if (args.password) {
// return cb(args.password);
// } else {
// if (setPassword)
// return cb(setPassword);
// read({
// prompt: 'Password for ' + args.file + ' : ',
// silent: true
// }, function(er, password) {
// setPassword = password;
// return cb(password);
// })
// }
// });
// c.on('needNewPassword', function(cb) {
// if (args.password) {
// return cb(args.password);
// } else {
// read({
// prompt: 'New Password: ',
// silent: true
// }, function(er, password) {
// return cb(password);
// })
// }
// });
return c;
}
Utils.findOneTxProposal = function(txps, id) {
var matches = _.filter(txps, function(tx) {
return _.endsWith(Utils.shortID(tx.id), id);

30
bit-wallet/filestorage.js Normal file
View File

@ -0,0 +1,30 @@
var fs = require('fs')
function FileStorage(opts) {
if (!opts.filename) {
throw new Error('Please set wallet filename');
}
this.filename = opts.filename;
this.fs = opts.fs || fs;
};
FileStorage.prototype.getName = function() {
return this.filename;
};
FileStorage.prototype.save = function(data, cb) {
this.fs.writeFile(this.filename, JSON.stringify(data), cb);
};
FileStorage.prototype.load = function(cb) {
this.fs.readFile(this.filename, 'utf8', function(err, data) {
if (err) return cb(err);
try {
data = JSON.parse(data);
} catch (e) {}
return cb(null, data);
});
};
module.exports = FileStorage;

View File

@ -145,16 +145,18 @@ API.prototype.import = function(str, opts) {
}
}
var credentials;
try {
if (opts.compressed) {
this.credentials = Credentials.importCompressed(input);
credentials = Credentials.importCompressed(input);
// TODO: complete missing fields that live on the server only such as: walletId, walletName, copayerName
} else {
this.credentials = Credentials.fromObj(JSON.parse(input));
credentials = Credentials.fromObj(JSON.parse(input));
}
} catch (ex) {
throw new Error('Error importing from source');
}
this.credentials = credentials;
};
API.prototype.toString = function(password) {