Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrliu committed Feb 14, 2023
1 parent 0662bf3 commit 2c303d4
Show file tree
Hide file tree
Showing 19 changed files with 828 additions and 46 deletions.
7 changes: 7 additions & 0 deletions app/_locales/en/messages.json

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

5 changes: 5 additions & 0 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,14 @@ 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 @@ -794,6 +798,7 @@ async function triggerUi() {
uiIsTriggering = false;
}
}
console.log(3, { isVivaldi, popupIsOpen, uiIsTriggering });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/lib/createMetamaskMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function createMetamaskMiddleware({
processPersonalMessage,
processDecryptMessage,
processEncryptionPublicKey,
getPlumeSignature,
processGetPlumeSignature,
getPendingNonce,
getPendingTransactionByHash,
}) {
Expand All @@ -39,7 +39,7 @@ export default function createMetamaskMiddleware({
}),
createPendingNonceMiddleware({ getPendingNonce }),
createPendingTxMiddleware({ getPendingTransactionByHash }),
createGetPlumeSignatureMiddleware({ getPlumeSignature }),
createGetPlumeSignatureMiddleware({ processGetPlumeSignature }),
]);
return metamaskMiddleware;
}
11 changes: 9 additions & 2 deletions app/scripts/lib/middleware/plume.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { ethErrors } from 'eth-rpc-errors';
import { createAsyncMiddleware } from 'json-rpc-engine';

export function createGetPlumeSignatureMiddleware({ getPlumeSignature }) {
export function createGetPlumeSignatureMiddleware({
processGetPlumeSignature,
}) {
return createAsyncMiddleware(async (req, res, next) => {
if (!processGetPlumeSignature) {
throw ethErrors.rpc.methodNotSupported();
}
const { method, params } = req;
if (method !== 'eth_getPlumeSignature') {
next();
return;
}
res.result = `plume result ${params.data}`;
const [data, from] = params;
res.result = await processGetPlumeSignature({ data, from }, req);
// TODO: Insert Plume logic here
});
}
20 changes: 10 additions & 10 deletions app/scripts/lib/plume-signature-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ export default class PlumeSignatureManager extends EventEmitter {
* the new PlumeMessage to this.messages, and to save the unapproved PlumeMessages from that list to
* this.memStore.
*
* @param {object} address - The param for the eth_getPlumeSignature call to be made after the message is approved.
* @param {object} msgParams - The param for the eth_getPlumeSignature call to be made after the message is approved.
* @param {object} [req] - The original request object possibly containing the origin
* @returns {Promise<Buffer>} The genereated plume signature
*/
addUnapprovedMessageAsync(address, req) {
addUnapprovedMessageAsync(msgParams, req) {
return new Promise((resolve, reject) => {
if (!address) {
reject(new Error('MetaMask Message: address field is required.'));
if (!msgParams.from) {
reject(new Error('MetaMask Message: from field is required.'));
return;
}
const msgId = this.addUnapprovedMessage(address, req);
const msgId = this.addUnapprovedMessage(msgParams, req);
this.once(`${msgId}:finished`, (data) => {
switch (data.status) {
case 'received':
Expand All @@ -91,7 +91,7 @@ export default class PlumeSignatureManager extends EventEmitter {
reject(
new Error(
`MetaMask PlumeSignature: Unknown problem: ${JSON.stringify(
address,
msgParams,
)}`,
),
);
Expand All @@ -105,18 +105,18 @@ export default class PlumeSignatureManager extends EventEmitter {
* the new PlumeMessage to this.messages, and to save the unapproved PlumeMessages from that list to
* this.memStore.
*
* @param {object} address - The param for the eth_getPlumeSignature call to be made after the message is approved.
* @param {object} msgParams - The param for the eth_getPlumeSignature call to be made after the message is approved.
* @param {object} [req] - The original request object possibly containing the origin
* @returns {number} The id of the newly created PlumeMessage.
*/
addUnapprovedMessage(address, req) {
log.debug(`PlumeSignatureManager addUnapprovedMessage: ${address}`);
addUnapprovedMessage(msgParams, req) {
log.debug(`PlumeSignatureManager addUnapprovedMessage: ${msgParams}`);
// create txData obj with parameters and meta data
const time = new Date().getTime();
const msgId = createId();
const msgData = {
id: msgId,
msgParams: address,
msgParams,
time,
status: 'unapproved',
type: MESSAGE_TYPE.ETH_GET_PLUME_SIGNATURE,
Expand Down
53 changes: 42 additions & 11 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { JsonRpcEngine } from 'json-rpc-engine';
import { debounce } from 'lodash';
import createEngineStream from 'json-rpc-middleware-stream/engineStream';
import { providerAsMiddleware } from 'eth-json-rpc-middleware';
import { computeAllInputs } from 'plume-sig';
import {
KeyringController,
keyringBuilderFactory,
Expand Down Expand Up @@ -1162,6 +1163,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),
getPendingNonce: this.getPendingNonce.bind(this),
getPendingTransactionByHash: (hash) =>
this.txController.getTransactions({
Expand Down Expand Up @@ -3184,13 +3186,43 @@ export default class MetamaskController extends EventEmitter {
* Passed back to the requesting Dapp.
*/
async newRequestGetPlumeSignature(msgParams, req) {
const promise = this.plumeSignatureManager.addUnapprovedMessageAsync(
msgParams,
req,
console.log('newRequestGetPlumeSignature', { msgParams, req });
console.log(1);

// return this.keyringController.exportAccount(msgParams.from);
const privateKey = await this.keyringController.exportAccount(
msgParams.from,
);
this.sendUpdate();
this.opts.showUserConfirmation();
return promise;
// 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;
}

/**
Expand All @@ -3204,14 +3236,13 @@ export default class MetamaskController extends EventEmitter {
log.info('MetaMaskController - plumeMessage');
const msgId = msgParams.metamaskId;
// sets the status op the message to 'approved'
// and removes the metamaskId for decryption
// and removes the metamaskId for generating plume
try {
const params = await this.plumeSignatureManager.approveMessage(msgParams);
// TODO / FIXME: Import and use zk-nullifier library
const mockPlume = `mockPlume-${params.data}`;
const inputs = await computeAllInputs(msgParams.data, privateKey);

// tells the listener that the message has been decrypted and can be returned to the dapp
this.plumeSignatureManager.setMsgStatusReceived(msgId, mockPlume);
// tells the listener that the message has been received and can be returned to the dapp
this.plumeSignatureManager.setMsgStatusReceived(msgId, inputs);
} catch (error) {
log.info('MetaMaskController - eth_getPlumeSignature failed.', error);
this.plumeSignatureManager.errorMessage(msgId, error);
Expand Down
27 changes: 23 additions & 4 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@
"@metamask/snaps-utils>@noble/hashes": true,
"@metamask/snaps-utils>@scure/base": true,
"@metamask/utils": true,
"plume-sig>@noble/secp256k1": true
"@noble/secp256k1": true
}
},
"@metamask/rpc-methods>@metamask/key-tree>@noble/ed25519": {
Expand Down Expand Up @@ -1362,6 +1362,14 @@
"define": true
}
},
"@noble/secp256k1": {
"globals": {
"crypto": true
},
"packages": {
"browserify>browser-resolve": true
}
},
"@popperjs/core": {
"globals": {
"Element": true,
Expand Down Expand Up @@ -3569,12 +3577,23 @@
"readable-stream": true
}
},
"plume-sig>@noble/secp256k1": {
"plume-sig": {
"globals": {
"crypto": true
"TextEncoder": true
},
"packages": {
"browserify>browser-resolve": true
"@noble/secp256k1": true,
"browserify>buffer": true,
"plume-sig>amcl-js": true,
"plume-sig>js-sha512": true
}
},
"plume-sig>js-sha512": {
"globals": {
"define": true
},
"packages": {
"browserify>process": true
}
},
"promise-to-callback": {
Expand Down
27 changes: 23 additions & 4 deletions lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@
"@metamask/snaps-utils>@noble/hashes": true,
"@metamask/snaps-utils>@scure/base": true,
"@metamask/utils": true,
"plume-sig>@noble/secp256k1": true
"@noble/secp256k1": true
}
},
"@metamask/rpc-methods>@metamask/key-tree>@noble/ed25519": {
Expand Down Expand Up @@ -1686,6 +1686,14 @@
"define": true
}
},
"@noble/secp256k1": {
"globals": {
"crypto": true
},
"packages": {
"browserify>browser-resolve": true
}
},
"@popperjs/core": {
"globals": {
"Element": true,
Expand Down Expand Up @@ -3911,12 +3919,23 @@
"readable-stream": true
}
},
"plume-sig>@noble/secp256k1": {
"plume-sig": {
"globals": {
"crypto": true
"TextEncoder": true
},
"packages": {
"browserify>browser-resolve": true
"@noble/secp256k1": true,
"browserify>buffer": true,
"plume-sig>amcl-js": true,
"plume-sig>js-sha512": true
}
},
"plume-sig>js-sha512": {
"globals": {
"define": true
},
"packages": {
"browserify>process": true
}
},
"promise-to-callback": {
Expand Down
27 changes: 23 additions & 4 deletions lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@
"@metamask/snaps-utils>@noble/hashes": true,
"@metamask/snaps-utils>@scure/base": true,
"@metamask/utils": true,
"plume-sig>@noble/secp256k1": true
"@noble/secp256k1": true
}
},
"@metamask/rpc-methods>@metamask/key-tree>@noble/ed25519": {
Expand Down Expand Up @@ -1362,6 +1362,14 @@
"define": true
}
},
"@noble/secp256k1": {
"globals": {
"crypto": true
},
"packages": {
"browserify>browser-resolve": true
}
},
"@popperjs/core": {
"globals": {
"Element": true,
Expand Down Expand Up @@ -3569,12 +3577,23 @@
"readable-stream": true
}
},
"plume-sig>@noble/secp256k1": {
"plume-sig": {
"globals": {
"crypto": true
"TextEncoder": true
},
"packages": {
"browserify>browser-resolve": true
"@noble/secp256k1": true,
"browserify>buffer": true,
"plume-sig>amcl-js": true,
"plume-sig>js-sha512": true
}
},
"plume-sig>js-sha512": {
"globals": {
"define": true
},
"packages": {
"browserify>process": true
}
},
"promise-to-callback": {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
"@metamask/subject-metadata-controller": "^1.0.0",
"@metamask/utils": "^3.4.1",
"@ngraveio/bc-ur": "^1.1.6",
"@noble/secp256k1": "^1.7.1",
"@popperjs/core": "^2.4.0",
"@reduxjs/toolkit": "^1.6.2",
"@segment/loosely-validate-event": "^2.0.0",
Expand Down Expand Up @@ -312,7 +313,7 @@
"nonce-tracker": "^1.0.0",
"obj-multiplex": "^1.0.0",
"pify": "^5.0.0",
"plume-sig": "^1.0.0",
"plume-sig": "^1.1.2",
"promise-to-callback": "^1.0.0",
"prop-types": "^15.6.1",
"pubnub": "4.27.3",
Expand Down
Loading

0 comments on commit 2c303d4

Please sign in to comment.