Skip to content

Commit

Permalink
Refactored requestCall requestReadState and requestQuery to use the h…
Browse files Browse the repository at this point in the history
…ost in the app so that custom hosts work again
  • Loading branch information
rocky-fleek committed Jun 27, 2022
1 parent 661867f commit b2df29b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions source/Background/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -167,7 +167,7 @@ export const handleCallRequest = async ({
}
}
const signed = await keyring
.getAgent().call(
.getAgent({ host }).call(
Principal.fromText(request.canisterId),
{
methodName: request.methodName,
Expand Down
42 changes: 28 additions & 14 deletions source/Modules/Controller/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export class TransactionModule extends ControllerModuleBase {
canisterInfo,
requestInfo,
timeout: app?.timeout,
host: app.host,
}),
metadataJson: JSON.stringify(metadata),
}, callback);
Expand All @@ -495,6 +496,7 @@ export class TransactionModule extends ControllerModuleBase {
portId,
callId,
callback,
host: app.host,
});
});
});
Expand All @@ -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 }]);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion source/Options/Views/Welcome/steps/SeedPhraseStep.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
</Grid>
<Grid item xs={12}>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const portRPC = new PortRPC({

portRPC.start();

const formatRequest = ({ requestInfo, canisterInfo, payload }) => ({
const formatRequest = ({
requestInfo, canisterInfo, payload, host,
}) => ({
type: requestInfo.type || 'sign',
canisterId: requestInfo.canisterId,
methodName: requestInfo.methodName,
Expand All @@ -29,6 +31,7 @@ const formatRequest = ({ requestInfo, canisterInfo, payload }) => ({
decodedArguments: requestInfo.decodedArguments,
category: canisterInfo?.category,
payload,
host,
});

const useRequests = (incomingRequest, callId, portId, transactionId) => {
Expand Down
8 changes: 2 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1747,10 +1747,8 @@
cross-fetch "^3.1.4"
crypto-js "^4.1.1"

"@psychedelic/plug-controller@0.17.3":
"@psychedelic/plug-controller@../plug-controller":
version "0.17.3"
resolved "https://npm.pkg.github.com/download/@psychedelic/plug-controller/0.17.3/db8e98cf8af37ceca689195a9bc057533ba805c835ca911417e4d89a61f8d889#9ddb06613dfb5052288300337c6bfe45a60a4194"
integrity sha512-19BhVVdanfOIkNiTZ4nG0T7t+CexYU4OMkKlnAazdUc4b/OjDiuogTkvrmU3U1tZ+luawrVu9vnhed5ZUYtL4A==
dependencies:
"@dfinity/agent" "0.9.3"
"@dfinity/candid" "0.9.3"
Expand Down Expand Up @@ -1779,10 +1777,8 @@
text-encoding-shim "^1.0.5"
tweetnacl "^1.0.3"

"@psychedelic/plug-inpage-provider@2.1.2":
"@psychedelic/plug-inpage-provider@../plug-inpage-provider":
version "2.1.2"
resolved "https://npm.pkg.github.com/download/@psychedelic/plug-inpage-provider/2.1.2/ad720049d164796d0de1cd513ea7423beaef59d2dffc26416c2006a935b272e2#7b42861b860758bb59fe1d6a080f6ed1f2e62964"
integrity sha512-BsutLGWpIEYUw5Fz6vLKq1BjmTXzOKbstUrGBiIM/KtwQ9ralD5jMAOPkcLdQRxiZL0tE7VdnIKARBNNzenWIQ==
dependencies:
"@dfinity/agent" "0.9.3"
"@dfinity/candid" "0.9.3"
Expand Down

0 comments on commit b2df29b

Please sign in to comment.