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.
- package/LICENSE +661 -0
- package/README.md +88 -2
- package/dist/control/control.d.ts +89 -0
- package/dist/control/control.js +396 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +15 -0
- package/dist/internal/abort.d.ts +8 -0
- package/dist/internal/abort.js +52 -0
- package/dist/internal/cbor-codec.d.ts +25 -0
- package/dist/internal/cbor-codec.js +134 -0
- package/dist/internal/ed25519.d.ts +15 -0
- package/dist/internal/ed25519.js +55 -0
- package/dist/internal/secp256k1.d.ts +3 -0
- package/dist/internal/secp256k1.js +15 -0
- package/dist/node/codec.d.ts +23 -0
- package/dist/node/codec.js +231 -0
- package/dist/node/document.d.ts +10 -0
- package/dist/node/document.js +123 -0
- package/dist/node/media.d.ts +6 -0
- package/dist/node/media.js +69 -0
- package/dist/node/sequence.d.ts +44 -0
- package/dist/node/sequence.js +137 -0
- package/dist/node/types.d.ts +93 -0
- package/dist/node/types.js +105 -0
- package/dist/program/codec.d.ts +10 -0
- package/dist/program/codec.js +165 -0
- package/dist/program/resolver.d.ts +25 -0
- package/dist/program/resolver.js +189 -0
- package/dist/program/types.d.ts +61 -0
- package/dist/program/types.js +90 -0
- package/dist/protocol/constants.d.ts +33 -0
- package/dist/protocol/constants.js +32 -0
- package/dist/protocol/errors.d.ts +46 -0
- package/dist/protocol/errors.js +57 -0
- package/dist/protocol/hash.d.ts +14 -0
- package/dist/protocol/hash.js +42 -0
- package/dist/protocol/ids.d.ts +60 -0
- package/dist/protocol/ids.js +139 -0
- package/dist/wire/artifact.d.ts +28 -0
- package/dist/wire/artifact.js +74 -0
- package/dist/wire/codec.d.ts +12 -0
- package/dist/wire/codec.js +42 -0
- package/package.json +79 -3
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { MAX_PROGRAM_OBJECT_SIZE } from "../protocol/constants.js";
|
|
2
|
+
import { ERROR_CODES, StreamSeedError, streamSeedError } from "../protocol/errors.js";
|
|
3
|
+
import { copyBytes, newDigest, newStreamID } from "../protocol/ids.js";
|
|
4
|
+
import { verifyProgramObject } from "./codec.js";
|
|
5
|
+
import { awaitWithAbort } from "../internal/abort.js";
|
|
6
|
+
/** 获取并验证 ProgramInfo;普通 []byte getter 使用协议默认上限。 */
|
|
7
|
+
export async function resolveProgram(streamId, getter, signal) {
|
|
8
|
+
return resolveProgramWithMaxBytes(streamId, getter, MAX_PROGRAM_OBJECT_SIZE, signal);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 为 []byte getter 增加调用方大小上限。
|
|
12
|
+
*
|
|
13
|
+
* 该上限保护 SDK 的 hash/解析阶段;若要限制供应商读取阶段的内存,请使用
|
|
14
|
+
* resolveProgramReader。供应商在 GetByHash 回调内部如何分配内存不受此函数控制。
|
|
15
|
+
*/
|
|
16
|
+
export async function resolveProgramWithMaxBytes(streamId, getter, maxBytes, signal) {
|
|
17
|
+
validateMaxBytes(maxBytes);
|
|
18
|
+
checkAbort(signal);
|
|
19
|
+
const expected = newStreamID(streamId);
|
|
20
|
+
if (typeof getter !== "function")
|
|
21
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "getter must be a function");
|
|
22
|
+
let object;
|
|
23
|
+
try {
|
|
24
|
+
object = await awaitWithAbort(getter(newDigest(expected), signal), signal);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
throw mapReadError(error, signal);
|
|
28
|
+
}
|
|
29
|
+
checkAbort(signal);
|
|
30
|
+
if (!(object instanceof Uint8Array))
|
|
31
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "GetByHash did not return Uint8Array");
|
|
32
|
+
if (object.length > maxBytes)
|
|
33
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `program object exceeds ${maxBytes} bytes`);
|
|
34
|
+
return verifyProgramObject(expected, object, signal);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 流式获取并验证 ProgramInfo。
|
|
38
|
+
*
|
|
39
|
+
* 内部最多保留 maxBytes+1 字节;读到超限时立即取消 ReadableStream,或停止消费
|
|
40
|
+
* AsyncIterable,避免超大对象被完整读入内存。
|
|
41
|
+
*/
|
|
42
|
+
export async function resolveProgramReader(streamId, getter, maxBytes = MAX_PROGRAM_OBJECT_SIZE, signal) {
|
|
43
|
+
validateMaxBytes(maxBytes);
|
|
44
|
+
checkAbort(signal);
|
|
45
|
+
const expected = newStreamID(streamId);
|
|
46
|
+
if (typeof getter !== "function")
|
|
47
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "reader getter must be a function");
|
|
48
|
+
let stream;
|
|
49
|
+
try {
|
|
50
|
+
stream = await awaitWithAbort(getter(newDigest(expected), signal), signal);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw mapReadError(error, signal);
|
|
54
|
+
}
|
|
55
|
+
checkAbort(signal);
|
|
56
|
+
let object;
|
|
57
|
+
try {
|
|
58
|
+
object = await readLimitedBytes(stream, maxBytes, signal);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw mapReadError(error, signal);
|
|
62
|
+
}
|
|
63
|
+
checkAbort(signal);
|
|
64
|
+
return verifyProgramObject(expected, object, signal);
|
|
65
|
+
}
|
|
66
|
+
function validateMaxBytes(maxBytes) {
|
|
67
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0 || maxBytes > MAX_PROGRAM_OBJECT_SIZE) {
|
|
68
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, `maxBytes must be between 1 and ${MAX_PROGRAM_OBJECT_SIZE}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function readLimitedBytes(stream, maxBytes, signal) {
|
|
72
|
+
if (isReadableStream(stream)) {
|
|
73
|
+
const reader = stream.getReader();
|
|
74
|
+
const chunks = [];
|
|
75
|
+
let total = 0;
|
|
76
|
+
const abortCleanup = () => cancelReader(reader, "ProgramInfo resolution was aborted");
|
|
77
|
+
signal?.addEventListener("abort", abortCleanup, { once: true });
|
|
78
|
+
try {
|
|
79
|
+
while (true) {
|
|
80
|
+
checkAbort(signal);
|
|
81
|
+
const result = await awaitWithAbort(reader.read(), signal);
|
|
82
|
+
if (result.done)
|
|
83
|
+
break;
|
|
84
|
+
const chunk = result.value;
|
|
85
|
+
if (!(chunk instanceof Uint8Array))
|
|
86
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "ReadableStream returned non-byte data");
|
|
87
|
+
total += chunk.length;
|
|
88
|
+
if (total > maxBytes) {
|
|
89
|
+
cancelReader(reader, "ProgramInfo exceeds maxBytes");
|
|
90
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `program object exceeds ${maxBytes} bytes`);
|
|
91
|
+
}
|
|
92
|
+
chunks.push(copyBytes(chunk));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
signal?.removeEventListener("abort", abortCleanup);
|
|
97
|
+
try {
|
|
98
|
+
reader.releaseLock();
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// abort 时底层 read 可能仍在等待;cancel 已被触发,不能让清理覆盖 CANCELLED。
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return joinChunks(chunks, total);
|
|
105
|
+
}
|
|
106
|
+
if (isAsyncIterable(stream)) {
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let total = 0;
|
|
109
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
110
|
+
let completed = false;
|
|
111
|
+
let returned = false;
|
|
112
|
+
const returnIterator = () => {
|
|
113
|
+
if (returned || completed)
|
|
114
|
+
return;
|
|
115
|
+
returned = true;
|
|
116
|
+
try {
|
|
117
|
+
const result = iterator.return?.();
|
|
118
|
+
if (result !== undefined)
|
|
119
|
+
void Promise.resolve(result).catch(() => undefined);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// 清理阶段不能覆盖原始读取/取消错误。
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const abortCleanup = () => returnIterator();
|
|
126
|
+
signal?.addEventListener("abort", abortCleanup, { once: true });
|
|
127
|
+
try {
|
|
128
|
+
while (true) {
|
|
129
|
+
checkAbort(signal);
|
|
130
|
+
const result = await awaitWithAbort(iterator.next(), signal);
|
|
131
|
+
if (result.done) {
|
|
132
|
+
completed = true;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
const chunk = result.value;
|
|
136
|
+
if (!(chunk instanceof Uint8Array))
|
|
137
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "AsyncIterable returned non-byte data");
|
|
138
|
+
total += chunk.length;
|
|
139
|
+
if (total > maxBytes)
|
|
140
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `program object exceeds ${maxBytes} bytes`);
|
|
141
|
+
chunks.push(copyBytes(chunk));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
signal?.removeEventListener("abort", abortCleanup);
|
|
146
|
+
returnIterator();
|
|
147
|
+
}
|
|
148
|
+
return joinChunks(chunks, total);
|
|
149
|
+
}
|
|
150
|
+
throw streamSeedError(ERROR_CODES.READ_FAILURE, "reader getter did not return a byte stream");
|
|
151
|
+
}
|
|
152
|
+
function cancelReader(reader, reason) {
|
|
153
|
+
try {
|
|
154
|
+
void Promise.resolve(reader.cancel(reason)).catch(() => undefined);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// 资源清理不得覆盖 awaitWithAbort 返回的 CANCELLED。
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function joinChunks(chunks, total) {
|
|
161
|
+
const output = new Uint8Array(total);
|
|
162
|
+
let offset = 0;
|
|
163
|
+
for (const chunk of chunks) {
|
|
164
|
+
output.set(chunk, offset);
|
|
165
|
+
offset += chunk.length;
|
|
166
|
+
}
|
|
167
|
+
return output;
|
|
168
|
+
}
|
|
169
|
+
function isReadableStream(value) {
|
|
170
|
+
return typeof value === "object" && value !== null && typeof value.getReader === "function";
|
|
171
|
+
}
|
|
172
|
+
function isAsyncIterable(value) {
|
|
173
|
+
return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
|
|
174
|
+
}
|
|
175
|
+
function checkAbort(signal) {
|
|
176
|
+
if (signal?.aborted)
|
|
177
|
+
throw streamSeedError(ERROR_CODES.CANCELLED, "program resolution was aborted");
|
|
178
|
+
}
|
|
179
|
+
function mapReadError(error, signal) {
|
|
180
|
+
if (signal?.aborted || isAbortError(error))
|
|
181
|
+
return streamSeedError(ERROR_CODES.CANCELLED, "program resolution was aborted", error);
|
|
182
|
+
if (error instanceof StreamSeedError)
|
|
183
|
+
return error;
|
|
184
|
+
return streamSeedError(ERROR_CODES.READ_FAILURE, "program resolution failed", error);
|
|
185
|
+
}
|
|
186
|
+
function isAbortError(error) {
|
|
187
|
+
return typeof DOMException !== "undefined" && error instanceof DOMException && error.name === "AbortError";
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=resolver.js.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type ChannelPublicKey, type PublicKey, type ReleaseSalt, type StreamID } from "../protocol/ids.js";
|
|
2
|
+
/** 参与 stream ID 计算的不可变节目发行信息。 */
|
|
3
|
+
export interface ProgramInfo {
|
|
4
|
+
/** NFC 规范化后的节目名称。 */
|
|
5
|
+
name: string;
|
|
6
|
+
/** 发布者声明的 Unix Epoch 毫秒,完整 uint64。 */
|
|
7
|
+
publishedAtMs: bigint;
|
|
8
|
+
/** 签署 Node 的严格 Ed25519 公钥。 */
|
|
9
|
+
nodePublisherKey: PublicKey;
|
|
10
|
+
/** NFC 规范化后的节目简介,可以为空。 */
|
|
11
|
+
description: string;
|
|
12
|
+
/** image/jpeg、image/png 或 image/webp。 */
|
|
13
|
+
coverMediaType: string;
|
|
14
|
+
/** 内嵌封面原始字节,最多 1 MiB。 */
|
|
15
|
+
coverBytes: Uint8Array;
|
|
16
|
+
/** 本次发行的非全零随机盐。 */
|
|
17
|
+
releaseSalt: ReleaseSalt;
|
|
18
|
+
/** 签署 Channel 公共消息的压缩 secp256k1 公钥。 */
|
|
19
|
+
channelPublisherKey: ChannelPublicKey;
|
|
20
|
+
}
|
|
21
|
+
declare const PROGRAM_OBJECT_TOKEN: unique symbol;
|
|
22
|
+
declare const DECODED_PROGRAM_TOKEN: unique symbol;
|
|
23
|
+
declare const VERIFIED_PROGRAM_TOKEN: unique symbol;
|
|
24
|
+
/** 构造成功的完整 ProgramInfo CBOR 和 MasterSeed stream ID。byte getter 返回副本。 */
|
|
25
|
+
export declare class ProgramObject {
|
|
26
|
+
#private;
|
|
27
|
+
/** @internal 只有 buildProgramObject 通过模块内 token 创建实例。 */
|
|
28
|
+
constructor(info: ProgramInfo, streamId: StreamID, bytes: Uint8Array, token: typeof PROGRAM_OBJECT_TOKEN);
|
|
29
|
+
get info(): ProgramInfo;
|
|
30
|
+
get streamId(): StreamID;
|
|
31
|
+
get bytes(): Uint8Array;
|
|
32
|
+
}
|
|
33
|
+
/** 严格解析但尚未通过目标 Seed Hash 验证的 ProgramInfo;byte getter 返回副本。 */
|
|
34
|
+
export declare class DecodedProgram {
|
|
35
|
+
#private;
|
|
36
|
+
/** @internal 只有 decodeProgramObject 通过模块内 token 创建实例。 */
|
|
37
|
+
constructor(info: ProgramInfo, objectBytes: Uint8Array, token: typeof DECODED_PROGRAM_TOKEN);
|
|
38
|
+
get info(): ProgramInfo;
|
|
39
|
+
get objectBytes(): Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
/** 已与 expected stream ID 做 MasterSeed Seed Hash 校验的 ProgramInfo。 */
|
|
42
|
+
export declare class VerifiedProgram {
|
|
43
|
+
#private;
|
|
44
|
+
/** @internal 只有 verifyProgramObject 通过模块内 token 创建实例。 */
|
|
45
|
+
constructor(info: ProgramInfo, streamId: StreamID, objectBytes: Uint8Array, token: typeof VERIFIED_PROGRAM_TOKEN);
|
|
46
|
+
get info(): ProgramInfo;
|
|
47
|
+
get streamId(): StreamID;
|
|
48
|
+
get objectBytes(): Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
/** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
|
|
51
|
+
export declare function createProgramObject(info: ProgramInfo, streamId: StreamID, bytes: Uint8Array): ProgramObject;
|
|
52
|
+
/** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
|
|
53
|
+
export declare function createDecodedProgram(info: ProgramInfo, objectBytes: Uint8Array): DecodedProgram;
|
|
54
|
+
/** @internal 供 program codec 使用;不从 SDK 根入口导出。 */
|
|
55
|
+
export declare function createVerifiedProgram(info: ProgramInfo, streamId: StreamID, objectBytes: Uint8Array): VerifiedProgram;
|
|
56
|
+
/** 判断值是否为 SDK 验证流程生成的 VerifiedProgram 实例。 */
|
|
57
|
+
export declare function isVerifiedProgram(value: unknown): value is VerifiedProgram;
|
|
58
|
+
/** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
|
|
59
|
+
export declare function cloneProgramInfo(info: ProgramInfo): ProgramInfo;
|
|
60
|
+
export {};
|
|
61
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -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
|