From 4e87dfe5488eeeb313920af18eea9542b426c3a2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 17 Feb 2015 17:51:35 -0300 Subject: [PATCH] add import --- bit-wallet/bit-import | 27 +++++++++++++++++++++++++++ lib/client/api.js | 15 ++++++++++++++- lib/model/copayer.js | 2 +- lib/walletutils.js | 7 +++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100755 bit-wallet/bit-import diff --git a/bit-wallet/bit-import b/bit-wallet/bit-import new file mode 100755 index 0000000..5f0d4e9 --- /dev/null +++ b/bit-wallet/bit-import @@ -0,0 +1,27 @@ +#!/usr/bin/env node +var program = require('commander'); + +var Client = require('../lib/client'); +var utils = require('./cli-utils'); + +var fs = require('fs'); + +program + .version('0.0.1') + .option('-c, --config [file]', 'Wallet config filename') + .option('-v, --verbose', 'be verbose') + .usage('import [options] ') + .parse(process.argv); + +var args = program.args; + +if (!args[0]) + program.help(); + +var client = utils.getClient(program); +var str = fs.readFileSync(args[0]); + +client.import(str, function(err, x) { + utils.die(err); + console.log('Wallet Imported'); +}); diff --git a/lib/client/api.js b/lib/client/api.js index e007378..caafc49 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -308,8 +308,17 @@ API.prototype.export = function(cb) { this._loadAndCheck(function(err, data) { if (err) return cb(err); var v = []; + + var myXPubKey = (new Bitcore.HDPublicKey(data.xPrivKey)).toString(); + _.each(WALLET_CRITICAL_DATA, function(k) { - v.push(data[k]); + var d; + if (k === 'publicKeyRing') { + d = _.without(data[k], myXPubKey); + } else { + d = data[k]; + } + v.push(d); }); return cb(null, JSON.stringify(v)); }); @@ -334,6 +343,10 @@ API.prototype.import = function(str, cb) { return cb('Invalid wallet data'); }); + var xPubKey = (new Bitcore.HDPublicKey(data.xPrivKey)).toString(); + + data.publicKeyRing.push(xPubKey); + data.copayerId = WalletUtils.xpubToCopayerId(xPubKey); data.m = data.publicKeyRing.length; data.signingPrivKey = (new Bitcore.HDPrivateKey(data.xPrivKey)).derive('m/1/0').privateKey.toWIF(); data.network = data.xPrivKey.substr(0,4) === 'tprv' ? 'testnet' : 'livenet'; diff --git a/lib/model/copayer.js b/lib/model/copayer.js index 2c70adc..b9bfea1 100644 --- a/lib/model/copayer.js +++ b/lib/model/copayer.js @@ -8,7 +8,7 @@ var Bitcore = require('bitcore'); var HDPublicKey = Bitcore.HDPublicKey; var Uuid = require('uuid'); var AddressManager = require('./addressmanager'); -var Utils = require('../bitcoinutils'); +var Utils = require('../walletutils'); var VERSION = '1.0.0'; diff --git a/lib/walletutils.js b/lib/walletutils.js index 68de114..9e325fa 100644 --- a/lib/walletutils.js +++ b/lib/walletutils.js @@ -4,6 +4,7 @@ var Address = Bitcore.Address; var PrivateKey = Bitcore.PrivateKey; var PublicKey = Bitcore.PublicKey; var crypto = Bitcore.crypto; +var HDPath = require('./hdpath'); function WalletUtils() {}; @@ -56,4 +57,10 @@ WalletUtils.getProposalHash = function(toAddress, amount, message) { return toAddress + '|' + amount + '|' + (message || ''); }; +WalletUtils.xpubToCopayerId = function(xpub) { + return (new Bitcore.HDPublicKey(xpub)).derive(HDPath.IdBranch).publicKey.toString(); +}; + + + module.exports = WalletUtils;