Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Aug 12, 2024
1 parent 2360045 commit 934e764
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 53 deletions.
6 changes: 5 additions & 1 deletion packages/log/src/entry-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { LamportClock as Clock } from "./clock.js";
import type { Encoding } from "./encoding.js";
import type { EntryType } from "./entry-type.js";
import { type EntryEncryption, EntryV0 } from "./entry-v0.js";
import { EntryV1 } from "./entry-v1.js";
import type { CanAppend, Entry } from "./entry.js";
import type { SortableEntry } from "./log-sorting.js";

Expand All @@ -26,5 +27,8 @@ export const createEntry = async <T>(properties: {
data: Uint8Array,
) => Promise<SignatureWithKey> | SignatureWithKey)[];
}): Promise<Entry<T>> => {
return EntryV0.create(properties);
if (properties.encryption) {
return EntryV0.create(properties);
}
return EntryV1.create(properties);
};
46 changes: 46 additions & 0 deletions packages/log/src/entry-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { field, option, variant, vec } from "@dao-xyz/borsh";
import { LamportClock as Clock } from "./clock.js";
import { EntryType } from "./entry-type.js";

@variant(0)
export class Meta {
@field({ type: Clock })
clock: Clock;

@field({ type: "string" })
gid: string; // graph id

@field({ type: vec("string") })
next: string[];

@field({ type: "u8" })
type: EntryType;

@field({ type: option(Uint8Array) })
data?: Uint8Array; // Optional metadata

constructor(properties: {
gid: string;
clock: Clock;
type: EntryType;
data?: Uint8Array;
next: string[];
}) {
this.gid = properties.gid;
this.clock = properties.clock;
this.type = properties.type;
this.data = properties.data;
this.next = properties.next;
}

equals(other: Meta): boolean {
return (
this.gid === other.gid &&
this.clock.equals(other.clock) &&
this.clock.id === other.clock.id &&
this.type === other.type &&
this.data === other.data &&
this.next === other.next
);
}
}
45 changes: 7 additions & 38 deletions packages/log/src/entry-v0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { type Keychain } from "@peerbit/keychain";
import { compare } from "uint8arrays";
import { LamportClock as Clock, HLC, Timestamp } from "./clock.js";
import { type Encoding, NO_ENCODING } from "./encoding.js";
import { Meta } from "./entry-meta.js";
import { ShallowEntry, ShallowMeta } from "./entry-shallow.js";
import { EntryType } from "./entry-type.js";
import { type CanAppend, Entry } from "./entry.js";
Expand Down Expand Up @@ -70,39 +71,7 @@ export interface EntryEncryptionTemplate<A, B, C> {
}

@variant(0)
export class Meta {
@field({ type: Clock })
clock: Clock;

@field({ type: "string" })
gid: string; // graph id

@field({ type: vec("string") })
next: string[];

@field({ type: "u8" })
type: EntryType;

@field({ type: option(Uint8Array) })
data?: Uint8Array; // Optional metadata

constructor(properties: {
gid: string;
clock: Clock;
type: EntryType;
data?: Uint8Array;
next: string[];
}) {
this.gid = properties.gid;
this.clock = properties.clock;
this.type = properties.type;
this.data = properties.data;
this.next = properties.next;
}
}

@variant(0)
export class Signatures {
export class MaybeEncryptedSignatures {
@field({ type: vec(MaybeEncrypted) })
signatures!: MaybeEncrypted<SignatureWithKey>[];

Expand All @@ -112,7 +81,7 @@ export class Signatures {
}
}

equals(other: Signatures) {
equals(other: MaybeEncryptedSignatures) {
if (this.signatures.length !== other.signatures.length) {
return false;
}
Expand Down Expand Up @@ -164,8 +133,8 @@ export class EntryV0<T>
@field({ type: fixedArray("u8", 4) })
_reserved?: Uint8Array;

@field({ type: option(Signatures) })
_signatures?: Signatures;
@field({ type: option(MaybeEncryptedSignatures) })
_signatures?: MaybeEncryptedSignatures;

@field({ type: option("string") }) // we do option because we serialize and store this in a block without the hash, to receive the hash, which we later set
hash!: string; // "zd...Foo", we'll set the hash after persisting the entry
Expand All @@ -177,7 +146,7 @@ export class EntryV0<T>

constructor(obj: {
payload: MaybeEncrypted<Payload<T>>;
signatures?: Signatures;
signatures?: MaybeEncryptedSignatures;
meta: MaybeEncrypted<Meta>;
reserved?: Uint8Array; // intentational type 0 (not used)h
hash?: string;
Expand Down Expand Up @@ -557,7 +526,7 @@ export class EntryV0<T>
encryptedSignatures.push(signatureEncrypted);
}

entry._signatures = new Signatures({
entry._signatures = new MaybeEncryptedSignatures({
signatures: encryptedSignatures,
});

Expand Down
Loading

0 comments on commit 934e764

Please sign in to comment.