@chaincraft/core 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,9 +3,9 @@
3
3
  TypeScript implementation of the Chaincraft wire protocol, object semantics, and
4
4
  Level 2 stack (consensus, ledgers, BitTorrent, channels). Same observable
5
5
  behaviour as the Python `chaincraft` package and the Rust `chaincraft` crate.
6
- Release **[1.5.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
7
- 1.5.0 adds `storage.index` on `MessageStore` (`register`, `query`). 45 of 45
8
- registry features (44 implemented plus the UDP framing exemption).
6
+ Release **[1.7.0](https://www.npmjs.com/package/@chaincraft/core/v/1.7.0)** on npm.
7
+ 1.7.0 adds `consensus.poet` (Proof of Elapsed Time on a shared Sloth seed).
8
+ 46 of 47 registry features implemented plus the UDP framing exemption.
9
9
 
10
10
  Open-source toolkit for peer-to-peer protocols like blockchains, chat rooms,
11
11
  BitTorrent swarms, and content-addressed stores.
@@ -0,0 +1,26 @@
1
+ /** Proof-of-authority object — SPEC/features/consensus-poa.md. */
2
+ import type { JsonValue } from './canonical.ts';
3
+ import { StateMemento } from './memento.ts';
4
+ import { BaseSharedObject } from './object.ts';
5
+ export declare function proposerFor(height: number, authorities: readonly string[]): string;
6
+ export declare function blockId(height: number, parent: string, payload: string, proposer: string): string;
7
+ export declare class ProofOfAuthority extends BaseSharedObject {
8
+ readonly authorities: readonly string[];
9
+ private readonly blocks;
10
+ private readonly order;
11
+ constructor(authorities: readonly string[]);
12
+ isValid(message: JsonValue): boolean;
13
+ addMessage(message: JsonValue, _frontierState?: StateMemento | undefined): StateMemento | undefined;
14
+ emitStateMemento(): StateMemento;
15
+ getStateDigests(): string[];
16
+ isMerkelized(): boolean;
17
+ state(): {
18
+ blocks: string[];
19
+ canonical: string[];
20
+ height: number;
21
+ tip: string;
22
+ };
23
+ private canonical;
24
+ private tip;
25
+ private parse;
26
+ }
@@ -0,0 +1,129 @@
1
+ /** Proof-of-authority object — SPEC/features/consensus-poa.md. */
2
+ import { StateMemento } from "./memento.js";
3
+ import { BaseSharedObject, merkleDigest } from "./object.js";
4
+ function isInt(value) {
5
+ return typeof value === 'number' && Number.isSafeInteger(value);
6
+ }
7
+ function asRecord(message) {
8
+ if (message === null || typeof message !== 'object' || Array.isArray(message)) {
9
+ return undefined;
10
+ }
11
+ return message;
12
+ }
13
+ export function proposerFor(height, authorities) {
14
+ const index = (height - 1) % authorities.length;
15
+ const proposer = authorities[index];
16
+ if (proposer === undefined) {
17
+ throw new RangeError('authorities must be non-empty');
18
+ }
19
+ return proposer;
20
+ }
21
+ export function blockId(height, parent, payload, proposer) {
22
+ return merkleDigest({ height, parent, payload, proposer });
23
+ }
24
+ export class ProofOfAuthority extends BaseSharedObject {
25
+ authorities;
26
+ blocks = new Map();
27
+ order = [];
28
+ constructor(authorities) {
29
+ super();
30
+ if (authorities.length === 0) {
31
+ throw new RangeError('authorities must be non-empty');
32
+ }
33
+ const seen = new Set();
34
+ for (const authority of authorities) {
35
+ if (typeof authority !== 'string' || authority.length === 0) {
36
+ throw new RangeError('each authority must be a non-empty string');
37
+ }
38
+ if (seen.has(authority)) {
39
+ throw new RangeError('authorities must be distinct');
40
+ }
41
+ seen.add(authority);
42
+ }
43
+ this.authorities = [...authorities];
44
+ }
45
+ isValid(message) {
46
+ return this.parse(message) !== undefined;
47
+ }
48
+ addMessage(message, _frontierState) {
49
+ const parsed = this.parse(message);
50
+ if (parsed === undefined)
51
+ return this.emitStateMemento();
52
+ this.blocks.set(parsed.digest, parsed.record);
53
+ this.order.push(parsed.digest);
54
+ return this.emitStateMemento();
55
+ }
56
+ emitStateMemento() {
57
+ const tip = this.tip();
58
+ return StateMemento.normalize(merkleDigest({ height: tip.height, tip: tip.id, v: 1 }), []);
59
+ }
60
+ getStateDigests() {
61
+ const tip = this.tip();
62
+ return tip.id ? [tip.id] : [];
63
+ }
64
+ isMerkelized() {
65
+ return true;
66
+ }
67
+ state() {
68
+ const tip = this.tip();
69
+ return {
70
+ blocks: [...this.order],
71
+ canonical: this.canonical(tip.id),
72
+ height: tip.height,
73
+ tip: tip.id,
74
+ };
75
+ }
76
+ canonical(tip) {
77
+ const chain = [];
78
+ let cursor = tip;
79
+ while (cursor !== '') {
80
+ chain.push(cursor);
81
+ cursor = this.blocks.get(cursor)?.parent ?? '';
82
+ }
83
+ chain.reverse();
84
+ return chain;
85
+ }
86
+ tip() {
87
+ let id = '';
88
+ let height = 0;
89
+ for (const [digest, record] of this.blocks) {
90
+ if (record.height > height || (record.height === height && digest < id)) {
91
+ id = digest;
92
+ height = record.height;
93
+ }
94
+ }
95
+ return { id, height };
96
+ }
97
+ parse(message) {
98
+ const record = asRecord(message);
99
+ if (record === undefined || record['consensus'] !== 'poa')
100
+ return undefined;
101
+ const height = record['height'];
102
+ const proposer = record['proposer'];
103
+ const parent = record['parent'];
104
+ const payload = record['payload'];
105
+ if (!isInt(height) || height < 1)
106
+ return undefined;
107
+ if (typeof proposer !== 'string' || proposer.length === 0)
108
+ return undefined;
109
+ if (typeof parent !== 'string')
110
+ return undefined;
111
+ if (typeof payload !== 'string' || payload.length === 0)
112
+ return undefined;
113
+ if (proposer !== proposerFor(height, this.authorities))
114
+ return undefined;
115
+ if (parent === '') {
116
+ if (height !== 1)
117
+ return undefined;
118
+ }
119
+ else {
120
+ const prior = this.blocks.get(parent);
121
+ if (prior === undefined || height !== prior.height + 1)
122
+ return undefined;
123
+ }
124
+ const digest = blockId(height, parent, payload, proposer);
125
+ if (this.blocks.has(digest))
126
+ return undefined;
127
+ return { digest, record: { height, parent, payload, proposer } };
128
+ }
129
+ }
@@ -0,0 +1,24 @@
1
+ /** Proof-of-elapsed-time object — SPEC/features/consensus-poet.md. */
2
+ import type { JsonValue } from './canonical.ts';
3
+ import { StateMemento } from './memento.ts';
4
+ import { BaseSharedObject } from './object.ts';
5
+ export declare function ticketId(author: string, payload: string, proof: string, wait: number): string;
6
+ export declare class ProofOfElapsedTime extends BaseSharedObject {
7
+ private readonly seedBytes;
8
+ private readonly tickets;
9
+ private readonly order;
10
+ constructor(seed: string);
11
+ isValid(message: JsonValue): boolean;
12
+ addMessage(message: JsonValue, _frontierState?: StateMemento | undefined): StateMemento | undefined;
13
+ emitStateMemento(): StateMemento;
14
+ getStateDigests(): string[];
15
+ isMerkelized(): boolean;
16
+ state(): {
17
+ canonical: string[];
18
+ tickets: string[];
19
+ wait: number;
20
+ winner: string;
21
+ };
22
+ private winner;
23
+ private parse;
24
+ }
@@ -0,0 +1,121 @@
1
+ /** Proof-of-elapsed-time object — SPEC/features/consensus-poet.md. */
2
+ import { StateMemento } from "./memento.js";
3
+ import { BaseSharedObject, merkleDigest } from "./object.js";
4
+ import { proofToHex, hexToProof, verify } from "./vdf.js";
5
+ function isWait(value) {
6
+ return (typeof value === 'number' &&
7
+ Number.isSafeInteger(value) &&
8
+ value >= 1 &&
9
+ value <= 4_294_967_295);
10
+ }
11
+ function asRecord(message) {
12
+ if (message === null || typeof message !== 'object' || Array.isArray(message)) {
13
+ return undefined;
14
+ }
15
+ return message;
16
+ }
17
+ function canonicalProofHex(text) {
18
+ if (typeof text !== 'string')
19
+ return undefined;
20
+ let proof;
21
+ try {
22
+ proof = hexToProof(text);
23
+ }
24
+ catch {
25
+ return undefined;
26
+ }
27
+ const canonical = proofToHex(proof);
28
+ if (canonical !== text)
29
+ return undefined;
30
+ return text;
31
+ }
32
+ export function ticketId(author, payload, proof, wait) {
33
+ return merkleDigest({ author, payload, proof, wait });
34
+ }
35
+ export class ProofOfElapsedTime extends BaseSharedObject {
36
+ seedBytes;
37
+ tickets = new Map();
38
+ order = [];
39
+ constructor(seed) {
40
+ super();
41
+ if (typeof seed !== 'string' || seed.length === 0) {
42
+ throw new Error('seed must be a non-empty string');
43
+ }
44
+ this.seedBytes = new TextEncoder().encode(seed);
45
+ }
46
+ isValid(message) {
47
+ return this.parse(message) !== undefined;
48
+ }
49
+ addMessage(message, _frontierState) {
50
+ const parsed = this.parse(message);
51
+ if (parsed === undefined)
52
+ return this.emitStateMemento();
53
+ this.tickets.set(parsed.digest, parsed.record);
54
+ this.order.push(parsed.digest);
55
+ return this.emitStateMemento();
56
+ }
57
+ emitStateMemento() {
58
+ const winner = this.winner();
59
+ return StateMemento.normalize(merkleDigest({ v: 1, wait: winner.wait, winner: winner.id }), []);
60
+ }
61
+ getStateDigests() {
62
+ const winner = this.winner();
63
+ return winner.id ? [winner.id] : [];
64
+ }
65
+ isMerkelized() {
66
+ return true;
67
+ }
68
+ state() {
69
+ const winner = this.winner();
70
+ return {
71
+ canonical: winner.id ? [winner.id] : [],
72
+ tickets: [...this.order],
73
+ wait: winner.wait,
74
+ winner: winner.id,
75
+ };
76
+ }
77
+ winner() {
78
+ let id = '';
79
+ let wait = 0;
80
+ for (const [digest, record] of this.tickets) {
81
+ if (record.wait < wait ||
82
+ wait === 0 ||
83
+ (record.wait === wait && (id === '' || digest < id))) {
84
+ id = digest;
85
+ wait = record.wait;
86
+ }
87
+ }
88
+ return { id, wait };
89
+ }
90
+ parse(message) {
91
+ const record = asRecord(message);
92
+ if (record === undefined || record['consensus'] !== 'poet')
93
+ return undefined;
94
+ const wait = record['wait'];
95
+ const author = record['author'];
96
+ const payload = record['payload'];
97
+ const proof = record['proof'];
98
+ if (!isWait(wait))
99
+ return undefined;
100
+ if (typeof author !== 'string' || author.length === 0)
101
+ return undefined;
102
+ if (typeof payload !== 'string' || payload.length === 0)
103
+ return undefined;
104
+ const proofHex = canonicalProofHex(proof);
105
+ if (proofHex === undefined)
106
+ return undefined;
107
+ let proofInt;
108
+ try {
109
+ proofInt = hexToProof(proofHex);
110
+ }
111
+ catch {
112
+ return undefined;
113
+ }
114
+ if (!verify(this.seedBytes, wait, proofInt))
115
+ return undefined;
116
+ const digest = ticketId(author, payload, proofHex, wait);
117
+ if (this.tickets.has(digest))
118
+ return undefined;
119
+ return { digest, record: { wait } };
120
+ }
121
+ }
@@ -7,9 +7,9 @@
7
7
  *
8
8
  * Add an ID here only when the feature is implemented and its vectors pass.
9
9
  */
10
- export declare const VERSION = "1.5.0";
10
+ export declare const VERSION = "1.7.0";
11
11
  export declare const WIRE_VERSION = 1;
12
- export declare const IMPL = "ts-chaincraft/1.5.0";
12
+ export declare const IMPL = "ts-chaincraft/1.7.0";
13
13
  export declare const LANGUAGE = "typescript";
14
14
  export declare const FEATURES: readonly string[];
15
15
  export interface Manifest {
package/dist/features.js CHANGED
@@ -7,7 +7,7 @@
7
7
  *
8
8
  * Add an ID here only when the feature is implemented and its vectors pass.
9
9
  */
10
- export const VERSION = '1.5.0';
10
+ export const VERSION = '1.7.0';
11
11
  export const WIRE_VERSION = 1;
12
12
  export const IMPL = `ts-chaincraft/${VERSION}`;
13
13
  export const LANGUAGE = 'typescript';
@@ -41,6 +41,8 @@ export const FEATURES = [
41
41
  'consensus.pow',
42
42
  'consensus.pos',
43
43
  'consensus.pbft',
44
+ 'consensus.poa',
45
+ 'consensus.poet',
44
46
  'consensus.avalanche',
45
47
  'ledger.balance',
46
48
  'ledger.utxo',
package/dist/index.d.ts CHANGED
@@ -17,6 +17,8 @@ export { type ApplyResult, ObjectRegistry } from './pipeline.ts';
17
17
  export { Avalanche, DEFAULT_ALPHA as AVALANCHE_DEFAULT_ALPHA, DEFAULT_BETA as AVALANCHE_DEFAULT_BETA, DEFAULT_K as AVALANCHE_DEFAULT_K, } from './consensusAvalanche.ts';
18
18
  export { PBFT, DEFAULT_REPLICAS as PBFT_DEFAULT_REPLICAS } from './consensusPbft.ts';
19
19
  export { ProofOfStake, DEFAULT_SEED, DEFAULT_VALIDATORS, proposerFor } from './consensusPos.ts';
20
+ export { ProofOfAuthority, blockId as poaBlockId, proposerFor as poaProposerFor } from './consensusPoa.ts';
21
+ export { ProofOfElapsedTime, ticketId as poetTicketId } from './consensusPoet.ts';
20
22
  export { ProofOfWork, DEFAULT_DIFFICULTY, blockId, meetsWork } from './consensusPow.ts';
21
23
  export { BalanceLedger, DEFAULT_ALLOCATIONS as BALANCE_DEFAULT_ALLOCATIONS } from './ledgerBalance.ts';
22
24
  export { CrdtKv, MAX_KEYS as CRDT_MAX_KEYS, MAX_VALUE as CRDT_MAX_VALUE } from './crdt.ts';
package/dist/index.js CHANGED
@@ -17,6 +17,8 @@ export { ObjectRegistry } from "./pipeline.js";
17
17
  export { Avalanche, DEFAULT_ALPHA as AVALANCHE_DEFAULT_ALPHA, DEFAULT_BETA as AVALANCHE_DEFAULT_BETA, DEFAULT_K as AVALANCHE_DEFAULT_K, } from "./consensusAvalanche.js";
18
18
  export { PBFT, DEFAULT_REPLICAS as PBFT_DEFAULT_REPLICAS } from "./consensusPbft.js";
19
19
  export { ProofOfStake, DEFAULT_SEED, DEFAULT_VALIDATORS, proposerFor } from "./consensusPos.js";
20
+ export { ProofOfAuthority, blockId as poaBlockId, proposerFor as poaProposerFor } from "./consensusPoa.js";
21
+ export { ProofOfElapsedTime, ticketId as poetTicketId } from "./consensusPoet.js";
20
22
  export { ProofOfWork, DEFAULT_DIFFICULTY, blockId, meetsWork } from "./consensusPow.js";
21
23
  export { BalanceLedger, DEFAULT_ALLOCATIONS as BALANCE_DEFAULT_ALLOCATIONS } from "./ledgerBalance.js";
22
24
  export { CrdtKv, MAX_KEYS as CRDT_MAX_KEYS, MAX_VALUE as CRDT_MAX_VALUE } from "./crdt.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaincraft/core",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "A platform for decentralized protocols — wire, objects, and pedagogical consensus",
5
5
  "license": "MIT",
6
6
  "type": "module",