fix fRescan check for callback.

This commit is contained in:
Christopher Jeffrey 2014-11-11 11:53:03 -08:00
parent 317ff8a6bd
commit 82859274d6
1 changed files with 56 additions and 12 deletions

View File

@ -4657,6 +4657,9 @@ NAN_METHOD(WalletImportKey) {
async_import_key_data *data = new async_import_key_data(); async_import_key_data *data = new async_import_key_data();
data->err_msg = std::string("");
data->fRescan = false;
Local<Object> options = Local<Object>::Cast(args[0]); Local<Object> options = Local<Object>::Cast(args[0]);
Local<Function> callback; Local<Function> callback;
@ -4686,13 +4689,15 @@ NAN_METHOD(WalletImportKey) {
strAccount = std::string(*label_); strAccount = std::string(*label_);
} }
if (strAccount == EMPTY) { rescan:
return NanThrowError("No account name provided."); if (data->fRescan) {
} uv_work_t *req = new uv_work_t();
req->data = data;
// Call to: EnsureWalletIsUnlocked() int status = uv_queue_work(uv_default_loop(),
if (pwalletMain->IsLocked()) { req, async_import_key,
return NanThrowError("Please enter the wallet passphrase with walletpassphrase first."); (uv_after_work_cb)async_import_key_after);
assert(status == 0);
NanReturnValue(Undefined());
} }
// Whether to perform rescan after import // Whether to perform rescan after import
@ -4700,17 +4705,46 @@ NAN_METHOD(WalletImportKey) {
? true ? true
: false; : false;
if (strAccount == EMPTY) {
if (data->fRescan) {
data->err_msg = std::string("No account name provided.");
goto rescan;
} else {
return NanThrowError("No account name provided.");
}
}
// Call to: EnsureWalletIsUnlocked()
if (pwalletMain->IsLocked()) {
if (data->fRescan) {
data->err_msg = std::string("Please enter the wallet passphrase with walletpassphrase first.");
goto rescan;
} else {
return NanThrowError("Please enter the wallet passphrase with walletpassphrase first.");
}
}
CBitcoinSecret vchSecret; CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret); bool fGood = vchSecret.SetString(strSecret);
if (!fGood) { if (!fGood) {
if (data->fRescan) {
data->err_msg = std::string("Invalid private key encoding");
goto rescan;
} else {
return NanThrowError("Invalid private key encoding"); return NanThrowError("Invalid private key encoding");
} }
}
CKey key = vchSecret.GetKey(); CKey key = vchSecret.GetKey();
if (!key.IsValid()) { if (!key.IsValid()) {
if (data->fRescan) {
data->err_msg = std::string("Private key outside allowed range");
goto rescan;
} else {
return NanThrowError("Private key outside allowed range"); return NanThrowError("Private key outside allowed range");
} }
}
CPubKey pubkey = key.GetPubKey(); CPubKey pubkey = key.GetPubKey();
CKeyID vchAddress = pubkey.GetID(); CKeyID vchAddress = pubkey.GetID();
@ -4728,8 +4762,13 @@ NAN_METHOD(WalletImportKey) {
pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1; pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;
if (!pwalletMain->AddKeyPubKey(key, pubkey)) { if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
if (data->fRescan) {
data->err_msg = std::string("Error adding key to wallet");
goto rescan;
} else {
return NanThrowError("Error adding key to wallet"); return NanThrowError("Error adding key to wallet");
} }
}
// whenever a key is imported, we need to scan the whole chain // whenever a key is imported, we need to scan the whole chain
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
@ -4752,9 +4791,14 @@ NAN_METHOD(WalletImportKey) {
static void static void
async_import_key(uv_work_t *req) { async_import_key(uv_work_t *req) {
async_import_key_data* data = static_cast<async_import_key_data*>(req->data); async_import_key_data* data = static_cast<async_import_key_data*>(req->data);
if (data->err_msg != "") {
return;
}
if (data->fRescan) {
// This may take a long time, do it on the libuv thread pool: // This may take a long time, do it on the libuv thread pool:
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
} }
}
static void static void
async_import_key_after(uv_work_t *req) { async_import_key_after(uv_work_t *req) {