keymaster-connect 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.
@@ -0,0 +1,3 @@
1
+ export { MSFILE_BLOCK_SIZE_BYTES, MSFILE_DIGEST_SIZE_BYTES, MSFILE_LOCAL_SOURCE_ID, MSFILE_MAX_BLOCK_BYTES, MSFILE_MAX_CONTENT_BYTES, MSFILE_MAX_ERROR_MESSAGE_BYTES, MSFILE_MAX_HEADER_BYTES, MSFILE_MAX_SEED_BYTES, MSFILE_PROTOCOL_ID, PROTOCOL_METHODS, PROTOCOL_VERSION, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, STORAGE_MAX_PAYLOAD_BYTES, isValidMsFileSourceId, sameNetworkIdentity, validateBsvNetwork, validateNetworkIdentity } from './chunk-AAVOIS5Q.js';
2
+ //# sourceMappingURL=protocol.js.map
3
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"protocol.js"}
@@ -0,0 +1,162 @@
1
+ import { ProtocolErrorCode, P2pkhTransferAssetId, ProtocolRequestMessage, NetworkIdentity, ProtocolMethod, MethodParams, AppIdentityProofV1, VerifiedAppIdentity, ProtocolResultMessage, ProtocolReadyMessage, ProtocolClosingMessage, MethodResult, ProtocolError, ProtocolEventPayload } from './protocol.js';
2
+
3
+ /** 校验失败:抛出的对象。service 会捕获并组装 ProtocolError。 */
4
+ declare class ProtocolValidationError extends Error {
5
+ readonly code: ProtocolErrorCode;
6
+ constructor(code: ProtocolErrorCode, message: string);
7
+ }
8
+ /** 严格比较 `aud` 与 `event.origin`;不补端口 / 不 lower host。 */
9
+ declare function assertOriginMatches(aud: string, eventOrigin: string): void;
10
+ declare function validateExactChannel(value: unknown, field?: string): string;
11
+ /**
12
+ * 校验主网 P2PKH 地址:base58check 解码 → 25 字节 → version 0x00。
13
+ * testnet(version 0x6f)直接 invalid_request;本次硬切换只支持主网。
14
+ *
15
+ * 不做 checksum 校验:base58 解码长度 + version 足够排除绝大多数误传;
16
+ * 真正的 chain-level 校验由 p2pkhTransferService.prepare 在签名时承担。
17
+ */
18
+ declare function assertMainnetP2pkhAddress(addr: string): void;
19
+ /**
20
+ * 按资产校验 P2PKH 地址:mainnet version 0x00,testnet version 0x6f。
21
+ *
22
+ * 不做 checksum 校验:base58 解码长度 + version 足够排除绝大多数误传;
23
+ * 真正的 chain-level 校验由 p2pkhTransferService.prepare 在签名时承担。
24
+ */
25
+ declare function assertP2pkhAddressForAsset(addr: string, assetId: P2pkhTransferAssetId): void;
26
+ /** 33-byte compressed secp256k1 公钥 hex:66 字符,全 hex 字符。 */
27
+ declare function assertCompressedPubkeyHex(hex: string): void;
28
+ /** 顶层 message 形状的"只看是不是已绑定 source 会话能接受的 request"。 */
29
+ declare function isStructuredCloneRequestCandidate(v: unknown): v is ProtocolRequestMessage;
30
+ /**
31
+ * 校验顶层 `cancel` 报文。
32
+ *
33
+ * 返回 `{ id }` 或抛错。**只**做结构 + id 非空字符串校验:
34
+ * - `v` 必须是 `PROTOCOL_VERSION`;
35
+ * - `type` 必须是 `"cancel"`;
36
+ * - `id` 必须是非空字符串。
37
+ *
38
+ * 设计缘由(施工单 003):
39
+ * - cancel 是 transport 控制消息;除了 id 之外没有 params。
40
+ * - "是否生效"的判定(current binding 匹配 / source/origin 匹配 /
41
+ * phase === "executing" 时忽略)由 service 在 `handleMessage` 路径
42
+ * 里集中做;validation 只负责"报文本身能不能进入 service"。
43
+ * - 校验失败不抛 cancel 专属 error code:复用 `invalid_request` 与
44
+ * 其它顶层报文保持一致;popup 收到非法 cancel 直接忽略即可。
45
+ */
46
+ declare function parseCancelMessage(raw: unknown): {
47
+ id: string;
48
+ };
49
+ /** 公开请求只含业务参数;可信钱包身份来自接收端宿主,而不是消息字段。 */
50
+ declare function parseRequestMessage(raw: unknown, receiverIdentity: NetworkIdentity): {
51
+ id: string;
52
+ method: ProtocolMethod;
53
+ params: MethodParams<ProtocolMethod>;
54
+ };
55
+
56
+ declare const APP_IDENTITY_DOMAIN_V1 = "keymaster-app-identity:v1";
57
+ declare class AppIdentityValidationError extends Error {
58
+ constructor(message: string);
59
+ }
60
+ /** RFC 8785-compatible canonical JSON for the proof payload. */
61
+ declare function canonicalizeJson(value: unknown): string;
62
+ declare function identityPayload(proof: AppIdentityProofV1): Omit<AppIdentityProofV1, "signature">;
63
+ declare function identityDigestBytes(proof: AppIdentityProofV1): Uint8Array;
64
+ /** 验证外部签名 proof,并返回可持久化的 digest snapshot。 */
65
+ declare function verifyAppIdentityProof(raw: unknown): VerifiedAppIdentity;
66
+ /** 校验 session 中已验证 proof 的不可变 snapshot。 */
67
+ declare function isVerifiedAppIdentitySnapshot(value: unknown): value is VerifiedAppIdentity;
68
+
69
+ /** Connect v1 接收端。只拥有协议状态,不拥有钱包授权、私钥、余额或持久业务账本。 */
70
+
71
+ /** 窗口对象只作为消息源身份与发送目标;不读取跨源页面内容。 */
72
+ interface ConnectPeer {
73
+ postMessage(message: unknown, targetOrigin: string): void;
74
+ readonly closed?: boolean;
75
+ }
76
+ /** 来源由宿主装配;appView 的 origin 来自已验证的启动上下文。 */
77
+ type ReceiverPeerPolicy = {
78
+ mode: "popup";
79
+ source: ConnectPeer | null;
80
+ } | {
81
+ mode: "appView";
82
+ origin: string;
83
+ source?: ConnectPeer | null;
84
+ };
85
+ /** 世代真值属于宿主;接收端仅作快照比较,重置或 Worker 更换后旧绑定失效。 */
86
+ interface ReceiverLifecycle {
87
+ walletGeneration?: string;
88
+ runGeneration?: string;
89
+ }
90
+ interface ReceiverMessage {
91
+ source: unknown;
92
+ origin: string;
93
+ data: unknown;
94
+ }
95
+ interface ConnectReceiverOptions {
96
+ peer(): ReceiverPeerPolicy;
97
+ lifecycle(): ReceiverLifecycle;
98
+ /** 首次建立连接时读取真实钱包身份;不得从请求参数取值。 */
99
+ identity(): Promise<NetworkIdentity>;
100
+ send?(target: ConnectPeer, origin: string, message: ProtocolResultMessage): void;
101
+ control?(target: ConnectPeer, message: ProtocolReadyMessage | ProtocolClosingMessage): void;
102
+ randomUUID?(): string;
103
+ }
104
+ /** 只由接收端签发的已验证请求;业务代码不能凭同形对象获得回复/执行权。 */
105
+ interface ReceiverRequest<M extends ProtocolMethod = ProtocolMethod> {
106
+ readonly source: ConnectPeer;
107
+ readonly origin: string;
108
+ readonly id: string;
109
+ readonly method: M;
110
+ readonly params: MethodParams<M>;
111
+ readonly networkIdentity: NetworkIdentity;
112
+ readonly walletGeneration: string;
113
+ readonly runGeneration: string;
114
+ readonly bindingId?: string;
115
+ readonly signal: AbortSignal;
116
+ }
117
+ /** 判断建立连接的方法;ready 只表示传输可用,不属于身份授权。 */
118
+ declare function isEstablishing(method: string): boolean;
119
+ /** 生产钱包与闭环测试共用同一接收端,不内置测试业务或默认自动授权。 */
120
+ declare class KeymasterConnectReceiver {
121
+ private readonly options;
122
+ private readonly bindings;
123
+ private readonly pending;
124
+ private appViewSource;
125
+ private closingSent;
126
+ private epoch;
127
+ constructor(options: ConnectReceiverOptions);
128
+ /** 每次从宿主读取来源策略;只接受 HTTP(S) 实际来源,拒绝 opaque origin。 */
129
+ isAllowedPeer(event: ReceiverMessage, claim?: boolean): event is ReceiverMessage & {
130
+ source: ConnectPeer;
131
+ };
132
+ /** appView 首条合法 ready 才认领来源;无效版本和外来窗口不改变绑定。 */
133
+ acceptReady(event: ReceiverMessage): boolean;
134
+ /** 校验并签发请求上下文。重复在途 ID 被忽略;非法可信请求返回标准错误。 */
135
+ accept(event: ReceiverMessage): Promise<ReceiverRequest | null>;
136
+ /** 取消只通知宿主;宿主决定是否仍可取消,执行中的不可逆操作不能被协议回滚。 */
137
+ acceptCancel(event: ReceiverMessage): ReceiverRequest | null;
138
+ /** 宿主在排队结束及不可逆业务前复核;这里只判断协议权威,不代替资金审批。 */
139
+ assertCurrent(context: ReceiverRequest): void;
140
+ /** 回复成功时才签发新的连接绑定;业务返回值必须已由宿主转换成公开 DTO。 */
141
+ replyResult<M extends ProtocolMethod>(context: ReceiverRequest<M>, result: MethodResult<M>): void;
142
+ /** 失败仍属于原请求;不额外生成取消响应,不误撤销同窗口后来建立的新绑定。 */
143
+ replyError(context: ReceiverRequest, error: ProtocolError): void;
144
+ /** 事件只给当前有效绑定添加接收端上下文;业务发布者不能自报绑定/钱包身份。 */
145
+ sendEvent(target: ConnectPeer, origin: string, event: ProtocolEventPayload): void;
146
+ /** ready/closing 只承载生命周期,不携带授权或业务结果。 */
147
+ sendReady(target: ConnectPeer): void;
148
+ /** 同一运行期至多通知一次 closing,发送失败不重试。 */
149
+ sendClosing(target: ConnectPeer): void;
150
+ /** 生命周期控制帧不包含敏感业务数据,未建绑定时也可以发送。 */
151
+ private sendControl;
152
+ /** 开始新的 Session Window 运行期,才重置 closing 一次性通知。 */
153
+ reset(): void;
154
+ /** clear 仅清协议运行态;授权撤销和业务责任收尾必须由宿主另外执行。 */
155
+ clear(): void;
156
+ /** 终态释放请求关联,不保留业务结果或交易材料。 */
157
+ private finish;
158
+ /** 精确 origin 回复,窗口退出时不重试,不能据发送成功宣告业务提交成功。 */
159
+ private send;
160
+ }
161
+
162
+ export { APP_IDENTITY_DOMAIN_V1, AppIdentityValidationError, type ConnectPeer, type ConnectReceiverOptions, KeymasterConnectReceiver, ProtocolEventPayload, ProtocolValidationError, type ReceiverLifecycle, type ReceiverMessage, type ReceiverPeerPolicy, type ReceiverRequest, assertCompressedPubkeyHex, assertMainnetP2pkhAddress, assertOriginMatches, assertP2pkhAddressForAsset, canonicalizeJson, identityDigestBytes, identityPayload, isEstablishing, isStructuredCloneRequestCandidate, isVerifiedAppIdentitySnapshot, parseCancelMessage, parseRequestMessage, validateExactChannel, verifyAppIdentityProof };