@interop/did-method-webvh 3.0.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,95 @@
1
+ import { concatBuffers } from './utils/buffer.js';
2
+ import { canonicalizeStrict } from './utils/canonicalize.js';
3
+ import { createHash } from './utils/crypto.js';
4
+ import { createDate } from './utils.js';
5
+ /**
6
+ * Creates a proof object for a document
7
+ * @param verificationMethodId - The verification method ID to use in the proof
8
+ * @returns A proof object with type, cryptosuite, verificationMethod, created, and proofPurpose
9
+ */
10
+ export const createProof = (verificationMethodId) => {
11
+ return {
12
+ type: 'DataIntegrityProof',
13
+ cryptosuite: 'eddsa-jcs-2022',
14
+ verificationMethod: verificationMethodId,
15
+ created: createDate(),
16
+ proofPurpose: 'assertionMethod',
17
+ };
18
+ };
19
+ /**
20
+ * Prepares data for signing by hashing and concatenating the document and proof
21
+ * @param document - The document to sign
22
+ * @param proof - The proof object
23
+ * @returns The prepared data for signing as a Uint8Array
24
+ */
25
+ export const prepareDataForSigning = async (document, proof) => {
26
+ const dataHash = await createHash(canonicalizeStrict(document));
27
+ const proofHash = await createHash(canonicalizeStrict(proof));
28
+ return concatBuffers(proofHash, dataHash);
29
+ };
30
+ /**
31
+ * Abstract base class for signers
32
+ * Users should extend this class to implement their own signing logic
33
+ */
34
+ export class AbstractCrypto {
35
+ verificationMethod;
36
+ useStaticId;
37
+ constructor(options) {
38
+ if (options.verificationMethod) {
39
+ this.verificationMethod = options.verificationMethod;
40
+ }
41
+ this.useStaticId = options.useStaticId !== undefined ? options.useStaticId : true;
42
+ }
43
+ /**
44
+ * Get the verification method ID
45
+ * @returns The verification method ID
46
+ */
47
+ getVerificationMethodId() {
48
+ if (!this.verificationMethod) {
49
+ throw new Error('Verification method not set');
50
+ }
51
+ return this.useStaticId
52
+ ? `did:key:${this.verificationMethod.publicKeyMultibase}#${this.verificationMethod.publicKeyMultibase}`
53
+ : this.verificationMethod.id || '';
54
+ }
55
+ }
56
+ /**
57
+ * Creates a document signer from any Signer implementation
58
+ * @param signer - The signer to use
59
+ * @param verificationMethodId - The verification method ID to use in proofs
60
+ * @returns A function that signs a document and returns the document with proof
61
+ */
62
+ export const createDocumentSigner = (signer, verificationMethodId) => {
63
+ return async (doc) => {
64
+ try {
65
+ const proof = createProof(verificationMethodId);
66
+ const result = await signer.sign({ document: doc, proof });
67
+ return { ...doc, proof: { ...proof, proofValue: result.proofValue } };
68
+ }
69
+ catch (e) {
70
+ console.error(e);
71
+ throw new Error(`Document signing failure: ${e.message || e}`);
72
+ }
73
+ };
74
+ };
75
+ /**
76
+ * @deprecated Use createDocumentSigner with your own Signer implementation instead
77
+ */
78
+ export const createSigner = (vm, useStatic = true) => {
79
+ console.warn('createSigner is deprecated. Use createDocumentSigner with your own Signer implementation instead.');
80
+ return async (doc) => {
81
+ try {
82
+ const verificationMethodId = useStatic
83
+ ? `did:key:${vm.publicKeyMultibase}#${vm.publicKeyMultibase}`
84
+ : vm.id || '';
85
+ const proof = createProof(verificationMethodId);
86
+ // This is a placeholder for backward compatibility
87
+ // Users should implement their own signing logic
88
+ throw new Error('createSigner is deprecated. Implement your own Signer and use createDocumentSigner instead.');
89
+ }
90
+ catch (e) {
91
+ console.error(e);
92
+ throw new Error(`Document signing failure: ${e.message || e}`);
93
+ }
94
+ };
95
+ };
@@ -0,0 +1,6 @@
1
+ export { AbstractCrypto, createDocumentSigner, createProof, createSigner, prepareDataForSigning, } from './cryptography.js';
2
+ export * from './interfaces.js';
3
+ export { createDID, deactivateDID, resolveDID, resolveDIDFromLog, updateDID } from './method.js';
4
+ export { MultibaseEncoding, multibaseDecode, multibaseEncode } from './utils/multiformats.js';
5
+ export { generateParallelDidWeb, parseDidKeyDid, parseDidKeyVerificationMethod } from './utils.js';
6
+ export { createWitnessProof, signWitnessProofEntries, signWitnessProofEntry, } from './witness.js';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { AbstractCrypto, createDocumentSigner, createProof, createSigner, prepareDataForSigning, } from './cryptography.js';
2
+ export * from './interfaces.js';
3
+ export { createDID, deactivateDID, resolveDID, resolveDIDFromLog, updateDID } from './method.js';
4
+ export { MultibaseEncoding, multibaseDecode, multibaseEncode } from './utils/multiformats.js';
5
+ export { generateParallelDidWeb, parseDidKeyDid, parseDidKeyVerificationMethod } from './utils.js';
6
+ export { createWitnessProof, signWitnessProofEntries, signWitnessProofEntry, } from './witness.js';
@@ -0,0 +1,231 @@
1
+ export type DataIntegrityProofPurpose = 'authentication' | 'assertionMethod' | 'keyAgreement' | 'capabilityInvocation' | 'capabilityDelegation';
2
+ export type JsonPrimitive = string | number | boolean | null;
3
+ export type JsonValue = JsonPrimitive | JsonObject | JsonValue[];
4
+ export type JsonObject = {
5
+ [key: string]: JsonValue;
6
+ };
7
+ export type DataIntegrityProofType = 'DataIntegrityProof';
8
+ export type DataIntegrityCryptosuite = 'eddsa-jcs-2022';
9
+ export interface DataIntegrityProofTemplate {
10
+ id?: string;
11
+ type: DataIntegrityProofType;
12
+ cryptosuite: DataIntegrityCryptosuite;
13
+ verificationMethod: string;
14
+ created: string;
15
+ proofPurpose: DataIntegrityProofPurpose;
16
+ }
17
+ export interface SigningInput {
18
+ document: unknown;
19
+ proof: DataIntegrityProofTemplate;
20
+ }
21
+ export interface SigningOutput {
22
+ proofValue: string;
23
+ }
24
+ export interface Signer {
25
+ sign(input: SigningInput): Promise<SigningOutput>;
26
+ getVerificationMethodId(): string;
27
+ }
28
+ export interface Verifier {
29
+ verify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
30
+ }
31
+ export interface SignerOptions {
32
+ verificationMethod?: VerificationMethod | null;
33
+ useStaticId?: boolean;
34
+ }
35
+ export interface ProblemDetails {
36
+ type: string;
37
+ title: string;
38
+ detail: string;
39
+ }
40
+ export declare enum DidResolutionError {
41
+ NotFound = "notFound",
42
+ InvalidDid = "invalidDid",
43
+ LegacyNotFound = "NOT_FOUND",
44
+ LegacyInvalidDid = "INVALID_DID",
45
+ InvalidDidUrl = "INVALID_DID_URL",
46
+ InvalidOptions = "INVALID_OPTIONS",
47
+ RepresentationNotSupported = "REPRESENTATION_NOT_SUPPORTED",
48
+ MethodNotSupported = "METHOD_NOT_SUPPORTED",
49
+ UnsupportedPublicKeyType = "UNSUPPORTED_PUBLIC_KEY_TYPE",
50
+ LegacyInvalidDidDocument = "INVALID_DID_DOCUMENT",
51
+ InvalidPublicKey = "INVALID_PUBLIC_KEY",
52
+ InvalidPublicKeyLength = "INVALID_PUBLIC_KEY_LENGTH",
53
+ InvalidPublicKeyType = "INVALID_PUBLIC_KEY_TYPE",
54
+ InternalError = "INTERNAL_ERROR"
55
+ }
56
+ export interface DIDResolutionMeta {
57
+ versionId: string;
58
+ created: string;
59
+ updated: string;
60
+ previousLogEntryHash?: string;
61
+ updateKeys: string[];
62
+ scid: string;
63
+ prerotation: boolean;
64
+ portable: boolean;
65
+ nextKeyHashes: string[];
66
+ deactivated: boolean;
67
+ witness?: WitnessParameterResolution;
68
+ watchers?: string[] | null;
69
+ error?: DidResolutionError;
70
+ problemDetails?: ProblemDetails;
71
+ latestVersionId?: string;
72
+ }
73
+ export interface DIDDoc {
74
+ '@context'?: string | string[] | object | object[];
75
+ id?: string;
76
+ controller?: string | string[];
77
+ alsoKnownAs?: string[];
78
+ authentication?: string[];
79
+ assertionMethod?: string[];
80
+ keyAgreement?: string[];
81
+ capabilityInvocation?: string[];
82
+ capabilityDelegation?: string[];
83
+ verificationMethod?: VerificationMethod[];
84
+ service?: ServiceEndpoint[];
85
+ }
86
+ export interface VerificationMethod {
87
+ id?: string;
88
+ type: string;
89
+ controller?: string;
90
+ publicKeyMultibase?: string;
91
+ secretKeyMultibase?: string;
92
+ purpose?: DataIntegrityProofPurpose;
93
+ publicKeyJwk?: any;
94
+ use?: string;
95
+ }
96
+ export interface WitnessEntry {
97
+ id: string;
98
+ }
99
+ export interface ParsedDidKeyVerificationMethod {
100
+ did: string;
101
+ fragment?: string;
102
+ keyMultibase: string;
103
+ }
104
+ export interface WitnessSigningOptions {
105
+ versionId: string;
106
+ witnesses: WitnessEntry[];
107
+ witnessSignersByDid: Record<string, Signer>;
108
+ created?: string;
109
+ }
110
+ export interface WitnessSigningResult {
111
+ versionId: string;
112
+ proof: DataIntegrityProof[];
113
+ }
114
+ export interface WitnessParameter {
115
+ threshold?: number;
116
+ witnesses?: WitnessEntry[];
117
+ }
118
+ export interface WitnessParameterResolution {
119
+ threshold?: string | number;
120
+ witnesses?: WitnessEntry[];
121
+ }
122
+ export interface DataIntegrityProof {
123
+ id?: string;
124
+ type: DataIntegrityProofType;
125
+ cryptosuite: DataIntegrityCryptosuite;
126
+ verificationMethod: string;
127
+ created: string;
128
+ proofValue: string;
129
+ proofPurpose: DataIntegrityProofPurpose;
130
+ }
131
+ export interface DIDLogEntry {
132
+ versionId: string;
133
+ versionTime: string;
134
+ parameters: {
135
+ method?: string;
136
+ scid?: string;
137
+ updateKeys?: string[];
138
+ nextKeyHashes?: string[];
139
+ portable?: boolean;
140
+ witness?: WitnessParameter;
141
+ watchers?: string[] | null;
142
+ deactivated?: boolean;
143
+ };
144
+ state: DIDDoc;
145
+ proof?: DataIntegrityProof[];
146
+ }
147
+ export type DIDLog = DIDLogEntry[];
148
+ export interface ServiceEndpoint {
149
+ id?: string;
150
+ type: string | string[];
151
+ serviceEndpoint?: string | string[] | any;
152
+ [key: string]: unknown;
153
+ }
154
+ export interface CreateDIDResult {
155
+ did: string;
156
+ doc: DIDDoc;
157
+ meta: DIDResolutionMeta;
158
+ log: DIDLog;
159
+ webDoc?: DIDDoc;
160
+ }
161
+ export interface UpdateDIDResult {
162
+ did: string;
163
+ doc: DIDDoc;
164
+ meta: DIDResolutionMeta;
165
+ log: DIDLog;
166
+ webDoc?: DIDDoc;
167
+ }
168
+ export interface CreateDIDInterface {
169
+ domain?: string;
170
+ address?: string;
171
+ signer: Signer;
172
+ updateKeys: string[];
173
+ verificationMethods?: VerificationMethod[];
174
+ didDocument?: DIDDoc;
175
+ services?: ServiceEndpoint[];
176
+ paths?: string[];
177
+ controller?: string;
178
+ context?: string | string[] | object | object[];
179
+ alsoKnownAs?: string[];
180
+ alsoKnownAsWeb?: boolean;
181
+ portable?: boolean;
182
+ nextKeyHashes?: string[];
183
+ witness?: WitnessParameter | null;
184
+ watchers?: string[] | null;
185
+ created?: string;
186
+ verifier?: Verifier;
187
+ authentication?: string[];
188
+ assertionMethod?: string[];
189
+ keyAgreement?: string[];
190
+ }
191
+ export interface SignDIDDocInterface {
192
+ document: unknown;
193
+ proof: DataIntegrityProofTemplate;
194
+ verificationMethod: VerificationMethod;
195
+ }
196
+ export interface UpdateDIDInterface {
197
+ log: DIDLog;
198
+ signer: Signer;
199
+ updateKeys?: string[];
200
+ verificationMethods?: VerificationMethod[];
201
+ controller?: string;
202
+ context?: string | string[] | object | object[];
203
+ alsoKnownAs?: string[];
204
+ portable?: boolean;
205
+ nextKeyHashes?: string[];
206
+ witness?: WitnessParameter | null;
207
+ watchers?: string[] | null;
208
+ verifier?: Verifier;
209
+ authentication?: string[];
210
+ assertionMethod?: string[];
211
+ keyAgreement?: string[];
212
+ witnessProofs?: WitnessProofFileEntry[];
213
+ }
214
+ export interface DeactivateDIDInterface {
215
+ log: DIDLog;
216
+ signer: Signer;
217
+ verifier?: Verifier;
218
+ }
219
+ export interface ResolutionOptions {
220
+ versionNumber?: number;
221
+ versionId?: string;
222
+ versionTime?: Date;
223
+ verificationMethod?: string;
224
+ verifier?: Verifier;
225
+ scid?: string;
226
+ fastResolve?: boolean;
227
+ }
228
+ export interface WitnessProofFileEntry {
229
+ versionId: string;
230
+ proof: DataIntegrityProof[];
231
+ }
@@ -0,0 +1,17 @@
1
+ export var DidResolutionError;
2
+ (function (DidResolutionError) {
3
+ DidResolutionError["NotFound"] = "notFound";
4
+ DidResolutionError["InvalidDid"] = "invalidDid";
5
+ DidResolutionError["LegacyNotFound"] = "NOT_FOUND";
6
+ DidResolutionError["LegacyInvalidDid"] = "INVALID_DID";
7
+ DidResolutionError["InvalidDidUrl"] = "INVALID_DID_URL";
8
+ DidResolutionError["InvalidOptions"] = "INVALID_OPTIONS";
9
+ DidResolutionError["RepresentationNotSupported"] = "REPRESENTATION_NOT_SUPPORTED";
10
+ DidResolutionError["MethodNotSupported"] = "METHOD_NOT_SUPPORTED";
11
+ DidResolutionError["UnsupportedPublicKeyType"] = "UNSUPPORTED_PUBLIC_KEY_TYPE";
12
+ DidResolutionError["LegacyInvalidDidDocument"] = "INVALID_DID_DOCUMENT";
13
+ DidResolutionError["InvalidPublicKey"] = "INVALID_PUBLIC_KEY";
14
+ DidResolutionError["InvalidPublicKeyLength"] = "INVALID_PUBLIC_KEY_LENGTH";
15
+ DidResolutionError["InvalidPublicKeyType"] = "INVALID_PUBLIC_KEY_TYPE";
16
+ DidResolutionError["InternalError"] = "INTERNAL_ERROR";
17
+ })(DidResolutionError || (DidResolutionError = {}));
@@ -0,0 +1,73 @@
1
+ import type { CreateDIDInterface, CreateDIDResult, DeactivateDIDInterface, DIDLog, ResolutionOptions, UpdateDIDInterface, UpdateDIDResult, WitnessProofFileEntry } from './interfaces.js';
2
+ import { DidResolutionError } from './interfaces.js';
3
+ /**
4
+ * Creates a new did:webvh DID and initial DID log.
5
+ *
6
+ * @param options DID creation options.
7
+ * @returns The created DID, resolved document, and DID log.
8
+ */
9
+ export declare const createDID: (options: CreateDIDInterface) => Promise<CreateDIDResult>;
10
+ /**
11
+ * Resolves a DID by fetching and validating its DID log.
12
+ *
13
+ * @param did The DID to resolve.
14
+ * @param options Optional resolver settings.
15
+ * @returns The resolved DID result with resolution metadata.
16
+ */
17
+ export declare const resolveDID: (did: string, options?: ResolutionOptions & {
18
+ witnessProofs?: WitnessProofFileEntry[];
19
+ }) => Promise<{
20
+ did: string;
21
+ doc: any;
22
+ meta: import("./interfaces.js").DIDResolutionMeta;
23
+ } | {
24
+ did: string;
25
+ doc: null;
26
+ meta: {
27
+ error: DidResolutionError.NotFound | DidResolutionError.InvalidDid;
28
+ problemDetails: {
29
+ type: string;
30
+ title: string;
31
+ detail: string;
32
+ };
33
+ };
34
+ }>;
35
+ /**
36
+ * Resolves a DID from an in-memory DID log.
37
+ *
38
+ * @param log In-memory DID log entries.
39
+ * @param options Optional resolver settings.
40
+ * @returns The resolved DID result with resolution metadata.
41
+ */
42
+ export declare const resolveDIDFromLog: (log: DIDLog, options?: ResolutionOptions & {
43
+ witnessProofs?: WitnessProofFileEntry[];
44
+ }) => Promise<{
45
+ did: string;
46
+ doc: any;
47
+ meta: import("./interfaces.js").DIDResolutionMeta;
48
+ }>;
49
+ /**
50
+ * Updates an existing DID log with a new entry.
51
+ *
52
+ * @param options DID update options.
53
+ * @returns The updated DID, resolved document, and DID log.
54
+ */
55
+ export declare const updateDID: (options: UpdateDIDInterface & {
56
+ services?: any[];
57
+ domain?: string;
58
+ updated?: string;
59
+ }) => Promise<UpdateDIDResult>;
60
+ /**
61
+ * Deactivates an existing DID by appending a deactivation entry.
62
+ *
63
+ * @param options DID deactivation options.
64
+ * @returns The deactivated DID result and updated DID log.
65
+ */
66
+ export declare const deactivateDID: (options: DeactivateDIDInterface & {
67
+ updateKeys?: string[];
68
+ }) => Promise<{
69
+ did: string;
70
+ doc: any;
71
+ meta: import("./interfaces.js").DIDResolutionMeta;
72
+ log: DIDLog;
73
+ }>;
package/dist/method.js ADDED
@@ -0,0 +1,95 @@
1
+ import { DidResolutionError } from './interfaces.js';
2
+ import * as v1 from './method_versions/method.v1.0.js';
3
+ import { fetchLogFromIdentifier, maybeWriteTestLog } from './utils.js';
4
+ const SUPPORTED_METHOD = 'did:webvh:1.0';
5
+ /**
6
+ * Creates a new did:webvh DID and initial DID log.
7
+ *
8
+ * @param options DID creation options.
9
+ * @returns The created DID, resolved document, and DID log.
10
+ */
11
+ export const createDID = async (options) => {
12
+ const method = options.method;
13
+ if (method && method !== SUPPORTED_METHOD) {
14
+ throw new Error(`'${method}' is not a supported method version.`);
15
+ }
16
+ const result = await v1.createDID(options);
17
+ maybeWriteTestLog(result.did, result.log);
18
+ return result;
19
+ };
20
+ /**
21
+ * Resolves a DID by fetching and validating its DID log.
22
+ *
23
+ * @param did The DID to resolve.
24
+ * @param options Optional resolver settings.
25
+ * @returns The resolved DID result with resolution metadata.
26
+ */
27
+ export const resolveDID = async (did, options = {}) => {
28
+ // Extract the expected SCID from the DID string so the resolver can
29
+ // verify the log's SCID matches what the DID claims.
30
+ const didParts = did.split(':');
31
+ const scid = didParts.length > 2 && didParts[0] === 'did' && didParts[1] === 'webvh' ? didParts[2] : undefined;
32
+ try {
33
+ const log = await fetchLogFromIdentifier(did);
34
+ const result = await v1.resolveDIDFromLog(log, { ...options, scid });
35
+ maybeWriteTestLog(result.did, log);
36
+ return result;
37
+ }
38
+ catch (e) {
39
+ let errorType = DidResolutionError.InvalidDid;
40
+ const message = e instanceof Error ? e.message : String(e);
41
+ if (/not found/i.test(message) || /404/.test(message)) {
42
+ errorType = DidResolutionError.NotFound;
43
+ }
44
+ return {
45
+ did,
46
+ doc: null,
47
+ meta: {
48
+ error: errorType,
49
+ problemDetails: {
50
+ type: errorType === DidResolutionError.NotFound
51
+ ? 'https://w3id.org/security#NOT_FOUND'
52
+ : 'https://w3id.org/security#INVALID_CONTROLLED_IDENTIFIER_DOCUMENT_ID',
53
+ title: errorType === DidResolutionError.NotFound
54
+ ? 'The DID Log or resource was not found.'
55
+ : 'The resolved DID is invalid.',
56
+ detail: message,
57
+ },
58
+ },
59
+ };
60
+ }
61
+ };
62
+ /**
63
+ * Resolves a DID from an in-memory DID log.
64
+ *
65
+ * @param log In-memory DID log entries.
66
+ * @param options Optional resolver settings.
67
+ * @returns The resolved DID result with resolution metadata.
68
+ */
69
+ export const resolveDIDFromLog = async (log, options = {}) => {
70
+ const result = await v1.resolveDIDFromLog(log, options);
71
+ maybeWriteTestLog(result.did, log);
72
+ return result;
73
+ };
74
+ /**
75
+ * Updates an existing DID log with a new entry.
76
+ *
77
+ * @param options DID update options.
78
+ * @returns The updated DID, resolved document, and DID log.
79
+ */
80
+ export const updateDID = async (options) => {
81
+ const result = await v1.updateDID(options);
82
+ maybeWriteTestLog(result.did, result.log);
83
+ return result;
84
+ };
85
+ /**
86
+ * Deactivates an existing DID by appending a deactivation entry.
87
+ *
88
+ * @param options DID deactivation options.
89
+ * @returns The deactivated DID result and updated DID log.
90
+ */
91
+ export const deactivateDID = async (options) => {
92
+ const result = await v1.deactivateDID(options);
93
+ maybeWriteTestLog(result.did, result.log);
94
+ return result;
95
+ };
@@ -0,0 +1,23 @@
1
+ import type { CreateDIDInterface, CreateDIDResult, DeactivateDIDInterface, DIDLog, DIDResolutionMeta, ResolutionOptions, UpdateDIDInterface, UpdateDIDResult, WitnessProofFileEntry } from '../interfaces.js';
2
+ export declare const createDID: (options: CreateDIDInterface) => Promise<CreateDIDResult>;
3
+ export declare const resolveDIDFromLog: (log: DIDLog, options?: ResolutionOptions & {
4
+ witnessProofs?: WitnessProofFileEntry[];
5
+ fastResolve?: boolean;
6
+ }) => Promise<{
7
+ did: string;
8
+ doc: any;
9
+ meta: DIDResolutionMeta;
10
+ }>;
11
+ export declare const updateDID: (options: UpdateDIDInterface & {
12
+ services?: any[];
13
+ domain?: string;
14
+ updated?: string;
15
+ }) => Promise<UpdateDIDResult>;
16
+ export declare const deactivateDID: (options: DeactivateDIDInterface & {
17
+ updateKeys?: string[];
18
+ }) => Promise<{
19
+ did: string;
20
+ doc: any;
21
+ meta: DIDResolutionMeta;
22
+ log: DIDLog;
23
+ }>;