-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
49 lines (40 loc) · 1.31 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Agent from '@dfinity/agent';
import { identityFromSeed } from './Utils/identities.js';
import {readFileSync} from 'fs';
const fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));
import {idlFactory} from './idl/service.did.js';
import "dotenv/config.js";
const registerModule = async (name, version, network, canisterId, path) => {
const moduleRegistryActor = await createActor(canisterId, idlFactory, {
agentOptions: { host: network, fetch },
});
try {
console.log("path", path);
const res = await moduleRegistryActor.reboot_registry_registerModule(
name,
version,
getWasm(path)
);
return res;
} catch (e) {
console.error(e);
};
};
const createActor = async (canisterId, idl, options)=> {
const phrase = process.env.SEEDPHRASE;
const identity = await identityFromSeed(phrase);
const agent = new Agent.HttpAgent({ ...options?.agentOptions, identity });
await agent.fetchRootKey();
return Agent.Actor.createActor(idl, {
agent,
canisterId,
...options?.actorOptions,
});
};
const getWasm = (path) => {
const localPath = path;
const buffer = readFileSync(localPath);
return [...new Uint8Array(buffer)];
};
export { registerModule };