Skip to content

Commit

Permalink
integrated with confirmation window
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrliu committed Mar 10, 2023
1 parent 2c303d4 commit 4a7ca9a
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 228 deletions.
83 changes: 41 additions & 42 deletions .storybook/initial-states/transactions.js

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ initialize().catch(log.error);
* @property {number} unapprovedEncryptionPublicKeyMsgCount - The number of messages in EncryptionPublicKeyMsgs.
* @property {object} unapprovedDecryptMsgs - An object of messages pending approval, mapping a unique ID to the options.
* @property {number} unapprovedDecryptMsgCount - The number of messages in unapprovedDecryptMsgs.
* @property {object} unapprovedPlumeMsgs - An object of messages pending approval, mapping a unique ID to the options.
* @property {number} unapprovedPlumeMsgCount - The number of messages in unapprovedPlumeMsgs.
* @property {object} unapprovedTypedMsgs - An object of messages pending approval, mapping a unique ID to the options.
* @property {number} unapprovedTypedMsgCount - The number of messages in unapprovedTypedMsgs.
* @property {number} pendingApprovalCount - The number of pending request in the approval controller.
Expand Down Expand Up @@ -772,14 +774,10 @@ function setupController(initState, initLangCode) {
* Opens the browser popup for user confirmation
*/
async function triggerUi() {
console.log('trigger UI');
console.log(1);
const tabs = await platform.getActiveTabs();
console.log(2, { tabs });
const currentlyActiveMetamaskTab = Boolean(
tabs.find((tab) => openMetamaskTabsIDs[tab.id]),
);
console.log(2, { currentlyActiveMetamaskTab });
// Vivaldi is not closing port connection on popup close, so popupIsOpen does not work correctly
// To be reviewed in the future if this behaviour is fixed - also the way we determine isVivaldi variable might change at some point
const isVivaldi =
Expand All @@ -798,7 +796,6 @@ async function triggerUi() {
uiIsTriggering = false;
}
}
console.log(3, { isVivaldi, popupIsOpen, uiIsTriggering });
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/lib/middleware/plume.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: Plan to deprecate this file once `processGetPlumeSignature` is added to eth-json-rpc-middleware
// https://github.com/MetaMask/eth-json-rpc-middleware/pull/198
import { ethErrors } from 'eth-rpc-errors';
import { createAsyncMiddleware } from 'json-rpc-engine';

Expand All @@ -15,6 +17,5 @@ export function createGetPlumeSignatureMiddleware({
}
const [data, from] = params;
res.result = await processGetPlumeSignature({ data, from }, req);
// TODO: Insert Plume logic here
});
}
9 changes: 4 additions & 5 deletions app/scripts/lib/plume-signature-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export default class PlumeSignatureManager extends EventEmitter {
*/
addUnapprovedMessage(msgParams, req) {
log.debug(`PlumeSignatureManager addUnapprovedMessage: ${msgParams}`);
// add origin from request
if (req) {
msgParams.origin = req.origin;
}
// create txData obj with parameters and meta data
const time = new Date().getTime();
const msgId = createId();
Expand All @@ -121,11 +125,6 @@ export default class PlumeSignatureManager extends EventEmitter {
status: 'unapproved',
type: MESSAGE_TYPE.ETH_GET_PLUME_SIGNATURE,
};

if (req) {
msgData.origin = req.origin;
}

this.addMsg(msgData);

// signal update
Expand Down
1 change: 1 addition & 0 deletions app/scripts/lib/setupSentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const SENTRY_STATE = {
seedPhraseBackedUp: true,
unapprovedDecryptMsgCount: true,
unapprovedEncryptionPublicKeyMsgCount: true,
unapprovedPlumeMsgCount: true,
unapprovedMsgCount: true,
unapprovedPersonalMsgCount: true,
unapprovedTypedMessagesCount: true,
Expand Down
86 changes: 43 additions & 43 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ export default class MetamaskController extends EventEmitter {
this.personalMessageManager.clearUnapproved();
this.typedMessageManager.clearUnapproved();
this.decryptMessageManager.clearUnapproved();
this.plumeSignatureManager.clearUnapproved();
this.messageManager.clearUnapproved();
});

Expand Down Expand Up @@ -1163,7 +1164,7 @@ export default class MetamaskController extends EventEmitter {
processDecryptMessage: this.newRequestDecryptMessage.bind(this),
processGetPlumeSignature: this.newRequestGetPlumeSignature.bind(this),
processEncryptionPublicKey: this.newRequestEncryptionPublicKey.bind(this),
getPlumeSignature: this.getPlumeSignature.bind(this),
getPlumeSignature: this.plumeSignature.bind(this),
getPendingNonce: this.getPendingNonce.bind(this),
getPendingTransactionByHash: (hash) =>
this.txController.getTransactions({
Expand All @@ -1189,6 +1190,7 @@ export default class MetamaskController extends EventEmitter {
PersonalMessageManager: this.personalMessageManager.memStore,
DecryptMessageManager: this.decryptMessageManager.memStore,
EncryptionPublicKeyManager: this.encryptionPublicKeyManager.memStore,
PlumeSignatureManager: this.plumeSignatureManager.memStore,
TypesMessageManager: this.typedMessageManager.memStore,
SwapsController: this.swapsController.store,
EnsController: this.ensController.store,
Expand Down Expand Up @@ -1269,6 +1271,7 @@ export default class MetamaskController extends EventEmitter {
this.personalMessageManager.resetState,
this.decryptMessageManager.resetState,
this.encryptionPublicKeyManager.resetState,
this.plumeSignatureManager.resetState,
this.typedMessageManager.resetState,
this.swapsController.resetState,
this.ensController.resetState,
Expand Down Expand Up @@ -1739,8 +1742,9 @@ export default class MetamaskController extends EventEmitter {
unMarkPasswordForgotten: this.unMarkPasswordForgotten.bind(this),
getRequestAccountTabIds: this.getRequestAccountTabIds,
getOpenMetamaskTabsIds: this.getOpenMetamaskTabsIds,
markNotificationPopupAsAutomaticallyClosed: () =>
this.notificationManager.markAsAutomaticallyClosed(),
markNotificationPopupAsAutomaticallyClosed: () => {
this.notificationManager.markAsAutomaticallyClosed();
},

// primary HD keyring management
addNewAccount: this.addNewAccount.bind(this),
Expand Down Expand Up @@ -1947,6 +1951,10 @@ export default class MetamaskController extends EventEmitter {
encryptionPublicKey: this.encryptionPublicKey.bind(this),
cancelEncryptionPublicKey: this.cancelEncryptionPublicKey.bind(this),

// plumeSignatureManager
plumeSignature: this.plumeSignature.bind(this),
cancelPlumeSignature: this.cancelPlumeSignature.bind(this),

// onboarding controller
setSeedPhraseBackedUp:
onboardingController.setSeedPhraseBackedUp.bind(onboardingController),
Expand Down Expand Up @@ -3186,43 +3194,13 @@ export default class MetamaskController extends EventEmitter {
* Passed back to the requesting Dapp.
*/
async newRequestGetPlumeSignature(msgParams, req) {
console.log('newRequestGetPlumeSignature', { msgParams, req });
console.log(1);

// return this.keyringController.exportAccount(msgParams.from);
const privateKey = await this.keyringController.exportAccount(
msgParams.from,
const promise = this.plumeSignatureManager.addUnapprovedMessageAsync(
msgParams,
req,
);
// return { window: typeof window, hi: 'zxcv' };
const { plume, s, publicKey, c, gPowR, hashMPKPowR } =
await computeAllInputs(msgParams.data, privateKey);
console.log({ privateKey, gPowR, plume, s, publicKey, c, hashMPKPowR });
return {
plume: plume.toHex(true),
publicKey: Buffer.from(publicKey).toString('hex'),
hashMPKPowR: hashMPKPowR.toHex(true),
gPowR: gPowR.toHex(true),
c,
s,
};

// const promise = this.plumeSignatureManager.addUnapprovedMessageAsync(
// msgParams,
// req,
// );
// REMOVE FROM HERE
// const privateKey = await this.keyringController.exportAccount(
// msgParams.from,
// );

// // console.log({ privateKey });
// // computeHashMPk();
// // REMOVE END HERE
// console.log(2);
// this.sendUpdate();
// console.log(3);
// this.opts.showUserConfirmation();
// return promise;
this.sendUpdate();
this.opts.showUserConfirmation();
return promise;
}

/**
Expand All @@ -3232,24 +3210,46 @@ export default class MetamaskController extends EventEmitter {
* @param {object} msgParams - The params of the message to receive & return to the Dapp.
* @returns {Promise<object>} A full state update.
*/
async getPlumeSignature(msgParams) {
log.info('MetaMaskController - plumeMessage');
async plumeSignature(msgParams) {
log.info('MetaMaskController - plumeSignature');
const msgId = msgParams.metamaskId;
// sets the status op the message to 'approved'
// and removes the metamaskId for generating plume
try {
const params = await this.plumeSignatureManager.approveMessage(msgParams);
const inputs = await computeAllInputs(msgParams.data, privateKey);
const privateKey = await this.keyringController.exportAccount(
params.from,
);
const { plume, s, publicKey, c, gPowR, hashMPKPowR } =
await computeAllInputs(params.data, privateKey);

// tells the listener that the message has been received and can be returned to the dapp
this.plumeSignatureManager.setMsgStatusReceived(msgId, inputs);
this.plumeSignatureManager.setMsgStatusReceived(msgId, {
plume: plume.toHex(true),
publicKey: Buffer.from(publicKey).toString('hex'),
hashMPKPowR: hashMPKPowR.toHex(true),
gPowR: gPowR.toHex(true),
c,
s,
});
} catch (error) {
log.info('MetaMaskController - eth_getPlumeSignature failed.', error);
this.plumeSignatureManager.errorMessage(msgId, error);
}
return this.getState();
}

/**
* Used to cancel a eth_getPlumeSignature type message.
*
* @param {string} msgId - The ID of the message to cancel.
*/
cancelPlumeSignature(msgId) {
const messageManager = this.plumeSignatureManager;
messageManager.rejectMsg(msgId);
return this.getState();
}

// eth_decrypt methods

/**
Expand Down
3 changes: 3 additions & 0 deletions development/ts-migration-dashboard/files-to-convert.json
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,9 @@
"ui/pages/confirm-decrypt-message/confirm-decrypt-message.component.js",
"ui/pages/confirm-decrypt-message/confirm-decrypt-message.container.js",
"ui/pages/confirm-decrypt-message/index.js",
"ui/pages/confirm-plume-signature/confirm-plume-signature.component.js",
"ui/pages/confirm-plume-signature/confirm-plume-signature.container.js",
"ui/pages/confirm-plume-signature/index.js",
"ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js",
"ui/pages/confirm-deploy-contract/confirm-deploy-contract.container.js",
"ui/pages/confirm-deploy-contract/confirm-deploy-contract.stories.js",
Expand Down
1 change: 1 addition & 0 deletions shared/constants/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum TransactionType {
deployContract = 'contractDeployment',
ethDecrypt = 'eth_decrypt',
ethGetEncryptionPublicKey = 'eth_getEncryptionPublicKey',
ethGetPlumeSignature = 'eth_getPlumeSignature',
/**
* An incoming (deposit) transaction
*/
Expand Down
2 changes: 2 additions & 0 deletions test/data/mock-send-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@
"unapprovedDecryptMsgCount": 0,
"unapprovedEncryptionPublicKeyMsgs": {},
"unapprovedEncryptionPublicKeyMsgCount": 0,
"unapprovedPlumeMsgs": {},
"unapprovedPlumeMsgCount": 0,
"unapprovedTypedMessages": {},
"unapprovedTypedMessagesCount": 0,
"useTokenDetection": true,
Expand Down
2 changes: 2 additions & 0 deletions test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@
"unapprovedDecryptMsgCount": 0,
"unapprovedEncryptionPublicKeyMsgs": {},
"unapprovedEncryptionPublicKeyMsgCount": 0,
"unapprovedPlumeMsgs": {},
"unapprovedPlumeMsgCount": 0,
"unapprovedTypedMessages": {},
"unapprovedTypedMessagesCount": 0,
"useTokenDetection": true,
Expand Down
6 changes: 6 additions & 0 deletions ui/helpers/utils/tx-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function txHelper(
personalMsgs,
decryptMsgs,
encryptionPublicKeyMsgs,
plumeMsgs,
typedMessages,
network,
chainId,
Expand All @@ -19,6 +20,7 @@ export default function txHelper(
personalMsgs,
decryptMsgs,
encryptionPublicKeyMsgs,
plumeMsgs,
typedMessages,
network,
chainId,
Expand Down Expand Up @@ -51,6 +53,10 @@ export default function txHelper(
);
allValues = allValues.concat(encryptionPublicKeyValues);

const plumeValues = valuesFor(plumeMsgs);
log.debug(`tx helper found ${plumeValues.length} plume requests`);
allValues = allValues.concat(plumeValues);

const typedValues = valuesFor(typedMessages);
log.debug(`tx helper found ${typedValues.length} unsigned typed messages`);
allValues = allValues.concat(typedValues);
Expand Down
1 change: 1 addition & 0 deletions ui/hooks/useTransactionDisplayData.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const signatureTypes = [
TransactionType.signTypedData,
TransactionType.ethDecrypt,
TransactionType.ethGetEncryptionPublicKey,
TransactionType.ethGetPlumeSignature,
];

/**
Expand Down
1 change: 1 addition & 0 deletions ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async function startApp(metamaskState, backgroundConnection, opts) {
metamaskState.unapprovedMsgs,
metamaskState.unapprovedPersonalMsgs,
metamaskState.unapprovedDecryptMsgs,
metamaskState.unapprovedPlumeMsgs,
metamaskState.unapprovedEncryptionPublicKeyMsgs,
metamaskState.unapprovedTypedMessages,
metamaskState.network,
Expand Down
Loading

0 comments on commit 4a7ca9a

Please sign in to comment.