streamseed 0.0.0-stage → 0.1.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.
Files changed (43) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +88 -2
  3. package/dist/control/control.d.ts +89 -0
  4. package/dist/control/control.js +396 -0
  5. package/dist/index.d.ts +19 -0
  6. package/dist/index.js +15 -0
  7. package/dist/internal/abort.d.ts +8 -0
  8. package/dist/internal/abort.js +52 -0
  9. package/dist/internal/cbor-codec.d.ts +25 -0
  10. package/dist/internal/cbor-codec.js +134 -0
  11. package/dist/internal/ed25519.d.ts +15 -0
  12. package/dist/internal/ed25519.js +55 -0
  13. package/dist/internal/secp256k1.d.ts +3 -0
  14. package/dist/internal/secp256k1.js +15 -0
  15. package/dist/node/codec.d.ts +23 -0
  16. package/dist/node/codec.js +231 -0
  17. package/dist/node/document.d.ts +10 -0
  18. package/dist/node/document.js +123 -0
  19. package/dist/node/media.d.ts +6 -0
  20. package/dist/node/media.js +69 -0
  21. package/dist/node/sequence.d.ts +44 -0
  22. package/dist/node/sequence.js +137 -0
  23. package/dist/node/types.d.ts +93 -0
  24. package/dist/node/types.js +105 -0
  25. package/dist/program/codec.d.ts +10 -0
  26. package/dist/program/codec.js +165 -0
  27. package/dist/program/resolver.d.ts +25 -0
  28. package/dist/program/resolver.js +189 -0
  29. package/dist/program/types.d.ts +61 -0
  30. package/dist/program/types.js +90 -0
  31. package/dist/protocol/constants.d.ts +33 -0
  32. package/dist/protocol/constants.js +32 -0
  33. package/dist/protocol/errors.d.ts +46 -0
  34. package/dist/protocol/errors.js +57 -0
  35. package/dist/protocol/hash.d.ts +14 -0
  36. package/dist/protocol/hash.js +42 -0
  37. package/dist/protocol/ids.d.ts +60 -0
  38. package/dist/protocol/ids.js +139 -0
  39. package/dist/wire/artifact.d.ts +28 -0
  40. package/dist/wire/artifact.js +74 -0
  41. package/dist/wire/codec.d.ts +12 -0
  42. package/dist/wire/codec.js +42 -0
  43. package/package.json +79 -3
@@ -0,0 +1,139 @@
1
+ import { CHANNEL_PUBLIC_KEY_SIZE, DIGEST_SIZE, MAX_UINT64, PUBLIC_KEY_SIZE, SIGNATURE_SIZE } from "./constants.js";
2
+ import { ERROR_CODES, streamSeedError } from "./errors.js";
3
+ import { isValidCompressedPublicKey } from "../internal/secp256k1.js";
4
+ /** 复制字节,同时保留已经通过构造器建立的 brand。 */
5
+ export function copyBytes(value) {
6
+ return new Uint8Array(value);
7
+ }
8
+ function brand(value) {
9
+ return value;
10
+ }
11
+ export function assertLength(value, expected, code = ERROR_CODES.INVALID_LENGTH) {
12
+ if (value.length !== expected) {
13
+ throw streamSeedError(code, `byte length ${value.length}, expected ${expected}`);
14
+ }
15
+ }
16
+ export function newDigest(value) {
17
+ assertLength(value, DIGEST_SIZE);
18
+ return brand(copyBytes(value));
19
+ }
20
+ export function newStreamID(value) {
21
+ assertLength(value, DIGEST_SIZE);
22
+ return brand(copyBytes(value));
23
+ }
24
+ export function newPublicKey(value) {
25
+ assertLength(value, PUBLIC_KEY_SIZE);
26
+ return brand(copyBytes(value));
27
+ }
28
+ export function newChannelPublicKey(value) {
29
+ assertLength(value, CHANNEL_PUBLIC_KEY_SIZE);
30
+ if (!isValidCompressedPublicKey(value)) {
31
+ throw streamSeedError(ERROR_CODES.INVALID_PUBLIC_KEY, "channel public key is not a canonical compressed secp256k1 point");
32
+ }
33
+ return brand(copyBytes(value));
34
+ }
35
+ export function newSignature(value) {
36
+ assertLength(value, SIGNATURE_SIZE);
37
+ return brand(copyBytes(value));
38
+ }
39
+ export function newReleaseSalt(value) {
40
+ assertLength(value, DIGEST_SIZE);
41
+ if (value.every((byte) => byte === 0)) {
42
+ throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "release salt must not be all zero");
43
+ }
44
+ return brand(copyBytes(value));
45
+ }
46
+ /** 使用 Web Crypto getRandomValues 生成非全零发行盐。 */
47
+ export function generateReleaseSalt() {
48
+ const output = new Uint8Array(DIGEST_SIZE);
49
+ try {
50
+ if (!globalThis.crypto?.getRandomValues)
51
+ throw new Error("Web Crypto getRandomValues is unavailable");
52
+ globalThis.crypto.getRandomValues(output);
53
+ }
54
+ catch (error) {
55
+ throw streamSeedError(ERROR_CODES.RANDOM_SOURCE_FAILURE, "unable to generate release salt", error);
56
+ }
57
+ if (output.every((byte) => byte === 0))
58
+ throw streamSeedError(ERROR_CODES.RANDOM_SOURCE_FAILURE, "random source returned all-zero release salt");
59
+ return newReleaseSalt(output);
60
+ }
61
+ const EMPTY_PAYLOAD_HASH = new Uint8Array([
62
+ 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
63
+ 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
64
+ 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
65
+ 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
66
+ ]);
67
+ /** 返回 END Node 使用的 SHA-256(empty) 副本;不会暴露可变全局数组。 */
68
+ export function emptyPayloadHash() {
69
+ return newDigest(EMPTY_PAYLOAD_HASH);
70
+ }
71
+ export function bytesEqual(left, right) {
72
+ if (left.length !== right.length)
73
+ return false;
74
+ let difference = 0;
75
+ for (let index = 0; index < left.length; index += 1) {
76
+ difference |= (left[index] ?? 0) ^ (right[index] ?? 0);
77
+ }
78
+ return difference === 0;
79
+ }
80
+ export function bytesToHex(value) {
81
+ let output = "";
82
+ for (const byte of value)
83
+ output += byte.toString(16).padStart(2, "0");
84
+ return output;
85
+ }
86
+ export function hexToBytes(value) {
87
+ if (value.length !== DIGEST_SIZE * 2 || !/^[0-9a-fA-F]+$/.test(value)) {
88
+ throw streamSeedError(ERROR_CODES.INVALID_HASH_ENCODING, "expected exactly 64 hexadecimal characters");
89
+ }
90
+ const output = new Uint8Array(DIGEST_SIZE);
91
+ for (let index = 0; index < output.length; index += 1) {
92
+ output[index] = Number.parseInt(value.slice(index * 2, index * 2 + 2), 16);
93
+ }
94
+ return output;
95
+ }
96
+ export function parseDigestHex(value) {
97
+ return newDigest(hexToBytes(value));
98
+ }
99
+ export function parseStreamIDHex(value) {
100
+ return newStreamID(hexToBytes(value));
101
+ }
102
+ /** 接受恰好 32 字节 raw 或 64 字符 hex。 */
103
+ export function parseStreamID(value) {
104
+ if (typeof value === "string")
105
+ return parseStreamIDHex(value);
106
+ if (value.length === DIGEST_SIZE)
107
+ return newStreamID(value);
108
+ throw streamSeedError(ERROR_CODES.INVALID_HASH_ENCODING, "stream ID must be 32 raw bytes or 64 hex characters");
109
+ }
110
+ export function nodeIDFromDecimal(value) {
111
+ if (!/^(0|[1-9][0-9]*)$/.test(value) || value.length > 20) {
112
+ throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "node ID must be an unsigned decimal string in uint64 range");
113
+ }
114
+ const nodeID = BigInt(value);
115
+ assertNodeID(nodeID);
116
+ return nodeID;
117
+ }
118
+ export function nodeIDToDecimal(value) {
119
+ assertNodeID(value);
120
+ return value.toString(10);
121
+ }
122
+ export function assertNodeID(value) {
123
+ if (typeof value !== "bigint" || value < 0n || value > MAX_UINT64) {
124
+ throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "node ID must be bigint in uint64 range");
125
+ }
126
+ }
127
+ export function nextNodeID(current) {
128
+ assertNodeID(current);
129
+ if (current === MAX_UINT64) {
130
+ throw streamSeedError(ERROR_CODES.NODE_ID_OVERFLOW, "node ID cannot advance past uint64 maximum");
131
+ }
132
+ return current + 1n;
133
+ }
134
+ /** 供 JSON/log 使用的小写 hex 形式。 */
135
+ export function digestToJSON(value) {
136
+ assertLength(value, DIGEST_SIZE);
137
+ return bytesToHex(value);
138
+ }
139
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1,28 @@
1
+ import type { WireKind } from "../protocol/constants.js";
2
+ import { type DecodedNode } from "../node/types.js";
3
+ import { type DecodedProgram } from "../program/types.js";
4
+ type ArtifactValue = DecodedProgram | DecodedNode;
5
+ declare const ARTIFACT_TOKEN: unique symbol;
6
+ /**
7
+ * 顶层路由后的规范对象。
8
+ *
9
+ * Artifact 只表示严格结构解析成功,不表示 hash 或签名可信;所有字节 getter
10
+ * 都返回副本,避免调用方修改 Artifact 内部状态。
11
+ */
12
+ export declare class Artifact {
13
+ #private;
14
+ /** @internal 只有 parseArtifact 通过模块内 token 创建实例。 */
15
+ constructor(version: number, kind: WireKind, raw: Uint8Array, value: ArtifactValue, token: typeof ARTIFACT_TOKEN);
16
+ get version(): number;
17
+ get kind(): WireKind;
18
+ get raw(): Uint8Array;
19
+ get value(): ArtifactValue;
20
+ }
21
+ /** @internal 供 wire codec 使用;不从 SDK 根入口导出。 */
22
+ export declare function createArtifact(version: number, kind: WireKind, raw: Uint8Array, value: ArtifactValue): Artifact;
23
+ /** 判断值是否为 SDK parseArtifact 生成的实例。 */
24
+ export declare function isArtifact(value: unknown): value is Artifact;
25
+ /** 返回 Artifact exact bytes 的独立副本。 */
26
+ export declare function artifactBytes(artifact: Artifact): Uint8Array;
27
+ export {};
28
+ //# sourceMappingURL=artifact.d.ts.map
@@ -0,0 +1,74 @@
1
+ import { copyBytes } from "../protocol/ids.js";
2
+ import { createDecodedNode } from "../node/types.js";
3
+ import { createDecodedProgram } from "../program/types.js";
4
+ const ARTIFACT_TOKEN = Symbol("streamseed.artifact");
5
+ const artifactInstances = new WeakSet();
6
+ /**
7
+ * 顶层路由后的规范对象。
8
+ *
9
+ * Artifact 只表示严格结构解析成功,不表示 hash 或签名可信;所有字节 getter
10
+ * 都返回副本,避免调用方修改 Artifact 内部状态。
11
+ */
12
+ export class Artifact {
13
+ #storedVersion;
14
+ #storedKind;
15
+ #storedRaw;
16
+ #storedValue;
17
+ /** @internal 只有 parseArtifact 通过模块内 token 创建实例。 */
18
+ constructor(version, kind, raw, value, token) {
19
+ if (token !== ARTIFACT_TOKEN)
20
+ throw new TypeError("Artifact must be created by parseArtifact");
21
+ this.#storedVersion = version;
22
+ this.#storedKind = kind;
23
+ this.#storedRaw = copyBytes(raw);
24
+ this.#storedValue = cloneArtifactValue(value);
25
+ artifactInstances.add(this);
26
+ Object.freeze(this);
27
+ }
28
+ get version() { return this.#storedVersion; }
29
+ get kind() { return this.#storedKind; }
30
+ get raw() { return copyBytes(this.#storedRaw); }
31
+ get value() { return cloneArtifactValue(this.#storedValue); }
32
+ }
33
+ Object.freeze(Artifact.prototype);
34
+ /** @internal 供 wire codec 使用;不从 SDK 根入口导出。 */
35
+ export function createArtifact(version, kind, raw, value) {
36
+ return new Artifact(version, kind, raw, value, ARTIFACT_TOKEN);
37
+ }
38
+ /** 判断值是否为 SDK parseArtifact 生成的实例。 */
39
+ export function isArtifact(value) {
40
+ return typeof value === "object" && value !== null && artifactInstances.has(value);
41
+ }
42
+ /** 返回 Artifact exact bytes 的独立副本。 */
43
+ export function artifactBytes(artifact) {
44
+ return copyBytes(artifact.raw);
45
+ }
46
+ function cloneArtifactValue(value) {
47
+ if ("info" in value) {
48
+ return createDecodedProgram(cloneProgramInfo(value.info), value.objectBytes);
49
+ }
50
+ return createDecodedNode(value.kind, cloneDocument(value.document), value.documentBytes, value.signature, value.seedHash, value.mediaContainer, value.mediaBlocks, value.objectBytes);
51
+ }
52
+ function cloneProgramInfo(info) {
53
+ return {
54
+ name: info.name,
55
+ publishedAtMs: info.publishedAtMs,
56
+ nodePublisherKey: copyBytes(info.nodePublisherKey),
57
+ description: info.description,
58
+ coverMediaType: info.coverMediaType,
59
+ coverBytes: copyBytes(info.coverBytes),
60
+ releaseSalt: copyBytes(info.releaseSalt),
61
+ channelPublisherKey: copyBytes(info.channelPublisherKey)
62
+ };
63
+ }
64
+ function cloneDocument(document) {
65
+ return {
66
+ streamId: copyBytes(document.streamId),
67
+ nodeId: document.nodeId,
68
+ publisherPublicKey: copyBytes(document.publisherPublicKey),
69
+ payloadSize: document.payloadSize,
70
+ payloadHash: copyBytes(document.payloadHash),
71
+ refs: document.refs.map((ref) => ({ nodeId: ref.nodeId, seedHash: copyBytes(ref.seedHash) }))
72
+ };
73
+ }
74
+ //# sourceMappingURL=artifact.js.map
@@ -0,0 +1,12 @@
1
+ import type { WireKind } from "../protocol/constants.js";
2
+ import { type Artifact } from "./artifact.js";
3
+ /** 读取 version/kind 并路由到唯一 typed decoder。 */
4
+ export declare function parseArtifact(data: Uint8Array): Promise<Artifact>;
5
+ /** 顶层解析并额外要求 expectedKind。 */
6
+ export declare function parseArtifactAs(data: Uint8Array, expectedKind: WireKind): Promise<Artifact>;
7
+ /** 只读取顶层前缀,不宣称对象已经完整解析或认证。 */
8
+ export declare function readVersionKind(data: Uint8Array): {
9
+ version: number;
10
+ kind: WireKind | number;
11
+ };
12
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { KIND_DATA_NODE, KIND_END_NODE, KIND_PROGRAM_INFO, MAX_NODE_OBJECT_SIZE, WIRE_VERSION } from "../protocol/constants.js";
2
+ import { ERROR_CODES, streamSeedError } from "../protocol/errors.js";
3
+ import { copyBytes } from "../protocol/ids.js";
4
+ import { CBORDecoder } from "../internal/cbor-codec.js";
5
+ import { decodeNode } from "../node/codec.js";
6
+ import { decodeProgramObject } from "../program/codec.js";
7
+ import { createArtifact } from "./artifact.js";
8
+ /** 读取 version/kind 并路由到唯一 typed decoder。 */
9
+ export async function parseArtifact(data) {
10
+ if (data.length > MAX_NODE_OBJECT_SIZE)
11
+ throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, "wire object exceeds V1 limit");
12
+ const snapshot = copyBytes(data);
13
+ const { version, kind } = readVersionKind(snapshot);
14
+ if (version !== WIRE_VERSION)
15
+ throw streamSeedError(ERROR_CODES.UNSUPPORTED_VERSION, "unsupported wire version");
16
+ let value;
17
+ if (kind === KIND_PROGRAM_INFO)
18
+ value = decodeProgramObject(snapshot);
19
+ else if (kind === KIND_DATA_NODE || kind === KIND_END_NODE)
20
+ value = await decodeNode(snapshot);
21
+ else
22
+ throw streamSeedError(ERROR_CODES.UNSUPPORTED_KIND, "unsupported wire kind");
23
+ return createArtifact(version, kind, snapshot, value);
24
+ }
25
+ /** 顶层解析并额外要求 expectedKind。 */
26
+ export async function parseArtifactAs(data, expectedKind) {
27
+ const artifact = await parseArtifact(data);
28
+ if (artifact.kind !== expectedKind)
29
+ throw streamSeedError(ERROR_CODES.UNSUPPORTED_KIND, "artifact kind does not match expected kind");
30
+ return artifact;
31
+ }
32
+ /** 只读取顶层前缀,不宣称对象已经完整解析或认证。 */
33
+ export function readVersionKind(data) {
34
+ const input = new CBORDecoder(data, BigInt(MAX_NODE_OBJECT_SIZE));
35
+ const length = input.array();
36
+ if (length < 2)
37
+ throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "top-level array must contain version and kind");
38
+ const version = Number(input.uint());
39
+ const kind = Number(input.uint());
40
+ return { version, kind: kind };
41
+ }
42
+ //# sourceMappingURL=codec.js.map
package/package.json CHANGED
@@ -1,6 +1,82 @@
1
1
  {
2
2
  "name": "streamseed",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "StreamSeed V1 publisher-authenticated headerless sequential media-block stream SDK",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist/index.js",
18
+ "dist/index.d.ts",
19
+ "dist/internal/cbor-codec.js",
20
+ "dist/internal/cbor-codec.d.ts",
21
+ "dist/internal/ed25519.js",
22
+ "dist/internal/ed25519.d.ts",
23
+ "dist/internal/abort.js",
24
+ "dist/internal/abort.d.ts",
25
+ "dist/internal/secp256k1.js",
26
+ "dist/internal/secp256k1.d.ts",
27
+ "dist/node/codec.js",
28
+ "dist/node/codec.d.ts",
29
+ "dist/node/document.js",
30
+ "dist/node/document.d.ts",
31
+ "dist/node/media.js",
32
+ "dist/node/media.d.ts",
33
+ "dist/node/sequence.js",
34
+ "dist/node/sequence.d.ts",
35
+ "dist/node/types.js",
36
+ "dist/node/types.d.ts",
37
+ "dist/control/control.js",
38
+ "dist/control/control.d.ts",
39
+ "dist/program/codec.js",
40
+ "dist/program/codec.d.ts",
41
+ "dist/program/resolver.js",
42
+ "dist/program/resolver.d.ts",
43
+ "dist/program/types.js",
44
+ "dist/program/types.d.ts",
45
+ "dist/protocol/constants.js",
46
+ "dist/protocol/constants.d.ts",
47
+ "dist/protocol/errors.js",
48
+ "dist/protocol/errors.d.ts",
49
+ "dist/protocol/hash.js",
50
+ "dist/protocol/hash.d.ts",
51
+ "dist/protocol/ids.js",
52
+ "dist/protocol/ids.d.ts",
53
+ "dist/wire/artifact.js",
54
+ "dist/wire/artifact.d.ts",
55
+ "dist/wire/codec.js",
56
+ "dist/wire/codec.d.ts",
57
+ "README.md",
58
+ "LICENSE"
59
+ ],
60
+ "engines": {
61
+ "node": ">=20"
62
+ },
63
+ "license": "AGPL-3.0-only",
64
+ "dependencies": {
65
+ "@noble/curves": "2.4.0",
66
+ "@noble/hashes": "2.4.0",
67
+ "masterseed": "1.1.0"
68
+ },
69
+ "devDependencies": {
70
+ "@types/node": "^22.10.2",
71
+ "esbuild": "0.28.2",
72
+ "typescript": "^5.7.3"
73
+ },
74
+ "scripts": {
75
+ "build": "tsc -p tsconfig.json",
76
+ "build:test": "tsc -p tsconfig.test.json",
77
+ "test:runtime": "node --test dist-test/test/*.test.js",
78
+ "check:browser": "node scripts/browser-bundle.mjs",
79
+ "check": "pnpm run build && pnpm run build:test && pnpm run test:runtime && pnpm run check:browser",
80
+ "pack:check": "pnpm pack --dry-run"
81
+ }
6
82
  }