Skip to content
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
50 changes: 22 additions & 28 deletions manifest/base.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
{
"author": "BlockWallet",
"background": {
"scripts": [
"background.js",
"bw-libs.js"
]
"service_worker": "background.js"
},
"content_scripts": [
{
"js": [
"content.js"
],
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["content.js"],
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": [
"https://block-wallet.github.io/eth-ledger-bridge-keyring/*",
"https://connect.trezor.io/*"
Expand All @@ -23,15 +15,11 @@
"all_frames": true
},
{
"js": [
"vendor/trezor/trezor-content.js"
],
"matches": [
"*://connect.trezor.io/*/popup.html"
]
"js": ["vendor/trezor/trezor-content.js"],
"matches": ["*://connect.trezor.io/*/popup.html"]
}
],
"browser_action": {
"action": {
"default_popup": "popup.html",
"default_title": "BlockWallet"
},
Expand All @@ -42,29 +30,35 @@
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"content_security_policy": "script-src 'self'; object-src 'self'",
"manifest_version": 2,
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"manifest_version": 3,
"name": "BlockWallet",
"permissions": [
"activeTab",
"storage",
"unlimitedStorage",
"notifications",
"https://*.blockwallet.io/*",
"https://*.etherscan.io/*",
"https://*.bscscan.com/*",
"https://*.polygonscan.com/*"
"alarms",
"scripting"
],
"host_permissions": ["file://*/*", "http://*/*", "https://*/*"],
"short_name": "BlockWallet",
"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Alt+O"
}
},
"description": "Start the extension"
}
},
"version": "1.2.14",
"version_name": "1.2.14",
"web_accessible_resources": [
"blankProvider.js"
{
"resources": ["blankProvider.js", "keep-alive"],
"matches": ["<all_urls>"]
}
]
}
}
25 changes: 6 additions & 19 deletions manifest/chrome.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
{
"background": {
"scripts": [
"hot-reload.js",
"background.js",
"bw-libs.js"
]
"service_worker": "background.js"
},
"content_scripts": [
{
"js": [
"content.js"
],
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["content.js"],
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": [
"https://block-wallet.github.io/eth-ledger-bridge-keyring/*",
"https://connect.trezor.io/*"
Expand All @@ -23,12 +14,8 @@
"all_frames": true
},
{
"js": [
"vendor/trezor/trezor-content.js"
],
"matches": [
"*://connect.trezor.io/*/popup.html"
]
"js": ["vendor/trezor/trezor-content.js"],
"matches": ["*://connect.trezor.io/*/popup.html"]
}
]
}
}
8 changes: 4 additions & 4 deletions packages/background/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@block-wallet/background",
"version": "1.1.25",
"version": "1.1.24",
"private": true,
"dependencies": {
"@block-wallet/chains-assets": "https://github.com/block-wallet/chains-assets#v0.0.65",
"@block-wallet/eth-ledger-bridge-keyring": "https://github.com/block-wallet/eth-ledger-bridge-keyring#v0.14.1",
"@block-wallet/eth-trezor-keyring": "https://github.com/block-wallet/eth-trezor-keyring#v1.0.1",
"eth-trezor-keyring": "https://github.com/block-wallet/eth-trezor-keyring#v0.10.2",
"@block-wallet/explorer-link": "https://github.com/block-wallet/explorer-link#v2.2.2",
"@block-wallet/remote-configs": "https://github.com/block-wallet/remote-configs#v1.1.0",
"@metamask/browser-passworder": "https://github.com/block-wallet/browser-passworder#v1.0.1",
"@ethereumjs/tx": "^4.0.2",
"@ethereumjs/util": "^8.0.3",
"@keystonehq/bc-ur-registry-eth": "^0.19.1",
Expand All @@ -16,11 +17,10 @@
"@unstoppabledomains/resolution": "^8.5.0",
"async-mutex": "^0.4.0",
"bip39": "^3.0.4",
"browser-passworder": "^2.0.3",
"compare-versions": "^6.0.0-rc.1",
"eslint-webpack-plugin": "^4.0.0",
"eth-ens-namehash": "^2.0.8",
"eth-keyring-controller": "^7.0.2",
"@metamask/eth-keyring-controller": "^10.0.0",
"ethereumjs-wallet": "^1.0.2",
"ethers": "^5.7.0",
"lodash": "^4.17.21",
Expand Down
105 changes: 76 additions & 29 deletions packages/background/src/controllers/AppStateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export enum AppStateEvents {
APP_UNLOCKED = 'APP_UNLOCKED',
}

export type SessionToken = {
encryptionKey: string;
encryptionSalt: string;
};

export default class AppStateController extends BaseController<AppStateControllerState> {
private _timer: ReturnType<typeof setTimeout> | null;

Expand Down Expand Up @@ -118,19 +123,15 @@ export default class AppStateController extends BaseController<AppStateControlle
public unlock = async (password: string): Promise<void> => {
try {
// Unlock vault
const loginToken = await this._keyringController.submitPassword(
const sessionToken = await this._keyringController.submitPassword(
password
);

if (isManifestV3()) {
// @ts-ignore
browser.storage.session &&
// @ts-ignore
browser.storage.session
.set({ loginToken })
.catch((err: any) => {
log.error('error setting loginToken', err);
});
this._storeSessionToken({
encryptionKey: sessionToken.encryptionKey,
encryptionSalt: sessionToken.encryptionSalt,
});
}

await this._postLoginAction();
Expand All @@ -139,29 +140,75 @@ export default class AppStateController extends BaseController<AppStateControlle
}
};

private _getSessionToken(): Promise<SessionToken | undefined> {
return new Promise((resolve) => {
// @ts-ignore
if (browser.storage.session) {
// @ts-ignore
browser.storage.session
.get(['sessionToken'])
.then(async ({ sessionToken }: { [key: string]: any }) => {
if (!sessionToken) {
resolve(undefined);
}
resolve(sessionToken as SessionToken);
});
} else {
resolve(undefined);
}
});
}

private async _storeSessionToken(
sessionToken: SessionToken
): Promise<SessionToken> {
// @ts-ignore
if (browser.storage.session) {
// @ts-ignore
await browser.storage.session
.set({
sessionToken: {
encryptionKey: sessionToken.encryptionKey,
encryptionSalt: sessionToken.encryptionSalt,
},
})
.catch((err: any) => {
log.error('error setting sessionToken', err);
});
}

return sessionToken;
}

public autoUnlock = async (): Promise<void> => {
if (isManifestV3()) {
const { isAppUnlocked } = this.store.getState();
if (!isAppUnlocked) {
// @ts-ignore
browser.storage.session &&
// @ts-ignore
browser.storage.session
.get(['loginToken'])
.then(
async ({
loginToken,
}: {
[key: string]: string;
}) => {
if (loginToken) {
await (this._keyringController as any)[
'submitEncryptionKey'
](loginToken);
await this._postLoginAction();
}
}
);
if (isAppUnlocked) {
let forceLock = true;
try {
const sessionToken = await this._getSessionToken();
if (sessionToken) {
const newSessionToken =
await this._keyringController.submitEncryptionKey(
sessionToken.encryptionKey,
sessionToken.encryptionSalt
);
await this._storeSessionToken({
encryptionKey: newSessionToken.encryptionKey,
encryptionSalt: newSessionToken.encryptionSalt,
});
await this._postLoginAction();
forceLock = false;
}
} catch (e) {
log.error('Unable to autoUnlock keyring', e);
} finally {
// if we were unable to unlock keyring we should lock the wallet
// the user needs to unlock the keyring and the wallet by his own
if (forceLock) {
this.lock();
}
}
}
}
};
Expand Down
3 changes: 3 additions & 0 deletions packages/background/src/controllers/BlankController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export default class BlankController extends EventEmitter {
private isSetupComplete: boolean;

constructor(props: BlankControllerProps) {
console.log('blank controller constructor');
super();

const initState = props.initState;
Expand Down Expand Up @@ -570,6 +571,8 @@ export default class BlankController extends EventEmitter {

// mv3 auto unlock
this.appStateController.autoUnlock();

console.log('blank controller constructor finished');
}

/**
Expand Down
34 changes: 24 additions & 10 deletions packages/background/src/controllers/KeyringControllerDerivated.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import KeyringController, {
import {
KeyringController,
keyringBuilderFactory,
KeyringControllerProps,
KeyringControllerState,
} from 'eth-keyring-controller';
} from '@metamask/eth-keyring-controller';
import * as customEncryptor from '@metamask/browser-passworder';
import { Hash, Hasheable } from '../utils/hasher';
import { Mutex } from 'async-mutex';
import LedgerBridgeKeyring from '@block-wallet/eth-ledger-bridge-keyring';
import { TrezorKeyring } from '@block-wallet/eth-trezor-keyring';
import TrezorKeyring from 'eth-trezor-keyring';
import { Devices } from '../utils/types/hardware';
import log from 'loglevel';
import { HDPaths, BIP44_PATH } from '../utils/types/hardware';
import { isManifestV3 } from '../utils/manifest';
import {
AccessListEIP2930Transaction,
Transaction,
TypedTransaction,
FeeMarketEIP1559Transaction,
} from '@ethereumjs/tx';
import { concatSig, SignTypedDataVersion } from '@metamask/eth-sig-util';
import {
Expand All @@ -28,7 +33,6 @@ import {
import rlp from 'rlp';
import { v4 } from 'uuid';
import { SignatureData } from './transactions/utils/types';
import { FeeMarketEIP1559Transaction } from '@ethereumjs/tx';
import {
arrToBufArr,
bigIntToBuffer,
Expand Down Expand Up @@ -65,7 +69,13 @@ export default class KeyringControllerDerivated extends KeyringController {
private readonly _qrHardwareKeyring: QRHardwareKeyring;

constructor(opts: KeyringControllerProps) {
opts.keyringTypes = [LedgerBridgeKeyring, TrezorKeyring, QRKeyring];
opts.keyringBuilders = [
keyringBuilderFactory(LedgerBridgeKeyring),
keyringBuilderFactory(TrezorKeyring),
keyringBuilderFactory(QRKeyring),
];
opts.cacheEncryptionKey = isManifestV3();
opts.encryptor = customEncryptor;
super(opts);

this._mutex = new Mutex();
Expand Down Expand Up @@ -198,7 +208,6 @@ export default class KeyringControllerDerivated extends KeyringController {
)[0];
const serialized = await primaryKeyring.serialize();
const seedPhrase = hexToString(bufferToHex(serialized.mnemonic));

return seedPhrase;
}

Expand Down Expand Up @@ -294,15 +303,20 @@ export default class KeyringControllerDerivated extends KeyringController {

// Generate a new keyring
const keyringController = new KeyringController({});
const Keyring = keyringController.getKeyringClassForType(
KeyringTypes.HD_KEY_TREE
);

const opts = {
mnemonic: seedPhrase,
numberOfAccounts: createdAccounts.length,
};

const keyring = new Keyring(opts);
const keyring = await keyringController._newKeyring(
KeyringTypes.HD_KEY_TREE,
opts
);
if (!keyring) {
throw new Error('Unable to generate keyring of type HD_KEY_TREE');
}

const restoredAccounts = await keyring.getAccounts();

if (restoredAccounts.length !== createdAccounts.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyringControllerState } from 'eth-keyring-controller';
import { KeyringControllerState } from '@metamask/eth-keyring-controller';
import { BaseController } from '../infrastructure/BaseController';
import KeyringControllerDerivated from './KeyringControllerDerivated';

Expand Down
Loading