@bcts/xid 1.0.0-alpha.10

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/src/error.ts ADDED
@@ -0,0 +1,305 @@
1
+ /**
2
+ * XID Error Types
3
+ *
4
+ * Error types returned when operating on XID Documents.
5
+ * Ported from bc-xid-rust/src/error.rs
6
+ */
7
+
8
+ export enum XIDErrorCode {
9
+ DUPLICATE = "DUPLICATE",
10
+ NOT_FOUND = "NOT_FOUND",
11
+ STILL_REFERENCED = "STILL_REFERENCED",
12
+ EMPTY_VALUE = "EMPTY_VALUE",
13
+ UNKNOWN_PRIVILEGE = "UNKNOWN_PRIVILEGE",
14
+ INVALID_XID = "INVALID_XID",
15
+ MISSING_INCEPTION_KEY = "MISSING_INCEPTION_KEY",
16
+ INVALID_RESOLUTION_METHOD = "INVALID_RESOLUTION_METHOD",
17
+ MULTIPLE_PROVENANCE_MARKS = "MULTIPLE_PROVENANCE_MARKS",
18
+ UNEXPECTED_PREDICATE = "UNEXPECTED_PREDICATE",
19
+ UNEXPECTED_NESTED_ASSERTIONS = "UNEXPECTED_NESTED_ASSERTIONS",
20
+ NO_PERMISSIONS = "NO_PERMISSIONS",
21
+ NO_REFERENCES = "NO_REFERENCES",
22
+ UNKNOWN_KEY_REFERENCE = "UNKNOWN_KEY_REFERENCE",
23
+ UNKNOWN_DELEGATE_REFERENCE = "UNKNOWN_DELEGATE_REFERENCE",
24
+ KEY_NOT_FOUND_IN_DOCUMENT = "KEY_NOT_FOUND_IN_DOCUMENT",
25
+ DELEGATE_NOT_FOUND_IN_DOCUMENT = "DELEGATE_NOT_FOUND_IN_DOCUMENT",
26
+ INVALID_PASSWORD = "INVALID_PASSWORD",
27
+ ENVELOPE_NOT_SIGNED = "ENVELOPE_NOT_SIGNED",
28
+ SIGNATURE_VERIFICATION_FAILED = "SIGNATURE_VERIFICATION_FAILED",
29
+ NO_PROVENANCE_MARK = "NO_PROVENANCE_MARK",
30
+ GENERATOR_CONFLICT = "GENERATOR_CONFLICT",
31
+ NO_GENERATOR = "NO_GENERATOR",
32
+ CHAIN_ID_MISMATCH = "CHAIN_ID_MISMATCH",
33
+ SEQUENCE_MISMATCH = "SEQUENCE_MISMATCH",
34
+ ENVELOPE_PARSING = "ENVELOPE_PARSING",
35
+ COMPONENT = "COMPONENT",
36
+ CBOR = "CBOR",
37
+ PROVENANCE_MARK = "PROVENANCE_MARK",
38
+ }
39
+
40
+ export class XIDError extends Error {
41
+ readonly code: XIDErrorCode;
42
+ declare readonly cause?: Error;
43
+
44
+ constructor(code: XIDErrorCode, message: string, cause?: Error) {
45
+ super(message);
46
+ this.name = "XIDError";
47
+ this.code = code;
48
+ if (cause !== undefined) {
49
+ this.cause = cause;
50
+ }
51
+
52
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
53
+ if ("captureStackTrace" in Error) {
54
+ (
55
+ Error as {
56
+ captureStackTrace(target: object, constructor: typeof XIDError): void;
57
+ }
58
+ ).captureStackTrace(this, XIDError);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Returned when attempting to add a duplicate item.
64
+ */
65
+ static duplicate(item: string): XIDError {
66
+ return new XIDError(XIDErrorCode.DUPLICATE, `duplicate item: ${item}`);
67
+ }
68
+
69
+ /**
70
+ * Returned when an item is not found.
71
+ */
72
+ static notFound(item: string): XIDError {
73
+ return new XIDError(XIDErrorCode.NOT_FOUND, `item not found: ${item}`);
74
+ }
75
+
76
+ /**
77
+ * Returned when an item is still referenced by other items.
78
+ */
79
+ static stillReferenced(item: string): XIDError {
80
+ return new XIDError(XIDErrorCode.STILL_REFERENCED, `item is still referenced: ${item}`);
81
+ }
82
+
83
+ /**
84
+ * Returned when a value is invalid or empty.
85
+ */
86
+ static emptyValue(field: string): XIDError {
87
+ return new XIDError(XIDErrorCode.EMPTY_VALUE, `invalid or empty value: ${field}`);
88
+ }
89
+
90
+ /**
91
+ * Returned when an unknown privilege is encountered.
92
+ */
93
+ static unknownPrivilege(): XIDError {
94
+ return new XIDError(XIDErrorCode.UNKNOWN_PRIVILEGE, "unknown privilege");
95
+ }
96
+
97
+ /**
98
+ * Returned when the XID is invalid.
99
+ */
100
+ static invalidXid(): XIDError {
101
+ return new XIDError(XIDErrorCode.INVALID_XID, "invalid XID");
102
+ }
103
+
104
+ /**
105
+ * Returned when the inception key is missing.
106
+ */
107
+ static missingInceptionKey(): XIDError {
108
+ return new XIDError(XIDErrorCode.MISSING_INCEPTION_KEY, "missing inception key");
109
+ }
110
+
111
+ /**
112
+ * Returned when the resolution method is invalid.
113
+ */
114
+ static invalidResolutionMethod(): XIDError {
115
+ return new XIDError(XIDErrorCode.INVALID_RESOLUTION_METHOD, "invalid resolution method");
116
+ }
117
+
118
+ /**
119
+ * Returned when multiple provenance marks are found.
120
+ */
121
+ static multipleProvenanceMarks(): XIDError {
122
+ return new XIDError(XIDErrorCode.MULTIPLE_PROVENANCE_MARKS, "multiple provenance marks");
123
+ }
124
+
125
+ /**
126
+ * Returned when an unexpected predicate is encountered.
127
+ */
128
+ static unexpectedPredicate(predicate: string): XIDError {
129
+ return new XIDError(XIDErrorCode.UNEXPECTED_PREDICATE, `unexpected predicate: ${predicate}`);
130
+ }
131
+
132
+ /**
133
+ * Returned when unexpected nested assertions are found.
134
+ */
135
+ static unexpectedNestedAssertions(): XIDError {
136
+ return new XIDError(XIDErrorCode.UNEXPECTED_NESTED_ASSERTIONS, "unexpected nested assertions");
137
+ }
138
+
139
+ /**
140
+ * Returned when a service has no permissions.
141
+ */
142
+ static noPermissions(uri: string): XIDError {
143
+ return new XIDError(XIDErrorCode.NO_PERMISSIONS, `no permissions in service '${uri}'`);
144
+ }
145
+
146
+ /**
147
+ * Returned when a service has no key or delegate references.
148
+ */
149
+ static noReferences(uri: string): XIDError {
150
+ return new XIDError(
151
+ XIDErrorCode.NO_REFERENCES,
152
+ `no key or delegate references in service '${uri}'`,
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Returned when an unknown key reference is found in a service.
158
+ */
159
+ static unknownKeyReference(reference: string, uri: string): XIDError {
160
+ return new XIDError(
161
+ XIDErrorCode.UNKNOWN_KEY_REFERENCE,
162
+ `unknown key reference ${reference} in service '${uri}'`,
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Returned when an unknown delegate reference is found in a service.
168
+ */
169
+ static unknownDelegateReference(reference: string, uri: string): XIDError {
170
+ return new XIDError(
171
+ XIDErrorCode.UNKNOWN_DELEGATE_REFERENCE,
172
+ `unknown delegate reference ${reference} in service '${uri}'`,
173
+ );
174
+ }
175
+
176
+ /**
177
+ * Returned when a key is not found in the XID document.
178
+ */
179
+ static keyNotFoundInDocument(key: string): XIDError {
180
+ return new XIDError(
181
+ XIDErrorCode.KEY_NOT_FOUND_IN_DOCUMENT,
182
+ `key not found in XID document: ${key}`,
183
+ );
184
+ }
185
+
186
+ /**
187
+ * Returned when a delegate is not found in the XID document.
188
+ */
189
+ static delegateNotFoundInDocument(delegate: string): XIDError {
190
+ return new XIDError(
191
+ XIDErrorCode.DELEGATE_NOT_FOUND_IN_DOCUMENT,
192
+ `delegate not found in XID document: ${delegate}`,
193
+ );
194
+ }
195
+
196
+ /**
197
+ * Returned when the password is invalid.
198
+ */
199
+ static invalidPassword(): XIDError {
200
+ return new XIDError(XIDErrorCode.INVALID_PASSWORD, "invalid password");
201
+ }
202
+
203
+ /**
204
+ * Returned when the envelope is not signed.
205
+ */
206
+ static envelopeNotSigned(): XIDError {
207
+ return new XIDError(XIDErrorCode.ENVELOPE_NOT_SIGNED, "envelope is not signed");
208
+ }
209
+
210
+ /**
211
+ * Returned when signature verification fails.
212
+ */
213
+ static signatureVerificationFailed(): XIDError {
214
+ return new XIDError(
215
+ XIDErrorCode.SIGNATURE_VERIFICATION_FAILED,
216
+ "signature verification failed",
217
+ );
218
+ }
219
+
220
+ /**
221
+ * Returned when there is no provenance mark to advance.
222
+ */
223
+ static noProvenanceMark(): XIDError {
224
+ return new XIDError(XIDErrorCode.NO_PROVENANCE_MARK, "no provenance mark to advance");
225
+ }
226
+
227
+ /**
228
+ * Returned when document already has generator but external generator was provided.
229
+ */
230
+ static generatorConflict(): XIDError {
231
+ return new XIDError(
232
+ XIDErrorCode.GENERATOR_CONFLICT,
233
+ "document already has generator, cannot provide external generator",
234
+ );
235
+ }
236
+
237
+ /**
238
+ * Returned when document does not have generator but needs one.
239
+ */
240
+ static noGenerator(): XIDError {
241
+ return new XIDError(
242
+ XIDErrorCode.NO_GENERATOR,
243
+ "document does not have generator, must provide external generator",
244
+ );
245
+ }
246
+
247
+ /**
248
+ * Returned when generator chain ID doesn't match.
249
+ */
250
+ static chainIdMismatch(expected: Uint8Array, actual: Uint8Array): XIDError {
251
+ const expectedHex = Array.from(expected)
252
+ .map((b) => b.toString(16).padStart(2, "0"))
253
+ .join("");
254
+ const actualHex = Array.from(actual)
255
+ .map((b) => b.toString(16).padStart(2, "0"))
256
+ .join("");
257
+ return new XIDError(
258
+ XIDErrorCode.CHAIN_ID_MISMATCH,
259
+ `generator chain ID mismatch: expected ${expectedHex}, got ${actualHex}`,
260
+ );
261
+ }
262
+
263
+ /**
264
+ * Returned when generator sequence doesn't match.
265
+ */
266
+ static sequenceMismatch(expected: number, actual: number): XIDError {
267
+ return new XIDError(
268
+ XIDErrorCode.SEQUENCE_MISMATCH,
269
+ `generator sequence mismatch: expected ${expected}, got ${actual}`,
270
+ );
271
+ }
272
+
273
+ /**
274
+ * Envelope parsing error wrapper.
275
+ */
276
+ static envelopeParsing(cause?: Error): XIDError {
277
+ return new XIDError(XIDErrorCode.ENVELOPE_PARSING, "envelope parsing error", cause);
278
+ }
279
+
280
+ /**
281
+ * Component error wrapper.
282
+ */
283
+ static component(cause?: Error): XIDError {
284
+ return new XIDError(XIDErrorCode.COMPONENT, "component error", cause);
285
+ }
286
+
287
+ /**
288
+ * CBOR error wrapper.
289
+ */
290
+ static cbor(cause?: Error): XIDError {
291
+ return new XIDError(XIDErrorCode.CBOR, "CBOR error", cause);
292
+ }
293
+
294
+ /**
295
+ * Provenance mark error wrapper.
296
+ */
297
+ static provenanceMark(cause?: Error): XIDError {
298
+ return new XIDError(XIDErrorCode.PROVENANCE_MARK, "provenance mark error", cause);
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Result type for XID operations.
304
+ */
305
+ export type XIDResult<T> = T;
package/src/index.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @bcts/xid - XID Document Library
3
+ *
4
+ * TypeScript implementation of Blockchain Commons' XID specification
5
+ * for eXtensible IDentifiers and XID Documents.
6
+ *
7
+ * Ported from bc-xid-rust
8
+ */
9
+
10
+ // Error handling
11
+ export { XIDError, XIDErrorCode, type XIDResult } from "./error";
12
+
13
+ // Privilege system
14
+ export {
15
+ Privilege,
16
+ privilegeToKnownValue,
17
+ privilegeFromKnownValue,
18
+ privilegeToEnvelope,
19
+ privilegeFromEnvelope,
20
+ } from "./privilege";
21
+
22
+ // Permissions
23
+ export { Permissions, type HasPermissions, HasPermissionsMixin } from "./permissions";
24
+
25
+ // Name/Nickname
26
+ export { type HasNickname, HasNicknameMixin } from "./name";
27
+
28
+ // Shared reference wrapper
29
+ export { Shared } from "./shared";
30
+
31
+ // Key types
32
+ export {
33
+ Key,
34
+ XIDPrivateKeyOptions,
35
+ type XIDPrivateKeyEncryptConfig,
36
+ type XIDPrivateKeyOptionsValue,
37
+ type PrivateKeyData,
38
+ } from "./key";
39
+
40
+ // Service
41
+ export { Service } from "./service";
42
+
43
+ // Delegate
44
+ export { Delegate, registerXIDDocumentClass, type XIDDocumentType } from "./delegate";
45
+
46
+ // Provenance
47
+ export {
48
+ Provenance,
49
+ XIDGeneratorOptions,
50
+ type XIDGeneratorEncryptConfig,
51
+ type XIDGeneratorOptionsValue,
52
+ type GeneratorData,
53
+ } from "./provenance";
54
+
55
+ // XID Document (main export)
56
+ export {
57
+ XIDDocument,
58
+ type XIDInceptionKeyOptions,
59
+ type XIDGenesisMarkOptions,
60
+ type XIDSigningOptions,
61
+ XIDVerifySignature,
62
+ } from "./xid-document";
63
+
64
+ // Version information
65
+ export const VERSION = "1.0.0-alpha.3";