From b2df29bd7c3e27f3edf0337644e20592c4836873 Mon Sep 17 00:00:00 2001 From: rocky-fleek Date: Mon, 27 Jun 2022 19:09:04 +0200 Subject: [PATCH] Refactored requestCall requestReadState and requestQuery to use the host in the app so that custom hosts work again --- package.json | 4 +- source/Background/utils.js | 4 +- source/Modules/Controller/transaction.js | 42 ++++++++++++------- .../Views/Welcome/steps/SeedPhraseStep.jsx | 3 +- .../components/Sign/hooks/useRequest.jsx | 5 ++- yarn.lock | 8 +--- 6 files changed, 40 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index fb1263d4..f95ce472 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,8 @@ "@material-ui/icons": "^4.11.2", "@metamask/post-message-stream": "^4.0.0", "@psychedelic/dab-js": "1.3.6", - "@psychedelic/plug-controller": "0.17.3", - "@psychedelic/plug-inpage-provider": "2.1.2", + "@psychedelic/plug-controller": "../plug-controller", + "@psychedelic/plug-inpage-provider": "../plug-inpage-provider", "@reduxjs/toolkit": "^1.6.0", "advanced-css-reset": "^1.2.2", "axios": "^0.21.1", diff --git a/source/Background/utils.js b/source/Background/utils.js index c14da237..ef019e84 100644 --- a/source/Background/utils.js +++ b/source/Background/utils.js @@ -144,7 +144,7 @@ export const validateBatchTx = (savedTxInfo, canisterId, methodName, arg) => { }; export const handleCallRequest = async ({ - keyring, request, callId, portId, callback, redirected, + keyring, request, callId, portId, callback, redirected, host, }) => { const arg = blobFromBuffer(base64ToBuffer(request.arguments)); try { @@ -167,7 +167,7 @@ export const handleCallRequest = async ({ } } const signed = await keyring - .getAgent().call( + .getAgent({ host }).call( Principal.fromText(request.canisterId), { methodName: request.methodName, diff --git a/source/Modules/Controller/transaction.js b/source/Modules/Controller/transaction.js index 140e3e65..3a8b5102 100644 --- a/source/Modules/Controller/transaction.js +++ b/source/Modules/Controller/transaction.js @@ -475,6 +475,7 @@ export class TransactionModule extends ControllerModuleBase { canisterInfo, requestInfo, timeout: app?.timeout, + host: app.host, }), metadataJson: JSON.stringify(metadata), }, callback); @@ -495,6 +496,7 @@ export class TransactionModule extends ControllerModuleBase { portId, callId, callback, + host: app.host, }); }); }); @@ -517,7 +519,13 @@ export class TransactionModule extends ControllerModuleBase { if (status === CONNECTION_STATUS.accepted) { await handleCallRequest({ - keyring: this.keyring, request, portId, callId, callback, redirected: true, + keyring: this.keyring, + request, + portId, + callId, + callback, + redirected: true, + host: request?.host, }); } else { callback(ERRORS.SIGN_REJECTED, null, [{ portId, callId }]); @@ -530,15 +538,17 @@ export class TransactionModule extends ControllerModuleBase { #requestReadState() { return { methodName: 'requestReadState', - handler: async (opts, { canisterId, paths }) => { + handler: async (opts, { canisterId, paths, url }) => { const { callback } = opts; try { - const response = await this.keyring.getAgent().readState(canisterId, { - paths: [paths.map((path) => blobFromBuffer(base64ToBuffer(path)))], - }); - callback(null, { - certificate: bufferToBase64(blobToUint8Array(response.certificate)), + getApp(this.keyring?.currentWalletId.toString(), url, async (app) => { + const response = await this.keyring.getAgent({ host: app.host }).readState(canisterId, { + paths: [paths.map((path) => blobFromBuffer(base64ToBuffer(path)))], + }); + callback(null, { + certificate: bufferToBase64(blobToUint8Array(response.certificate)), + }); }); } catch (e) { callback(ERRORS.SERVER_ERROR(e), null); @@ -550,18 +560,22 @@ export class TransactionModule extends ControllerModuleBase { #requestQuery() { return { methodName: 'requestQuery', - handler: async (opts, { canisterId, methodName, arg }) => { + handler: async (opts, { + canisterId, methodName, arg, url, + }) => { const { callback } = opts; try { - const response = await this.keyring.getAgent() - .query(canisterId, { methodName, arg: blobFromBuffer(base64ToBuffer(arg)) }); + getApp(this.keyring?.currentWalletId.toString(), url, async (app) => { + const response = await this.keyring.getAgent({ host: app.host }) + .query(canisterId, { methodName, arg: blobFromBuffer(base64ToBuffer(arg)) }); - if (response.reply) { - response.reply.arg = bufferToBase64(blobToUint8Array(response.reply.arg)); - } + if (response.reply) { + response.reply.arg = bufferToBase64(blobToUint8Array(response.reply.arg)); + } - callback(null, response); + callback(null, response); + }); } catch (e) { callback(ERRORS.SERVER_ERROR(e), null); } diff --git a/source/Options/Views/Welcome/steps/SeedPhraseStep.jsx b/source/Options/Views/Welcome/steps/SeedPhraseStep.jsx index 77a23116..915f229e 100644 --- a/source/Options/Views/Welcome/steps/SeedPhraseStep.jsx +++ b/source/Options/Views/Welcome/steps/SeedPhraseStep.jsx @@ -43,7 +43,8 @@ const SeedPhraseStep = ({ handleNextStep, mnemonic }) => { checked={checked} handleChange={handleChangeCheckbox} label={t('welcome.seedCheckbox')} - data-testid="seedphrase-confirmation-checkbox" /> + data-testid="seedphrase-confirmation-checkbox" + />