@tik-choco/mistsign 0.0.0-stage → 0.1.1

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 (86) hide show
  1. package/LICENSE +373 -0
  2. package/README.ja.md +125 -0
  3. package/README.md +129 -2
  4. package/README.zh.md +122 -0
  5. package/dist/agreement/bundle.d.ts +23 -0
  6. package/dist/agreement/bundle.js +139 -0
  7. package/dist/agreement/decision.d.ts +7 -0
  8. package/dist/agreement/decision.js +77 -0
  9. package/dist/agreement/errors.d.ts +9 -0
  10. package/dist/agreement/errors.js +19 -0
  11. package/dist/agreement/evaluate.d.ts +5 -0
  12. package/dist/agreement/evaluate.js +112 -0
  13. package/dist/agreement/index.d.ts +9 -0
  14. package/dist/agreement/index.js +6 -0
  15. package/dist/agreement/policy.d.ts +31 -0
  16. package/dist/agreement/policy.js +113 -0
  17. package/dist/agreement/proposal.d.ts +4 -0
  18. package/dist/agreement/proposal.js +107 -0
  19. package/dist/agreement/schema.d.ts +16 -0
  20. package/dist/agreement/schema.js +93 -0
  21. package/dist/agreement/strict.d.ts +5 -0
  22. package/dist/agreement/strict.js +167 -0
  23. package/dist/agreement/types.d.ts +214 -0
  24. package/dist/agreement/types.js +3 -0
  25. package/dist/agreement/verify.d.ts +4 -0
  26. package/dist/agreement/verify.js +151 -0
  27. package/dist/bytes.d.ts +6 -0
  28. package/dist/bytes.js +19 -0
  29. package/dist/content.d.ts +10 -0
  30. package/dist/content.js +58 -0
  31. package/dist/didKey.d.ts +4 -0
  32. package/dist/didKey.js +48 -0
  33. package/dist/elements/index.d.ts +1 -0
  34. package/dist/elements/index.js +2 -0
  35. package/dist/errors.d.ts +7 -0
  36. package/dist/errors.js +8 -0
  37. package/dist/index.d.ts +13 -0
  38. package/dist/index.js +6 -0
  39. package/dist/json.d.ts +20 -0
  40. package/dist/json.js +170 -0
  41. package/dist/preact/index.d.ts +1 -0
  42. package/dist/preact/index.js +2 -0
  43. package/dist/preact/ui.css +0 -0
  44. package/dist/sha256.d.ts +11 -0
  45. package/dist/sha256.js +103 -0
  46. package/dist/sign.d.ts +33 -0
  47. package/dist/sign.js +61 -0
  48. package/dist/testing/index.d.ts +5 -0
  49. package/dist/testing/index.js +27 -0
  50. package/dist/time.d.ts +9 -0
  51. package/dist/time.js +36 -0
  52. package/dist/timestamp/asnTime.d.ts +2 -0
  53. package/dist/timestamp/asnTime.js +17 -0
  54. package/dist/timestamp/cms.d.ts +30 -0
  55. package/dist/timestamp/cms.js +126 -0
  56. package/dist/timestamp/constraints.d.ts +17 -0
  57. package/dist/timestamp/constraints.js +159 -0
  58. package/dist/timestamp/crypto.d.ts +6 -0
  59. package/dist/timestamp/crypto.js +101 -0
  60. package/dist/timestamp/der.d.ts +24 -0
  61. package/dist/timestamp/der.js +118 -0
  62. package/dist/timestamp/index.d.ts +2 -0
  63. package/dist/timestamp/index.js +1 -0
  64. package/dist/timestamp/name.d.ts +2 -0
  65. package/dist/timestamp/name.js +55 -0
  66. package/dist/timestamp/policy.d.ts +9 -0
  67. package/dist/timestamp/policy.js +56 -0
  68. package/dist/timestamp/verify.d.ts +33 -0
  69. package/dist/timestamp/verify.js +74 -0
  70. package/dist/timestamp/x509.d.ts +35 -0
  71. package/dist/timestamp/x509.js +232 -0
  72. package/dist/transport/index.d.ts +1 -0
  73. package/dist/transport/index.js +2 -0
  74. package/dist/trust/delegation.d.ts +15 -0
  75. package/dist/trust/delegation.js +40 -0
  76. package/dist/trust/index.d.ts +15 -0
  77. package/dist/trust/index.js +3 -0
  78. package/dist/trust/revocation.d.ts +33 -0
  79. package/dist/trust/revocation.js +78 -0
  80. package/dist/trust/signers.d.ts +31 -0
  81. package/dist/trust/signers.js +166 -0
  82. package/dist/verify.d.ts +88 -0
  83. package/dist/verify.js +195 -0
  84. package/dist/wire.d.ts +6 -0
  85. package/dist/wire.js +38 -0
  86. package/package.json +89 -4
@@ -0,0 +1,88 @@
1
+ import type { FileInput, ProgressCallback } from './content.js';
2
+ import type { ErrorCode } from './errors.js';
3
+ import type { JsonInput } from './json.js';
4
+ import type { SignatureFileInfo } from './sign.js';
5
+ import type { TimeInput } from './time.js';
6
+ import type { CertificateInput } from './timestamp/x509.js';
7
+ import type { RevocationNotice, RevocationResult } from './trust/revocation.js';
8
+ import type { TrustPolicy } from './trust/index.js';
9
+ export interface VerifyOptions extends TrustPolicy {
10
+ now?: TimeInput;
11
+ /** Detached RFC 3161 TimeStampToken in DER. */
12
+ timestamp?: Uint8Array;
13
+ /** Trusted DER certificates or PEM bundles. Omit to leave the token unverified. */
14
+ tsaRoots?: readonly CertificateInput[];
15
+ }
16
+ export interface VerifyInput {
17
+ signature: JsonInput;
18
+ content?: FileInput;
19
+ /** Defaults to File.name, or empty if the input has no name. */
20
+ contentName?: string;
21
+ options?: VerifyOptions;
22
+ progress?: ProgressCallback;
23
+ }
24
+ export type VerificationStatus = 'verified' | 'signer-unconfirmed' | 'invalid' | 'error';
25
+ export type NoticeCode = 'signer-period-self-declared' | 'timestamp-late';
26
+ export interface ResultSigner {
27
+ id: string;
28
+ leaf: string;
29
+ root?: string;
30
+ delegated: boolean;
31
+ name?: string;
32
+ }
33
+ export interface ResultIdentity {
34
+ checked: boolean;
35
+ by: ('signers' | 'expect')[];
36
+ }
37
+ export interface ResultTime {
38
+ source: 'self-declared' | 'timestamp';
39
+ at: string;
40
+ timestamp: ResultTimestamp;
41
+ }
42
+ export interface ResultTimestamp {
43
+ status: 'none' | 'unverified' | 'verified';
44
+ digest: string;
45
+ genTime?: string;
46
+ accuracyMs?: number;
47
+ tsa?: string;
48
+ }
49
+ export interface ResultDelegation {
50
+ valid: boolean;
51
+ iat: string;
52
+ exp: string;
53
+ }
54
+ export interface VerificationResult {
55
+ valid: boolean;
56
+ signer: ResultSigner;
57
+ identity: ResultIdentity;
58
+ signedAt: string;
59
+ time: ResultTime;
60
+ comment?: string;
61
+ file: SignatureFileInfo & {
62
+ nameMatches: boolean;
63
+ };
64
+ delegation?: ResultDelegation;
65
+ notices: NoticeCode[];
66
+ errors: ErrorCode[];
67
+ revocation?: RevocationResult;
68
+ revocationNotices: RevocationNotice[];
69
+ }
70
+ export type VerifyOutcome = {
71
+ status: 'verified' | 'signer-unconfirmed';
72
+ exitCode: 0;
73
+ result: VerificationResult;
74
+ } | {
75
+ status: 'invalid';
76
+ exitCode: 2;
77
+ result: VerificationResult;
78
+ } | {
79
+ status: 'error';
80
+ exitCode: 1;
81
+ diagnostic: ErrorCode;
82
+ };
83
+ /**
84
+ * Verify a detached signature and evaluate verifier-supplied trust conditions.
85
+ * Result field population follows tc-sign's early-return order; input errors
86
+ * have no result, whereas invalid signatures/conditions retain partial results.
87
+ */
88
+ export declare function verify(input: VerifyInput): Promise<VerifyOutcome>;
package/dist/verify.js ADDED
@@ -0,0 +1,195 @@
1
+ import { hex, sha256, utf8 } from './bytes.js';
2
+ import { basename, hashContent, inputName } from './content.js';
3
+ import { isEd25519DidKey } from './didKey.js';
4
+ import { canonicalJson, isObject, MAX_SIGNATURE_SIZE, parseStrictJson } from './json.js';
5
+ import { SIGNATURE_TYPE } from './sign.js';
6
+ import { CLOCK_SKEW, formatTime, MS, nowInstant, parseTime } from './time.js';
7
+ import { verifyTimestampWithRoots } from './timestamp/verify.js';
8
+ import { parseRoots } from './timestamp/x509.js';
9
+ import { verifyDelegationAt } from './trust/delegation.js';
10
+ import { evaluateRevocations, parseRevocation } from './trust/revocation.js';
11
+ import { matchSignerAt, parseAllowedSigners } from './trust/signers.js';
12
+ import { verifyWire } from './wire.js';
13
+ const inputError = (diagnostic) => ({ status: 'error', exitCode: 1, diagnostic });
14
+ const text = (value) => typeof value === 'string' ? value : '';
15
+ /**
16
+ * Verify a detached signature and evaluate verifier-supplied trust conditions.
17
+ * Result field population follows tc-sign's early-return order; input errors
18
+ * have no result, whereas invalid signatures/conditions retain partial results.
19
+ */
20
+ export async function verify(input) {
21
+ const opts = input.options ?? {};
22
+ let roots;
23
+ try {
24
+ if (opts.tsaRoots !== undefined)
25
+ roots = parseRoots(opts.tsaRoots);
26
+ }
27
+ catch {
28
+ return inputError('timestamp-roots');
29
+ }
30
+ const name = basename(input.contentName ?? inputName(input.content) ?? '');
31
+ const result = {
32
+ valid: false, signer: { id: '', leaf: '', delegated: false },
33
+ identity: { checked: false, by: [] }, signedAt: '',
34
+ time: { source: 'self-declared', at: '', timestamp: { status: opts.timestamp === undefined ? 'none' : 'unverified', digest: '' } },
35
+ file: { name: '', size: 0, sha256: '', nameMatches: false },
36
+ notices: [], errors: [], revocationNotices: [],
37
+ };
38
+ const invalid = (...codes) => {
39
+ result.errors.push(...codes);
40
+ return { status: 'invalid', exitCode: 2, result };
41
+ };
42
+ if (opts.signerName && opts.signers === undefined)
43
+ return inputError('signer-name-without-signers');
44
+ let signers;
45
+ try {
46
+ if (opts.signers !== undefined)
47
+ signers = parseAllowedSigners(opts.signers);
48
+ }
49
+ catch {
50
+ return inputError('invalid-signers');
51
+ }
52
+ const revocations = [];
53
+ try {
54
+ for (const raw of opts.revocations ?? [])
55
+ revocations.push(await parseRevocation(raw));
56
+ }
57
+ catch {
58
+ return inputError('invalid-revocation');
59
+ }
60
+ let now;
61
+ try {
62
+ now = nowInstant(opts.now);
63
+ }
64
+ catch {
65
+ return inputError('invalid-time');
66
+ }
67
+ // Go treats the 1 MiB bound as an input error, unlike invalid JSON.
68
+ if (typeof input.signature === 'string') {
69
+ if (input.signature.length > MAX_SIGNATURE_SIZE || utf8.encode(input.signature).length > MAX_SIGNATURE_SIZE) {
70
+ return inputError('document-too-large');
71
+ }
72
+ }
73
+ else if (input.signature.byteLength > MAX_SIGNATURE_SIZE) {
74
+ return inputError('document-too-large');
75
+ }
76
+ let wire;
77
+ try {
78
+ const decoded = parseStrictJson(input.signature, 'signature');
79
+ if (!isObject(decoded))
80
+ return invalid('invalid-json');
81
+ wire = decoded;
82
+ }
83
+ catch {
84
+ return invalid('invalid-json');
85
+ }
86
+ if (wire.type !== SIGNATURE_TYPE || wire.v !== 1)
87
+ return invalid('invalid-format');
88
+ const leaf = wire.fromId;
89
+ if (!isEd25519DidKey(leaf))
90
+ return invalid('invalid-did');
91
+ result.signer = { id: leaf, leaf, delegated: false };
92
+ if (typeof wire.signature !== 'string')
93
+ return invalid('missing-signature');
94
+ if (!isObject(wire.file))
95
+ return invalid('invalid-file-info');
96
+ const fileSize = wire.file.size, digest = wire.file.sha256;
97
+ if (typeof fileSize !== 'number' || !Number.isSafeInteger(fileSize) || fileSize < 0)
98
+ return invalid('invalid-file-size');
99
+ if (typeof digest !== 'string' || !/^[0-9a-fA-F]{64}$/.test(digest))
100
+ return invalid('invalid-sha256');
101
+ const recordedName = text(wire.file.name);
102
+ result.file = { name: recordedName, size: fileSize === 0 ? 0 : fileSize, sha256: digest, nameMatches: name !== '' && recordedName === name };
103
+ let signedTime;
104
+ try {
105
+ signedTime = parseTime(wire.signedAt);
106
+ }
107
+ catch {
108
+ return invalid('invalid-time');
109
+ }
110
+ result.signedAt = wire.signedAt;
111
+ result.time.at = formatTime(signedTime);
112
+ if (typeof wire.comment === 'string')
113
+ result.comment = wire.comment;
114
+ let sigDigest;
115
+ try {
116
+ if (!await verifyWire(wire))
117
+ return invalid('signature-mismatch');
118
+ sigDigest = await sha256(utf8.encode(canonicalJson(wire)));
119
+ result.time.timestamp.digest = hex(sigDigest);
120
+ }
121
+ catch {
122
+ return inputError('crypto-error');
123
+ }
124
+ if (input.content === undefined)
125
+ return inputError('missing-content');
126
+ result.file.nameMatches = recordedName === name;
127
+ try {
128
+ const hash = await hashContent(input.content, fileSize, input.progress);
129
+ if (hash.size !== fileSize || hash.sha256 !== digest.toLowerCase())
130
+ return invalid('file-mismatch');
131
+ }
132
+ catch {
133
+ return inputError('content-read-error');
134
+ }
135
+ if (signedTime > now + CLOCK_SKEW)
136
+ return invalid('future-signed-at');
137
+ let judgeTime = signedTime;
138
+ if (opts.timestamp !== undefined && roots !== undefined) {
139
+ const ts = await verifyTimestampWithRoots(opts.timestamp, sigDigest, roots, now);
140
+ if (ts.status === 'unverified')
141
+ return invalid(ts.reason);
142
+ judgeTime = parseTime(ts.genTime);
143
+ result.time.source = 'timestamp';
144
+ result.time.at = formatTime(judgeTime);
145
+ result.time.timestamp = { status: 'verified', digest: result.time.timestamp.digest, genTime: result.time.at, tsa: ts.tsa };
146
+ if (ts.accuracyNs !== undefined)
147
+ result.time.timestamp.accuracyMs = Number(ts.accuracyNs / MS);
148
+ if (signedTime > judgeTime + CLOCK_SKEW)
149
+ return invalid('signed-at-after-timestamp');
150
+ if (judgeTime - signedTime >= 3600000n * MS)
151
+ result.notices.push('timestamp-late');
152
+ }
153
+ if (opts.requireTimestamp && result.time.timestamp.status !== 'verified')
154
+ return invalid('timestamp-required');
155
+ let delegationRoot = '';
156
+ if (Object.hasOwn(wire, 'delegation')) {
157
+ result.delegation = { valid: false, iat: '', exp: '' };
158
+ if (isObject(wire.delegation)) {
159
+ const d = wire.delegation;
160
+ delegationRoot = text(d.root);
161
+ result.delegation.iat = text(d.iat);
162
+ result.delegation.exp = text(d.exp);
163
+ if (await verifyDelegationAt(d, judgeTime, leaf)) {
164
+ result.delegation.valid = true;
165
+ result.signer = { id: delegationRoot, root: delegationRoot, leaf, delegated: true };
166
+ }
167
+ }
168
+ }
169
+ const applied = evaluateRevocations(revocations, delegationRoot, leaf);
170
+ result.revocationNotices = applied.revocationNotices;
171
+ if (applied.revocation) {
172
+ result.revocation = applied.revocation;
173
+ if (result.time.timestamp.status !== 'verified')
174
+ return invalid('revoked-without-timestamp');
175
+ if (judgeTime >= parseTime(applied.revocation.revokedAt))
176
+ return invalid('revoked-at-timestamp');
177
+ result.revocation.beforeRevocation = true;
178
+ }
179
+ if (opts.expect && opts.expect !== result.signer.id && opts.expect !== leaf)
180
+ return invalid('expect-mismatch');
181
+ if (signers !== undefined) {
182
+ const match = matchSignerAt(signers, leaf, result.signer.root ?? '', SIGNATURE_TYPE, opts.signerName ?? '', judgeTime);
183
+ if (match.periodChecked && result.time.source === 'self-declared')
184
+ result.notices.push('signer-period-self-declared');
185
+ if (!match.matched)
186
+ return invalid(...match.reasons);
187
+ result.signer.name = match.matched.name;
188
+ result.identity.by.push('signers');
189
+ }
190
+ if (opts.expect)
191
+ result.identity.by.push('expect');
192
+ result.identity.checked = result.identity.by.length > 0;
193
+ result.valid = true;
194
+ return { status: result.identity.checked ? 'verified' : 'signer-unconfirmed', exitCode: 0, result };
195
+ }
package/dist/wire.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { DidSigner } from '@tik-choco/mistseed/identity/core';
2
+ export type { DidSigner } from '@tik-choco/mistseed/identity/core';
3
+ export declare function signingPayload(wire: object): Uint8Array;
4
+ export declare function verifyEd25519(did: string, payload: Uint8Array, signature: unknown): Promise<boolean>;
5
+ export declare function verifyWire(wire: Record<string, unknown>): Promise<boolean>;
6
+ export declare function signWire(wire: Record<string, unknown>, signer: DidSigner): Promise<string>;
package/dist/wire.js ADDED
@@ -0,0 +1,38 @@
1
+ import { base64url, decodeEd25519Signature, utf8 } from './bytes.js';
2
+ import { ed25519PublicKeyFromDidKey } from './didKey.js';
3
+ import { MistsignError } from './errors.js';
4
+ import { canonicalJson } from './json.js';
5
+ export function signingPayload(wire) {
6
+ const { signature: _signature, ...unsigned } = wire;
7
+ return utf8.encode(canonicalJson(unsigned));
8
+ }
9
+ export async function verifyEd25519(did, payload, signature) {
10
+ const decoded = decodeEd25519Signature(signature);
11
+ if (!decoded)
12
+ return false;
13
+ try {
14
+ const publicKey = await globalThis.crypto.subtle.importKey('raw', new Uint8Array(ed25519PublicKeyFromDidKey(did)), 'Ed25519', false, ['verify']);
15
+ return await globalThis.crypto.subtle.verify('Ed25519', publicKey, new Uint8Array(decoded), new Uint8Array(payload));
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ export async function verifyWire(wire) {
22
+ if (typeof wire.fromId !== 'string')
23
+ return false;
24
+ return verifyEd25519(wire.fromId, signingPayload(wire), wire.signature);
25
+ }
26
+ export async function signWire(wire, signer) {
27
+ const payload = signingPayload(wire);
28
+ // Check that an injected signer actually signs with the advertised DID;
29
+ // unlike Go, we do not have access to its private key.
30
+ const signature = await signer.sign(payload.slice());
31
+ if (!(signature instanceof Uint8Array) || signature.length !== 64)
32
+ throw new MistsignError('signature-mismatch');
33
+ const encoded = base64url(signature);
34
+ if (typeof wire.fromId !== 'string' || !await verifyEd25519(wire.fromId, payload, encoded)) {
35
+ throw new MistsignError('signature-mismatch');
36
+ }
37
+ return encoded;
38
+ }
package/package.json CHANGED
@@ -1,6 +1,91 @@
1
1
  {
2
2
  "name": "@tik-choco/mistsign",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Browser-oriented electronic signatures, trust evaluation, and multi-party agreements with injectable mistlib transport.",
5
+ "type": "module",
6
+ "license": "MPL-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/tik-choco-lab/mistsign.git"
10
+ },
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./trust": {
19
+ "types": "./dist/trust/index.d.ts",
20
+ "default": "./dist/trust/index.js"
21
+ },
22
+ "./agreement": {
23
+ "types": "./dist/agreement/index.d.ts",
24
+ "default": "./dist/agreement/index.js"
25
+ },
26
+ "./transport": {
27
+ "types": "./dist/transport/index.d.ts",
28
+ "default": "./dist/transport/index.js"
29
+ },
30
+ "./timestamp": {
31
+ "types": "./dist/timestamp/index.d.ts",
32
+ "default": "./dist/timestamp/index.js"
33
+ },
34
+ "./elements": {
35
+ "types": "./dist/elements/index.d.ts",
36
+ "default": "./dist/elements/index.js"
37
+ },
38
+ "./preact": {
39
+ "types": "./dist/preact/index.d.ts",
40
+ "default": "./dist/preact/index.js"
41
+ },
42
+ "./testing": {
43
+ "types": "./dist/testing/index.d.ts",
44
+ "default": "./dist/testing/index.js"
45
+ },
46
+ "./ui.css": "./dist/preact/ui.css"
47
+ },
48
+ "files": [
49
+ "dist",
50
+ "README.md",
51
+ "README.ja.md",
52
+ "README.zh.md",
53
+ "LICENSE"
54
+ ],
55
+ "sideEffects": [
56
+ "./dist/elements/**/*.js",
57
+ "./dist/preact/ui.css"
58
+ ],
59
+ "scripts": {
60
+ "build": "tsc -p tsconfig.build.json && node -e \"require('fs').copyFileSync('src/preact/ui.css','dist/preact/ui.css')\"",
61
+ "test": "vitest run",
62
+ "test:browser": "vitest run --config vitest.browser.config.ts",
63
+ "bench:sha256": "node test/sha256-comparison.mjs",
64
+ "vectors:sync": "node scripts/sync-vectors.mjs",
65
+ "typecheck": "tsc --noEmit",
66
+ "prepare": "npm run build"
67
+ },
68
+ "devDependencies": {
69
+ "@noble/hashes": "^2.4.0",
70
+ "@vitest/browser-playwright": "^4.1.12",
71
+ "happy-dom": "^21.0.4",
72
+ "hash-wasm": "^4.13.0",
73
+ "playwright": "^1.65.2",
74
+ "preact": "^10.29.1",
75
+ "typescript": "~6.0.2",
76
+ "vite": "^8.4.0",
77
+ "vitest": "^4.0.0"
78
+ },
79
+ "peerDependencies": {
80
+ "@tik-choco/mistseed": "^0.2.0",
81
+ "preact": "^10.0.0"
82
+ },
83
+ "peerDependenciesMeta": {
84
+ "preact": {
85
+ "optional": true
86
+ }
87
+ },
88
+ "publishConfig": {
89
+ "access": "public"
90
+ }
91
+ }