Skip to content

Commit

Permalink
view-only wallets throw on get seed, language, or spend key
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jul 24, 2023
1 parent b27f8bf commit 3df333b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
32 changes: 26 additions & 6 deletions src/main/cpp/monero_wasm_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ string monero_wasm_bridge::get_version(int handle) {
}

string monero_wasm_bridge::get_seed(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
try {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_seed();
} catch (exception& e) {
return string("error: ") + string(e.what());
Expand All @@ -291,27 +291,47 @@ string monero_wasm_bridge::get_seed(int handle) {

string monero_wasm_bridge::get_seed_language(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_seed_language();
try {
return wallet->get_seed_language();
} catch (exception& e) {
return string("error: ") + string(e.what());
}
}

string monero_wasm_bridge::get_public_view_key(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_public_view_key();
try {
return wallet->get_public_view_key();
} catch (exception& e) {
return string("error: ") + string(e.what());
}
}

string monero_wasm_bridge::get_private_view_key(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_private_view_key();
try {
return wallet->get_private_view_key();
} catch (exception& e) {
return string("error: ") + string(e.what());
}
}

string monero_wasm_bridge::get_public_spend_key(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_public_spend_key();
try {
return wallet->get_public_spend_key();
} catch (exception& e) {
return string("error: ") + string(e.what());
}
}

string monero_wasm_bridge::get_private_spend_key(int handle) {
monero_wallet* wallet = (monero_wallet*) handle;
return wallet->get_private_spend_key();
try {
return wallet->get_private_spend_key();
} catch (exception& e) {
return string("error: ") + string(e.what());
}
}

string monero_wasm_bridge::get_address(int handle, const uint32_t account_idx, const uint32_t subaddress_idx) {
Expand Down
35 changes: 24 additions & 11 deletions src/main/js/wallet/MoneroWalletKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,52 +209,65 @@ class MoneroWalletKeys extends MoneroWallet {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
let seed = that._module.get_seed(that._cppAddress);
let resp = that._module.get_seed(that._cppAddress);
const errorStr = "error: ";
if (seed.indexOf(errorStr) === 0) throw new MoneroError(seed.substring(errorStr.length));
return seed ? seed : undefined;
if (resp.indexOf(errorStr) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

async getSeedLanguage() {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
let seedLanguage = that._module.get_seed_language(that._cppAddress);
return seedLanguage ? seedLanguage : undefined;
let resp = that._module.get_seed_language(that._cppAddress);
let errorKey = "error: ";
if (resp.indexOf(errorKey) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

async getPrivateSpendKey() {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
let privateSpendKey = that._module.get_private_spend_key(that._cppAddress);
return privateSpendKey ? privateSpendKey : undefined;
let resp = that._module.get_private_spend_key(that._cppAddress);
let errorKey = "error: ";
if (resp.indexOf(errorKey) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

async getPrivateViewKey() {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
return that._module.get_private_view_key(that._cppAddress);
let resp = that._module.get_private_view_key(that._cppAddress);
let errorKey = "error: ";
if (resp.indexOf(errorKey) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

async getPublicViewKey() {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
return that._module.get_public_view_key(that._cppAddress);
let resp = that._module.get_public_view_key(that._cppAddress);
let errorKey = "error: ";
if (resp.indexOf(errorKey) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

async getPublicSpendKey() {
let that = this;
return that._module.queueTask(async function() {
that._assertNotClosed();
return that._module.get_public_spend_key(that._cppAddress);
let resp = that._module.get_public_spend_key(that._cppAddress);
let errorKey = "error: ";
if (resp.indexOf(errorKey) === 0) throw new MoneroError(resp.substring(errorStr.length));
return resp ? resp : undefined;
});
}

Expand Down
20 changes: 4 additions & 16 deletions src/main/js/wallet/MoneroWalletRpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,8 @@ class MoneroWalletRpc extends MoneroWallet {
}

async getSeed() {
try {
let resp = await this.rpc.sendJsonRequest("query_key", { key_type: "mnemonic" });
return resp.result.key;
} catch (e) {
if (e.getCode() === -29) return undefined; // wallet is view-only
throw e;
}
let resp = await this.rpc.sendJsonRequest("query_key", { key_type: "mnemonic" });
return resp.result.key;
}

async getSeedLanguage() {
Expand All @@ -512,15 +507,8 @@ class MoneroWalletRpc extends MoneroWallet {
}

async getPrivateSpendKey() {

// get private spend key which will throw error if wallet is view-only
try {
let resp = await this.rpc.sendJsonRequest("query_key", { key_type: "spend_key" });
return resp.result.key;
} catch (e) {
if (e.getCode() === -29 && e.message.indexOf("watch-only") !== -1) return undefined; // return undefined if wallet is view-only
throw e;
}
let resp = await this.rpc.sendJsonRequest("query_key", { key_type: "spend_key" });
return resp.result.key;
}

async getAddress(accountIdx, subaddressIdx) {
Expand Down
23 changes: 19 additions & 4 deletions src/test/TestMoneroWalletCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5041,12 +5041,27 @@ class TestMoneroWalletCommon {
// test and sync view-only wallet
assert.equal(await viewOnlyWallet.getPrimaryAddress(), primaryAddress);
assert.equal(await viewOnlyWallet.getPrivateViewKey(), privateViewKey);
assert.equal(await viewOnlyWallet.getPrivateSpendKey(), undefined);
assert.equal(await viewOnlyWallet.getSeed(), undefined);
assert.equal(await viewOnlyWallet.getSeedLanguage(), undefined);
assert(await viewOnlyWallet.isViewOnly());
let errMsg = "Should have failed";
try {
await await viewOnlyWallet.getSeed();
throw new Error(errMsg);
} catch (e) {
if (e.message === errMsg) throw e;
}
try {
await await viewOnlyWallet.getSeedLanguage();
throw new Error(errMsg);
} catch (e) {
if (e.message === errMsg) throw e;
}
try {
await await viewOnlyWallet.getPrivateSpendKey();
throw new Error(errMsg);
} catch (e) {
if (e.message === errMsg) throw e;
}
assert(await viewOnlyWallet.isConnectedToDaemon(), "Wallet created from keys is not connected to authenticated daemon"); // TODO
assert.equal(await viewOnlyWallet.getSeed(), undefined);
await viewOnlyWallet.sync();
assert((await viewOnlyWallet.getTxs()).length > 0);

Expand Down

0 comments on commit 3df333b

Please sign in to comment.