Browse Source

bit-create & bit-join

activeAddress
Ivan Socolsky 10 years ago
parent
commit
6dcd387461
  1. 16
      bit-wallet/bit-create
  2. 10
      bit-wallet/bit-join
  3. 86
      bit-wallet/cli-utils.js
  4. 30
      bit-wallet/filestorage.js
  5. 6
      lib/client/api.js

16
bit-wallet/bit-create

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

10
bit-wallet/bit-join

@ -17,8 +17,10 @@ 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 client = utils.getClient(program); utils.getClient(program, function (client) {
client.joinWallet(secret, copayerName, function(err, xx) { client.joinWallet(secret, copayerName, function(err, wallet) {
utils.die(err); utils.die(err);
console.log(' * Wallet Joined.', xx || ''); console.log(' * Wallet Joined.', wallet.name);
utils.saveClient(program, client, function () {});
});
}); });

86
bit-wallet/cli-utils.js

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

30
bit-wallet/filestorage.js

@ -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;

6
lib/client/api.js

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

Loading…
Cancel
Save