Skip to content

Commit

Permalink
Merge pull request #2495 from SelfKeyFoundation/release
Browse files Browse the repository at this point in the history
Release 1.8.2
  • Loading branch information
sk91 authored Dec 11, 2020
2 parents bc08822 + f190052 commit f53c266
Show file tree
Hide file tree
Showing 28 changed files with 493 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "selfkey-identity-wallet",
"productName": "SelfKey Identity Wallet",
"version": "1.8.1",
"version": "1.8.2",
"description": "The Official SelfKey Identity Wallet for Desktop",
"browser": [
"chrome"
Expand Down
37 changes: 32 additions & 5 deletions src/common/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Logger } from 'common/logger';
import { featureIsEnabled } from 'common/feature-flags';
import { kycOperations } from '../kyc';
import { schedulerOperations } from '../scheduler';
import { createWalletOperations } from '../create-wallet';

const log = new Logger('app-redux');

Expand Down Expand Up @@ -74,7 +75,9 @@ export const appTypes = {
APP_UNLOCK_WALLET: 'app/unlock/wallet',
APP_SET_KEYSTORE_VALUE: 'app/keystore/SET',
LOAD_KEYSTORE_VALUE: 'app/keystore/LOAD',
APP_SEED_UNLOCK_START: 'app/seed/unlock'
APP_SEED_UNLOCK_START: 'app/seed/unlock',
APP_SEED_GENERATE: 'app/seed/generate',
APP_RESET: 'app/reset'
};

const appActions = {
Expand Down Expand Up @@ -133,6 +136,10 @@ const appActions = {
setKeystoreValue: payload => ({
type: appTypes.APP_SET_KEYSTORE_VALUE,
payload
}),
resetAppAction: payload => ({
type: appTypes.APP_RESET,
payload
})
};

Expand Down Expand Up @@ -181,6 +188,7 @@ const unlockWalletOperation = (wallet, type) => async dispatch => {
throw error;
} finally {
dispatch(appActions.setSeedAction(null));
dispatch(createWalletOperations.setPasswordAction(''));
dispatch(appActions.setHardwareWalletsAction([]));
}
};
Expand Down Expand Up @@ -227,10 +235,10 @@ const unlockWalletWithNewFile = (filePath, password) => async dispatch => {
}
};

const unlockWalletWithPrivateKey = privateKey => async dispatch => {
const unlockWalletWithPrivateKey = (privateKey, password) => async dispatch => {
const walletService = getGlobalContext().walletService;
try {
const wallet = await walletService.unlockWalletWithPrivateKey(privateKey);
const wallet = await walletService.unlockWalletWithPrivateKey(privateKey, password);
await dispatch(appOperations.unlockWalletOperation(wallet, 'privateKey'));
await dispatch(push('/main/dashboard'));
} catch (error) {
Expand Down Expand Up @@ -282,6 +290,13 @@ const unlockWalletWithPublicKey = (address, path) => async (dispatch, getState)
}
};

const generateSeedPhraseOperation = () => async (dispatch, getState) => {
const { walletService } = getGlobalContext();
const seed = walletService.generateSeedPhrase();
await dispatch(appActions.setSeedAction(seed));
return seed;
};

const startSeedUnlockOperation = seed => async dispatch => {
await dispatch(appActions.setSeedAction(seed));
await dispatch(loadHDWalletsOperation());
Expand Down Expand Up @@ -482,7 +497,8 @@ const operations = {
installUpdate,
unlockWalletOperation,
loadKeystoreValue,
startSeedUnlockOperation
startSeedUnlockOperation,
generateSeedPhraseOperation
};

const appOperations = {
Expand Down Expand Up @@ -556,6 +572,10 @@ const appOperations = {
startSeedUnlockOperation: createAliasedAction(
appTypes.APP_SEED_UNLOCK_START,
operations.startSeedUnlockOperation
),
generateSeedPhraseOperation: createAliasedAction(
appTypes.APP_SEED_GENERATE,
operations.generateSeedPhraseOperation
)
};

Expand Down Expand Up @@ -615,6 +635,10 @@ const setSeedReducer = (state, action) => {
return { ...state, seed: action.payload };
};

const appResetReducer = (state, action) => {
return { ...initialState };
};

const appReducers = {
setWalletsReducer,
setWalletsLoadingReducer,
Expand All @@ -629,7 +653,8 @@ const appReducers = {
setAutoUpdateProgressReducer,
setAutoUpdateDownloadedReducer,
setKeystoreValueReducer,
setSeedReducer
setSeedReducer,
appResetReducer
};

const reducer = (state = initialState, action) => {
Expand Down Expand Up @@ -662,6 +687,8 @@ const reducer = (state = initialState, action) => {
return appReducers.setAutoUpdateDownloadedReducer(state, action);
case appTypes.APP_SET_KEYSTORE_VALUE:
return appReducers.setKeystoreValueReducer(state, action);
case appTypes.APP_RESET:
return appReducers.appResetReducer(state, action);
}
return state;
};
Expand Down
12 changes: 6 additions & 6 deletions src/common/wallet-connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ export const operations = {
);
await dispatch(push('/wallet-connect/sign-message'));
},
signMessageDenyOperation: () => async (dispatch, getState) => {
signMessageDenyOperation: error => async (dispatch, getState) => {
try {
const wc = walletConnectSelectors.selectWalletConnect(getState());
const { walletConnectService } = getGlobalContext();
walletConnectService.rejectRequest(wc.requestId);
walletConnectService.rejectRequest(wc.requestId, error);
await dispatch(walletConnectOperations.resetSessionAction());
await dispatch(push('/main/dashboard'));
} catch (error) {
Expand Down Expand Up @@ -151,7 +151,7 @@ export const operations = {
await dispatch(push('/main/hd-error'));
}
}
walletConnectService.rejectRequest(wc.requestId, error);
await dispatch(walletConnectOperations.signMessageDenyOperation(error));
return;
}

Expand Down Expand Up @@ -185,11 +185,11 @@ export const operations = {
);
await dispatch(push('/wallet-connect/transaction'));
},
transactionDenyOperation: () => async (dispatch, getState) => {
transactionDenyOperation: error => async (dispatch, getState) => {
try {
const wc = walletConnectSelectors.selectWalletConnect(getState());
const { walletConnectService } = getGlobalContext();
walletConnectService.rejectRequest(wc.requestId);
walletConnectService.rejectRequest(wc.requestId, error);
await dispatch(walletConnectOperations.resetSessionAction());
await dispatch(push('/main/dashboard'));
} catch (error) {
Expand Down Expand Up @@ -245,7 +245,7 @@ export const operations = {
await dispatch(push('/main/hd-error'));
}
}
walletConnectService.rejectRequest(wc.requestId, error);
await dispatch(walletConnectOperations.transactionDenyOperation(error));
return;
}

Expand Down
19 changes: 15 additions & 4 deletions src/main/blockchain/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ export class Web3Service extends EventEmitter {
return this.getTransactionStatus(tx);
}

async getNextNonce(address, opt) {
opt = opt || {};
let block = 'pending';

if (opt.block) {
block = opt.block;
}

let nonce = await this.web3.eth.getTransactionCount(address, block);
if (nonce <= this.nonce) {
return this.nonce;
}
}

async sendSignedTransaction(contactMethodInstance, contractAdress, args, wallet) {
let opts = { ...(args || [])[0] };
if (!opts.from) {
Expand Down Expand Up @@ -333,10 +347,7 @@ export class Web3Service extends EventEmitter {
});
}
let data = contactMethodInstance.encodeABI();
let nonce = await this.web3.eth.getTransactionCount(opts.from, 'pending');
if (nonce === this.nonce) {
nonce++;
}
let nonce = await this.getNextNonce();
let rawTx = {
nonce: this.web3.utils.toHex(nonce),
to: contractAdress,
Expand Down
7 changes: 4 additions & 3 deletions src/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ if (!gotTheLock) {
} else {
electron.app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (electron.app.win) {
if (electron.app.win.isMinimized()) electron.app.win.restore();
electron.app.win.focus();
const { mainWindow } = getGlobalContext();
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}
Expand Down
46 changes: 35 additions & 11 deletions src/main/wallet-connect/wallet-connect-service.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import WalletConnect from '@walletconnect/client';
import { convertHexToNumber } from '@walletconnect/utils';
import { Logger } from 'common/logger';
import { walletConnectOperations } from '../../common/wallet-connect';
import { Identity } from '../platform/identity';
import { getWallet } from '../../common/wallet/selectors';
import { identitySelectors } from 'common/identity';
import EthUtils from '../../common/utils/eth-utils';
const log = new Logger('WalletConnectService');

export class WalletConnectService {
HANDLER_NAME = 'wallet-connect';

constructor({ config, store }) {
constructor({ config, store, mainWindow, web3Service }) {
this.config = config;
this.store = store;
this.mainWindow = mainWindow;
this.web3Service = web3Service;
}

focusWindow() {
if (this.mainWindow) {
if (this.mainWindow.isMinimized()) this.mainWindow.restore();

let attempts = 0;
let timeout = null;
const refocus = () => {
if ((this.mainWindow.isFocused() || attempts > 5) && timeout) {
clearTimeout(timeout);
timeout = null;
return;
}
this.mainWindow.setFocusable(true);
this.mainWindow.moveTop();
this.mainWindow.focus();
this.mainWindow.flashFrame(true);
attempts++;
timeout = setTimeout(refocus, 1000);
};
refocus();
}
}

async handleUrlCommand(cmd) {
Expand Down Expand Up @@ -40,7 +65,7 @@ export class WalletConnectService {

this.peerMeta = peerMeta;
this.peerId = peerId;

this.focusWindow();
this.store.dispatch(walletConnectOperations.sessionRequestOperation(peerId, peerMeta));
});

Expand Down Expand Up @@ -94,21 +119,20 @@ export class WalletConnectService {

handlePersonalSignRequest({ id, method, params }) {
const [message] = params;

this.focusWindow();
this.store.dispatch(
walletConnectOperations.signMessageOperation(id, this.peerMeta, this.peerId, message)
);
}

handleTransaction({ id, method, params }) {
async handleTransaction({ id, method, params }) {
const rawTx = params[0];

rawTx.nonce = await this.web3Service.getNextNonce(rawTx.from);
const tx = { ...rawTx };
tx.gas = convertHexToNumber(tx.gas);
tx.gasPrice = convertHexToNumber(tx.gasPrice);
tx.nonce = convertHexToNumber(tx.nonce || '');
tx.value = convertHexToNumber(tx.value || '');

tx.gas = EthUtils.hexToDecimal(tx.gas);
tx.gasPrice = EthUtils.hexToDecimal(tx.gasPrice);
if (tx.value) tx.value = EthUtils.hexToDecimal(tx.value);
this.focusWindow();
this.store.dispatch(
walletConnectOperations.transactionOperation(
id,
Expand Down
22 changes: 19 additions & 3 deletions src/main/wallet/wallet-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class WalletService {
return wallet;
}

async updateKeystorePath(id, keystoreFilePath) {
return Wallet.updateKeyStorePath({
id,
keystoreFilePath
});
}

async createWalletWithPassword(password) {
const account = this.web3Service.createAccount(password);
this.web3Service.setDefaultAccount(account);
Expand Down Expand Up @@ -157,7 +164,7 @@ export class WalletService {
return newWallet;
}

async unlockWalletWithPrivateKey(privateKey) {
async unlockWalletWithPrivateKey(privateKey, password) {
if (!privateKey.startsWith('0x')) {
privateKey = Buffer.from(privateKey).toString('hex');
}
Expand All @@ -167,14 +174,19 @@ export class WalletService {
const account = this.web3Service.privateKeyToAccount(privateKey);

this.web3Service.setDefaultAccount(account);

let keystoreFilePath;
let wallet = await Wallet.findByPublicKey(account.address);

if ((!wallet && password) || (wallet && password && !wallet.keystoreFilePath)) {
keystoreFilePath = await this.saveAccountToKeystore(account, password);
}
if (!wallet) {
wallet = await this.createWallet({
address: account.address,
keystoreFilePath,
profile: 'local'
});
} else if (keystoreFilePath) {
wallet = await this.updateKeystorePath(wallet.id, keystoreFilePath);
}
const newWallet = {
...wallet,
Expand Down Expand Up @@ -256,6 +268,10 @@ export class WalletService {
return this._getWallets(page, accountsQuantity, 'trezor');
}

generateSeedPhrase() {
return HDWallet.generateMnemonic();
}

async getHDWalletAccounts(seed, offset, limit) {
const wallet = await HDWallet.createFromMnemonic(seed);
const accounts = wallet.getAccounts(offset, limit);
Expand Down
5 changes: 5 additions & 0 deletions src/main/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ export class Wallet extends BaseModel {
return wallet;
}

static async updateKeyStorePath({ id, keystoreFilePath }) {
let wallet = await this.query().patchAndFetchById(id, { keystoreFilePath });
return wallet;
}

async hasSignedUpTo(websiteUrl) {
let logins = await this.$relatedQuery('loginAttempts')
.where({
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import CreatePassword from './wallet/create/password';
import NoConnection from './no-connection';
import PasswordConfirmation from './wallet/create/password/confirmation-container';
import BackupAddress from './wallet/create/backup-address';
import BackupHDWallet from './wallet/create/backup-hd-container';
import ConfirmHDWallet from './wallet/create/confirm-hd-container';
import BackupPK from './wallet/create/backup-pk';
import Main from './wallet/main';
import Unlock from './wallet/unlock';
Expand Down Expand Up @@ -71,6 +73,8 @@ class AppContainerComponent extends PureComponent {
<Route path="/createPasswordConfirmation" component={PasswordConfirmation} />
<Route path="/backupAddress" component={BackupAddress} />
<Route path="/backupPrivateKey" component={BackupPK} />
<Route path="/backupHDWallet" component={BackupHDWallet} />
<Route path="/confirmHDWallet" component={ConfirmHDWallet} />
<Route path="/main" component={Main} />
<Route path="/unlockWallet" component={Unlock} />
<Route path="/enterTrezorPin" component={EnterPin} />
Expand Down
Loading

0 comments on commit f53c266

Please sign in to comment.