diff --git a/scripts/console.upgrade.mjs b/scripts/console.upgrade.mjs index b36e798bd..45f6348ba 100755 --- a/scripts/console.upgrade.mjs +++ b/scripts/console.upgrade.mjs @@ -1,61 +1,9 @@ #!/usr/bin/env node -import { IDL } from '@dfinity/candid'; -import { ICManagementCanister, InstallMode } from '@dfinity/ic-management'; -import { fromNullable, uint8ArrayToHexString } from '@dfinity/utils'; -import { fileExists } from '@junobuild/cli-tools'; -import { createHash } from 'crypto'; -import { readFile } from 'fs/promises'; -import { join } from 'node:path'; -import { localAgent } from './actor.mjs'; import { CONSOLE_ID } from './constants.mjs'; +import { upgrade } from './module.upgrade.mjs'; -const target = join(process.cwd(), 'target', 'deploy'); - -const loadGzippedWasm = async (destination) => { - const buffer = await readFile(destination); - - return { - wasm: [...new Uint8Array(buffer)], - hash: createHash('sha256').update(buffer).digest('hex') - }; -}; - -const agent = await localAgent(); - -const upgrade = async () => { - const sourceFilename = `console.wasm.gz`; - const source = join(target, sourceFilename); - - if (!(await fileExists(source))) { - throw new Error(`${source} not found.`); - } - - const EMPTY_ARG = IDL.encode([], []); - - const { installCode, canisterStatus } = ICManagementCanister.create({ - agent - }); - - const { wasm, hash } = await loadGzippedWasm(source); - - const { module_hash } = await canisterStatus(CONSOLE_ID); - - const currentHash = fromNullable(module_hash); - - if (uint8ArrayToHexString(currentHash) === hash) { - console.log(`Module hash ${hash} already installed.`); - return; - } - - await installCode({ - mode: InstallMode.Upgrade, - canisterId: CONSOLE_ID, - wasmModule: wasm, - arg: new Uint8Array(EMPTY_ARG) - }); - - console.log(`Module upgraded to hash ${hash}.`); -}; - -await upgrade(); +await upgrade({ + sourceFilename: 'console.wasm.gz', + canisterId: CONSOLE_ID +}); diff --git a/scripts/module.upgrade.mjs b/scripts/module.upgrade.mjs new file mode 100755 index 000000000..4ac39e7aa --- /dev/null +++ b/scripts/module.upgrade.mjs @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +import { IDL } from '@dfinity/candid'; +import { ICManagementCanister, InstallMode } from '@dfinity/ic-management'; +import { fromNullable, uint8ArrayToHexString } from '@dfinity/utils'; +import { fileExists } from '@junobuild/cli-tools'; +import { createHash } from 'crypto'; +import { readFile } from 'fs/promises'; +import { join } from 'node:path'; +import { icAgent, localAgent } from './actor.mjs'; +import { targetMainnet } from './utils.mjs'; + +const target = join(process.cwd(), 'target', 'deploy'); + +const loadGzippedWasm = async (destination) => { + const buffer = await readFile(destination); + + return { + wasm: [...new Uint8Array(buffer)], + hash: createHash('sha256').update(buffer).digest('hex') + }; +}; + +const fnAgent = targetMainnet() ? icAgent : localAgent; +const agent = await fnAgent(); + +export const upgrade = async ({ sourceFilename, canisterId }) => { + const source = join(target, sourceFilename); + + console.log(`About to upgrade module ${canisterId} with source ${source}...`); + + if (!(await fileExists(source))) { + throw new Error(`${source} not found.`); + } + + const EMPTY_ARG = IDL.encode([], []); + + const { installCode, canisterStatus } = ICManagementCanister.create({ + agent + }); + + const { wasm, hash } = await loadGzippedWasm(source); + + const { module_hash } = await canisterStatus(canisterId); + + const currentHash = fromNullable(module_hash); + + if (uint8ArrayToHexString(currentHash) === hash) { + console.log(`Module hash ${hash} already installed.`); + return; + } + + await installCode({ + mode: InstallMode.Upgrade, + canisterId, + wasmModule: wasm, + arg: new Uint8Array(EMPTY_ARG) + }); + + console.log(`Module upgraded to hash ${hash}.`); +}; diff --git a/scripts/observatory.upgrade.mjs b/scripts/observatory.upgrade.mjs new file mode 100755 index 000000000..f398a54f5 --- /dev/null +++ b/scripts/observatory.upgrade.mjs @@ -0,0 +1,9 @@ +#!/usr/bin/env node + +import { OBSERVATORY_ID } from './constants.mjs'; +import { upgrade } from './module.upgrade.mjs'; + +await upgrade({ + sourceFilename: 'observatory.wasm.gz', + canisterId: OBSERVATORY_ID +});