@chaincraft/core 1.6.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.6.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
7
- 1.6.0 adds `consensus.poa` (Proof of Authority with rotation by height).
8
- 45 of 46 registry features 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,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.6.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.6.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.6.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';
@@ -42,6 +42,7 @@ export const FEATURES = [
42
42
  'consensus.pos',
43
43
  'consensus.pbft',
44
44
  'consensus.poa',
45
+ 'consensus.poet',
45
46
  'consensus.avalanche',
46
47
  'ledger.balance',
47
48
  'ledger.utxo',
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export { Avalanche, DEFAULT_ALPHA as AVALANCHE_DEFAULT_ALPHA, DEFAULT_BETA as AV
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
20
  export { ProofOfAuthority, blockId as poaBlockId, proposerFor as poaProposerFor } from './consensusPoa.ts';
21
+ export { ProofOfElapsedTime, ticketId as poetTicketId } from './consensusPoet.ts';
21
22
  export { ProofOfWork, DEFAULT_DIFFICULTY, blockId, meetsWork } from './consensusPow.ts';
22
23
  export { BalanceLedger, DEFAULT_ALLOCATIONS as BALANCE_DEFAULT_ALLOCATIONS } from './ledgerBalance.ts';
23
24
  export { CrdtKv, MAX_KEYS as CRDT_MAX_KEYS, MAX_VALUE as CRDT_MAX_VALUE } from './crdt.ts';
package/dist/index.js CHANGED
@@ -18,6 +18,7 @@ export { Avalanche, DEFAULT_ALPHA as AVALANCHE_DEFAULT_ALPHA, DEFAULT_BETA as AV
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
20
  export { ProofOfAuthority, blockId as poaBlockId, proposerFor as poaProposerFor } from "./consensusPoa.js";
21
+ export { ProofOfElapsedTime, ticketId as poetTicketId } from "./consensusPoet.js";
21
22
  export { ProofOfWork, DEFAULT_DIFFICULTY, blockId, meetsWork } from "./consensusPow.js";
22
23
  export { BalanceLedger, DEFAULT_ALLOCATIONS as BALANCE_DEFAULT_ALLOCATIONS } from "./ledgerBalance.js";
23
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.6.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",