@interop/did-method-peer 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,8 @@
1
+ /**
2
+ * The `@interop/did-method-peer/codec` subpath: the pure numalgo 4 codec and
3
+ * nothing from the driver, so a DIDComm library that only needs encode/decode
4
+ * never loads `DidPeerDriver`, `@interop/bnid` or a key suite.
5
+ */
6
+ export { decode, encodeLongForm, encodeShortForm, isDidPeer4, isLongForm, isShortForm, longToShort, resolveLongForm, resolveShortForm, LONG_FORM_PATTERN, MAX_LONG_FORM_LENGTH, SHORT_FORM_PATTERN } from './numalgo4.js';
7
+ export type { InputDocument } from './types.js';
8
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,MAAM,EACN,cAAc,EACd,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,eAAe,CAAA;AACtB,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA"}
package/dist/codec.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The `@interop/did-method-peer/codec` subpath: the pure numalgo 4 codec and
3
+ * nothing from the driver, so a DIDComm library that only needs encode/decode
4
+ * never loads `DidPeerDriver`, `@interop/bnid` or a key suite.
5
+ */
6
+ export { decode, encodeLongForm, encodeShortForm, isDidPeer4, isLongForm, isShortForm, longToShort, resolveLongForm, resolveShortForm, LONG_FORM_PATTERN, MAX_LONG_FORM_LENGTH, SHORT_FORM_PATTERN } from './numalgo4.js';
7
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,MAAM,EACN,cAAc,EACd,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,eAAe,CAAA"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Shared constants for the did:peer numalgo 4 codec and driver: the DID Core
3
+ * context URL, the did:peer prefixes, the two multicodec/multihash headers the
4
+ * codec needs (inlined as byte constants so no varint library is required),
5
+ * the long form length limit, and the default verification relationships for
6
+ * a generated verification key.
7
+ */
8
+ export declare const DID_CONTEXT_URL = "https://www.w3.org/ns/did/v1";
9
+ export declare const DID_PEER_PREFIX = "did:peer:";
10
+ export declare const NUMALGO_4_PREFIX = "did:peer:4";
11
+ /**
12
+ * Multicodec `json` code (0x0200) as its two-byte unsigned varint.
13
+ */
14
+ export declare const MULTICODEC_JSON_HEADER: Uint8Array<ArrayBuffer>;
15
+ /**
16
+ * Multihash header for sha2-256: code 0x12, digest length 0x20 (32 bytes).
17
+ */
18
+ export declare const MULTIHASH_SHA2_256_HEADER: Uint8Array<ArrayBuffer>;
19
+ export declare const MULTIBASE_BASE58BTC_PREFIX = "z";
20
+ /**
21
+ * Longest long form `decode` accepts, in characters. A long form carries an
22
+ * attacker-chosen document, and base58-decoding plus parsing a multi-megabyte
23
+ * string would block the event loop, so anything longer is rejected before
24
+ * hashing. 4096 characters is roughly four times the largest published vector
25
+ * and matches the longest base58 string `@scure/base` will decode, so the
26
+ * length check, not the base58 decoder, is what rejects an oversized long form.
27
+ */
28
+ export declare const MAX_LONG_FORM_LENGTH = 4096;
29
+ /**
30
+ * The verification relationships a generated verification key is wired into
31
+ * when no explicit `purposes` are given for it (the same four as
32
+ * `@interop/did-method-key` and `@interop/did-web-resolver`).
33
+ */
34
+ export declare const DEFAULT_PURPOSES: string[];
35
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,eAAO,MAAM,eAAe,iCAAiC,CAAA;AAE7D,eAAO,MAAM,eAAe,cAAc,CAAA;AAC1C,eAAO,MAAM,gBAAgB,eAAe,CAAA;AAE5C;;GAEG;AACH,eAAO,MAAM,sBAAsB,yBAA+B,CAAA;AAElE;;GAEG;AACH,eAAO,MAAM,yBAAyB,yBAA+B,CAAA;AAErE,eAAO,MAAM,0BAA0B,MAAM,CAAA;AAE7C;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,OAAO,CAAA;AAExC;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,UAK5B,CAAA"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Shared constants for the did:peer numalgo 4 codec and driver: the DID Core
3
+ * context URL, the did:peer prefixes, the two multicodec/multihash headers the
4
+ * codec needs (inlined as byte constants so no varint library is required),
5
+ * the long form length limit, and the default verification relationships for
6
+ * a generated verification key.
7
+ */
8
+ export const DID_CONTEXT_URL = 'https://www.w3.org/ns/did/v1';
9
+ export const DID_PEER_PREFIX = 'did:peer:';
10
+ export const NUMALGO_4_PREFIX = 'did:peer:4';
11
+ /**
12
+ * Multicodec `json` code (0x0200) as its two-byte unsigned varint.
13
+ */
14
+ export const MULTICODEC_JSON_HEADER = new Uint8Array([0x80, 0x04]);
15
+ /**
16
+ * Multihash header for sha2-256: code 0x12, digest length 0x20 (32 bytes).
17
+ */
18
+ export const MULTIHASH_SHA2_256_HEADER = new Uint8Array([0x12, 0x20]);
19
+ export const MULTIBASE_BASE58BTC_PREFIX = 'z';
20
+ /**
21
+ * Longest long form `decode` accepts, in characters. A long form carries an
22
+ * attacker-chosen document, and base58-decoding plus parsing a multi-megabyte
23
+ * string would block the event loop, so anything longer is rejected before
24
+ * hashing. 4096 characters is roughly four times the largest published vector
25
+ * and matches the longest base58 string `@scure/base` will decode, so the
26
+ * length check, not the base58 decoder, is what rejects an oversized long form.
27
+ */
28
+ export const MAX_LONG_FORM_LENGTH = 4096;
29
+ /**
30
+ * The verification relationships a generated verification key is wired into
31
+ * when no explicit `purposes` are given for it (the same four as
32
+ * `@interop/did-method-key` and `@interop/did-web-resolver`).
33
+ */
34
+ export const DEFAULT_PURPOSES = [
35
+ 'authentication',
36
+ 'assertionMethod',
37
+ 'capabilityDelegation',
38
+ 'capabilityInvocation'
39
+ ];
40
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,CAAC,MAAM,eAAe,GAAG,8BAA8B,CAAA;AAE7D,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAA;AAC1C,MAAM,CAAC,MAAM,gBAAgB,GAAG,YAAY,CAAA;AAE5C;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AAElE;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AAErE,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,CAAA;AAE7C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAA;AAExC;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,gBAAgB;IAChB,iBAAiB;IACjB,sBAAsB;IACtB,sBAAsB;CACvB,CAAA"}
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * Copyright (c) 2026 Interop Alliance. All rights reserved.
3
+ */
4
+ export { driver, DidPeerDriver } from './DidPeerDriver.js';
5
+ export { decode, encodeLongForm, encodeShortForm, isDidPeer4, isLongForm, isShortForm, longToShort, resolveLongForm, resolveShortForm, LONG_FORM_PATTERN, MAX_LONG_FORM_LENGTH, SHORT_FORM_PATTERN } from './numalgo4.js';
6
+ export { validateInputDocument } from './inputDocument.js';
7
+ export type { DidPeerDriverOptions, FromMultibase, InputDocument, KeyPairClass, LongFormStore } from './types.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EACL,MAAM,EACN,cAAc,EACd,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,YAAY,EACV,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,YAAY,EACZ,aAAa,EACd,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * Copyright (c) 2026 Interop Alliance. All rights reserved.
3
+ */
4
+ export { driver, DidPeerDriver } from './DidPeerDriver.js';
5
+ export { decode, encodeLongForm, encodeShortForm, isDidPeer4, isLongForm, isShortForm, longToShort, resolveLongForm, resolveShortForm, LONG_FORM_PATTERN, MAX_LONG_FORM_LENGTH, SHORT_FORM_PATTERN } from './numalgo4.js';
6
+ export { validateInputDocument } from './inputDocument.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EACL,MAAM,EACN,cAAc,EACd,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Input document helpers: the superficial validation applied on every
3
+ * `generate()` path (never on resolution), and the builder that exports a live
4
+ * key pair into an owner-controlled, relative-id verification method and wires
5
+ * it into the requested relationships.
6
+ */
7
+ import type { AbstractKeyPair, IVerificationMethodEntry } from '@interop/data-integrity-core';
8
+ import type { InputDocument } from './types.js';
9
+ /**
10
+ * Validates an input document before encoding. Deliberately superficial, as
11
+ * the Python reference's `validate_input_document`: the document must be a
12
+ * non-empty plain object without a root `id`; `alsoKnownAs`, if present, and
13
+ * each of `verificationMethod`, the five relationships and `service`, if
14
+ * present, must be arrays; every object entry in those arrays must have a
15
+ * relative string `id` (`#fragment`) and a `type`; and resource ids must be
16
+ * unique across all of them. String entries are references and are not
17
+ * checked. Key material is not validated.
18
+ *
19
+ * @param options {object}
20
+ * @param options.inputDocument {InputDocument}
21
+ *
22
+ * @throws {TypeError} Naming the failing path, e.g.
23
+ * `authentication[1]: resource id must be relative`.
24
+ */
25
+ export declare function validateInputDocument({ inputDocument }: {
26
+ inputDocument: InputDocument;
27
+ }): void;
28
+ /**
29
+ * Exports a live key pair's public node and adds it to an input document as
30
+ * an owner-controlled verification method. The node's `@context` moves into
31
+ * the document's `@context` (de-duplicated); its `id` becomes the relative
32
+ * `#${fragment}`; `controller` and the absolute id a live pair carries are
33
+ * stripped, since the owner DID does not exist until the hash is computed. The
34
+ * node is listed under `verificationMethod` and referenced by id from each
35
+ * purpose.
36
+ *
37
+ * @param options {object}
38
+ * @param options.inputDocument {InputDocument} - Document to mutate.
39
+ * @param options.keyPair {AbstractKeyPair} - A live key pair (with `export()`).
40
+ * @param [options.fragment] {string} - Defaults to the pair's `fingerprint()`.
41
+ * @param [options.purposes] {string[]} - Verification relationships to wire.
42
+ *
43
+ * @returns {Promise<{inputDocument: InputDocument, fragment: string}>}
44
+ */
45
+ export declare function addVerificationMethod({ inputDocument, keyPair, fragment, purposes }: {
46
+ inputDocument: InputDocument;
47
+ keyPair: AbstractKeyPair;
48
+ fragment?: string;
49
+ purposes?: string[];
50
+ }): Promise<{
51
+ inputDocument: InputDocument;
52
+ fragment: string;
53
+ }>;
54
+ /**
55
+ * Adds a third-party-controlled method to an input document, placed verbatim:
56
+ * a string is a reference appended to each purpose; an object (carrying its
57
+ * own `id`, `type`, `controller` and key material) is listed under
58
+ * `verificationMethod` and referenced from each purpose. No live pair is bound
59
+ * for it.
60
+ *
61
+ * @param options {object}
62
+ * @param options.inputDocument {InputDocument} - Document to mutate.
63
+ * @param options.entry {IVerificationMethodEntry}
64
+ * @param options.purposes {string[]}
65
+ */
66
+ export declare function addThirdPartyMethod({ inputDocument, entry, purposes }: {
67
+ inputDocument: InputDocument;
68
+ entry: IVerificationMethodEntry;
69
+ purposes: string[];
70
+ }): void;
71
+ //# sourceMappingURL=inputDocument.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inputDocument.d.ts","sourceRoot":"","sources":["../src/inputDocument.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EACV,eAAe,EACf,wBAAwB,EACzB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAQ/C;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,aAAa,EACd,EAAE;IACD,aAAa,EAAE,aAAa,CAAA;CAC7B,GAAG,IAAI,CAmDP;AA2ED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,qBAAqB,CAAC,EAC1C,aAAa,EACb,OAAO,EACP,QAAQ,EACR,QAAa,EACd,EAAE;IACD,aAAa,EAAE,aAAa,CAAA;IAC5B,OAAO,EAAE,eAAe,CAAA;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CACpB,GAAG,OAAO,CAAC;IAAE,aAAa,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC,CA0B9D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,aAAa,EACb,KAAK,EACL,QAAQ,EACT,EAAE;IACD,aAAa,EAAE,aAAa,CAAA;IAC5B,KAAK,EAAE,wBAAwB,CAAA;IAC/B,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB,GAAG,IAAI,CAkBP"}
@@ -0,0 +1,192 @@
1
+ import { VERIFICATION_RELATIONSHIPS, deepClone } from '@interop/did-io';
2
+ import { RESOURCE_PROPERTIES, isPlainObject } from './resources.js';
3
+ /**
4
+ * A relative fragment reference: `#` followed by a non-empty fragment that
5
+ * contains no further `#`.
6
+ */
7
+ const RELATIVE_ID_PATTERN = /^#[^#]+$/;
8
+ /**
9
+ * Validates an input document before encoding. Deliberately superficial, as
10
+ * the Python reference's `validate_input_document`: the document must be a
11
+ * non-empty plain object without a root `id`; `alsoKnownAs`, if present, and
12
+ * each of `verificationMethod`, the five relationships and `service`, if
13
+ * present, must be arrays; every object entry in those arrays must have a
14
+ * relative string `id` (`#fragment`) and a `type`; and resource ids must be
15
+ * unique across all of them. String entries are references and are not
16
+ * checked. Key material is not validated.
17
+ *
18
+ * @param options {object}
19
+ * @param options.inputDocument {InputDocument}
20
+ *
21
+ * @throws {TypeError} Naming the failing path, e.g.
22
+ * `authentication[1]: resource id must be relative`.
23
+ */
24
+ export function validateInputDocument({ inputDocument }) {
25
+ if (!isPlainObject(inputDocument) ||
26
+ Object.keys(inputDocument).length === 0) {
27
+ throw new TypeError('inputDocument: must be a non-empty object.');
28
+ }
29
+ if (Object.hasOwn(inputDocument, 'id')) {
30
+ throw new TypeError('inputDocument: must not have a root "id".');
31
+ }
32
+ if (Object.hasOwn(inputDocument, 'alsoKnownAs') &&
33
+ !Array.isArray(inputDocument.alsoKnownAs)) {
34
+ throw new TypeError('alsoKnownAs: must be an array.');
35
+ }
36
+ const seen = new Map();
37
+ for (const property of RESOURCE_PROPERTIES) {
38
+ if (!Object.hasOwn(inputDocument, property)) {
39
+ continue;
40
+ }
41
+ const entries = inputDocument[property];
42
+ if (!Array.isArray(entries)) {
43
+ throw new TypeError(`${property}: must be an array.`);
44
+ }
45
+ entries.forEach((entry, index) => {
46
+ if (typeof entry === 'string') {
47
+ return;
48
+ }
49
+ const path = `${property}[${index}]`;
50
+ if (!isPlainObject(entry)) {
51
+ throw new TypeError(`${path}: resource must be an object.`);
52
+ }
53
+ if (typeof entry.id !== 'string') {
54
+ throw new TypeError(`${path}: resource id must be a string.`);
55
+ }
56
+ if (!RELATIVE_ID_PATTERN.test(entry.id)) {
57
+ throw new TypeError(`${path}: resource id must be relative.`);
58
+ }
59
+ if (typeof entry.type !== 'string' || entry.type.length === 0) {
60
+ throw new TypeError(`${path}: resource must have a type.`);
61
+ }
62
+ const previous = seen.get(entry.id);
63
+ if (previous !== undefined) {
64
+ throw new TypeError(`${path}: duplicate resource id "${entry.id}" (also at ${previous}).`);
65
+ }
66
+ seen.set(entry.id, path);
67
+ });
68
+ }
69
+ }
70
+ /**
71
+ * Adds one or more contexts to an input document's `@context`, de-duplicating.
72
+ */
73
+ function addContext({ inputDocument, context }) {
74
+ const existing = inputDocument['@context'];
75
+ let contexts;
76
+ if (Array.isArray(existing)) {
77
+ contexts = existing;
78
+ }
79
+ else if (existing === undefined) {
80
+ contexts = [];
81
+ }
82
+ else {
83
+ contexts = [existing];
84
+ }
85
+ for (const ctx of Array.isArray(context) ? context : [context]) {
86
+ if (!contexts.includes(ctx)) {
87
+ contexts.push(ctx);
88
+ }
89
+ }
90
+ inputDocument['@context'] = contexts;
91
+ }
92
+ function assertPurposes(purposes) {
93
+ for (const purpose of purposes) {
94
+ if (!VERIFICATION_RELATIONSHIPS.has(purpose)) {
95
+ throw new Error(`Unsupported key purpose: "${purpose}".`);
96
+ }
97
+ }
98
+ }
99
+ function pushTo({ inputDocument, property, value }) {
100
+ const existing = inputDocument[property];
101
+ if (existing === undefined) {
102
+ inputDocument[property] = [value];
103
+ }
104
+ else if (Array.isArray(existing)) {
105
+ existing.push(value);
106
+ }
107
+ else {
108
+ throw new TypeError(`${property}: must be an array.`);
109
+ }
110
+ }
111
+ /**
112
+ * Lists a method node under `verificationMethod` and references its id from
113
+ * each purpose.
114
+ */
115
+ function placeMethod({ inputDocument, node, purposes }) {
116
+ pushTo({ inputDocument, property: 'verificationMethod', value: node });
117
+ for (const purpose of purposes) {
118
+ pushTo({ inputDocument, property: purpose, value: node.id });
119
+ }
120
+ }
121
+ /**
122
+ * Exports a live key pair's public node and adds it to an input document as
123
+ * an owner-controlled verification method. The node's `@context` moves into
124
+ * the document's `@context` (de-duplicated); its `id` becomes the relative
125
+ * `#${fragment}`; `controller` and the absolute id a live pair carries are
126
+ * stripped, since the owner DID does not exist until the hash is computed. The
127
+ * node is listed under `verificationMethod` and referenced by id from each
128
+ * purpose.
129
+ *
130
+ * @param options {object}
131
+ * @param options.inputDocument {InputDocument} - Document to mutate.
132
+ * @param options.keyPair {AbstractKeyPair} - A live key pair (with `export()`).
133
+ * @param [options.fragment] {string} - Defaults to the pair's `fingerprint()`.
134
+ * @param [options.purposes] {string[]} - Verification relationships to wire.
135
+ *
136
+ * @returns {Promise<{inputDocument: InputDocument, fragment: string}>}
137
+ */
138
+ export async function addVerificationMethod({ inputDocument, keyPair, fragment, purposes = [] }) {
139
+ if (typeof keyPair?.export !== 'function') {
140
+ throw new TypeError('"keyPair" must be a live key pair with "export()".');
141
+ }
142
+ assertPurposes(purposes);
143
+ if (fragment === undefined) {
144
+ if (typeof keyPair.fingerprint !== 'function') {
145
+ throw new TypeError('Cannot determine a default fragment; provide a "fragment".');
146
+ }
147
+ fragment = keyPair.fingerprint();
148
+ }
149
+ const node = (await keyPair.export({
150
+ publicKey: true,
151
+ includeContext: true
152
+ }));
153
+ const context = node['@context'];
154
+ delete node['@context'];
155
+ delete node.controller;
156
+ node.id = `#${fragment}`;
157
+ if (context !== undefined) {
158
+ addContext({ inputDocument, context });
159
+ }
160
+ placeMethod({ inputDocument, node, purposes });
161
+ return { inputDocument, fragment };
162
+ }
163
+ /**
164
+ * Adds a third-party-controlled method to an input document, placed verbatim:
165
+ * a string is a reference appended to each purpose; an object (carrying its
166
+ * own `id`, `type`, `controller` and key material) is listed under
167
+ * `verificationMethod` and referenced from each purpose. No live pair is bound
168
+ * for it.
169
+ *
170
+ * @param options {object}
171
+ * @param options.inputDocument {InputDocument} - Document to mutate.
172
+ * @param options.entry {IVerificationMethodEntry}
173
+ * @param options.purposes {string[]}
174
+ */
175
+ export function addThirdPartyMethod({ inputDocument, entry, purposes }) {
176
+ assertPurposes(purposes);
177
+ if (typeof entry === 'string') {
178
+ for (const purpose of purposes) {
179
+ pushTo({ inputDocument, property: purpose, value: entry });
180
+ }
181
+ return;
182
+ }
183
+ if (!isPlainObject(entry) || typeof entry.id !== 'string') {
184
+ throw new TypeError('A "verificationMethods" entry must be a key pair entry, a DID URL string, or a verification method object with a string "id".');
185
+ }
186
+ placeMethod({
187
+ inputDocument,
188
+ node: deepClone(entry),
189
+ purposes
190
+ });
191
+ }
192
+ //# sourceMappingURL=inputDocument.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inputDocument.js","sourceRoot":"","sources":["../src/inputDocument.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,0BAA0B,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AACvE,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAGnE;;;GAGG;AACH,MAAM,mBAAmB,GAAG,UAAU,CAAA;AAEtC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,qBAAqB,CAAC,EACpC,aAAa,EAGd;IACC,IACE,CAAC,aAAa,CAAC,aAAa,CAAC;QAC7B,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,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,aAAa,EACb,KAAK,EACL,QAAQ,EAKT;IACC,cAAc,CAAC,QAAQ,CAAC,CAAA;IACxB,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,MAAM,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;QAC5D,CAAC;QACD,OAAM;IACR,CAAC;IACD,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;QAC1D,MAAM,IAAI,SAAS,CACjB,+HAA+H,CAChI,CAAA;IACH,CAAC;IACD,WAAW,CAAC;QACV,aAAa;QACb,IAAI,EAAE,SAAS,CAAC,KAAK,CAA4B;QACjD,QAAQ;KACT,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,137 @@
1
+ import { DIDResolutionError, type IDIDDocument, type IDIDResolutionErrorCode } from '@interop/data-integrity-core';
2
+ import type { InputDocument } from './types.js';
3
+ export { MAX_LONG_FORM_LENGTH } from './constants.js';
4
+ /**
5
+ * Matches a long form: a fixed `zQm` prefix (every hash is a base58btc
6
+ * sha2-256 multihash), 44 more base58 characters, then the encoded document,
7
+ * at least 6 base58 characters (what `{}` encodes to). Bitcoin alphabet.
8
+ */
9
+ export declare const LONG_FORM_PATTERN: RegExp;
10
+ /**
11
+ * Matches a short form: `did:peer:4` followed by the 46-character hash.
12
+ */
13
+ export declare const SHORT_FORM_PATTERN: RegExp;
14
+ /**
15
+ * Builds a `DIDResolutionError` with the spec problem details for its code.
16
+ * The codec and the driver throw this directly (no private error class), so
17
+ * `CachedResolver.resolveDID()` produces the right envelope without a
18
+ * translation layer.
19
+ *
20
+ * @param options {object}
21
+ * @param options.code {IDIDResolutionErrorCode}
22
+ * @param options.message {string}
23
+ * @param [options.cause] {unknown}
24
+ *
25
+ * @returns {DIDResolutionError}
26
+ */
27
+ export declare function resolutionError({ code, message, cause }: {
28
+ code: IDIDResolutionErrorCode;
29
+ message: string;
30
+ cause?: unknown;
31
+ }): DIDResolutionError;
32
+ /**
33
+ * Encodes an input document into its long form. Does not validate; validation
34
+ * is the driver's job at `generate()` time.
35
+ *
36
+ * @param options {object}
37
+ * @param options.inputDocument {InputDocument}
38
+ *
39
+ * @returns {string} The long form `did:peer:4{hash}:{encoded document}`.
40
+ */
41
+ export declare function encodeLongForm({ inputDocument }: {
42
+ inputDocument: InputDocument;
43
+ }): string;
44
+ /**
45
+ * Encodes an input document into its short form. Does not validate.
46
+ *
47
+ * @param options {object}
48
+ * @param options.inputDocument {InputDocument}
49
+ *
50
+ * @returns {string} The short form `did:peer:4{hash}`.
51
+ */
52
+ export declare function encodeShortForm({ inputDocument }: {
53
+ inputDocument: InputDocument;
54
+ }): string;
55
+ /**
56
+ * Whether a string starts with the numalgo 4 prefix `did:peer:4`.
57
+ *
58
+ * @param did {string}
59
+ *
60
+ * @returns {boolean}
61
+ */
62
+ export declare function isDidPeer4(did: string): boolean;
63
+ /**
64
+ * Whether a string matches the long form grammar. Does not verify the hash.
65
+ *
66
+ * @param did {string}
67
+ *
68
+ * @returns {boolean}
69
+ */
70
+ export declare function isLongForm(did: string): boolean;
71
+ /**
72
+ * Whether a string matches the short form grammar.
73
+ *
74
+ * @param did {string}
75
+ *
76
+ * @returns {boolean}
77
+ */
78
+ export declare function isShortForm(did: string): boolean;
79
+ /**
80
+ * Returns the short form of a long form by string surgery (regex guarded). It
81
+ * does not verify the hash; callers that need that call `decode` first.
82
+ *
83
+ * @param options {object}
84
+ * @param options.did {string} - A long form.
85
+ *
86
+ * @throws {DIDResolutionError} `invalidDid` if `did` is not a long form.
87
+ *
88
+ * @returns {string} The short form.
89
+ */
90
+ export declare function longToShort({ did }: {
91
+ did: string;
92
+ }): string;
93
+ /**
94
+ * Decodes a long form into its input document, verifying the hash over the
95
+ * encoded document string exactly as present in the DID. Rejects anything that
96
+ * is not a numalgo 4 long form, a long form over `MAX_LONG_FORM_LENGTH`, a
97
+ * hash mismatch, a bad multicodec prefix, invalid UTF-8 or JSON, and a payload
98
+ * that is not a plain object. Returns a freshly parsed document on every call.
99
+ *
100
+ * @param options {object}
101
+ * @param options.did {string} - A long form.
102
+ *
103
+ * @throws {TypeError} If `did` is not a string.
104
+ * @throws {DIDResolutionError} With code `invalidDid` for every other failure.
105
+ *
106
+ * @returns {InputDocument} The input document, relative references intact.
107
+ */
108
+ export declare function decode({ did }: {
109
+ did: string;
110
+ }): InputDocument;
111
+ /**
112
+ * Resolves a long form to its DID document: `decode` (regex, length and hash
113
+ * checks) followed by contextualization with the long form as `id` and the
114
+ * short form appended to `alsoKnownAs`. References stay relative.
115
+ *
116
+ * @param options {object}
117
+ * @param options.did {string} - A long form.
118
+ *
119
+ * @returns {IDIDDocument}
120
+ */
121
+ export declare function resolveLongForm({ did }: {
122
+ did: string;
123
+ }): IDIDDocument;
124
+ /**
125
+ * Resolves a short form from its long form: decodes the long form (so the
126
+ * regex and hash checks run), then contextualizes with the short form as `id`
127
+ * and the long form appended to `alsoKnownAs`. References stay relative.
128
+ *
129
+ * @param options {object}
130
+ * @param options.longFormDid {string} - The long form previously seen.
131
+ *
132
+ * @returns {IDIDDocument}
133
+ */
134
+ export declare function resolveShortForm({ longFormDid }: {
135
+ longFormDid: string;
136
+ }): IDIDDocument;
137
+ //# sourceMappingURL=numalgo4.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numalgo4.d.ts","sourceRoot":"","sources":["../src/numalgo4.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,kBAAkB,EAClB,KAAK,YAAY,EACjB,KAAK,uBAAuB,EAC7B,MAAM,8BAA8B,CAAA;AAUrC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C,OAAO,EAAE,oBAAoB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