|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1'; |
| 3 | +import { readFile } from 'fs/promises'; |
| 4 | +import { homedir } from 'os'; |
| 5 | +import { join } from 'path'; |
| 6 | +import { HttpAgent } from '@dfinity/agent'; |
| 7 | + |
| 8 | +export function getCanisterId(canisterName: string): string { |
| 9 | + return execSync( |
| 10 | + `dfx canister --network ${ |
| 11 | + process.env.DFX_NETWORK ?? 'local' |
| 12 | + } id ${canisterName}` |
| 13 | + ) |
| 14 | + .toString() |
| 15 | + .trim(); |
| 16 | +} |
| 17 | + |
| 18 | +export function getWebServerPort(): string { |
| 19 | + return execSync(`dfx info webserver-port`).toString().trim(); |
| 20 | +} |
| 21 | + |
| 22 | +export function getCanisterOrigin(canisterName: string): string { |
| 23 | + return `http://${getCanisterId( |
| 24 | + canisterName |
| 25 | + )}.localhost:${getWebServerPort()}`; |
| 26 | +} |
| 27 | + |
| 28 | +export function getAgentHost(): string { |
| 29 | + return process.env.DFX_NETWORK === 'ic' |
| 30 | + ? `https://icp-api.io` |
| 31 | + : `http://127.0.0.1:${getWebServerPort()}`; |
| 32 | +} |
| 33 | + |
| 34 | +export async function createAnonymousAgent() { |
| 35 | + const agent = new HttpAgent({ |
| 36 | + host: getAgentHost() |
| 37 | + }); |
| 38 | + |
| 39 | + if (process.env.DFX_NETWORK !== 'ic') { |
| 40 | + await agent.fetchRootKey(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export async function createAuthenticatedAgent( |
| 45 | + identityName?: string |
| 46 | +): Promise<HttpAgent> { |
| 47 | + const agent = new HttpAgent({ |
| 48 | + host: getAgentHost(), |
| 49 | + identity: getIdentity(identityName) |
| 50 | + }); |
| 51 | + |
| 52 | + if (process.env.DFX_NETWORK !== 'ic') { |
| 53 | + await agent.fetchRootKey(); |
| 54 | + } |
| 55 | + |
| 56 | + return agent; |
| 57 | +} |
| 58 | + |
| 59 | +export function getIdentityName(): string { |
| 60 | + return execSync(`dfx identity whoami`).toString().trim(); |
| 61 | +} |
| 62 | + |
| 63 | +export function generateIdentity(name: string) { |
| 64 | + execSync(`dfx identity new ${name} --storage-mode plaintext`); |
| 65 | +} |
| 66 | + |
| 67 | +export function useIdentity(name: string) { |
| 68 | + execSync(`dfx identity use ${name}`); |
| 69 | +} |
| 70 | + |
| 71 | +export function getIdentities(): string[] { |
| 72 | + const list = execSync(`dfx identity list`).toString().trim(); |
| 73 | + const identities = list.split('\n'); |
| 74 | + |
| 75 | + return identities; |
| 76 | +} |
| 77 | + |
| 78 | +export function removeIdentity(name: string) { |
| 79 | + execSync(`dfx identity remove ${name}`); |
| 80 | +} |
| 81 | + |
| 82 | +export async function getIdentity( |
| 83 | + identityName: string = getIdentityName() |
| 84 | +): Promise<Secp256k1KeyIdentity> { |
| 85 | + const identityPath = join( |
| 86 | + homedir(), |
| 87 | + '.config', |
| 88 | + 'dfx', |
| 89 | + 'identity', |
| 90 | + identityName, |
| 91 | + 'identity.pem' |
| 92 | + ); |
| 93 | + return Secp256k1KeyIdentity.fromPem(await readFile(identityPath, 'utf-8')); |
| 94 | +} |
0 commit comments