Skip to content

Commit

Permalink
[cli] cache http agents; add MOPS_VERIFY_QUERY_SIGNATURES env support
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenVoich committed Aug 15, 2024
1 parent 0323aa8 commit f0bb0f3
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions cli/api/actors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,42 @@ import {_SERVICE as _STORAGE_SERVICE} from '../declarations/storage/storage.did.
import {getEndpoint} from './network.js';
import {getNetwork} from './network.js';

export let mainActor = async (identity ?: Identity) : Promise<_SERVICE> => {
let network = getNetwork();
let host = getEndpoint(network).host;
let canisterId = getEndpoint(network).canisterId;
let agentPromiseByPrincipal = new Map <string, Promise<HttpAgent>>();

let getAgent = async (identity ?: Identity) : Promise<HttpAgent> => {
let principal = identity ? identity?.getPrincipal().toText() : '';
let agentPromise = agentPromiseByPrincipal.get(principal);

// @ts-ignore exactOptionalPropertyTypes
let agent = new HttpAgent({host, identity});
if (!agentPromise) {
let network = getNetwork();
let host = getEndpoint(network).host;

if (network === 'local') {
await agent.fetchRootKey();
agentPromise = HttpAgent.create({
host,
identity,
shouldFetchRootKey: network === 'local',
verifyQuerySignatures: process.env.MOPS_VERIFY_QUERY_SIGNATURES !== 'false',
});

agentPromiseByPrincipal.set(principal, agentPromise);
}

return agentPromise;
};

export let mainActor = async (identity ?: Identity) : Promise<_SERVICE> => {
let agent = await getAgent(identity);
let network = getNetwork();
let canisterId = getEndpoint(network).canisterId;

return Actor.createActor(idlFactory, {
agent,
canisterId,
});
};

export let storageActor = async (storageId : Principal, identity ?: Identity) : Promise<_STORAGE_SERVICE> => {
let network = getNetwork();
let host = getEndpoint(network).host;

// @ts-ignore exactOptionalPropertyTypes
let agent = new HttpAgent({host, identity});

if (network === 'local') {
await agent.fetchRootKey();
}
let agent = await getAgent(identity);

return Actor.createActor(storageIdlFactory, {
agent,
Expand Down

0 comments on commit f0bb0f3

Please sign in to comment.