keymaster-connect 0.1.3 → 0.1.5

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/dist/protocol.js CHANGED
@@ -1,3 +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-W2TNWBLP.js';
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-ZDIFZJYD.js';
2
2
  //# sourceMappingURL=protocol.js.map
3
3
  //# sourceMappingURL=protocol.js.map
@@ -0,0 +1,58 @@
1
+ import { ax as PaymentOutput } from './protocol-C8z30CKK.js';
2
+
3
+ type PaymentScriptKind = "p2pkh" | "p2pk" | "multisig" | "op_return" | "unknown";
4
+ interface PaymentScriptP2pkh {
5
+ kind: "p2pkh";
6
+ scriptHex: string;
7
+ amountSats: number;
8
+ hash160Hex: string;
9
+ addressMainnet: string;
10
+ addressTestnet: string;
11
+ summary: string;
12
+ }
13
+ interface PaymentScriptP2pk {
14
+ kind: "p2pk";
15
+ scriptHex: string;
16
+ amountSats: number;
17
+ publicKeyHex: string;
18
+ summary: string;
19
+ }
20
+ interface PaymentScriptMultisig {
21
+ kind: "multisig";
22
+ scriptHex: string;
23
+ amountSats: number;
24
+ m: number;
25
+ n: number;
26
+ publicKeysHex: string[];
27
+ summary: string;
28
+ }
29
+ interface PaymentScriptOpReturn {
30
+ kind: "op_return";
31
+ scriptHex: string;
32
+ amountSats: number;
33
+ dataHex: string;
34
+ dataText: string;
35
+ summary: string;
36
+ }
37
+ interface PaymentScriptUnknown {
38
+ kind: "unknown";
39
+ scriptHex: string;
40
+ amountSats: number;
41
+ reason: string;
42
+ summary: string;
43
+ }
44
+ type PaymentScriptInterpretation = PaymentScriptP2pkh | PaymentScriptP2pk | PaymentScriptMultisig | PaymentScriptOpReturn | PaymentScriptUnknown;
45
+ interface InterpretedPaymentOutput {
46
+ index: number;
47
+ amountSats: number;
48
+ scriptHex: string;
49
+ interpretation: PaymentScriptInterpretation;
50
+ }
51
+ /** 解释单个 locking script;匹配必须完整,失败按未知脚本展示。 */
52
+ declare function interpretLockingScript(scriptHexRaw: string, amountSats: number): PaymentScriptInterpretation;
53
+ /** 按 App 顺序逐项解释业务输出。 */
54
+ declare function interpretPaymentOutputs(outputs: PaymentOutput[]): InterpretedPaymentOutput[];
55
+ /** 业务输出总额(不含矿工费与找零)。 */
56
+ declare function totalBusinessSats(outputs: PaymentOutput[]): number;
57
+
58
+ export { type InterpretedPaymentOutput, type PaymentScriptInterpretation, type PaymentScriptKind, type PaymentScriptMultisig, type PaymentScriptOpReturn, type PaymentScriptP2pk, type PaymentScriptP2pkh, type PaymentScriptUnknown, interpretLockingScript, interpretPaymentOutputs, totalBusinessSats };
package/dist/script.js ADDED
@@ -0,0 +1,253 @@
1
+ import { sha256 } from '@noble/hashes/sha2.js';
2
+ import '@noble/hashes/legacy.js';
3
+
4
+ // src/script.ts
5
+ function hexToBytes(hex) {
6
+ if (hex.length % 2 !== 0 || hex.length > 0 && !/^[0-9a-fA-F]+$/u.test(hex)) return null;
7
+ const out = new Uint8Array(hex.length / 2);
8
+ for (let i = 0; i < out.length; i++) out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
9
+ return out;
10
+ }
11
+ function bytesToHex(bytes) {
12
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
13
+ }
14
+ var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
15
+ function base58Encode(bytes) {
16
+ if (bytes.length === 0) return "";
17
+ const digits = [0];
18
+ for (const byte of bytes) {
19
+ let carry = byte;
20
+ for (let i = 0; i < digits.length; i++) {
21
+ const v = digits[i] * 256 + carry;
22
+ digits[i] = v % 58;
23
+ carry = Math.floor(v / 58);
24
+ }
25
+ while (carry > 0) {
26
+ digits.push(carry % 58);
27
+ carry = Math.floor(carry / 58);
28
+ }
29
+ }
30
+ let leadingZeros = 0;
31
+ for (const b of bytes) {
32
+ if (b === 0) leadingZeros++;
33
+ else break;
34
+ }
35
+ let result = "1".repeat(leadingZeros);
36
+ for (let i = digits.length - 1; i >= 0; i--) result += BASE58_ALPHABET[digits[i]];
37
+ return result;
38
+ }
39
+ function p2pkhAddress(hash160, version) {
40
+ const payload = new Uint8Array(1 + hash160.length);
41
+ payload[0] = version;
42
+ payload.set(hash160, 1);
43
+ const checksum = sha256(sha256(payload)).slice(0, 4);
44
+ const full = new Uint8Array(payload.length + 4);
45
+ full.set(payload, 0);
46
+ full.set(checksum, payload.length);
47
+ return base58Encode(full);
48
+ }
49
+ function decodeText(bytes) {
50
+ try {
51
+ return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
52
+ } catch {
53
+ return "";
54
+ }
55
+ }
56
+ function parsePushes(bytes, offset) {
57
+ const pushes = [];
58
+ let pos = offset;
59
+ while (pos < bytes.length) {
60
+ const op = bytes[pos];
61
+ if (op >= 1 && op <= 75) {
62
+ const len = op;
63
+ if (pos + 1 + len > bytes.length) return null;
64
+ pushes.push(bytes.slice(pos + 1, pos + 1 + len));
65
+ pos += 1 + len;
66
+ } else if (op === 76) {
67
+ if (pos + 2 > bytes.length) return null;
68
+ const len = bytes[pos + 1];
69
+ if (pos + 2 + len > bytes.length) return null;
70
+ pushes.push(bytes.slice(pos + 2, pos + 2 + len));
71
+ pos += 2 + len;
72
+ } else if (op === 77) {
73
+ if (pos + 3 > bytes.length) return null;
74
+ const len = bytes[pos + 1] | bytes[pos + 2] << 8;
75
+ if (pos + 3 + len > bytes.length) return null;
76
+ pushes.push(bytes.slice(pos + 3, pos + 3 + len));
77
+ pos += 3 + len;
78
+ } else if (op === 78) {
79
+ if (pos + 5 > bytes.length) return null;
80
+ const len = bytes[pos + 1] | bytes[pos + 2] << 8 | bytes[pos + 3] << 16 | bytes[pos + 4] * 16777216;
81
+ if (len < 0 || pos + 5 + len > bytes.length) return null;
82
+ pushes.push(bytes.slice(pos + 5, pos + 5 + len));
83
+ pos += 5 + len;
84
+ } else if (op === 0) {
85
+ pushes.push(new Uint8Array(0));
86
+ pos += 1;
87
+ } else if (op >= 81 && op <= 96) {
88
+ return null;
89
+ } else {
90
+ return null;
91
+ }
92
+ }
93
+ return { pushes, consumed: pos };
94
+ }
95
+ function isCompressedPubkey(bytes) {
96
+ return bytes.length === 33 && (bytes[0] === 2 || bytes[0] === 3);
97
+ }
98
+ function isUncompressedPubkey(bytes) {
99
+ return bytes.length === 65 && bytes[0] === 4;
100
+ }
101
+ function isPubkey(bytes) {
102
+ return isCompressedPubkey(bytes) || isUncompressedPubkey(bytes);
103
+ }
104
+ function unknown(scriptHex, amountSats) {
105
+ return {
106
+ kind: "unknown",
107
+ scriptHex,
108
+ amountSats,
109
+ reason: "\u4E0D\u8BA4\u8BC6\u6B64\u811A\u672C\uFF0C\u65E0\u6CD5\u89E3\u91CA\u5176\u7528\u9014",
110
+ summary: "\u4E0D\u8BA4\u8BC6\u6B64\u811A\u672C\uFF0C\u65E0\u6CD5\u89E3\u91CA\u5176\u7528\u9014"
111
+ };
112
+ }
113
+ function interpretLockingScript(scriptHexRaw, amountSats) {
114
+ const scriptHex = scriptHexRaw.toLowerCase();
115
+ const bytes = hexToBytes(scriptHex);
116
+ if (!bytes) return unknown(scriptHex, amountSats);
117
+ if (bytes.length === 25 && bytes[0] === 118 && bytes[1] === 169 && bytes[2] === 20 && bytes[23] === 136 && bytes[24] === 172) {
118
+ const hash = bytes.slice(3, 23);
119
+ const hashHex = bytesToHex(hash);
120
+ return {
121
+ kind: "p2pkh",
122
+ scriptHex,
123
+ amountSats,
124
+ hash160Hex: hashHex,
125
+ addressMainnet: p2pkhAddress(hash, 0),
126
+ addressTestnet: p2pkhAddress(hash, 111),
127
+ summary: `P2PKH \u6536\u6B3E\u5730\u5740 ${p2pkhAddress(hash, 0)} / ${p2pkhAddress(hash, 111)}\uFF0C\u91D1\u989D ${amountSats} sat`
128
+ };
129
+ }
130
+ if (bytes.length >= 2 && bytes[bytes.length - 1] === 172) {
131
+ const pushOp = bytes[0];
132
+ const pubLen = pushOp >= 1 && pushOp <= 75 ? pushOp : -1;
133
+ if (pubLen > 0 && bytes.length === 1 + pubLen + 1) {
134
+ const pub = bytes.slice(1, 1 + pubLen);
135
+ if (isPubkey(pub)) {
136
+ const pubHex = bytesToHex(pub);
137
+ return {
138
+ kind: "p2pk",
139
+ scriptHex,
140
+ amountSats,
141
+ publicKeyHex: pubHex,
142
+ summary: `P2PK \u6536\u6B3E\u516C\u94A5 ${pubHex}\uFF0C\u91D1\u989D ${amountSats} sat`
143
+ };
144
+ }
145
+ }
146
+ }
147
+ if (bytes.length >= 3 && bytes[bytes.length - 1] === 174) {
148
+ const mOp = bytes[0];
149
+ const nOp = bytes[bytes.length - 2];
150
+ if (mOp >= 81 && mOp <= 96 && nOp >= 81 && nOp <= 96) {
151
+ const m = mOp - 80;
152
+ const n = nOp - 80;
153
+ let pos = 1;
154
+ const pubs = [];
155
+ let ok = true;
156
+ for (let i = 0; i < n; i++) {
157
+ if (pos >= bytes.length - 2) {
158
+ ok = false;
159
+ break;
160
+ }
161
+ const op = bytes[pos];
162
+ let len = -1;
163
+ let header = 1;
164
+ if (op >= 1 && op <= 75) {
165
+ len = op;
166
+ header = 1;
167
+ } else {
168
+ ok = false;
169
+ break;
170
+ }
171
+ if (pos + header + len > bytes.length - 2) {
172
+ ok = false;
173
+ break;
174
+ }
175
+ const pub = bytes.slice(pos + header, pos + header + len);
176
+ if (!isPubkey(pub)) {
177
+ ok = false;
178
+ break;
179
+ }
180
+ pubs.push(pub);
181
+ pos += header + len;
182
+ }
183
+ if (ok && pubs.length === n && pos === bytes.length - 2 && m >= 1 && m <= n && n >= 1 && n <= 16) {
184
+ const hexes = pubs.map(bytesToHex);
185
+ return {
186
+ kind: "multisig",
187
+ scriptHex,
188
+ amountSats,
189
+ m,
190
+ n,
191
+ publicKeysHex: hexes,
192
+ summary: `\u6807\u51C6\u591A\u7B7E ${m}/${n}\uFF0C\u516C\u94A5 ${hexes.join(", ")}\uFF0C\u91D1\u989D ${amountSats} sat`
193
+ };
194
+ }
195
+ }
196
+ }
197
+ {
198
+ let dataStart = -1;
199
+ if (bytes.length >= 1 && bytes[0] === 106) dataStart = 1;
200
+ else if (bytes.length >= 2 && bytes[0] === 0 && bytes[1] === 106) dataStart = 2;
201
+ if (dataStart >= 0) {
202
+ const rest = bytes.slice(dataStart);
203
+ if (rest.length === 0) {
204
+ return {
205
+ kind: "op_return",
206
+ scriptHex,
207
+ amountSats,
208
+ dataHex: "",
209
+ dataText: "",
210
+ summary: `\u6570\u636E\u8F93\u51FA\uFF0C\u7A7A\u6570\u636E\uFF0C\u91D1\u989D ${amountSats} sat`
211
+ };
212
+ }
213
+ const parsed = parsePushes(rest, 0);
214
+ if (parsed && parsed.consumed === rest.length) {
215
+ const combined = parsed.pushes.reduce((acc, cur) => {
216
+ const next = new Uint8Array(acc.length + cur.length);
217
+ next.set(acc, 0);
218
+ next.set(cur, acc.length);
219
+ return next;
220
+ }, new Uint8Array(0));
221
+ const dataHex = bytesToHex(combined);
222
+ const dataText = decodeText(combined);
223
+ return {
224
+ kind: "op_return",
225
+ scriptHex,
226
+ amountSats,
227
+ dataHex,
228
+ dataText,
229
+ summary: `\u6570\u636E\u8F93\u51FA\uFF0C\u6570\u636E ${dataHex || "(\u7A7A)"}\uFF0C\u91D1\u989D ${amountSats} sat`
230
+ };
231
+ }
232
+ return unknown(scriptHex, amountSats);
233
+ }
234
+ }
235
+ return unknown(scriptHex, amountSats);
236
+ }
237
+ function interpretPaymentOutputs(outputs) {
238
+ return outputs.map((output, index) => ({
239
+ index,
240
+ amountSats: output.amount,
241
+ scriptHex: output.script.toLowerCase(),
242
+ interpretation: interpretLockingScript(output.script, output.amount)
243
+ }));
244
+ }
245
+ function totalBusinessSats(outputs) {
246
+ let total = 0;
247
+ for (const output of outputs) total += output.amount;
248
+ return total;
249
+ }
250
+
251
+ export { interpretLockingScript, interpretPaymentOutputs, totalBusinessSats };
252
+ //# sourceMappingURL=script.js.map
253
+ //# sourceMappingURL=script.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/script.ts"],"names":[],"mappings":";;;;AAkEA,SAAS,WAAW,GAAA,EAAgC;AAClD,EAAA,IAAI,GAAA,CAAI,MAAA,GAAS,CAAA,KAAM,CAAA,IAAM,GAAA,CAAI,MAAA,GAAS,CAAA,IAAK,CAAC,iBAAA,CAAkB,IAAA,CAAK,GAAG,CAAA,EAAI,OAAO,IAAA;AACrF,EAAA,MAAM,GAAA,GAAM,IAAI,UAAA,CAAW,GAAA,CAAI,SAAS,CAAC,CAAA;AACzC,EAAA,KAAA,IAAS,IAAI,CAAA,EAAG,CAAA,GAAI,IAAI,MAAA,EAAQ,CAAA,EAAA,MAAS,CAAC,CAAA,GAAI,QAAA,CAAS,GAAA,CAAI,MAAM,CAAA,GAAI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,cAAA,EAAgB,sBAAA,CAAuB,MAAA,CAAO,MAAA,EAAQ,OAAO,MAAM;AAAA,GACrE,CAAE,CAAA;AACJ;AAGO,SAAS,kBAAkB,OAAA,EAAkC;AAClE,EAAA,IAAI,KAAA,GAAQ,CAAA;AACZ,EAAA,KAAA,MAAW,MAAA,IAAU,OAAA,EAAS,KAAA,IAAS,MAAA,CAAO,MAAA;AAC9C,EAAA,OAAO,KAAA;AACT","file":"script.js","sourcesContent":["/** Connect v1 业务脚本解释:只做展示,不决定是否允许支付。 */\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { ripemd160 } from \"@noble/hashes/legacy.js\";\nimport type { PaymentOutput } from \"./protocol.js\";\n\nexport type PaymentScriptKind = \"p2pkh\" | \"p2pk\" | \"multisig\" | \"op_return\" | \"unknown\";\n\nexport interface PaymentScriptP2pkh {\n kind: \"p2pkh\";\n scriptHex: string;\n amountSats: number;\n hash160Hex: string;\n addressMainnet: string;\n addressTestnet: string;\n summary: string;\n}\n\nexport interface PaymentScriptP2pk {\n kind: \"p2pk\";\n scriptHex: string;\n amountSats: number;\n publicKeyHex: string;\n summary: string;\n}\n\nexport interface PaymentScriptMultisig {\n kind: \"multisig\";\n scriptHex: string;\n amountSats: number;\n m: number;\n n: number;\n publicKeysHex: string[];\n summary: string;\n}\n\nexport interface PaymentScriptOpReturn {\n kind: \"op_return\";\n scriptHex: string;\n amountSats: number;\n dataHex: string;\n dataText: string;\n summary: string;\n}\n\nexport interface PaymentScriptUnknown {\n kind: \"unknown\";\n scriptHex: string;\n amountSats: number;\n reason: string;\n summary: string;\n}\n\nexport type PaymentScriptInterpretation =\n | PaymentScriptP2pkh\n | PaymentScriptP2pk\n | PaymentScriptMultisig\n | PaymentScriptOpReturn\n | PaymentScriptUnknown;\n\nexport interface InterpretedPaymentOutput {\n index: number;\n amountSats: number;\n scriptHex: string;\n interpretation: PaymentScriptInterpretation;\n}\n\nfunction hexToBytes(hex: string): Uint8Array | null {\n if (hex.length % 2 !== 0 || (hex.length > 0 && !/^[0-9a-fA-F]+$/u.test(hex))) return null;\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n return out;\n}\n\nfunction bytesToHex(bytes: Uint8Array): string {\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nconst BASE58_ALPHABET = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\";\n\nfunction base58Encode(bytes: Uint8Array): string {\n if (bytes.length === 0) return \"\";\n const digits: number[] = [0];\n for (const byte of bytes) {\n let carry = byte;\n for (let i = 0; i < digits.length; i++) {\n const v = digits[i]! * 256 + carry;\n digits[i] = v % 58;\n carry = Math.floor(v / 58);\n }\n while (carry > 0) {\n digits.push(carry % 58);\n carry = Math.floor(carry / 58);\n }\n }\n let leadingZeros = 0;\n for (const b of bytes) {\n if (b === 0) leadingZeros++;\n else break;\n }\n let result = \"1\".repeat(leadingZeros);\n for (let i = digits.length - 1; i >= 0; i--) result += BASE58_ALPHABET[digits[i]!];\n return result;\n}\n\nfunction p2pkhAddress(hash160: Uint8Array, version: number): string {\n const payload = new Uint8Array(1 + hash160.length);\n payload[0] = version;\n payload.set(hash160, 1);\n const checksum = sha256(sha256(payload)).slice(0, 4);\n const full = new Uint8Array(payload.length + 4);\n full.set(payload, 0);\n full.set(checksum, payload.length);\n return base58Encode(full);\n}\n\nfunction hash160Of(data: Uint8Array): Uint8Array {\n return ripemd160(sha256(data));\n}\n\nfunction decodeText(bytes: Uint8Array): string {\n try {\n return new TextDecoder(\"utf-8\", { fatal: false }).decode(bytes);\n } catch {\n return \"\";\n }\n}\n\nfunction parsePushes(bytes: Uint8Array, offset: number): { pushes: Uint8Array[]; consumed: number } | null {\n const pushes: Uint8Array[] = [];\n let pos = offset;\n while (pos < bytes.length) {\n const op = bytes[pos]!;\n if (op >= 0x01 && op <= 0x4b) {\n const len = op;\n if (pos + 1 + len > bytes.length) return null;\n pushes.push(bytes.slice(pos + 1, pos + 1 + len));\n pos += 1 + len;\n } else if (op === 0x4c) {\n if (pos + 2 > bytes.length) return null;\n const len = bytes[pos + 1]!;\n if (pos + 2 + len > bytes.length) return null;\n pushes.push(bytes.slice(pos + 2, pos + 2 + len));\n pos += 2 + len;\n } else if (op === 0x4d) {\n if (pos + 3 > bytes.length) return null;\n const len = bytes[pos + 1]! | (bytes[pos + 2]! << 8);\n if (pos + 3 + len > bytes.length) return null;\n pushes.push(bytes.slice(pos + 3, pos + 3 + len));\n pos += 3 + len;\n } else if (op === 0x4e) {\n if (pos + 5 > bytes.length) return null;\n const len = bytes[pos + 1]! | (bytes[pos + 2]! << 8) | (bytes[pos + 3]! << 16) | (bytes[pos + 4]! * 0x1000000);\n if (len < 0 || pos + 5 + len > bytes.length) return null;\n pushes.push(bytes.slice(pos + 5, pos + 5 + len));\n pos += 5 + len;\n } else if (op === 0x00) {\n pushes.push(new Uint8Array(0));\n pos += 1;\n } else if (op >= 0x51 && op <= 0x60) {\n // OP_1..OP_16 are not data pushes for OP_RETURN payload parsing;\n // treat as terminator for generic push parsing.\n return null;\n } else {\n return null;\n }\n }\n return { pushes, consumed: pos };\n}\n\nfunction isCompressedPubkey(bytes: Uint8Array): boolean {\n return bytes.length === 33 && (bytes[0] === 0x02 || bytes[0] === 0x03);\n}\n\nfunction isUncompressedPubkey(bytes: Uint8Array): boolean {\n return bytes.length === 65 && bytes[0] === 0x04;\n}\n\nfunction isPubkey(bytes: Uint8Array): boolean {\n return isCompressedPubkey(bytes) || isUncompressedPubkey(bytes);\n}\n\nfunction unknown(scriptHex: string, amountSats: number): PaymentScriptUnknown {\n return {\n kind: \"unknown\",\n scriptHex,\n amountSats,\n reason: \"不认识此脚本,无法解释其用途\",\n summary: \"不认识此脚本,无法解释其用途\",\n };\n}\n\n/** 解释单个 locking script;匹配必须完整,失败按未知脚本展示。 */\nexport function interpretLockingScript(scriptHexRaw: string, amountSats: number): PaymentScriptInterpretation {\n const scriptHex = scriptHexRaw.toLowerCase();\n const bytes = hexToBytes(scriptHex);\n if (!bytes) return unknown(scriptHex, amountSats);\n // P2PKH: 76 a9 14 <20> 88 ac\n if (bytes.length === 25 && bytes[0] === 0x76 && bytes[1] === 0xa9 && bytes[2] === 0x14 && bytes[23] === 0x88 && bytes[24] === 0xac) {\n const hash = bytes.slice(3, 23);\n const hashHex = bytesToHex(hash);\n return {\n kind: \"p2pkh\",\n scriptHex,\n amountSats,\n hash160Hex: hashHex,\n addressMainnet: p2pkhAddress(hash, 0x00),\n addressTestnet: p2pkhAddress(hash, 0x6f),\n summary: `P2PKH 收款地址 ${p2pkhAddress(hash, 0x00)} / ${p2pkhAddress(hash, 0x6f)},金额 ${amountSats} sat`,\n };\n }\n // P2PK: <pubkey> OP_CHECKSIG\n if (bytes.length >= 2 && bytes[bytes.length - 1] === 0xac) {\n const pushOp = bytes[0]!;\n const pubLen = (pushOp >= 0x01 && pushOp <= 0x4b) ? pushOp : -1;\n if (pubLen > 0 && bytes.length === 1 + pubLen + 1) {\n const pub = bytes.slice(1, 1 + pubLen);\n if (isPubkey(pub)) {\n const pubHex = bytesToHex(pub);\n return {\n kind: \"p2pk\",\n scriptHex,\n amountSats,\n publicKeyHex: pubHex,\n summary: `P2PK 收款公钥 ${pubHex},金额 ${amountSats} sat`,\n };\n }\n }\n }\n // 标准多签: OP_m <pubkey>* OP_n OP_CHECKMULTISIG\n if (bytes.length >= 3 && bytes[bytes.length - 1] === 0xae) {\n const mOp = bytes[0]!;\n const nOp = bytes[bytes.length - 2]!;\n if (mOp >= 0x51 && mOp <= 0x60 && nOp >= 0x51 && nOp <= 0x60) {\n const m = mOp - 0x50;\n const n = nOp - 0x50;\n // 中间必须全是 push,且数量 == n\n let pos = 1;\n const pubs: Uint8Array[] = [];\n let ok = true;\n for (let i = 0; i < n; i++) {\n if (pos >= bytes.length - 2) { ok = false; break; }\n const op = bytes[pos]!;\n let len = -1;\n let header = 1;\n if (op >= 0x01 && op <= 0x4b) { len = op; header = 1; }\n else { ok = false; break; }\n if (pos + header + len > bytes.length - 2) { ok = false; break; }\n const pub = bytes.slice(pos + header, pos + header + len);\n if (!isPubkey(pub)) { ok = false; break; }\n pubs.push(pub);\n pos += header + len;\n }\n if (ok && pubs.length === n && pos === bytes.length - 2 && m >= 1 && m <= n && n >= 1 && n <= 16) {\n const hexes = pubs.map(bytesToHex);\n return {\n kind: \"multisig\",\n scriptHex,\n amountSats,\n m,\n n,\n publicKeysHex: hexes,\n summary: `标准多签 ${m}/${n},公钥 ${hexes.join(\", \")},金额 ${amountSats} sat`,\n };\n }\n }\n }\n // OP_RETURN / OP_FALSE OP_RETURN\n {\n let dataStart = -1;\n if (bytes.length >= 1 && bytes[0] === 0x6a) dataStart = 1;\n else if (bytes.length >= 2 && bytes[0] === 0x00 && bytes[1] === 0x6a) dataStart = 2;\n if (dataStart >= 0) {\n const rest = bytes.slice(dataStart);\n if (rest.length === 0) {\n return {\n kind: \"op_return\",\n scriptHex,\n amountSats,\n dataHex: \"\",\n dataText: \"\",\n summary: `数据输出,空数据,金额 ${amountSats} sat`,\n };\n }\n const parsed = parsePushes(rest, 0);\n if (parsed && parsed.consumed === rest.length) {\n const combined = parsed.pushes.reduce((acc, cur) => {\n const next = new Uint8Array(acc.length + cur.length);\n next.set(acc, 0);\n next.set(cur, acc.length);\n return next;\n }, new Uint8Array(0));\n const dataHex = bytesToHex(combined);\n const dataText = decodeText(combined);\n return {\n kind: \"op_return\",\n scriptHex,\n amountSats,\n dataHex,\n dataText,\n summary: `数据输出,数据 ${dataHex || \"(空)\"},金额 ${amountSats} sat`,\n };\n }\n // 剩余字节不是标准 push:按未知处理,避免误导性解释。\n return unknown(scriptHex, amountSats);\n }\n }\n return unknown(scriptHex, amountSats);\n}\n\n/** 按 App 顺序逐项解释业务输出。 */\nexport function interpretPaymentOutputs(outputs: PaymentOutput[]): InterpretedPaymentOutput[] {\n return outputs.map((output, index) => ({\n index,\n amountSats: output.amount,\n scriptHex: output.script.toLowerCase(),\n interpretation: interpretLockingScript(output.script, output.amount),\n }));\n}\n\n/** 业务输出总额(不含矿工费与找零)。 */\nexport function totalBusinessSats(outputs: PaymentOutput[]): number {\n let total = 0;\n for (const output of outputs) total += output.amount;\n return total;\n}\n"]}
package/dist/server.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { ProtocolErrorCode, P2pkhTransferAssetId, ProtocolRequestMessage, NetworkIdentity, ProtocolMethod, MethodParams, AppIdentityProofV1, VerifiedAppIdentity, ProtocolResultMessage, ProtocolReadyMessage, ProtocolClosingMessage, MethodResult, ProtocolError, ProtocolEventPayload, ProtocolReconnectMessage } from './protocol.js';
1
+ import { U as ProtocolErrorCode, au as P2pkhTransferAssetId, aG as ProtocolRequestMessage, N as NetworkIdentity, b as ProtocolMethod, M as MethodParams, Z as AppIdentityProofV1, aN as VerifiedAppIdentity, X as AiToolMessage, W as AiToolDefinition, a7 as JSONValue, aH as ProtocolResultMessage, aE as ProtocolReadyMessage, aA as ProtocolClosingMessage, c as MethodResult, aB as ProtocolError, aD as ProtocolEventPayload, aF as ProtocolReconnectMessage } from './protocol-C8z30CKK.js';
2
+ export { V as AI_TOOLS_MAX_JSON_CHARS, A as AiTool, Y as AiToolSchema, a as AiToolsRegistered, aO as assertToolJson, aP as assertToolPayload, aQ as isAiToolMessage, aS as parseToolDefinitions, aW as validateToolArguments } from './protocol-C8z30CKK.js';
2
3
 
3
4
  /** 校验失败:抛出的对象。service 会捕获并组装 ProtocolError。 */
4
5
  declare class ProtocolValidationError extends Error {
@@ -66,6 +67,33 @@ declare function verifyAppIdentityProof(raw: unknown): VerifiedAppIdentity;
66
67
  /** 校验 session 中已验证 proof 的不可变 snapshot。 */
67
68
  declare function isVerifiedAppIdentitySnapshot(value: unknown): value is VerifiedAppIdentity;
68
69
 
70
+ interface AiToolReceiverOptions {
71
+ /** Must verify the same Receiver's current binding, exact peer, origin and wallet lifecycle. */
72
+ isCurrent(bindingId: string): boolean;
73
+ send(message: AiToolMessage): void;
74
+ /** Host derives globally unique names and rejects conflicting live connection ownership. */
75
+ names(definitions: readonly AiToolDefinition[]): string[];
76
+ changed(): void;
77
+ randomUUID?(): string;
78
+ }
79
+ /** Protocol-only reverse-call endpoint; owns no App authorization, funds or persistence. */
80
+ declare class KeymasterAiToolReceiver {
81
+ private options;
82
+ private definitions;
83
+ private bindingId;
84
+ private pending;
85
+ private disposed;
86
+ constructor(options: AiToolReceiverOptions);
87
+ /** Host calls on connection changes; revoke stale in-flight calls without replay. */
88
+ sync(): void;
89
+ list(): readonly AiToolDefinition[];
90
+ available(name: string): boolean;
91
+ handle(value: unknown): boolean;
92
+ invoke(name: string, args: Record<string, JSONValue>, signal: AbortSignal, timeoutMs?: number): Promise<JSONValue>;
93
+ private clear;
94
+ dispose(): void;
95
+ }
96
+
69
97
  /** 窗口对象只作为消息源身份与发送目标;不读取跨源页面内容。 */
70
98
  interface ConnectPeer {
71
99
  postMessage(message: unknown, targetOrigin: string): void;
@@ -116,7 +144,7 @@ interface ReceiverRequest<M extends ProtocolMethod = ProtocolMethod> {
116
144
  /** 判断建立连接的方法;ready 只表示传输可用,不属于身份授权。 */
117
145
  declare function isEstablishing(method: string): boolean;
118
146
  /** 生产钱包与闭环测试共用同一接收端,不内置测试业务或默认自动授权。 */
119
- /** 外部恢复控制只携带不透明授权引用,不能夹带业务参数或钱包选择器。 */
147
+ /** 外部恢复控制只携带不透明授权引用和可选当前入口 proof,不能夹带业务参数或钱包选择器。 */
120
148
  declare function parseReconnectMessage(data: unknown): ProtocolReconnectMessage | null;
121
149
  declare class KeymasterConnectReceiver {
122
150
  private readonly options;
@@ -162,4 +190,4 @@ declare class KeymasterConnectReceiver {
162
190
  private send;
163
191
  }
164
192
 
165
- 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, parseReconnectMessage, parseRequestMessage, validateExactChannel, verifyAppIdentityProof };
193
+ export { APP_IDENTITY_DOMAIN_V1, AiToolDefinition, AiToolMessage, type AiToolReceiverOptions, AppIdentityValidationError, type ConnectPeer, type ConnectReceiverOptions, KeymasterAiToolReceiver, KeymasterConnectReceiver, ProtocolEventPayload, ProtocolValidationError, type ReceiverLifecycle, type ReceiverMessage, type ReceiverPeerPolicy, type ReceiverRequest, assertCompressedPubkeyHex, assertMainnetP2pkhAddress, assertOriginMatches, assertP2pkhAddressForAsset, canonicalizeJson, identityDigestBytes, identityPayload, isEstablishing, isStructuredCloneRequestCandidate, isVerifiedAppIdentitySnapshot, parseCancelMessage, parseReconnectMessage, parseRequestMessage, validateExactChannel, verifyAppIdentityProof };