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

Add multi wallet support #233

Merged
merged 4 commits into from
Oct 19, 2020
Merged
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
1 change: 1 addition & 0 deletions app/background/wallet/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export const clientStub = ipcRendererInjector => makeClient(ipcRendererInjector,
'zap',
'importName',
'rpcGetWalletInfo',
'listWallets'
]);
69 changes: 38 additions & 31 deletions app/background/wallet/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const Covenant = require('hsd/lib/primitives/covenant');

// const walletHeightKey = 'wallet:lastSyncHeight';

const WALLET_ID = 'allison';
const randomAddrs = {
[NETWORKS.TESTNET]: 'ts1qfcljt5ylsa9rcyvppvl8k8gjnpeh079drfrmzq',
[NETWORKS.REGTEST]: 'rs1qh57neh8npuxeyxfsl35373vshs0d40cvxx57aj',
Expand All @@ -46,6 +45,11 @@ class WalletService {
this.nodeService = nodeService;
}

setWallet = (name) => {
this.didSelectWallet = false;
this.name = name;
};

reset = async () => {
await this._ensureClient();

Expand All @@ -68,7 +72,7 @@ class WalletService {
this.network,
this.apiKey,
);

this.setName(null);
return true;
} catch (e) {
console.error(e);
Expand All @@ -83,33 +87,31 @@ class WalletService {

getWalletInfo = async () => {
await this._ensureClient();
return this.client.getInfo(WALLET_ID);
return this.client.getInfo(this.name);
};


getAccountInfo = async () => {
await this._ensureClient();
return this.client.getAccount(WALLET_ID, 'default');
return this.client.getAccount(this.name, 'default');
};

getCoin = async (hash, index) => {
await this._ensureClient();
return this.client.getCoin(WALLET_ID, hash, index);
return this.client.getCoin(this.name, hash, index);
};

getNames = async () => {
await this._selectWallet();
return this.client.execute('getnames');
};

createNewWallet = async (passphraseOrXPub, isLedger) => {
createNewWallet = async (name, passphraseOrXPub, isLedger) => {
await this._ensureClient();

await this.reset();
this.didSelectWallet = false;
this.setWallet(name);

if (isLedger) {
return this.client.createWallet(WALLET_ID, {
return this.client.createWallet(name, {
watchOnly: true,
accountKey: passphraseOrXPub,
});
Expand All @@ -122,7 +124,7 @@ class WalletService {
watchOnly: false,
mnemonic: mnemonic.getPhrase(),
};
return this.client.createWallet(WALLET_ID, options);
return this.client.createWallet(name, options);
};

checkRescanStatus = async () => {
Expand Down Expand Up @@ -189,25 +191,24 @@ class WalletService {
});
};

importSeed = async (passphrase, mnemonic) => {
importSeed = async (name, passphrase, mnemonic) => {
await this._ensureClient();

await this.reset();
this.setWallet(name);
this.didSelectWallet = false;
const options = {
passphrase,
// hsd generates different keys for
// menmonics with trailing whitespace
mnemonic: mnemonic.trim(),
};
const res = await this.client.createWallet(WALLET_ID, options);
const res = await this.client.createWallet(this.name, options);
this.rescan(0);
return res;
};

generateReceivingAddress = async () => {
await this._ensureClient();
return this.client.createAddress(WALLET_ID, 'default');
return this.client.createAddress(this.name, 'default');
};

getAuctionInfo = async (name) => {
Expand All @@ -216,12 +217,12 @@ class WalletService {

getTransactionHistory = async () => {
await this._ensureClient();
return this.client.getHistory(WALLET_ID, 'default');
return this.client.getHistory(this.name, 'default');
};

getPendingTransactions = async () => {
await this._ensureClient();
return this.client.getPending(WALLET_ID, 'default');
return this.client.getPending(this.name, 'default');
};

getBids = async () => {
Expand All @@ -230,12 +231,12 @@ class WalletService {

getMasterHDKey = () => this._ledgerDisabled(
'cannot get HD key for watch-only wallet',
() => this.client.getMaster(WALLET_ID),
() => this.client.getMaster(this.name),
);

setPassphrase = (newPass) => this._ledgerDisabled(
'cannot set passphrase for watch-only wallet',
() => this.client.setPassphrase(WALLET_ID, newPass),
() => this.client.setPassphrase(this.name, newPass),
);

revealSeed = (passphrase) => this._ledgerDisabled(
Expand Down Expand Up @@ -268,7 +269,7 @@ class WalletService {
estimateTxFee = async (to, amount, feeRate, subtractFee = false) => {
await this._ensureClient();
const feeRateBaseUnits = Number(toBaseUnits(feeRate));
const createdTx = await this.client.createTX(WALLET_ID, {
const createdTx = await this.client.createTX(this.name, {
rate: feeRateBaseUnits,
outputs: [{
value: Number(toBaseUnits(amount)),
Expand Down Expand Up @@ -356,7 +357,7 @@ class WalletService {

send = (to, amount, fee) => this._ledgerProxy(
() => this._executeRPC('createsendtoaddress', [to, Number(amount), '', '', false, 'default']),
() => this.client.send(WALLET_ID, {
() => this.client.send(this.name, {
rate: Number(toBaseUnits(fee)),
outputs: [{
value: Number(toBaseUnits(amount)),
Expand All @@ -367,19 +368,22 @@ class WalletService {

lock = () => this._ledgerProxy(
() => null,
() => this.client.lock(WALLET_ID),
() => this.client.lock(this.name),
);

unlock = (passphrase) => this._ledgerProxy(
() => null,
() => this.client.unlock(WALLET_ID, passphrase),
);
unlock = (name, passphrase) => {
this.setWallet(name);
return this._ledgerProxy(
() => null,
() => this.client.unlock(this.name, passphrase),
);
};

isLocked = () => this._ledgerProxy(
() => false,
async () => {
try {
const info = await this.client.getInfo(WALLET_ID);
const info = await this.client.getInfo(this.name);
return info === null || info.master.until === 0;
} catch (e) {
console.error(e);
Expand All @@ -390,7 +394,7 @@ class WalletService {

getNonce = async (options) => {
await this._ensureClient();
return this.client.getNonce(WALLET_ID, options.name, options);
return this.client.getNonce(this.name, options.name, options);
};

importNonce = async (options) => {
Expand All @@ -399,7 +403,7 @@ class WalletService {

zap = async () => {
await this._ensureClient();
return this.client.zap(WALLET_ID, 'default', 1);
return this.client.zap(this.name, 'default', 1);
};

importName = (name, start) => {
Expand Down Expand Up @@ -525,6 +529,8 @@ class WalletService {
throw new Error('Transaction never appeared in the mempool.');
};

listWallets = () => this.client.getWallets();

_onNodeStart = async (networkName, network, apiKey) => {
const conn = await getConnection();

Expand Down Expand Up @@ -606,7 +612,7 @@ class WalletService {
return this.pendingSelection;
}

this.pendingSelection = this.client.execute('selectwallet', [WALLET_ID]);
this.pendingSelection = this.client.execute('selectwallet', [this.name]);
await this.pendingSelection;
this.pendingSelection = null;
this.didSelectWallet = true;
Expand Down Expand Up @@ -685,6 +691,7 @@ const methods = {
zap: service.zap,
importName: service.importName,
rpcGetWalletInfo: service.rpcGetWalletInfo,
listWallets: service.listWallets,
};

export async function start(server) {
Expand Down
3 changes: 2 additions & 1 deletion app/ducks/node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clientStub } from '../background/node/client';
import { clientStub as connClientStub } from '../background/connections/client';
import { getNetwork, setNetwork, getInitializationState } from '../db/system';
import { fetchWallet, fetchTransactions } from './walletActions';
import { fetchWallet, fetchTransactions, listWallets } from './walletActions';
import { getWatching } from './watching';
import * as logger from '../utils/logClient';
import { onNewBlock } from './backgroundMonitor';
Expand Down Expand Up @@ -109,6 +109,7 @@ export const start = (network) => async (dispatch, getState) => {
},
});
await dispatch(setNodeInfo());
await dispatch(listWallets());
await dispatch(fetchWallet());
if (await getInitializationState(network)) {
setTimeout(async () => {
Expand Down
25 changes: 18 additions & 7 deletions app/ducks/walletActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
STOP_SYNC_WALLET,
SYNC_WALLET_PROGRESS,
UNLOCK_WALLET,
SET_API_KEY, SET_FETCHING,
SET_API_KEY,
SET_FETCHING,
SET_WALLETS,
} from './walletReducer';
import { NEW_BLOCK_STATUS } from './nodeReducer';

Expand All @@ -43,9 +45,9 @@ export const setWallet = opts => {
};
};

export const completeInitialization = (passphrase) => async (dispatch, getState) => {
export const completeInitialization = (name, passphrase) => async (dispatch, getState) => {
const network = getState().node.network;
await walletClient.unlock(passphrase);
await walletClient.unlock(name, passphrase);
await setInitializationState(network, true);
await dispatch(fetchWallet());
dispatch({
Expand Down Expand Up @@ -94,8 +96,8 @@ export const revealSeed = (passphrase) => async () => {
return walletClient.revealSeed(passphrase);
};

export const unlockWallet = passphrase => async (dispatch) => {
await walletClient.unlock(passphrase);
export const unlockWallet = (name, passphrase) => async (dispatch) => {
await walletClient.unlock(name, passphrase);
dispatch({
type: UNLOCK_WALLET,
});
Expand All @@ -108,7 +110,7 @@ export const lockWallet = () => async (dispatch) => {
});
};

export const removeWallet = () => async (dispatch, getState) => {
export const reset = () => async (dispatch, getState) => {
const network = getState().node.network;
await walletClient.reset();
await setInitializationState(network, false);
Expand Down Expand Up @@ -265,6 +267,15 @@ export const watchActivity = () => dispatch => {
}
};

export const listWallets = () => async (dispatch) => {
const wallets = await walletClient.listWallets();

dispatch({
type: SET_WALLETS,
payload: wallets,
});
};

async function parseInputsOutputs(net, tx) {
// Look for covenants. A TX with multiple covenant types is not supported
let covAction = null;
Expand Down Expand Up @@ -353,7 +364,7 @@ async function parseInputsOutputs(net, tx) {
type: 'UNKNOWN',
meta: {},
fee: tx.fee,
value: 0
value: 0,
};
}

Expand Down
13 changes: 10 additions & 3 deletions app/ducks/walletReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const SYNC_WALLET_PROGRESS = 'app/wallet/syncWalletProgress';
export const GET_PASSPHRASE = 'app/wallet/getPassphrase';
export const SET_API_KEY = 'app/wallet/setApiKey';
export const SET_FETCHING = 'app/wallet/setFetching';
export const SET_WALLETS = 'app/wallet/setWallets';

export function getInitialState() {
return {
Expand All @@ -37,6 +38,7 @@ export function getInitialState() {
walletSync: false,
walletSyncProgress: 0,
getPassphrase: {get: false},
wallets: [],
};
}

Expand Down Expand Up @@ -66,17 +68,17 @@ export default function walletReducer(state = getInitialState(), {type, payload}
case LOCK_WALLET:
return {
...state,
isLocked: true
isLocked: true,
};
case UNLOCK_WALLET:
return {
...state,
isLocked: false
isLocked: false,
};
case SET_TRANSACTIONS:
return {
...state,
transactions: payload
transactions: payload,
};
case INCREMENT_IDLE:
return {
Expand Down Expand Up @@ -113,6 +115,11 @@ export default function walletReducer(state = getInitialState(), {type, payload}
...state,
isFetching: payload,
};
case SET_WALLETS:
return {
...state,
wallets: payload,
};
default:
return state;
}
Expand Down
Loading