Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Mar 29, 2024
1 parent 4d4f1c8 commit e808139
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ jobs:
env:
ETHEREUM_URL: ${{ secrets.ETHEREUM_URL }}
AZLE_USE_DOCKERFILE: ${{ matrix.azle_source == 'repo' }}
AZLE_PLAINTEXT_IDENTITY: true
AZLE_PLAINTEXT_IDENTITY: 'plaintext'
strategy:
fail-fast: false # We want to see which example tests succeed and which ones fail, we don't want one example test to cancel the rest
matrix:
Expand Down
68 changes: 28 additions & 40 deletions dfx/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { execSync } from 'child_process';
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';
import { readFile } from 'fs/promises';
import { homedir } from 'os';
import { join } from 'path';
import { HttpAgent } from '@dfinity/agent';

export function getCanisterId(canisterName: string): string {
Expand Down Expand Up @@ -42,11 +39,11 @@ export async function createAnonymousAgent() {
}

export async function createAuthenticatedAgent(
identityName?: string
identityName: string
): Promise<HttpAgent> {
const agent = new HttpAgent({
host: getAgentHost(),
identity: getIdentity(identityName)
identity: getSecp256k1KeyIdentity(identityName)
});

if (process.env.DFX_NETWORK !== 'ic') {
Expand All @@ -56,42 +53,45 @@ export async function createAuthenticatedAgent(
return agent;
}

export function getIdentityName(): string {
export function whoami(): string {
return execSync(`dfx identity whoami`).toString().trim();
}

type StorageMode = 'keyring' | 'password-protected' | 'plaintext';

export function generateIdentity(
name: string,
storageModeCool?: StorageMode
): Buffer {
export function generateIdentity(name: string): Buffer {
console.info();
console.info(`Generating identity ${name}`);
const storageMode = determineStorageMode(storageModeCool);
const storageMode = determineStorageMode();
if (storageMode === undefined) {
console.info(`You may be prompted to create a password for ${name}`);
console.info();
return execSync(`dfx identity new ${name}`, {
stdio: ['inherit', 'pipe', 'inherit']
stdio: ['inherit', 'pipe', 'inherit'] // TODO I would prefer it to pipe the stderr but pipe will cause this command to fail immediately
});
}
if (storageMode === 'password-protected') {
console.info(`You will be prompted to create a password for ${name}`);
console.info();
}
return execSync(`dfx identity new ${name} --storage-mode ${storageMode}`, {
stdio: ['inherit', 'pipe', 'inherit'] // TODO add todo here and aboe
stdio: ['inherit', 'pipe', 'inherit'] // TODO I would prefer it to pipe the stderr but pipe will cause this command to fail immediately
});
}

function determineStorageMode(
storageMode?: StorageMode
): StorageMode | undefined {
if (process.env.AZLE_PLAINTEXT_IDENTITY === 'true') {
return 'plaintext';
function determineStorageMode(): StorageMode | undefined {
const mode = process.env.AZLE_IDENTITY_STORAGE_MODE;
if (
mode !== 'plaintext' &&
mode !== 'keyring' &&
mode !== 'password-protected' &&
mode !== undefined
) {
throw new Error(
`AZLE_IDENTITY_STORAGE_MODE must be 'plaintext', 'keyring', 'password-protected', or undefined`
);
}
return storageMode;
return mode;
}

export function useIdentity(name: string) {
Expand All @@ -114,46 +114,34 @@ export function removeIdentity(name: string) {
execSync(`dfx identity remove ${name}`);
}

export async function getIdentityFromPemFile(
identityName: string = getIdentityName()
): Promise<Secp256k1KeyIdentity> {
const identityPath = join(
homedir(),
'.config',
'dfx',
'identity',
identityName,
'identity.pem'
);
return Secp256k1KeyIdentity.fromPem(await readFile(identityPath, 'utf-8'));
}

export function getPemKey(identityName: string = getIdentityName()): string {
export function getPemKey(identityName: string): string {
console.info();
console.info(`Exporting PEM key for ${identityName}`);
console.info(`You may be prompted for ${identityName}'s password`);
console.info();
const cmd = `dfx identity export ${identityName}`;
const result = execSync(cmd, {
stdio: ['inherit', 'pipe', 'inherit'] // TODO I would prefer it to pipe the stderr but it will fail immediately if you do that
stdio: ['inherit', 'pipe', 'inherit'] // TODO I would prefer it to pipe the stderr but pipe will cause this command to fail immediately
})
.toString()
.trim();
return result;
}

export function getIdentity(identityName?: string): Secp256k1KeyIdentity {
export function getSecp256k1KeyIdentity(
identityName: string
): Secp256k1KeyIdentity {
return Secp256k1KeyIdentity.fromPem(getPemKey(identityName));
}

export function getPrincipal(identityName: string = getIdentityName()): string {
export function getPrincipal(identityName: string = whoami()): string {
console.info();
console.info(`Getting principal for ${identityName}`);
console.info(`You may be prompted for ${identityName}'s password`);
console.info();
const cmd = `dfx identity get-principal --identity ${identityName}`;
return execSync(cmd, {
stdio: ['inherit', 'pipe', 'inherit'] // TODO add todo
stdio: ['inherit', 'pipe', 'inherit'] // TODO I would prefer it to pipe the stderr but pipe will cause this command to fail immediately
})
.toString()
.trim();
Expand All @@ -164,11 +152,11 @@ export function addController(
identityName: string,
principal: string
) {
const currentIdentity = getIdentityName();
const currentIdentity = whoami();
console.info();
console.info(`Adding ${identityName} as a controller for ${canisterName}`);
console.info(`You may be prompted for ${currentIdentity}'s password`);
console.info();
const cmd = `dfx canister update-settings ${canisterName} --add-controller ${principal}`;
return execSync(cmd, { stdio: ['inherit', 'pipe', 'inherit'] }); // TODO I would prefer it to pipe the stderr but it will fail immediately if you do that
return execSync(cmd, { stdio: ['inherit', 'pipe', 'inherit'] }); // TODO I would prefer it to pipe the stderr but pipe will cause this command to fail immediately
}
6 changes: 5 additions & 1 deletion examples/large_files/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { rm } from 'fs/promises';
import { generateTestFileOfSize } from './generateTestFiles';
import { createActor } from 'azle/src/compiler/file_uploader/uploader_actor';
import { v4 } from 'uuid';
import { AZLE_UPLOADER_IDENTITY_NAME } from '../../../src/compiler/file_uploader/uploader_identity';

export function getTests(canisterId: string): Test[] {
const origin = `http://${canisterId}.localhost:8000`;
Expand Down Expand Up @@ -228,7 +229,10 @@ function generateTest(
};
}

const actor = await createActor(getCanisterId('backend'));
const actor = await createActor(
getCanisterId('backend'),
AZLE_UPLOADER_IDENTITY_NAME
);
const hash = await actor.get_file_hash(canisterFilePath);

if (hash.length === 1) {
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/file_uploader/uploader_actor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Actor, ActorMethod, ActorSubclass } from '@dfinity/agent';
import { createAuthenticatedAgent } from '../../../dfx';
import { AZLE_UPLOADER_IDENTITY_NAME } from './uploader_identity';

export type UploaderActor = ActorSubclass<_SERVICE>;

export async function createActor(
canisterId: string,
identityName: string = AZLE_UPLOADER_IDENTITY_NAME
identityName: string
): Promise<UploaderActor> {
const agent = await createAuthenticatedAgent(identityName);

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/file_uploader/uploader_identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
identityExists
} from '../../../dfx';

export const AZLE_UPLOADER_IDENTITY_NAME = '_azle_file_uploader_identity';
export const AZLE_UPLOADER_IDENTITY_NAME =
process.env.AZLE_UPLOADER_IDENTITY_NAME || '_azle_file_uploader_identity';

export function generateUploaderIdentity(canisterName: string): string {
if (!identityExists(AZLE_UPLOADER_IDENTITY_NAME)) {
Expand Down
13 changes: 5 additions & 8 deletions src/compiler/file_watcher/file_watcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Actor, HttpAgent } from '@dfinity/agent';
import { Actor } from '@dfinity/agent';
import { watch } from 'chokidar';
import { readFileSync, writeFileSync } from 'fs';

import { getCanisterJavaScript } from '../get_canister_javascript';
import { ok } from '../utils/result';
import { createAuthenticatedAgent } from '../../../dfx';
import { createAuthenticatedAgent, whoami } from '../../../dfx';

const reloadedJsPath = process.argv[2];
const canisterId = process.argv[3];
const mainPath = process.argv[4];
const wasmedgeQuickJsPath = process.argv[5];
const replicaWebServerPort = process.argv[6];

// TODO https://github.com/demergent-labs/azle/issues/1664
watch(process.cwd(), {
Expand All @@ -27,8 +26,7 @@ watch(process.cwd(), {
reloadedJsPath,
canisterId,
mainPath,
wasmedgeQuickJsPath,
replicaWebServerPort
wasmedgeQuickJsPath
);
} catch (error) {
console.error(error);
Expand All @@ -40,8 +38,7 @@ async function reloadJs(
reloadedJsPath: string,
canisterId: string,
mainPath: string,
wasmedgeQuickJsPath: string,
replicaWebServerPort: string
wasmedgeQuickJsPath: string
) {
const canisterJavaScriptResult = getCanisterJavaScript(
mainPath,
Expand All @@ -60,7 +57,7 @@ async function reloadJs(

writeFileSync(reloadedJsPath, canisterJavaScriptResult.ok);

const agent = await createAuthenticatedAgent();
const agent = await createAuthenticatedAgent(whoami());

const actor = Actor.createActor(
({ IDL }) => {
Expand Down

0 comments on commit e808139

Please sign in to comment.