Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c46846c
chore: save current draft
Gozala Feb 21, 2025
aa68638
chore: save changes for pull
Gozala Feb 24, 2025
55b892a
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Feb 24, 2025
abebdab
feat: implement signing & verification
Gozala Feb 25, 2025
3614559
chore: enable CI tests
Gozala Feb 25, 2025
a9eeb93
chore: add multiformats to deps
Gozala Feb 25, 2025
2736a26
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Feb 27, 2025
891628e
fix: problems introduced by merge
Gozala Feb 27, 2025
126b374
setting nodeModulesDir to none to avoid npm compatiblity mode
Feb 26, 2025
a2ed4bd
fix: update toolshed with signing api
Gozala Feb 28, 2025
3748456
chore: format code
Gozala Feb 28, 2025
6776bcb
chore: update lockfile
Gozala Feb 28, 2025
63a7351
chore: update remote storage
Gozala Feb 28, 2025
2043660
fix: workaround deps across deno/node
Gozala Feb 28, 2025
ad16009
fix: remove deno deps from common code
Gozala Feb 28, 2025
25f11a6
fix: integrate with common-identity
Gozala Feb 28, 2025
a77fdea
Merge remote-tracking branch 'origin/main' into feat/signed-transacti…
Gozala Feb 28, 2025
e2f4478
fix: post-merge regressions
Gozala Mar 1, 2025
e090940
fix: post-merge regression
Gozala Mar 1, 2025
73fe05a
chore: format with deno
Gozala Mar 1, 2025
842890e
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Mar 1, 2025
ae869d0
fix: build error
Gozala Mar 1, 2025
bed4afd
fix: failing test
Gozala Mar 1, 2025
65e3f26
chore: remove memory.yml as it is subsumed by unit-tests.yml
Gozala Mar 3, 2025
9fa1825
fix: pass through signer
Gozala Mar 3, 2025
b700829
chore: save current state
Gozala Mar 3, 2025
6af0585
fix: sanitize data before JSON serialization
Gozala Mar 3, 2025
22ce52c
fix: issues that caused failures
Gozala Mar 4, 2025
d31be70
fix: remaining problems
Gozala Mar 4, 2025
9bfc5e5
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Mar 4, 2025
dad5957
chore: remove unrelated file
Gozala Mar 4, 2025
22edd38
chore: undo unintended changes
Gozala Mar 4, 2025
3fbcfc7
fix: common-identity tests
Gozala Mar 4, 2025
a30a561
fix: avoid DAG-JSON because we use / in an incompatible way
Gozala Mar 4, 2025
324dbb2
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Mar 4, 2025
50dfd92
chore: save for now
Gozala Mar 4, 2025
971d742
fix: bytes decoding
Gozala Mar 4, 2025
8d1e248
Merge remote-tracking branch 'origin/main' into feat/signed-transactions
Gozala Mar 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .prettierignore

This file was deleted.

12 changes: 0 additions & 12 deletions .prettierrc.json

This file was deleted.

1 change: 1 addition & 0 deletions typescript/packages/clipper-extension/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import browser from "webextension-polyfill";

browser.runtime.onInstalled.addListener(() => {
Expand Down
17 changes: 16 additions & 1 deletion typescript/packages/common-charm/src/charm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { storage } from "./storage.ts";
import { syncRecipeBlobby } from "./syncRecipe.ts";
import { getSpace, Space } from "@commontools/runner";
import { DID, Identity, Signer } from "@commontools/identity";

export type Charm = {
[NAME]?: string;
Expand Down Expand Up @@ -85,11 +86,25 @@ export class CharmManager {
private charms: Cell<Cell<Charm>[]>;
private pinnedCharms: Cell<Cell<Charm>[]>;

constructor(private spaceId: string) {
static async open(
{ space, signer }: { space: DID; signer?: Signer },
) {
return new this(
space,
signer ?? await Identity.fromPassphrase("charm manager"),
);
}

constructor(
private spaceId: string,
private signer: Signer,
) {
this.space = getSpace(this.spaceId);
this.charmsDoc = getDoc<DocLink[]>([], "charms", this.space);
this.pinned = getDoc<DocLink[]>([], "pinned-charms", this.space);
this.charms = this.charmsDoc.asCell([], undefined, charmListSchema);

storage.setSigner(signer);
this.pinnedCharms = this.pinned.asCell([], undefined, charmListSchema);
}

Expand Down
14 changes: 13 additions & 1 deletion typescript/packages/common-charm/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
Space,
useCancelGroup,
} from "@commontools/runner";

import { isStatic, markAsStatic } from "@commontools/builder";
import { StorageProvider, StorageValue } from "./storage/base.ts";
import { RemoteStorageProvider } from "./storage/remote.ts";
import { debug } from "@commontools/html"; // FIXME(ja): can we move debug to somewhere else?
import { InMemoryStorageProvider } from "./storage/memory.ts";
import { Signer } from "@commontools/identity";

export function log(fn: () => any[]) {
debug(() => {
Expand Down Expand Up @@ -52,6 +54,8 @@ export interface Storage {
*/
setRemoteStorage(url: URL): void;

setSigner(signer: Signer): void;

/**
* Load cell from storage. Will also subscribe to new changes.
*
Expand Down Expand Up @@ -157,6 +161,8 @@ class StorageImpl implements Storage {
private storageProviders = new Map<Space, StorageProvider>();
private remoteStorageUrl: URL | undefined;

private signer: Signer | undefined;

// Map from entity ID to doc, set at stage 2, i.e. already while loading
private docsById = new Map<string, DocImpl<any>>();

Expand Down Expand Up @@ -196,6 +202,10 @@ class StorageImpl implements Storage {
this.remoteStorageUrl = url;
}

setSigner(signer: Signer): void {
this.signer = signer;
}

syncCellById<T>(
space: Space,
id: EntityId | string,
Expand Down Expand Up @@ -240,6 +250,7 @@ class StorageImpl implements Storage {
// TODO(seefeld,gozala): Should just be one again.
private _getStorageProviderForSpace(space: Space): StorageProvider {
if (!space) throw new Error("No space set");
if (!this.signer) throw new Error("No signer set");

let provider = this.storageProviders.get(space);

Expand All @@ -258,6 +269,7 @@ class StorageImpl implements Storage {
provider = new RemoteStorageProvider({
address: new URL("/api/storage/memory", this.remoteStorageUrl!),
space: space.uri as `did:${string}:${string}`,
as: this.signer,
});
} else if (type === "memory") {
provider = new InMemoryStorageProvider(space.uri);
Expand Down Expand Up @@ -578,7 +590,7 @@ class StorageImpl implements Storage {
}
}
log(
() => ["loading", [...loading].map((c) => JSON.stringify(c.entityId))],
() => ["loading", [...loading].map((c) => JSON.stringify(c.entityId))]
);
log(() => [
"docIsLoading",
Expand Down
31 changes: 18 additions & 13 deletions typescript/packages/common-charm/src/storage/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import type {
Entity,
JSONValue,
MemorySpace,
Protocol,
UCAN,
} from "@commontools/memory/interface";
import * as Memory from "@commontools/memory/consumer";
import { assert } from "@commontools/memory/fact";
import * as Changes from "@commontools/memory/changes";
export * from "@commontools/memory/interface";
import * as Codec from "@commontools/memory/codec";

/**
* Represents a state of the memory space.
Expand All @@ -36,36 +39,36 @@ interface MemoryState<Space extends MemorySpace = MemorySpace> {
* ed25519 key derived from the sha256 of the "common knowledge".
*/
const HOME = "did:key:z6Mko2qR9b8mbdPnaEKXvcYwdK7iDnRkh8mEcEP2719aCu6P";
/**
* ed25519 key derived from the sha256 of the "common operator".
*/
const AS = "did:key:z6Mkge3xkXc4ksLsf8CtRxunUxcX6dByT4QdWCVEHbUJ8YVn";

export class RemoteStorageProvider implements StorageProvider {
connection: WebSocket | null = null;
address: URL;
workspace: MemorySpace;
the: string;
state: Map<MemorySpace, MemoryState> = new Map();
session: Memory.MemorySession;
session: Memory.MemorySession<MemorySpace>;

/**
* queue that holds commands that we read from the session, but could not
* send because connection was down.
*/
queue: Set<Memory.ConsumerCommand<Memory.Protocol>> = new Set();
queue: Set<UCAN<Memory.ConsumerCommandInvocation<Memory.Protocol>>> =
new Set();
writer: WritableStreamDefaultWriter<Memory.ProviderCommand<Memory.Protocol>>;
reader: ReadableStreamDefaultReader<Memory.ConsumerCommand<Memory.Protocol>>;
reader: ReadableStreamDefaultReader<
UCAN<Memory.ConsumerCommandInvocation<Memory.Protocol>>
>;

connectionCount = 0;

constructor({
address,
as = AS,
as,
space = HOME,
the = "application/json",
}: {
address: URL;
as?: Memory.Principal;
as: Memory.Signer;
space?: MemorySpace;
the?: string;
}) {
Expand All @@ -74,6 +77,7 @@ export class RemoteStorageProvider implements StorageProvider {
this.the = the;

const session = Memory.create({ as });

this.reader = session.readable.getReader();
this.writer = session.writable.getWriter();
this.session = session;
Expand Down Expand Up @@ -199,6 +203,7 @@ export class RemoteStorageProvider implements StorageProvider {
local.set(of, fact);
facts.push(fact);
}


const result = await memory.transact({ changes: Changes.from(facts) });

Expand Down Expand Up @@ -233,7 +238,7 @@ export class RemoteStorageProvider implements StorageProvider {
}

receive(data: string) {
return this.writer.write(JSON.parse(data));
return this.writer.write(Codec.Receipt.fromString(data));
}

handleEvent(event: MessageEvent) {
Expand Down Expand Up @@ -283,7 +288,7 @@ export class RemoteStorageProvider implements StorageProvider {
while (this.connection === socket) {
// First drain the queued commands if we have them.
for (const command of queue) {
socket.send(JSON.stringify(command));
socket.send(Codec.UCAN.toString(command));
queue.delete(command);
}

Expand All @@ -299,7 +304,7 @@ export class RemoteStorageProvider implements StorageProvider {
// Now we make that our socket is still a current connection as we may
// have lost connection while waiting to read a command.
if (this.connection === socket) {
socket.send(JSON.stringify(command));
socket.send(Codec.UCAN.toString(command));
} // If it is no longer our connection we simply add the command into a
// queue so it will be send once connection is reopen.
else {
Expand Down Expand Up @@ -391,7 +396,7 @@ export interface Subscriber {
class Query<Space extends MemorySpace> {
reader: ReadableStreamDefaultReader;
constructor(
public query: Memory.QueryView<Space>,
public query: Memory.QueryView<Space, Protocol<Space>>,
public subscribers: Set<Subscriber> = new Set(),
) {
this.reader = query.subscribe().getReader();
Expand Down
2 changes: 2 additions & 0 deletions typescript/packages/common-charm/test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { storage } from "../src/storage.ts";
import { StorageProvider } from "../src/storage/base.ts";
import { InMemoryStorageProvider } from "../src/storage/memory.ts";
import { createRef, DocImpl, getDoc, getSpace } from "@commontools/runner";
import { Identity } from "@commontools/identity";

storage.setRemoteStorage(new URL("memory://"));
storage.setSigner(await Identity.fromPassphrase("test operator"));

describe("Storage", () => {
let storage2: StorageProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { DocImpl, getDoc } from "../common-runner/src/doc.ts";
import { EntityId } from "../common-runner/src/doc-map.ts";
import { storage } from "../common-charm/src/storage.ts";
import { getSpace, Space } from "../common-runner/src/space.ts";
import { Identity } from "../common-identity/src/index.ts";

const replica = "ellyse7";
const TOOLSHED_API_URL = "https://toolshed.saga-castor.ts.net/";

// simple log function
Expand All @@ -26,7 +26,7 @@ function createCell(space: Space): Cell<Charm> {
const myCharm: Charm = {
NAME: "mycharm",
UI: "someui",
"somekey": "some value",
somekey: "some value",
};

// make this a DocImpl<Charm> because we need to return a Cell<Charm> since
Expand All @@ -40,12 +40,16 @@ function createCell(space: Space): Cell<Charm> {
}

async function main() {
const authority = await Identity.fromPassphrase("charm manager");
// create a charm manager to start things off
const charmManager = new CharmManager(replica);
const charmManager = await CharmManager.open({
space: authority.did(),
signer: authority,
});
log(charmManager, "charmManager");

// let's try to create a cell
const space: Space = getSpace(replica);
const space: Space = getSpace(authority.did());
const cell: Cell<Charm> = createCell(space);
log(cell.get(), "cell value from Cell.get()");

Expand Down
Empty file.
9 changes: 6 additions & 3 deletions typescript/packages/common-cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
storage,
} from "@commontools/charm";
import { getEntityId, isStream } from "@commontools/runner";
import { Identity } from "@commontools/identity";

let { space, charmId, recipeFile, cause } = parse(Deno.args);

Expand All @@ -17,10 +18,12 @@ storage.setRemoteStorage(new URL(toolshedUrl));
setBobbyServerUrl(toolshedUrl);

async function main() {
if (!space) space = "common-cli";
console.log("params:", { space, charmId, recipeFile, cause });

const manager = new CharmManager(space);
const identity = await Identity.fromPassphrase("common-cli");
const manager = await CharmManager.open({
space: space ?? identity.did(),
signer: identity,
});
const charms = await manager.getCharms();

charms.sink((charms) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { MemorySpace } from "@commontools/memory";
import { RemoteStorageProvider } from "../common-charm/src/storage/remote.ts";
import { StorageProvider } from "../common-charm/src/storage/base.ts";
import { EntityId } from "../common-runner/src/doc-map.ts";

// some config stuff, hardcoded, ofcourse
const replica = "ellyse5";
import { Identity } from "../common-identity/src/index.ts";

// i'm running common memory locally, so connect to it directly
const BASE_URL = "http://localhost:8000";
Expand Down Expand Up @@ -35,9 +33,12 @@ function entity_id(i: number): EntityId {
}

async function main() {
const authority = await Identity.fromPassphrase("ellyse5");

const storageProvider: StorageProvider = new RemoteStorageProvider({
address: new URL("/api/storage/memory", BASE_URL),
space: replica as MemorySpace,
space: authority.did(),
as: authority,
});

console.log(
Expand Down
1 change: 1 addition & 0 deletions typescript/packages/common-cli/recipes/simpleValue.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { h } from "@commontools/html";
import {
cell,
Expand Down
Loading