From 0873f7abe813b0856599db2ddaaf15d282f37034 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 19 Aug 2016 20:46:52 -0700 Subject: [PATCH] rpc: importwallet. --- lib/bcoin/http/rpc.js | 46 ++++++++++++++++++++++++++++++++++++++++++- lib/bcoin/wallet.js | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index a5230701..9adc9b2e 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -3110,7 +3110,9 @@ RPC.prototype.importprivkey = function importprivkey(args, callback) { }; RPC.prototype.importwallet = function importwallet(args, callback) { - var file, data; + var self = this; + var file, keys, lines, line, parts; + var i, secret, time, label, addr; if (args.help || args.length !== 1) return callback(new RPCError('importwallet "filename"')); @@ -3121,6 +3123,48 @@ RPC.prototype.importwallet = function importwallet(args, callback) { if (err) return callback(err); + lines = data.split(/\n+/); + keys = []; + + for (i = 0; i < lines.length; i++) { + line = lines[i].trim(); + + if (line.length === 0) + continue; + + if (/^\s*#/.test(line)) + continue; + + parts = line.split(/\s+/); + + if (parts.length < 4) + return callback(new RPCError('Malformed wallet.')); + + try { + secret = bcoin.keyring.fromSecret(parts[0]); + } catch (e) { + return callback(e); + } + + time = +parts[1]; + label = parts[2]; + addr = parts[3]; + + keys.push(secret); + } + + utils.forEachSerial(keys, function(key, next) { + self.wallet.importKey(0, key, null, next); + }, function(err) { + if (err) + return callback(err) + + self.walletdb.rescan(self.chain.db, 0, function(err) { + if (err) + return callback(err); + callback(null, null); + }); + }); }); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index f0c43a4b..deb0953b 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -815,7 +815,7 @@ Wallet.prototype.importKey = function importKey(account, ring, passphrase, callb account = 0; } - callback = this._lockWrite(importKey, [account, ring, callback]); + callback = this._lockWrite(importKey, [account, ring, passphrase, callback]); if (!callback) return;