streamseed 0.0.0-stage → 0.2.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 (57) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +117 -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/manifest/cbor.d.ts +54 -0
  16. package/dist/manifest/cbor.js +306 -0
  17. package/dist/manifest/codec.d.ts +6 -0
  18. package/dist/manifest/codec.js +360 -0
  19. package/dist/manifest/constants.d.ts +2 -0
  20. package/dist/manifest/constants.js +2 -0
  21. package/dist/manifest/errors.d.ts +20 -0
  22. package/dist/manifest/errors.js +23 -0
  23. package/dist/manifest/index.d.ts +6 -0
  24. package/dist/manifest/index.js +6 -0
  25. package/dist/manifest/language.d.ts +4 -0
  26. package/dist/manifest/language.js +26 -0
  27. package/dist/manifest/types.d.ts +73 -0
  28. package/dist/manifest/types.js +2 -0
  29. package/dist/node/codec.d.ts +23 -0
  30. package/dist/node/codec.js +231 -0
  31. package/dist/node/document.d.ts +10 -0
  32. package/dist/node/document.js +123 -0
  33. package/dist/node/media.d.ts +6 -0
  34. package/dist/node/media.js +69 -0
  35. package/dist/node/sequence.d.ts +44 -0
  36. package/dist/node/sequence.js +137 -0
  37. package/dist/node/types.d.ts +93 -0
  38. package/dist/node/types.js +105 -0
  39. package/dist/program/codec.d.ts +10 -0
  40. package/dist/program/codec.js +165 -0
  41. package/dist/program/resolver.d.ts +25 -0
  42. package/dist/program/resolver.js +189 -0
  43. package/dist/program/types.d.ts +61 -0
  44. package/dist/program/types.js +90 -0
  45. package/dist/protocol/constants.d.ts +33 -0
  46. package/dist/protocol/constants.js +32 -0
  47. package/dist/protocol/errors.d.ts +46 -0
  48. package/dist/protocol/errors.js +57 -0
  49. package/dist/protocol/hash.d.ts +14 -0
  50. package/dist/protocol/hash.js +42 -0
  51. package/dist/protocol/ids.d.ts +60 -0
  52. package/dist/protocol/ids.js +139 -0
  53. package/dist/wire/artifact.d.ts +28 -0
  54. package/dist/wire/artifact.js +74 -0
  55. package/dist/wire/codec.d.ts +12 -0
  56. package/dist/wire/codec.js +42 -0
  57. package/package.json +92 -3
@@ -0,0 +1,90 @@
1
+ import { copyBytes } from "../protocol/ids.js";
2
+ const PROGRAM_OBJECT_TOKEN = Symbol("streamseed.program-object");
3
+ const DECODED_PROGRAM_TOKEN = Symbol("streamseed.decoded-program");
4
+ const VERIFIED_PROGRAM_TOKEN = Symbol("streamseed.verified-program");
5
+ const verifiedProgramInstances = new WeakSet();
6
+ /** 构造成功的完整 ProgramInfo CBOR 和 MasterSeed stream ID。byte getter 返回副本。 */
7
+ export class ProgramObject {
8
+ #storedInfo;
9
+ #storedStreamId;
10
+ #storedBytes;
11
+ /** @internal 只有 buildProgramObject 通过模块内 token 创建实例。 */
12
+ constructor(info, streamId, bytes, token) {
13
+ if (token !== PROGRAM_OBJECT_TOKEN)
14
+ throw new TypeError("ProgramObject must be created by buildProgramObject");
15
+ this.#storedInfo = cloneProgramInfo(info);
16
+ this.#storedStreamId = copyBytes(streamId);
17
+ this.#storedBytes = copyBytes(bytes);
18
+ Object.freeze(this);
19
+ }
20
+ get info() { return cloneProgramInfo(this.#storedInfo); }
21
+ get streamId() { return copyBytes(this.#storedStreamId); }
22
+ get bytes() { return copyBytes(this.#storedBytes); }
23
+ }
24
+ /** 严格解析但尚未通过目标 Seed Hash 验证的 ProgramInfo;byte getter 返回副本。 */
25
+ export class DecodedProgram {
26
+ #storedInfo;
27
+ #storedObjectBytes;
28
+ /** @internal 只有 decodeProgramObject 通过模块内 token 创建实例。 */
29
+ constructor(info, objectBytes, token) {
30
+ if (token !== DECODED_PROGRAM_TOKEN)
31
+ throw new TypeError("DecodedProgram must be created by decodeProgramObject");
32
+ this.#storedInfo = cloneProgramInfo(info);
33
+ this.#storedObjectBytes = copyBytes(objectBytes);
34
+ Object.freeze(this);
35
+ }
36
+ get info() { return cloneProgramInfo(this.#storedInfo); }
37
+ get objectBytes() { return copyBytes(this.#storedObjectBytes); }
38
+ }
39
+ /** 已与 expected stream ID 做 MasterSeed Seed Hash 校验的 ProgramInfo。 */
40
+ export class VerifiedProgram {
41
+ #storedInfo;
42
+ #storedStreamId;
43
+ #storedObjectBytes;
44
+ /** @internal 只有 verifyProgramObject 通过模块内 token 创建实例。 */
45
+ constructor(info, streamId, objectBytes, token) {
46
+ if (token !== VERIFIED_PROGRAM_TOKEN)
47
+ throw new TypeError("VerifiedProgram must be created by verifyProgramObject");
48
+ this.#storedInfo = cloneProgramInfo(info);
49
+ this.#storedStreamId = copyBytes(streamId);
50
+ this.#storedObjectBytes = copyBytes(objectBytes);
51
+ verifiedProgramInstances.add(this);
52
+ Object.freeze(this);
53
+ }
54
+ get info() { return cloneProgramInfo(this.#storedInfo); }
55
+ get streamId() { return copyBytes(this.#storedStreamId); }
56
+ get objectBytes() { return copyBytes(this.#storedObjectBytes); }
57
+ }
58
+ Object.freeze(ProgramObject.prototype);
59
+ Object.freeze(DecodedProgram.prototype);
60
+ Object.freeze(VerifiedProgram.prototype);
61
+ /** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
62
+ export function createProgramObject(info, streamId, bytes) {
63
+ return new ProgramObject(info, streamId, bytes, PROGRAM_OBJECT_TOKEN);
64
+ }
65
+ /** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
66
+ export function createDecodedProgram(info, objectBytes) {
67
+ return new DecodedProgram(info, objectBytes, DECODED_PROGRAM_TOKEN);
68
+ }
69
+ /** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
70
+ export function createVerifiedProgram(info, streamId, objectBytes) {
71
+ return new VerifiedProgram(info, streamId, objectBytes, VERIFIED_PROGRAM_TOKEN);
72
+ }
73
+ /** 判断值是否为 SDK 验证流程生成的 VerifiedProgram 实例。 */
74
+ export function isVerifiedProgram(value) {
75
+ return typeof value === "object" && value !== null && verifiedProgramInstances.has(value);
76
+ }
77
+ /** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
78
+ export function cloneProgramInfo(info) {
79
+ return {
80
+ name: info.name,
81
+ publishedAtMs: info.publishedAtMs,
82
+ nodePublisherKey: copyBytes(info.nodePublisherKey),
83
+ description: info.description,
84
+ coverMediaType: info.coverMediaType,
85
+ coverBytes: copyBytes(info.coverBytes),
86
+ releaseSalt: copyBytes(info.releaseSalt),
87
+ channelPublisherKey: copyBytes(info.channelPublisherKey)
88
+ };
89
+ }
90
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,33 @@
1
+ /** StreamSeed V1 线上格式版本。 */
2
+ export declare const WIRE_VERSION = 1;
3
+ /** ProgramInfo、DATA、END 的线上种类。 */
4
+ export declare const KIND_PROGRAM_INFO = 1;
5
+ export declare const KIND_DATA_NODE = 2;
6
+ export declare const KIND_END_NODE = 3;
7
+ export type WireKind = 1 | 2 | 3;
8
+ /** 固定字节长度与解析上限。 */
9
+ export declare const DIGEST_SIZE = 32;
10
+ export declare const PUBLIC_KEY_SIZE = 32;
11
+ export declare const CHANNEL_PUBLIC_KEY_SIZE = 33;
12
+ export declare const SIGNATURE_SIZE = 64;
13
+ export declare const MAX_COVER_BYTES: number;
14
+ export declare const MAX_PROGRAM_OBJECT_SIZE: number;
15
+ export declare const MAX_NAME_BYTES = 1024;
16
+ export declare const MAX_DESCRIPTION_BYTES = 65535;
17
+ export declare const MAX_MEDIA_CONTAINER_BYTES: number;
18
+ /** 保留为媒体容器上限的兼容名称。 */
19
+ export declare const MAX_PAYLOAD_BYTES: number;
20
+ export declare const MAX_MEDIA_BLOCKS = 4096;
21
+ export declare const MAX_REFERENCES = 64;
22
+ export declare const DEFAULT_REFERENCE_COUNT = 16;
23
+ export declare const MAX_NODE_DOCUMENT_SIZE = 4096;
24
+ export declare const MAX_NODE_OBJECT_SIZE: number;
25
+ export declare const PROGRAM_INFO_ARRAY_LENGTH = 10;
26
+ export declare const NODE_DOCUMENT_ARRAY_LENGTH = 6;
27
+ export declare const NODE_REF_ARRAY_LENGTH = 2;
28
+ export declare const DATA_NODE_ARRAY_LENGTH = 5;
29
+ export declare const END_NODE_ARRAY_LENGTH = 4;
30
+ export declare const NODE_SIGNATURE_DOMAIN = "streamseed/node-signature";
31
+ /** CBOR uint64 的边界,公开 Node API 必须使用 bigint。 */
32
+ export declare const MAX_UINT64 = 18446744073709551615n;
33
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,32 @@
1
+ /** StreamSeed V1 线上格式版本。 */
2
+ export const WIRE_VERSION = 1;
3
+ /** ProgramInfo、DATA、END 的线上种类。 */
4
+ export const KIND_PROGRAM_INFO = 1;
5
+ export const KIND_DATA_NODE = 2;
6
+ export const KIND_END_NODE = 3;
7
+ /** 固定字节长度与解析上限。 */
8
+ export const DIGEST_SIZE = 32;
9
+ export const PUBLIC_KEY_SIZE = 32;
10
+ export const CHANNEL_PUBLIC_KEY_SIZE = 33;
11
+ export const SIGNATURE_SIZE = 64;
12
+ export const MAX_COVER_BYTES = 1024 * 1024;
13
+ export const MAX_PROGRAM_OBJECT_SIZE = MAX_COVER_BYTES + 128 * 1024;
14
+ export const MAX_NAME_BYTES = 1024;
15
+ export const MAX_DESCRIPTION_BYTES = 65535;
16
+ export const MAX_MEDIA_CONTAINER_BYTES = 16 * 1024 * 1024;
17
+ /** 保留为媒体容器上限的兼容名称。 */
18
+ export const MAX_PAYLOAD_BYTES = MAX_MEDIA_CONTAINER_BYTES;
19
+ export const MAX_MEDIA_BLOCKS = 4096;
20
+ export const MAX_REFERENCES = 64;
21
+ export const DEFAULT_REFERENCE_COUNT = 16;
22
+ export const MAX_NODE_DOCUMENT_SIZE = 4096;
23
+ export const MAX_NODE_OBJECT_SIZE = MAX_MEDIA_CONTAINER_BYTES + 4096;
24
+ export const PROGRAM_INFO_ARRAY_LENGTH = 10;
25
+ export const NODE_DOCUMENT_ARRAY_LENGTH = 6;
26
+ export const NODE_REF_ARRAY_LENGTH = 2;
27
+ export const DATA_NODE_ARRAY_LENGTH = 5;
28
+ export const END_NODE_ARRAY_LENGTH = 4;
29
+ export const NODE_SIGNATURE_DOMAIN = "streamseed/node-signature";
30
+ /** CBOR uint64 的边界,公开 Node API 必须使用 bigint。 */
31
+ export const MAX_UINT64 = 0xffffffffffffffffn;
32
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,46 @@
1
+ /** 与 Go SDK 对齐的稳定错误代码。 */
2
+ export declare const ERROR_CODES: {
3
+ readonly UNSUPPORTED_VERSION: "UNSUPPORTED_VERSION";
4
+ readonly UNSUPPORTED_KIND: "UNSUPPORTED_KIND";
5
+ readonly INVALID_LENGTH: "INVALID_LENGTH";
6
+ readonly FIELD_TOO_LARGE: "FIELD_TOO_LARGE";
7
+ readonly INVALID_CBOR: "INVALID_CBOR";
8
+ readonly NON_CANONICAL_CBOR: "NON_CANONICAL_CBOR";
9
+ readonly INVALID_UTF8: "INVALID_UTF8";
10
+ readonly NON_CANONICAL_TEXT: "NON_CANONICAL_TEXT";
11
+ readonly INVALID_HASH_ENCODING: "INVALID_HASH_ENCODING";
12
+ readonly INVALID_PUBLIC_KEY: "INVALID_PUBLIC_KEY";
13
+ readonly INVALID_SIGNATURE_ENCODING: "INVALID_SIGNATURE_ENCODING";
14
+ readonly INVALID_VERIFIED_PROGRAM: "INVALID_VERIFIED_PROGRAM";
15
+ readonly SEED_HASH_MISMATCH: "SEED_HASH_MISMATCH";
16
+ readonly SEED_HASH_FAILURE: "SEED_HASH_FAILURE";
17
+ readonly STREAM_ID_MISMATCH: "STREAM_ID_MISMATCH";
18
+ readonly PUBLISHER_KEY_MISMATCH: "PUBLISHER_KEY_MISMATCH";
19
+ readonly PAYLOAD_HASH_MISMATCH: "PAYLOAD_HASH_MISMATCH";
20
+ readonly SIGNATURE_MISMATCH: "SIGNATURE_MISMATCH";
21
+ readonly NODE_ID_MISMATCH: "NODE_ID_MISMATCH";
22
+ readonly INVALID_NODE_SEQUENCE: "INVALID_NODE_SEQUENCE";
23
+ readonly INVALID_REFERENCE: "INVALID_REFERENCE";
24
+ readonly INVALID_MEDIA_CONTAINER: "INVALID_MEDIA_CONTAINER";
25
+ readonly UNSUPPORTED_MEDIA_TYPE: "UNSUPPORTED_MEDIA_TYPE";
26
+ readonly NODE_ID_OVERFLOW: "NODE_ID_OVERFLOW";
27
+ readonly TRAILING_DATA: "TRAILING_DATA";
28
+ readonly RANDOM_SOURCE_FAILURE: "RANDOM_SOURCE_FAILURE";
29
+ readonly READ_FAILURE: "READ_FAILURE";
30
+ readonly WRITE_FAILURE: "WRITE_FAILURE";
31
+ readonly SIGNER_FAILURE: "SIGNER_FAILURE";
32
+ readonly CANCELLED: "CANCELLED";
33
+ };
34
+ export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
35
+ /** 带稳定 code 的 SDK 错误;调用方应按 code 判断,不解析 message。 */
36
+ export declare class StreamSeedError extends Error {
37
+ readonly code: ErrorCode;
38
+ constructor(code: ErrorCode, message?: string, options?: {
39
+ cause?: unknown;
40
+ });
41
+ }
42
+ export declare function streamSeedError(code: ErrorCode, message?: string, cause?: unknown): StreamSeedError;
43
+ export declare function isStreamSeedError(value: unknown): value is StreamSeedError;
44
+ /** 将未知异常映射为稳定 code;已分类错误保持原样。 */
45
+ export declare function asStreamSeedError(value: unknown, fallback: ErrorCode, message?: ErrorCode): StreamSeedError;
46
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1,57 @@
1
+ /** 与 Go SDK 对齐的稳定错误代码。 */
2
+ export const ERROR_CODES = {
3
+ UNSUPPORTED_VERSION: "UNSUPPORTED_VERSION",
4
+ UNSUPPORTED_KIND: "UNSUPPORTED_KIND",
5
+ INVALID_LENGTH: "INVALID_LENGTH",
6
+ FIELD_TOO_LARGE: "FIELD_TOO_LARGE",
7
+ INVALID_CBOR: "INVALID_CBOR",
8
+ NON_CANONICAL_CBOR: "NON_CANONICAL_CBOR",
9
+ INVALID_UTF8: "INVALID_UTF8",
10
+ NON_CANONICAL_TEXT: "NON_CANONICAL_TEXT",
11
+ INVALID_HASH_ENCODING: "INVALID_HASH_ENCODING",
12
+ INVALID_PUBLIC_KEY: "INVALID_PUBLIC_KEY",
13
+ INVALID_SIGNATURE_ENCODING: "INVALID_SIGNATURE_ENCODING",
14
+ INVALID_VERIFIED_PROGRAM: "INVALID_VERIFIED_PROGRAM",
15
+ SEED_HASH_MISMATCH: "SEED_HASH_MISMATCH",
16
+ SEED_HASH_FAILURE: "SEED_HASH_FAILURE",
17
+ STREAM_ID_MISMATCH: "STREAM_ID_MISMATCH",
18
+ PUBLISHER_KEY_MISMATCH: "PUBLISHER_KEY_MISMATCH",
19
+ PAYLOAD_HASH_MISMATCH: "PAYLOAD_HASH_MISMATCH",
20
+ SIGNATURE_MISMATCH: "SIGNATURE_MISMATCH",
21
+ NODE_ID_MISMATCH: "NODE_ID_MISMATCH",
22
+ INVALID_NODE_SEQUENCE: "INVALID_NODE_SEQUENCE",
23
+ INVALID_REFERENCE: "INVALID_REFERENCE",
24
+ INVALID_MEDIA_CONTAINER: "INVALID_MEDIA_CONTAINER",
25
+ UNSUPPORTED_MEDIA_TYPE: "UNSUPPORTED_MEDIA_TYPE",
26
+ NODE_ID_OVERFLOW: "NODE_ID_OVERFLOW",
27
+ TRAILING_DATA: "TRAILING_DATA",
28
+ RANDOM_SOURCE_FAILURE: "RANDOM_SOURCE_FAILURE",
29
+ READ_FAILURE: "READ_FAILURE",
30
+ WRITE_FAILURE: "WRITE_FAILURE",
31
+ SIGNER_FAILURE: "SIGNER_FAILURE",
32
+ CANCELLED: "CANCELLED"
33
+ };
34
+ /** 带稳定 code 的 SDK 错误;调用方应按 code 判断,不解析 message。 */
35
+ export class StreamSeedError extends Error {
36
+ code;
37
+ constructor(code, message, options) {
38
+ super(message ?? code, options);
39
+ this.name = "StreamSeedError";
40
+ this.code = code;
41
+ }
42
+ }
43
+ export function streamSeedError(code, message, cause) {
44
+ if (cause === undefined)
45
+ return new StreamSeedError(code, message ?? code);
46
+ return new StreamSeedError(code, message ?? code, { cause });
47
+ }
48
+ export function isStreamSeedError(value) {
49
+ return value instanceof StreamSeedError;
50
+ }
51
+ /** 将未知异常映射为稳定 code;已分类错误保持原样。 */
52
+ export function asStreamSeedError(value, fallback, message = fallback) {
53
+ if (isStreamSeedError(value))
54
+ return value;
55
+ return streamSeedError(fallback, message, value);
56
+ }
57
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1,14 @@
1
+ import { newDigest } from "./ids.js";
2
+ /**
3
+ * SHA-256/SHA-512 适配层。
4
+ *
5
+ * 协议代码不再内置自研 hash 实现,统一使用 @noble/hashes 的成熟实现。
6
+ * 每次调用都返回独立 Uint8Array,调用方修改返回值不会影响 SDK 内部状态。
7
+ */
8
+ export declare function sha256(input: Uint8Array): Uint8Array;
9
+ export declare function sha512(input: Uint8Array): Uint8Array;
10
+ /** Compute the MasterSeed Seed Hash of exact complete-file bytes. */
11
+ export declare function masterSeedHash(input: Uint8Array, signal?: AbortSignal): Promise<ReturnType<typeof newDigest>>;
12
+ /** 拼接字节片段;输入不会被修改。 */
13
+ export declare function concatBytes(...parts: Uint8Array[]): Uint8Array;
14
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { sha256 as nobleSha256, sha512 as nobleSha512 } from "@noble/hashes/sha2.js";
2
+ import { createSeed } from "masterseed";
3
+ import { ERROR_CODES, streamSeedError } from "./errors.js";
4
+ import { newDigest } from "./ids.js";
5
+ /**
6
+ * SHA-256/SHA-512 适配层。
7
+ *
8
+ * 协议代码不再内置自研 hash 实现,统一使用 @noble/hashes 的成熟实现。
9
+ * 每次调用都返回独立 Uint8Array,调用方修改返回值不会影响 SDK 内部状态。
10
+ */
11
+ export function sha256(input) {
12
+ return new Uint8Array(nobleSha256(input));
13
+ }
14
+ export function sha512(input) {
15
+ return new Uint8Array(nobleSha512(input));
16
+ }
17
+ /** Compute the MasterSeed Seed Hash of exact complete-file bytes. */
18
+ export async function masterSeedHash(input, signal) {
19
+ const snapshot = new Uint8Array(input);
20
+ try {
21
+ const info = await createSeed([snapshot], { write() { } }, signal);
22
+ return newDigest(info.seedHash.toBytes());
23
+ }
24
+ catch (error) {
25
+ if (signal?.aborted || (typeof error === "object" && error !== null && "code" in error && error.code === "ABORTED")) {
26
+ throw streamSeedError(ERROR_CODES.CANCELLED, "MasterSeed Seed Hash calculation was aborted", error);
27
+ }
28
+ throw streamSeedError(ERROR_CODES.SEED_HASH_FAILURE, "MasterSeed Seed Hash calculation failed", error);
29
+ }
30
+ }
31
+ /** 拼接字节片段;输入不会被修改。 */
32
+ export function concatBytes(...parts) {
33
+ const total = parts.reduce((sum, part) => sum + part.length, 0);
34
+ const output = new Uint8Array(total);
35
+ let offset = 0;
36
+ for (const part of parts) {
37
+ output.set(part, offset);
38
+ offset += part.length;
39
+ }
40
+ return output;
41
+ }
42
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1,60 @@
1
+ declare const digestBrand: unique symbol;
2
+ declare const streamIdBrand: unique symbol;
3
+ declare const publicKeyBrand: unique symbol;
4
+ declare const channelPublicKeyBrand: unique symbol;
5
+ declare const signatureBrand: unique symbol;
6
+ declare const releaseSaltBrand: unique symbol;
7
+ /** 固定 32 字节 SHA-256 摘要;brand 防止与其他 32 字节值混传。 */
8
+ export type Digest = Uint8Array & {
9
+ readonly [digestBrand]: "Digest";
10
+ };
11
+ /** ProgramInfo exact CBOR 的 MasterSeed Seed Hash;brand 防止误当普通 Digest。 */
12
+ export type StreamID = Uint8Array & {
13
+ readonly [streamIdBrand]: "StreamID";
14
+ };
15
+ /** 严格 Ed25519 32 字节公钥。 */
16
+ export type PublicKey = Uint8Array & {
17
+ readonly [publicKeyBrand]: "PublicKey";
18
+ };
19
+ /** 严格压缩 secp256k1 33 字节公钥。 */
20
+ export type ChannelPublicKey = Uint8Array & {
21
+ readonly [channelPublicKeyBrand]: "ChannelPublicKey";
22
+ };
23
+ /** 严格 Ed25519 64 字节签名。 */
24
+ export type Signature = Uint8Array & {
25
+ readonly [signatureBrand]: "Signature";
26
+ };
27
+ /** 非全零 32 字节发行盐。 */
28
+ export type ReleaseSalt = Uint8Array & {
29
+ readonly [releaseSaltBrand]: "ReleaseSalt";
30
+ };
31
+ /** V1 node ID;禁止使用 number。 */
32
+ export type NodeID = bigint;
33
+ /** 复制字节,同时保留已经通过构造器建立的 brand。 */
34
+ export declare function copyBytes<T extends Uint8Array>(value: T): T;
35
+ export declare function assertLength(value: Uint8Array, expected: number, code?: "INVALID_LENGTH"): void;
36
+ export declare function newDigest(value: Uint8Array): Digest;
37
+ export declare function newStreamID(value: Uint8Array): StreamID;
38
+ export declare function newPublicKey(value: Uint8Array): PublicKey;
39
+ export declare function newChannelPublicKey(value: Uint8Array): ChannelPublicKey;
40
+ export declare function newSignature(value: Uint8Array): Signature;
41
+ export declare function newReleaseSalt(value: Uint8Array): ReleaseSalt;
42
+ /** 使用 Web Crypto getRandomValues 生成非全零发行盐。 */
43
+ export declare function generateReleaseSalt(): ReleaseSalt;
44
+ /** 返回 END Node 使用的 SHA-256(empty) 副本;不会暴露可变全局数组。 */
45
+ export declare function emptyPayloadHash(): Digest;
46
+ export declare function bytesEqual(left: Uint8Array, right: Uint8Array): boolean;
47
+ export declare function bytesToHex(value: Uint8Array): string;
48
+ export declare function hexToBytes(value: string): Uint8Array;
49
+ export declare function parseDigestHex(value: string): Digest;
50
+ export declare function parseStreamIDHex(value: string): StreamID;
51
+ /** 接受恰好 32 字节 raw 或 64 字符 hex。 */
52
+ export declare function parseStreamID(value: Uint8Array | string): StreamID;
53
+ export declare function nodeIDFromDecimal(value: string): NodeID;
54
+ export declare function nodeIDToDecimal(value: NodeID): string;
55
+ export declare function assertNodeID(value: NodeID): void;
56
+ export declare function nextNodeID(current: NodeID): NodeID;
57
+ /** 供 JSON/log 使用的小写 hex 形式。 */
58
+ export declare function digestToJSON(value: Digest): string;
59
+ export {};
60
+ //# sourceMappingURL=ids.d.ts.map
@@ -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