feat: homes per caller and counters that survive upgrades
Nodes on a canister's subnet can read its state, so no authority rests
on swiss numbers. fetch("home") answers the caller principal's own
durable store of named objects (init, get), found through Loam's name
index and made on first use; fetch("makeCounter") answers the public
maker. Homes, counters, and the CapTP identity live in Loam over the
stable-memory log, reopened in post_upgrade; sessions end with the heap.
The canister installs from 32 bytes of entropy passed as its argument
and refuses the anonymous principal. The client takes a key file, so a
caller can come back to its home, and sends string and reference
arguments.
Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
1 parent
eb15a05668
commit
b717ccaedf
11 files changed
+782
-233
No files matched your search
+48
-19
@@ -2,17 +2,23 @@
|
||||
/**
|
||||
* @file Call the Counter canister over OCapN.
|
||||
*/
|
||||
const HELP = `usage: counter.mjs HOST CANISTER_ID [VERB...]
|
||||
const HELP = `usage: counter.mjs HOST CANISTER_ID KEY_FILE init NAME
|
||||
counter.mjs HOST CANISTER_ID KEY_FILE use NAME [VERB...]
|
||||
|
||||
Open a CapTP session to the canister, fetch "counter", and send it each
|
||||
VERB (incr, decr, value; default: incr incr decr) without waiting for the
|
||||
fetch, then print each answer.
|
||||
HOST is the replica, e.g. http://127.0.0.1:4959, or https://icp-api.io.
|
||||
A fresh Ed25519 identity makes each run its own caller, so its own session.`;
|
||||
Open a CapTP session to the canister as the principal whose Ed25519 seed is
|
||||
in KEY_FILE (32 bytes, made if missing), then:
|
||||
|
||||
import { readFile } from 'node:fs/promises';
|
||||
init NAME make a counter, count on it (incr incr decr), and keep it in
|
||||
your home under NAME
|
||||
use NAME get the counter kept under NAME and send it each VERB
|
||||
(incr, decr, value; default: incr value)
|
||||
|
||||
Each run's sends are pipelined: one update call after the handshake, plus
|
||||
one for init. HOST is the replica, e.g. http://127.0.0.1:4960, or
|
||||
https://icp-api.io.`;
|
||||
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { HttpAgent, Actor } from '@icp-sdk/core/agent';
|
||||
import { IDL } from '@icp-sdk/core/candid';
|
||||
import { Ed25519KeyIdentity } from '@icp-sdk/core/identity';
|
||||
import { openPeer } from './peer.js';
|
||||
|
||||
@@ -25,18 +31,30 @@ const idlFactory = ({ IDL }) => {
|
||||
});
|
||||
};
|
||||
|
||||
const main = async ([host, canisterId, ...verbs]) => {
|
||||
if (!host || !canisterId) {
|
||||
/** The seed in `path`, or a fresh one written there. */
|
||||
const keySeed = async path => {
|
||||
try {
|
||||
return new Uint8Array(await readFile(path));
|
||||
} catch (err) {
|
||||
if (err.code !== 'ENOENT') throw err;
|
||||
const seed = crypto.getRandomValues(new Uint8Array(32));
|
||||
await writeFile(path, seed, { mode: 0o600, flag: 'wx' });
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
const main = async ([host, canisterId, keyFile, command, name, ...verbs]) => {
|
||||
if (!name || !['init', 'use'].includes(command)) {
|
||||
console.error(HELP);
|
||||
process.exitCode = 2;
|
||||
return;
|
||||
}
|
||||
const local = new URL(host).hostname === '127.0.0.1' || new URL(host).hostname === 'localhost';
|
||||
const { hostname } = new URL(host);
|
||||
const agent = await HttpAgent.create({
|
||||
host,
|
||||
identity: Ed25519KeyIdentity.generate(),
|
||||
identity: Ed25519KeyIdentity.generate(await keySeed(keyFile)),
|
||||
// A local replica's root key is its own; mainnet's is built in.
|
||||
shouldFetchRootKey: local,
|
||||
shouldFetchRootKey: hostname === '127.0.0.1' || hostname === 'localhost',
|
||||
});
|
||||
const actor = Actor.createActor(idlFactory, { agent, canisterId });
|
||||
const carry = async (method, frames) => {
|
||||
@@ -52,12 +70,23 @@ const main = async ([host, canisterId, ...verbs]) => {
|
||||
|
||||
const peer = await openPeer({ module, seed, carry }, canisterId);
|
||||
console.log('session open');
|
||||
// Pipelined: the sends target the fetch's answer before it settles, so
|
||||
// the fetch and every send cross in one update call.
|
||||
const counter = peer.fetch('counter');
|
||||
const answers = (verbs.length ? verbs : ['incr', 'incr', 'decr']).map(verb => [verb, peer.send(counter, verb)]);
|
||||
await peer.flush();
|
||||
for (const [verb, answer] of answers) console.log(verb, peer.valueOf(answer));
|
||||
const home = peer.fetch('home');
|
||||
/** Send each verb without waiting, then print each answer. */
|
||||
const countOn = async (counter, sends) => {
|
||||
const answers = sends.map(verb => [verb, peer.send(counter, verb)]);
|
||||
await peer.flush();
|
||||
for (const [verb, answer] of answers) console.log(verb, peer.valueOf(answer));
|
||||
};
|
||||
if (command === 'init') {
|
||||
const counter = peer.send(peer.fetch('makeCounter'), 'make');
|
||||
await countOn(counter, ['incr', 'incr', 'decr']);
|
||||
const kept = peer.send(home, 'init', name, counter);
|
||||
await peer.flush();
|
||||
peer.valueOf(kept);
|
||||
console.log(`kept as ${name}`);
|
||||
} else {
|
||||
await countOn(peer.send(home, 'get', name), verbs.length ? verbs : ['incr', 'value']);
|
||||
}
|
||||
};
|
||||
|
||||
main(process.argv.slice(2)).catch(err => {
|
||||
|
||||
Reference in new issue
Block a user