1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/platforms/extension.js
Dan J Miller 904dad256f Connect Ledger via WebHID (#12411)
* Connect ledger via webhid if that option is available

* Explicitly setting preference for webhid

* Use ledgerTransportType enum instead of booleans for ledger live and webhid preferences

* Use single setLEdgerTransport preference methods and property

* Temp

* Lint fix

* Unit test fix

* Remove async keyword from setLedgerTransportPreference function definition in preferences controller

* Fix ledgelive setting toggle logic

* Migrate useLedgerLive preference property to ledgerTransportType

* Use shared constants for ledger transport type enums

* Use constant for ledger usb vendor id

* Use correct property to check if ledgerLive preference is set when deciding whether to ask for webhid connection

* Update eth-ledger-bridge-keyring to v0.9.0

* Only show ledger live transaction helper messages if using ledger live

* Only show ledger live part of tutorial if ledger live setting is on

* Fix ledger related prop type errors

* Explicitly use u2f enum instead of empty string as a transport type; default transport type to webhid if available; use constants for u2f and webhid

* Cleanup

* Wrap ledger webhid device request in try/catch

* Clean up

* Lint fix

* Ensure user can easily connect their ledger wallet when they need to.

* Fix locales

* Fix/improve locales changes

* Remove unused isFirefox property from confirm-transaction-base.container.js

* Disable transaction and message signing confirmation if ledger webhid requires connection

* Ensure translation keys for ledger connection options in settings dropdown can be properly detected by verify-locales

* Drop .component from ledger-instruction-field file name

* Move renderLedgerLiveStep to module scope

* Remove ledgerLive from function and message names in ledger-instruction-field

* Wrap ledger connection logic in ledger-instruction-field in try catch

* Clean up signature-request.component.js

* Check whether the signing address, and not the selected address, is a ledger account in singature-request.container

* Ensure ledger instructions and webhid connection button are shown on signature-request-original signatures

* Improve webhid selection handling in select-ledger-transport-type onChange handler

* Move metamask redux focused ledger selectors to metamask duck

* Lint fix

* Use async await in checkWebHidStatusRef.current

* Remove unnecessary use of ref in ledger-instruction-field.js

* Lint fix

* Remove unnecessary try/catch in ledger-instruction-field.js

* Check if from address, not selected address, is from a ledger account in confirm-approve

* Move findKeyringForAddress to metamask duck

* Fix typo in function name

* Ensure isEqualCaseInsensitive handles possible differences in address casing

* Fix Learn More link size in advanced settings tab

* Update app/scripts/migrations/066.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Update ui/pages/settings/advanced-tab/advanced-tab.component.test.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Add jsdoc comments for new selectors

* Use jest.spyOn for mocking navigator in ledger webhid migration tests

* Use LEDGER_TRANSPORT_TYPES values to set proptype of ledgerTransportType

* Use LEDGER_TRANSPORT_TYPES values to set proptype of ledgerTransportType

* Fix font size of link in ledger connection description in advanced settings

* Fix return type in setLedgerTransportPreference comment

* Clean up connectHardware code for webhid connection in actions.js

* Update app/scripts/migrations/066.test.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Update ui/ducks/metamask/metamask.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Add migration test for when useLedgerLive is true in a browser that supports webhid

* Lint fix

* Fix inline-link size

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-11-04 11:48:52 -07:00

271 lines
7.0 KiB
JavaScript

import extension from 'extensionizer';
import { getBlockExplorerLink } from '@metamask/etherscan-link';
import { getEnvironmentType, checkForError } from '../lib/util';
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
export default class ExtensionPlatform {
//
// Public
//
reload() {
extension.runtime.reload();
}
openTab(options) {
return new Promise((resolve, reject) => {
extension.tabs.create(options, (newTab) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(newTab);
});
});
}
openWindow(options) {
return new Promise((resolve, reject) => {
extension.windows.create(options, (newWindow) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(newWindow);
});
});
}
focusWindow(windowId) {
return new Promise((resolve, reject) => {
extension.windows.update(windowId, { focused: true }, () => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve();
});
});
}
updateWindowPosition(windowId, left, top) {
return new Promise((resolve, reject) => {
extension.windows.update(windowId, { left, top }, () => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve();
});
});
}
getLastFocusedWindow() {
return new Promise((resolve, reject) => {
extension.windows.getLastFocused((windowObject) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(windowObject);
});
});
}
closeCurrentWindow() {
return extension.windows.getCurrent((windowDetails) => {
return extension.windows.remove(windowDetails.id);
});
}
getVersion() {
const {
version,
version_name: versionName,
} = extension.runtime.getManifest();
const versionParts = version.split('.');
if (versionName) {
// On Chrome, the build type is stored as `version_name` in the manifest, and the fourth part
// of the version is the build version.
const buildType = versionName;
if (versionParts.length < 4) {
throw new Error(`Version missing build number: '${version}'`);
}
const [major, minor, patch, buildVersion] = versionParts;
return `${major}.${minor}.${patch}-${buildType}.${buildVersion}`;
} else if (versionParts.length === 4) {
// On Firefox, the build type and build version are in the fourth part of the version.
const [major, minor, patch, prerelease] = versionParts;
const matches = prerelease.match(/^(\w+)(\d)+$/u);
if (matches === null) {
throw new Error(`Version contains invalid prerelease: ${version}`);
}
const [, buildType, buildVersion] = matches;
return `${major}.${minor}.${patch}-${buildType}.${buildVersion}`;
}
// If there is no `version_name` and there are only 3 version parts, then this is not a
// prerelease and the version requires no modification.
return version;
}
openExtensionInBrowser(
route = null,
queryString = null,
keepWindowOpen = false,
) {
let extensionURL = extension.runtime.getURL('home.html');
if (queryString) {
extensionURL += `?${queryString}`;
}
if (route) {
extensionURL += `#${route}`;
}
this.openTab({ url: extensionURL });
if (
getEnvironmentType() !== ENVIRONMENT_TYPE_BACKGROUND &&
!keepWindowOpen
) {
window.close();
}
}
getPlatformInfo(cb) {
try {
extension.runtime.getPlatformInfo((platform) => {
cb(null, platform);
});
} catch (e) {
cb(e);
// eslint-disable-next-line no-useless-return
return;
}
}
showTransactionNotification(txMeta, rpcPrefs) {
const { status, txReceipt: { status: receiptStatus } = {} } = txMeta;
if (status === TRANSACTION_STATUSES.CONFIRMED) {
// There was an on-chain failure
receiptStatus === '0x0'
? this._showFailedTransaction(
txMeta,
'Transaction encountered an error.',
)
: this._showConfirmedTransaction(txMeta, rpcPrefs);
} else if (status === TRANSACTION_STATUSES.FAILED) {
this._showFailedTransaction(txMeta);
}
}
getAllWindows() {
return new Promise((resolve, reject) => {
extension.windows.getAll((windows) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(windows);
});
});
}
getActiveTabs() {
return new Promise((resolve, reject) => {
extension.tabs.query({ active: true }, (tabs) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(tabs);
});
});
}
currentTab() {
return new Promise((resolve, reject) => {
extension.tabs.getCurrent((tab) => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve(tab);
}
});
});
}
switchToTab(tabId) {
return new Promise((resolve, reject) => {
extension.tabs.update(tabId, { highlighted: true }, (tab) => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve(tab);
}
});
});
}
closeTab(tabId) {
return new Promise((resolve, reject) => {
extension.tabs.remove(tabId, () => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
_showConfirmedTransaction(txMeta, rpcPrefs) {
this._subscribeToNotificationClicked();
const url = getBlockExplorerLink(txMeta, rpcPrefs);
const nonce = parseInt(txMeta.txParams.nonce, 16);
const title = 'Confirmed transaction';
const message = `Transaction ${nonce} confirmed! ${
url.length ? 'View on Etherscan' : ''
}`;
this._showNotification(title, message, url);
}
_showFailedTransaction(txMeta, errorMessage) {
const nonce = parseInt(txMeta.txParams.nonce, 16);
const title = 'Failed transaction';
const message = `Transaction ${nonce} failed! ${
errorMessage || txMeta.err.message
}`;
this._showNotification(title, message);
}
_showNotification(title, message, url) {
extension.notifications.create(url, {
type: 'basic',
title,
iconUrl: extension.extension.getURL('../../images/icon-64.png'),
message,
});
}
_subscribeToNotificationClicked() {
if (!extension.notifications.onClicked.hasListener(this._viewOnEtherscan)) {
extension.notifications.onClicked.addListener(this._viewOnEtherscan);
}
}
_viewOnEtherscan(txId) {
if (txId.startsWith('https://')) {
extension.tabs.create({ url: txId });
}
}
}