Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deglobalize wallet.isHardwareWallet #348

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { ref } from 'vue';
import { strCurrency } from '../settings.js';
import { cMarket } from '../settings.js';
import { ledgerSignTransaction } from '../ledger.js';

Check warning on line 6 in scripts/composables/use_wallet.js

View workflow job for this annotation

GitHub Actions / Run linters

'ledgerSignTransaction' is defined but never used
import { defineStore } from 'pinia';

/**
Expand Down Expand Up @@ -86,11 +86,7 @@
);
const createAndSendTransaction = async (network, address, value, opts) => {
const tx = wallet.createTransaction(address, value, opts);
if (wallet.isHardwareWallet()) {
await ledgerSignTransaction(wallet, tx);
} else {
await wallet.sign(tx);
}
await wallet.sign(tx);
const res = await network.sendTransaction(tx.serialize());
if (res) {
await wallet.addTransaction(tx);
Expand Down
7 changes: 4 additions & 3 deletions scripts/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ async function encryptWallet(password, currentPassword = '') {

async function restoreWallet(strReason) {
if (!wallet.isEncrypted) return false;
if (wallet.isHardwareWallet) return true;
showRestoreWallet.value = true;
return await new Promise((res) => {
watch(
Expand Down Expand Up @@ -206,7 +205,7 @@ function lockWallet() {
*/
async function send(address, amount, useShieldInputs) {
// Ensure a wallet is unlocked
if (wallet.isViewOnly && !wallet.isHardwareWallet) {
if (wallet.isViewOnly) {
if (
!(await restoreWallet(
tr(ALERTS.WALLET_UNLOCK_IMPORT, [
Expand Down Expand Up @@ -516,7 +515,9 @@ defineExpose({
<div
class="col-12"
v-if="
!wallet.isViewOnly && !needsToEncrypt && wallet.isImported
!wallet.isViewOnly &&
wallet.isEncrypted &&
wallet.isImported
"
>
<center>
Expand Down
2 changes: 1 addition & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export {
switchSettings,
govVote,
} from './global.js';
export { wallet, getNewAddress } from './wallet.js';
export { wallet } from './wallet.js';
export {
logOut,
toggleTestnet,
Expand Down
8 changes: 2 additions & 6 deletions scripts/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from './misc.js';
import { Database } from './database.js';
import { getNetwork } from './network.js';
import { ledgerSignTransaction } from './ledger.js';

/**
* @deprecated use the new wallet method instead
Expand All @@ -37,10 +36,7 @@ export async function createAndSendTransaction({
changeAddress,
returnAddress: delegationOwnerAddress,
});
if (!wallet.isHardwareWallet()) await wallet.sign(tx);
else {
await ledgerSignTransaction(wallet, tx);
}
await wallet.sign(tx);
const res = await getNetwork().sendTransaction(tx.serialize());
if (res) {
await wallet.addTransaction(tx);
Expand All @@ -63,7 +59,7 @@ export async function createMasternode() {

// Generate the Masternode collateral
const [address] = await getNewAddress({
verify: wallet.isHardwareWallet(),
verify: true,
nReceiving: 1,
});
const result = await createAndSendTransaction({
Expand Down
2 changes: 1 addition & 1 deletion scripts/promos.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export async function updatePromoCreationTick(fRecursive = false) {
}

// Send the fill transaction if unlocked
if (!wallet.isViewOnly() || wallet.isHardwareWallet()) {
if (!wallet.isViewOnly()) {
const res = await createAndSendTransaction({
address: strAddress,
amount: Math.round(cThread.amount * COIN + PROMO_FEE),
Expand Down
1 change: 0 additions & 1 deletion scripts/stake/Stake.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ async function unstake(value) {

async function restoreWallet(strReason) {
if (!wallet.isEncrypted) return false;
if (wallet.isHardwareWallet) return true;
showRestoreWallet.value = true;
return await new Promise((res) => {
watch(
Expand Down
34 changes: 8 additions & 26 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RECEIVE_TYPES } from './contacts-book.js';
import { Account } from './accounts.js';
import { fAdvancedMode } from './settings.js';
import { bytesToHex, hexToBytes, sleep, startBatch } from './utils.js';
import { strHardwareName } from './ledger.js';
import { ledgerSignTransaction, strHardwareName } from './ledger.js';
import { OutpointState, Mempool } from './mempool.js';
import { getEventEmitter } from './event_bus.js';

Expand Down Expand Up @@ -178,38 +178,14 @@ export class Wallet {

isViewOnly() {
if (!this.#masterKey) return false;
return this.#masterKey.isViewOnly;
return this.#masterKey.isViewOnly && !this.isHardwareWallet();
}

isHD() {
if (!this.#masterKey) return false;
return this.#masterKey.isHD;
}

async hasWalletUnlocked(fIncludeNetwork = false) {
if (fIncludeNetwork && !getNetwork().enabled)
return createAlert(
'warning',
ALERTS.WALLET_OFFLINE_AUTOMATIC,
5500
);
if (!this.isLoaded()) {
return createAlert(
'warning',
tr(ALERTS.WALLET_UNLOCK_IMPORT, [
{
unlock: (await hasEncryptedWallet())
? 'unlock '
: 'import/create',
},
]),
3500
);
} else {
return true;
}
}

/**
* Set or replace the active Master Key with a new Master Key
* @param {import('./masterkey.js').MasterKey} mk - The new Master Key to set active
Expand Down Expand Up @@ -946,6 +922,9 @@ export class Wallet {
if (balance < value) {
throw new Error('Not enough balance');
}
if (isDelegation && this.isHardwareWallet()) {
throw new Error('Ledger does not support cold staking yet');
}
if (delegateChange && !changeDelegationAddress)
throw new Error(
'`delegateChange` was set to true, but no `changeDelegationAddress` was provided.'
Expand Down Expand Up @@ -1081,6 +1060,9 @@ export class Wallet {
if (this.isViewOnly()) {
throw new Error('Cannot sign with a view only wallet');
}
if (this.isHardwareWallet()) {
return await ledgerSignTransaction(this, transaction);
}
if (!transaction.vin.length || transaction.shieldOutput[0]) {
// TODO: separate signing and building process for shield?
return await this.#signShield(transaction);
Expand Down