Skip to content

Commit

Permalink
feat: add internet identity (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Jun 27, 2022
1 parent 23cb970 commit bba9e56
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import {gray} from 'kleur';
import {version} from '../package.json';
import {assertEmptyFolder} from './utils/cmd.utils';
import {
dfxDefaultNullArguments,
dfxNewProject,
promptDfxCanisterType,
promptDfxInstall, promptDfxNoFrontend,
promptDfxInstall,
promptDfxNoFrontend,
promptDfxVersion
} from './utils/dfx.utils';
import {nextStepsDisclaimer} from './utils/info.utils';
import {addIIToProject, promptIIInstall} from './utils/internet-identity.utils';
import {isWindows, osDisclaimer} from './utils/os.utils';
import {promptProject} from './utils/project.utils';

Expand All @@ -30,7 +34,20 @@ export const main = async () => {
const type: DfxCanisterType = await promptDfxCanisterType();
const noFrontend: boolean = await promptDfxNoFrontend();

const installII: boolean = noFrontend ? false : await promptIIInstall();

await dfxNewProject({project, type, noFrontend});

const dfxNullArguments = await dfxDefaultNullArguments();

if (!installII) {
nextStepsDisclaimer({installII, dir: project, dfxNullArguments});
return;
}

await addIIToProject(project);

nextStepsDisclaimer({installII, dir: project, dfxNullArguments});
};

(async () => {
Expand Down
18 changes: 18 additions & 0 deletions src/types/dfx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ interface DfxManifest {
}

type DfxCanisterType = 'motoko' | 'rust';

interface DfxJson {
canisters: Record<
string,
{
type: 'custom' | 'motoko' | 'assets' | 'rust';
candid?: string;
wasm?: string;
build?: string;
remote?: {
candid: string;
id: {
ic: string;
};
};
}
>;
}
7 changes: 7 additions & 0 deletions src/utils/dfx.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const dfxVersion = async (): Promise<string> => {
return version;
};

// v0.10.0 - https://github.com/dfinity/sdk/blob/master/CHANGELOG.adoc#feat-use-null-as-default-value-for-opt-arguments
export const dfxDefaultNullArguments = async (): Promise<boolean> => {
const version = await dfxVersion();

return version.localeCompare('0.10.0', undefined, { numeric: true, sensitivity: 'base' }) > -1
}

const dfxUpgrade = async (): Promise<number | null> => spawn({command: 'dfx', args: ['upgrade']});

export const promptDfxVersion = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/download.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const downloadDfxManifest = async (): Promise<DfxManifest> => {
return JSON.parse(decoder.decode(buffer));
};

function downloadFromURL(url: string): Promise<Buffer> {
export const downloadFromURL = (url: string): Promise<Buffer> => {
return new Promise((resolve, reject) => {
get(url, async (res) => {
if (res.statusCode !== undefined && [301, 302].includes(res.statusCode)) {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/info.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {cyan} from 'kleur';

export const nextStepsDisclaimer = ({
installII,
dir,
dfxNullArguments
}: {
installII: boolean;
dir: string;
dfxNullArguments: boolean;
}) =>
console.log(`\nNext steps:
1: cd ${cyan(dir)}
2: ${cyan('dfx start --background --clean')}
3: ${cyan(
`dfx deploy${installII ? ` --no-wallet${!dfxNullArguments ? " --argument '(null)'" : ''}` : ''}`
)}
`);
65 changes: 65 additions & 0 deletions src/utils/internet-identity.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {readFile, writeFile} from 'fs/promises';
import {green} from 'kleur';
import {confirm} from './prompt.utils';

export const promptIIInstall = async (): Promise<boolean> =>
confirm('Do you need authentication (Internet Identity)?');

const II_WASM_LOCAL_FILE = 'internet_identity.wasm';
const II_CANDID_LOCAL_FILE = 'internet_identity.did';

const II_LATEST_WASM = `https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_dev.wasm`;
const II_LATEST_CANDID = `https://raw.githubusercontent.com/dfinity/internet-identity/main/src/internet_identity/internet_identity.did`;

const updateDfxJson = async (dir: string) => {
const dfxJsonFilePath: string = `${dir}/dfx.json`;

const dfxJson: DfxJson = JSON.parse(await readFile(dfxJsonFilePath, 'utf-8'));

const {canisters}: DfxJson = dfxJson;

const testAndDownload = ({url, filename}: {url: string; filename: string}): string =>
`test -f ${filename} || curl -sSL ${url} -o ${filename}`;

const dfxJsonWithII: DfxJson = {
...dfxJson,
canisters: {
...canisters,
internet_identity: {
type: 'custom',
candid: II_CANDID_LOCAL_FILE,
wasm: II_WASM_LOCAL_FILE,
build: `bash -c '${testAndDownload({
url: II_LATEST_WASM,
filename: II_WASM_LOCAL_FILE
})}; ${testAndDownload({url: II_LATEST_CANDID, filename: II_CANDID_LOCAL_FILE})}'`,
remote: {
candid: II_CANDID_LOCAL_FILE,
id: {
ic: 'rdmx6-jaaaa-aaaaa-aaadq-cai'
}
}
}
}
} as DfxJson;

await writeFile(dfxJsonFilePath, JSON.stringify(dfxJsonWithII, null, 2), 'utf-8');
};

const updateGitIgnore = async (dir: string) => {
const gitIgnoreFilePath: string = `${dir}/.gitignore`;

const gitIgnore: string = await readFile(gitIgnoreFilePath, 'utf-8');

const gitIgnoreII: string = `${gitIgnore}\n\n${II_WASM_LOCAL_FILE}\n${II_CANDID_LOCAL_FILE}`;

await writeFile(gitIgnoreFilePath, gitIgnoreII, 'utf-8');
};

export const addIIToProject = async (dir: string) => {
console.log('Adding Internet Identity...');

await Promise.all([updateDfxJson(dir), updateGitIgnore(dir)]);

console.log(`II installed ${green('✔')}\n`);
};

0 comments on commit bba9e56

Please sign in to comment.