@interop/did-cli 0.6.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +341 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +1307 -0
  4. package/dist/commands/did.d.ts +3 -0
  5. package/dist/commands/did.d.ts.map +1 -0
  6. package/dist/commands/did.js +605 -0
  7. package/dist/commands/did.js.map +1 -0
  8. package/dist/commands/key.d.ts +3 -0
  9. package/dist/commands/key.d.ts.map +1 -0
  10. package/dist/commands/key.js +430 -0
  11. package/dist/commands/key.js.map +1 -0
  12. package/dist/commands/vc.d.ts +79 -0
  13. package/dist/commands/vc.d.ts.map +1 -0
  14. package/dist/commands/vc.js +528 -0
  15. package/dist/commands/vc.js.map +1 -0
  16. package/dist/commands/wallet.d.ts +14 -0
  17. package/dist/commands/wallet.d.ts.map +1 -0
  18. package/dist/commands/wallet.js +48 -0
  19. package/dist/commands/wallet.js.map +1 -0
  20. package/dist/commands/was.d.ts +500 -0
  21. package/dist/commands/was.d.ts.map +1 -0
  22. package/dist/commands/was.js +1833 -0
  23. package/dist/commands/was.js.map +1 -0
  24. package/dist/commands/zcap.d.ts +85 -0
  25. package/dist/commands/zcap.d.ts.map +1 -0
  26. package/dist/commands/zcap.js +447 -0
  27. package/dist/commands/zcap.js.map +1 -0
  28. package/dist/index.d.ts +3 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +20 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/keys/ecdsa.d.ts +40 -0
  33. package/dist/keys/ecdsa.d.ts.map +1 -0
  34. package/dist/keys/ecdsa.js +73 -0
  35. package/dist/keys/ecdsa.js.map +1 -0
  36. package/dist/meta.d.ts +164 -0
  37. package/dist/meta.d.ts.map +1 -0
  38. package/dist/meta.js +286 -0
  39. package/dist/meta.js.map +1 -0
  40. package/dist/storage.d.ts +191 -0
  41. package/dist/storage.d.ts.map +1 -0
  42. package/dist/storage.js +307 -0
  43. package/dist/storage.js.map +1 -0
  44. package/dist/table.d.ts +43 -0
  45. package/dist/table.d.ts.map +1 -0
  46. package/dist/table.js +61 -0
  47. package/dist/table.js.map +1 -0
  48. package/dist/vc/fixtures/welcomeCredential.d.ts +22 -0
  49. package/dist/vc/fixtures/welcomeCredential.d.ts.map +1 -0
  50. package/dist/vc/fixtures/welcomeCredential.js +25 -0
  51. package/dist/vc/fixtures/welcomeCredential.js.map +1 -0
  52. package/dist/vc/issue.d.ts +24 -0
  53. package/dist/vc/issue.d.ts.map +1 -0
  54. package/dist/vc/issue.js +211 -0
  55. package/dist/vc/issue.js.map +1 -0
  56. package/dist/vc/registries.d.ts +30 -0
  57. package/dist/vc/registries.d.ts.map +1 -0
  58. package/dist/vc/registries.js +53 -0
  59. package/dist/vc/registries.js.map +1 -0
  60. package/dist/vc/registryManager.d.ts +25 -0
  61. package/dist/vc/registryManager.d.ts.map +1 -0
  62. package/dist/vc/registryManager.js +29 -0
  63. package/dist/vc/registryManager.js.map +1 -0
  64. package/dist/vc/suites/expirationSuite.d.ts +23 -0
  65. package/dist/vc/suites/expirationSuite.d.ts.map +1 -0
  66. package/dist/vc/suites/expirationSuite.js +84 -0
  67. package/dist/vc/suites/expirationSuite.js.map +1 -0
  68. package/dist/vc/suites/issuerDetailsSuite.d.ts +22 -0
  69. package/dist/vc/suites/issuerDetailsSuite.d.ts.map +1 -0
  70. package/dist/vc/suites/issuerDetailsSuite.js +69 -0
  71. package/dist/vc/suites/issuerDetailsSuite.js.map +1 -0
  72. package/dist/vc/verify.d.ts +46 -0
  73. package/dist/vc/verify.d.ts.map +1 -0
  74. package/dist/vc/verify.js +147 -0
  75. package/dist/vc/verify.js.map +1 -0
  76. package/dist/was/address.d.ts +44 -0
  77. package/dist/was/address.d.ts.map +1 -0
  78. package/dist/was/address.js +98 -0
  79. package/dist/was/address.js.map +1 -0
  80. package/dist/was/capability.d.ts +65 -0
  81. package/dist/was/capability.d.ts.map +1 -0
  82. package/dist/was/capability.js +108 -0
  83. package/dist/was/capability.js.map +1 -0
  84. package/dist/was/client.d.ts +108 -0
  85. package/dist/was/client.d.ts.map +1 -0
  86. package/dist/was/client.js +142 -0
  87. package/dist/was/client.js.map +1 -0
  88. package/dist/was/io.d.ts +71 -0
  89. package/dist/was/io.d.ts.map +1 -0
  90. package/dist/was/io.js +146 -0
  91. package/dist/was/io.js.map +1 -0
  92. package/dist/was/registry.d.ts +79 -0
  93. package/dist/was/registry.d.ts.map +1 -0
  94. package/dist/was/registry.js +99 -0
  95. package/dist/was/registry.js.map +1 -0
  96. package/dist/zcap/create.d.ts +20 -0
  97. package/dist/zcap/create.d.ts.map +1 -0
  98. package/dist/zcap/create.js +29 -0
  99. package/dist/zcap/create.js.map +1 -0
  100. package/dist/zcap/delegate.d.ts +44 -0
  101. package/dist/zcap/delegate.d.ts.map +1 -0
  102. package/dist/zcap/delegate.js +77 -0
  103. package/dist/zcap/delegate.js.map +1 -0
  104. package/dist/zcap/encoding.d.ts +17 -0
  105. package/dist/zcap/encoding.d.ts.map +1 -0
  106. package/dist/zcap/encoding.js +37 -0
  107. package/dist/zcap/encoding.js.map +1 -0
  108. package/dist/zcap/signer.d.ts +20 -0
  109. package/dist/zcap/signer.d.ts.map +1 -0
  110. package/dist/zcap/signer.js +62 -0
  111. package/dist/zcap/signer.js.map +1 -0
  112. package/dist/zcap/ttl.d.ts +19 -0
  113. package/dist/zcap/ttl.d.ts.map +1 -0
  114. package/dist/zcap/ttl.js +40 -0
  115. package/dist/zcap/ttl.js.map +1 -0
  116. package/package.json +64 -0
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Derives a filesystem-safe storage id from an item identifier (a
3
+ * capability urn, a space id, etc.), replacing characters that are awkward
4
+ * in file names.
5
+ *
6
+ * @param id {string}
7
+ * @returns {string}
8
+ */
9
+ export declare function sanitizeStorageId(id: string): string;
10
+ /**
11
+ * User-editable metadata stored in a `.meta.json` sidecar next to a wallet
12
+ * item or DID document. All fields are optional; a missing sidecar simply
13
+ * means "no metadata".
14
+ */
15
+ export interface ItemMetadata {
16
+ /** ISO 8601 timestamp recorded when the item was saved. */
17
+ created?: string;
18
+ /** Short user-defined tag for telling items apart. */
19
+ handle?: string;
20
+ /** Longer free-text description. */
21
+ description?: string;
22
+ }
23
+ /**
24
+ * Metadata for a stored key. `dids` caches the DIDs whose documents reference
25
+ * the key; the displayed value is always re-derived from the stored DID
26
+ * documents, so this cache may lag without harm.
27
+ */
28
+ export interface KeyMetadata extends ItemMetadata {
29
+ dids?: string[];
30
+ }
31
+ /**
32
+ * List the storage IDs of all items saved in a wallet collection.
33
+ *
34
+ * The storage ID of an item is its file name without the `.json` extension --
35
+ * an internal addressing detail used to load the item back from storage.
36
+ * `.meta.json` metadata sidecars are not items and are excluded. Returns
37
+ * an empty array if the collection has no directory yet.
38
+ *
39
+ * @param collection {string}
40
+ * @returns {Promise<string[]>}
41
+ */
42
+ export declare function listCollection(collection: string): Promise<string[]>;
43
+ /**
44
+ * Load and JSON-parse a single item from a wallet collection.
45
+ *
46
+ * @param collection {string}
47
+ * @param storageId {string}
48
+ * @returns {Promise<T>}
49
+ */
50
+ export declare function loadFromCollection<T = unknown>(collection: string, storageId: string): Promise<T>;
51
+ export declare function saveToCollection(collection: string, storageId: string, data: object): Promise<string>;
52
+ /**
53
+ * Load the `.meta.json` metadata sidecar of a wallet collection item.
54
+ *
55
+ * @param options {object}
56
+ * @param options.collection {string}
57
+ * @param options.storageId {string}
58
+ * @returns {Promise<KeyMetadata | undefined>} undefined when no sidecar exists.
59
+ */
60
+ export declare function loadMetaFromCollection({ collection, storageId }: {
61
+ collection: string;
62
+ storageId: string;
63
+ }): Promise<KeyMetadata | undefined>;
64
+ /**
65
+ * Save the `.meta.json` metadata sidecar of a wallet collection item.
66
+ *
67
+ * @param options {object}
68
+ * @param options.collection {string}
69
+ * @param options.storageId {string}
70
+ * @param options.meta {KeyMetadata}
71
+ * @returns {Promise<string>} the sidecar file path.
72
+ */
73
+ export declare function saveMetaToCollection({ collection, storageId, meta }: {
74
+ collection: string;
75
+ storageId: string;
76
+ meta: KeyMetadata;
77
+ }): Promise<string>;
78
+ /**
79
+ * Remove an item from a wallet collection: the item file itself plus its
80
+ * `.meta.json` metadata sidecar (when one exists).
81
+ *
82
+ * @param options {object}
83
+ * @param options.collection {string}
84
+ * @param options.storageId {string}
85
+ * @returns {Promise<string[]>} the file paths that were deleted.
86
+ */
87
+ export declare function removeFromCollection({ collection, storageId }: {
88
+ collection: string;
89
+ storageId: string;
90
+ }): Promise<string[]>;
91
+ /**
92
+ * Find a stored wallet key by its publicKeyMultibase fingerprint.
93
+ *
94
+ * Fingerprints are not the file name (storage IDs carry a date/type prefix and
95
+ * may encode a full verification method id), so the lookup scans every key
96
+ * file in the collection.
97
+ *
98
+ * @param options {object}
99
+ * @param options.fingerprint {string}
100
+ * @returns {Promise<{storageId: string, key: object} | undefined>}
101
+ */
102
+ export declare function findStoredKey({ fingerprint }: {
103
+ fingerprint: string;
104
+ }): Promise<{
105
+ storageId: string;
106
+ key: {
107
+ publicKeyMultibase?: string;
108
+ secretKeyMultibase?: string;
109
+ };
110
+ } | undefined>;
111
+ /**
112
+ * List the DIDs saved in local storage, across all method subdirectories.
113
+ *
114
+ * Each saved DID is stored as `<did>.json` (the DID document) alongside a
115
+ * `<did>.keys.json` key file and an optional `<did>.meta.json` metadata
116
+ * sidecar; only the first is reported, and the DID is its file name without
117
+ * the `.json` extension. Returns an empty array if no DIDs have been saved
118
+ * yet.
119
+ *
120
+ * @returns {Promise<string[]>}
121
+ */
122
+ export declare function listDids(): Promise<string[]>;
123
+ /**
124
+ * Load and JSON-parse the DID document saved for a DID.
125
+ *
126
+ * Reads `<did>.json` from the method subdirectory of the DIDs storage dir, where
127
+ * the method is derived from the DID (e.g. `did:key:...` lives under `key/`).
128
+ *
129
+ * @param did {string}
130
+ * @returns {Promise<T>}
131
+ */
132
+ export declare function loadDidDocument<T = unknown>(did: string): Promise<T>;
133
+ /**
134
+ * Load and JSON-parse the exported key pair saved alongside a DID document.
135
+ *
136
+ * Reads the `<did>.keys.json` key file written by `saveToDids` with the `keys`
137
+ * suffix; it carries the DID's signing key material (including the secret key).
138
+ *
139
+ * @param did {string}
140
+ * @returns {Promise<T>}
141
+ */
142
+ export declare function loadDidKeys<T = unknown>(did: string): Promise<T>;
143
+ /**
144
+ * Load the `<did>.meta.json` metadata sidecar saved alongside a DID document.
145
+ *
146
+ * @param options {object}
147
+ * @param options.did {string}
148
+ * @returns {Promise<ItemMetadata | undefined>} undefined when no sidecar exists.
149
+ */
150
+ export declare function loadDidMeta({ did }: {
151
+ did: string;
152
+ }): Promise<ItemMetadata | undefined>;
153
+ /**
154
+ * Save the `<did>.meta.json` metadata sidecar alongside a DID document.
155
+ *
156
+ * @param options {object}
157
+ * @param options.did {string}
158
+ * @param options.meta {ItemMetadata}
159
+ * @returns {Promise<string>} the sidecar file path.
160
+ */
161
+ export declare function saveDidMeta({ did, meta }: {
162
+ did: string;
163
+ meta: ItemMetadata;
164
+ }): Promise<string>;
165
+ /**
166
+ * Remove a DID from local storage: the `<did>.json` DID document plus its
167
+ * `<did>.keys.json` key file and `<did>.meta.json` metadata sidecar (when
168
+ * they exist).
169
+ *
170
+ * @param options {object}
171
+ * @param options.did {string}
172
+ * @returns {Promise<string[]>} the file paths that were deleted.
173
+ */
174
+ export declare function removeDidFiles({ did }: {
175
+ did: string;
176
+ }): Promise<string[]>;
177
+ /**
178
+ * @param options {object}
179
+ * @param options.method {string}
180
+ * @param options.did {string}
181
+ * @param options.suffix {string}
182
+ * @param options.data {object}
183
+ * @returns {Promise<string>}
184
+ */
185
+ export declare function saveToDids({ method, did, suffix, data }: {
186
+ method: string;
187
+ did: string;
188
+ suffix?: string;
189
+ data: object;
190
+ }): Promise<string>;
191
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAYA;;;;;;;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;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,EAC/B,MAAM,EACN,GAAG,EACH,MAAM,EACN,IAAI,EACL,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;CACb,GAAG,OAAO,CAAC,MAAM,CAAC,CAOlB"}
@@ -0,0 +1,307 @@
1
+ import { mkdir, readFile, readdir, unlink, writeFile } from 'node:fs/promises';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ function getWalletDir() {
5
+ if (process.env.WALLET_DIR) {
6
+ return process.env.WALLET_DIR;
7
+ }
8
+ const configHome = process.env.XDG_CONFIG_HOME ?? join(homedir(), '.config');
9
+ return join(configHome, 'did-cli-wallet');
10
+ }
11
+ /**
12
+ * Derives a filesystem-safe storage id from an item identifier (a
13
+ * capability urn, a space id, etc.), replacing characters that are awkward
14
+ * in file names.
15
+ *
16
+ * @param id {string}
17
+ * @returns {string}
18
+ */
19
+ export function sanitizeStorageId(id) {
20
+ return id.replaceAll(':', '_').replaceAll('%', '_').replaceAll('/', '_');
21
+ }
22
+ /**
23
+ * List the storage IDs of all items saved in a wallet collection.
24
+ *
25
+ * The storage ID of an item is its file name without the `.json` extension --
26
+ * an internal addressing detail used to load the item back from storage.
27
+ * `.meta.json` metadata sidecars are not items and are excluded. Returns
28
+ * an empty array if the collection has no directory yet.
29
+ *
30
+ * @param collection {string}
31
+ * @returns {Promise<string[]>}
32
+ */
33
+ export async function listCollection(collection) {
34
+ const dir = join(getWalletDir(), collection);
35
+ let fileNames;
36
+ try {
37
+ fileNames = await readdir(dir);
38
+ }
39
+ catch (err) {
40
+ if (err.code === 'ENOENT') {
41
+ return [];
42
+ }
43
+ throw err;
44
+ }
45
+ return fileNames
46
+ .filter(name => name.endsWith('.json') && !name.endsWith('.meta.json'))
47
+ .map(name => name.slice(0, -'.json'.length))
48
+ .sort();
49
+ }
50
+ /**
51
+ * Load and JSON-parse a single item from a wallet collection.
52
+ *
53
+ * @param collection {string}
54
+ * @param storageId {string}
55
+ * @returns {Promise<T>}
56
+ */
57
+ export async function loadFromCollection(collection, storageId) {
58
+ const filePath = join(getWalletDir(), collection, `${storageId}.json`);
59
+ return JSON.parse(await readFile(filePath, 'utf8'));
60
+ }
61
+ export async function saveToCollection(collection, storageId, data) {
62
+ const dir = join(getWalletDir(), collection);
63
+ await mkdir(dir, { recursive: true });
64
+ const filePath = join(dir, `${storageId}.json`);
65
+ await writeFile(filePath, JSON.stringify(data, null, 2), 'utf8');
66
+ return filePath;
67
+ }
68
+ /**
69
+ * Load the `.meta.json` metadata sidecar of a wallet collection item.
70
+ *
71
+ * @param options {object}
72
+ * @param options.collection {string}
73
+ * @param options.storageId {string}
74
+ * @returns {Promise<KeyMetadata | undefined>} undefined when no sidecar exists.
75
+ */
76
+ export async function loadMetaFromCollection({ collection, storageId }) {
77
+ const filePath = join(getWalletDir(), collection, `${storageId}.meta.json`);
78
+ try {
79
+ return JSON.parse(await readFile(filePath, 'utf8'));
80
+ }
81
+ catch (err) {
82
+ if (err.code === 'ENOENT') {
83
+ return undefined;
84
+ }
85
+ throw err;
86
+ }
87
+ }
88
+ /**
89
+ * Save the `.meta.json` metadata sidecar of a wallet collection item.
90
+ *
91
+ * @param options {object}
92
+ * @param options.collection {string}
93
+ * @param options.storageId {string}
94
+ * @param options.meta {KeyMetadata}
95
+ * @returns {Promise<string>} the sidecar file path.
96
+ */
97
+ export async function saveMetaToCollection({ collection, storageId, meta }) {
98
+ const dir = join(getWalletDir(), collection);
99
+ await mkdir(dir, { recursive: true });
100
+ const filePath = join(dir, `${storageId}.meta.json`);
101
+ await writeFile(filePath, JSON.stringify(meta, null, 2), 'utf8');
102
+ return filePath;
103
+ }
104
+ /**
105
+ * Delete a file, ignoring the case where it does not exist.
106
+ *
107
+ * @param filePath {string}
108
+ * @returns {Promise<boolean>} true when the file existed and was deleted.
109
+ */
110
+ async function unlinkIfExists(filePath) {
111
+ try {
112
+ await unlink(filePath);
113
+ return true;
114
+ }
115
+ catch (err) {
116
+ if (err.code === 'ENOENT') {
117
+ return false;
118
+ }
119
+ throw err;
120
+ }
121
+ }
122
+ /**
123
+ * Remove an item from a wallet collection: the item file itself plus its
124
+ * `.meta.json` metadata sidecar (when one exists).
125
+ *
126
+ * @param options {object}
127
+ * @param options.collection {string}
128
+ * @param options.storageId {string}
129
+ * @returns {Promise<string[]>} the file paths that were deleted.
130
+ */
131
+ export async function removeFromCollection({ collection, storageId }) {
132
+ const dir = join(getWalletDir(), collection);
133
+ const removed = [];
134
+ const filePath = join(dir, `${storageId}.json`);
135
+ await unlink(filePath);
136
+ removed.push(filePath);
137
+ const metaPath = join(dir, `${storageId}.meta.json`);
138
+ if (await unlinkIfExists(metaPath)) {
139
+ removed.push(metaPath);
140
+ }
141
+ return removed;
142
+ }
143
+ /**
144
+ * Find a stored wallet key by its publicKeyMultibase fingerprint.
145
+ *
146
+ * Fingerprints are not the file name (storage IDs carry a date/type prefix and
147
+ * may encode a full verification method id), so the lookup scans every key
148
+ * file in the collection.
149
+ *
150
+ * @param options {object}
151
+ * @param options.fingerprint {string}
152
+ * @returns {Promise<{storageId: string, key: object} | undefined>}
153
+ */
154
+ export async function findStoredKey({ fingerprint }) {
155
+ const storageIds = await listCollection('keys');
156
+ for (const storageId of storageIds) {
157
+ const key = await loadFromCollection('keys', storageId);
158
+ if (key.publicKeyMultibase === fingerprint) {
159
+ return { storageId, key };
160
+ }
161
+ }
162
+ return undefined;
163
+ }
164
+ function getDidsDir() {
165
+ return process.env.DIDS_DIR ?? join(getWalletDir(), 'dids');
166
+ }
167
+ /**
168
+ * List the DIDs saved in local storage, across all method subdirectories.
169
+ *
170
+ * Each saved DID is stored as `<did>.json` (the DID document) alongside a
171
+ * `<did>.keys.json` key file and an optional `<did>.meta.json` metadata
172
+ * sidecar; only the first is reported, and the DID is its file name without
173
+ * the `.json` extension. Returns an empty array if no DIDs have been saved
174
+ * yet.
175
+ *
176
+ * @returns {Promise<string[]>}
177
+ */
178
+ export async function listDids() {
179
+ const baseDir = getDidsDir();
180
+ let methodEntries;
181
+ try {
182
+ methodEntries = await readdir(baseDir, { withFileTypes: true });
183
+ }
184
+ catch (err) {
185
+ if (err.code === 'ENOENT') {
186
+ return [];
187
+ }
188
+ throw err;
189
+ }
190
+ const dids = [];
191
+ for (const methodEntry of methodEntries) {
192
+ if (!methodEntry.isDirectory()) {
193
+ continue;
194
+ }
195
+ const fileNames = await readdir(join(baseDir, methodEntry.name));
196
+ for (const fileName of fileNames) {
197
+ if (!fileName.endsWith('.json') ||
198
+ fileName.endsWith('.keys.json') ||
199
+ fileName.endsWith('.meta.json')) {
200
+ continue;
201
+ }
202
+ dids.push(fileName.slice(0, -'.json'.length));
203
+ }
204
+ }
205
+ return dids.sort();
206
+ }
207
+ /**
208
+ * Load and JSON-parse the DID document saved for a DID.
209
+ *
210
+ * Reads `<did>.json` from the method subdirectory of the DIDs storage dir, where
211
+ * the method is derived from the DID (e.g. `did:key:...` lives under `key/`).
212
+ *
213
+ * @param did {string}
214
+ * @returns {Promise<T>}
215
+ */
216
+ export async function loadDidDocument(did) {
217
+ const method = did.split(':')[1];
218
+ const filePath = join(getDidsDir(), method, `${did}.json`);
219
+ return JSON.parse(await readFile(filePath, 'utf8'));
220
+ }
221
+ /**
222
+ * Load and JSON-parse the exported key pair saved alongside a DID document.
223
+ *
224
+ * Reads the `<did>.keys.json` key file written by `saveToDids` with the `keys`
225
+ * suffix; it carries the DID's signing key material (including the secret key).
226
+ *
227
+ * @param did {string}
228
+ * @returns {Promise<T>}
229
+ */
230
+ export async function loadDidKeys(did) {
231
+ const method = did.split(':')[1];
232
+ const filePath = join(getDidsDir(), method, `${did}.keys.json`);
233
+ return JSON.parse(await readFile(filePath, 'utf8'));
234
+ }
235
+ /**
236
+ * Load the `<did>.meta.json` metadata sidecar saved alongside a DID document.
237
+ *
238
+ * @param options {object}
239
+ * @param options.did {string}
240
+ * @returns {Promise<ItemMetadata | undefined>} undefined when no sidecar exists.
241
+ */
242
+ export async function loadDidMeta({ did }) {
243
+ const method = did.split(':')[1];
244
+ const filePath = join(getDidsDir(), method, `${did}.meta.json`);
245
+ try {
246
+ return JSON.parse(await readFile(filePath, 'utf8'));
247
+ }
248
+ catch (err) {
249
+ if (err.code === 'ENOENT') {
250
+ return undefined;
251
+ }
252
+ throw err;
253
+ }
254
+ }
255
+ /**
256
+ * Save the `<did>.meta.json` metadata sidecar alongside a DID document.
257
+ *
258
+ * @param options {object}
259
+ * @param options.did {string}
260
+ * @param options.meta {ItemMetadata}
261
+ * @returns {Promise<string>} the sidecar file path.
262
+ */
263
+ export async function saveDidMeta({ did, meta }) {
264
+ const method = did.split(':')[1];
265
+ return saveToDids({ method, did, suffix: 'meta', data: meta });
266
+ }
267
+ /**
268
+ * Remove a DID from local storage: the `<did>.json` DID document plus its
269
+ * `<did>.keys.json` key file and `<did>.meta.json` metadata sidecar (when
270
+ * they exist).
271
+ *
272
+ * @param options {object}
273
+ * @param options.did {string}
274
+ * @returns {Promise<string[]>} the file paths that were deleted.
275
+ */
276
+ export async function removeDidFiles({ did }) {
277
+ const method = did.split(':')[1];
278
+ const dir = join(getDidsDir(), method);
279
+ const removed = [];
280
+ const docPath = join(dir, `${did}.json`);
281
+ await unlink(docPath);
282
+ removed.push(docPath);
283
+ for (const suffix of ['keys', 'meta']) {
284
+ const sidecarPath = join(dir, `${did}.${suffix}.json`);
285
+ if (await unlinkIfExists(sidecarPath)) {
286
+ removed.push(sidecarPath);
287
+ }
288
+ }
289
+ return removed;
290
+ }
291
+ /**
292
+ * @param options {object}
293
+ * @param options.method {string}
294
+ * @param options.did {string}
295
+ * @param options.suffix {string}
296
+ * @param options.data {object}
297
+ * @returns {Promise<string>}
298
+ */
299
+ export async function saveToDids({ method, did, suffix, data }) {
300
+ const dir = join(getDidsDir(), method);
301
+ await mkdir(dir, { recursive: true });
302
+ const fileName = suffix ? `${did}.${suffix}.json` : `${did}.json`;
303
+ const filePath = join(dir, fileName);
304
+ await writeFile(filePath, JSON.stringify(data, null, 2), 'utf8');
305
+ return filePath;
306
+ }
307
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,SAAS,YAAY;IACnB,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,CAAA;IAC/B,CAAC;IACD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,CAAC,CAAA;IAC5E,OAAO,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAAU;IAC1C,OAAO,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AAC1E,CAAC;AAyBD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,UAAkB;IACrD,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,EAAE,UAAU,CAAC,CAAA;IAC5C,IAAI,SAAmB,CAAA;IACvB,IAAI,CAAC;QACH,SAAS,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAA;IAChC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,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AAO,CAAA;IACjE,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IACpC,MAAM,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;IAChE,OAAO,QAAQ,CAAA;AACjB,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Minimal column-aligned table rendering for CLI list/show output.
3
+ *
4
+ * Deliberately hand-rolled (padEnd-based, no dependencies) and deterministic:
5
+ * column widths are computed from the content and optional per-column caps,
6
+ * never from the terminal width, so output is stable for tests and pipes.
7
+ */
8
+ export interface Column {
9
+ header: string;
10
+ /** Cells longer than this are middle-truncated; undefined = unlimited. */
11
+ maxWidth?: number;
12
+ }
13
+ /**
14
+ * Truncate a value to `maxWidth` characters by removing the middle and
15
+ * inserting an ellipsis, keeping both ends -- the informative parts of
16
+ * fingerprints and DIDs.
17
+ *
18
+ * @param options {object}
19
+ * @param options.value {string}
20
+ * @param options.maxWidth {number}
21
+ * @returns {string}
22
+ */
23
+ export declare function truncateMiddle({ value, maxWidth }: {
24
+ value: string;
25
+ maxWidth: number;
26
+ }): string;
27
+ /**
28
+ * Render a padEnd-aligned table: a header row, a dash separator, then one
29
+ * line per row. Each column's width is the widest of its header and cells,
30
+ * capped at the column's `maxWidth` (cells beyond the cap are
31
+ * middle-truncated). Cells are joined with two spaces; missing values render
32
+ * as empty cells. Trailing whitespace is trimmed from every line.
33
+ *
34
+ * @param options {object}
35
+ * @param options.columns {Column[]}
36
+ * @param options.rows {string[][]}
37
+ * @returns {string}
38
+ */
39
+ export declare function renderTable({ columns, rows }: {
40
+ columns: Column[];
41
+ rows: string[][];
42
+ }): string;
43
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,MAAM;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAID;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,EAC7B,KAAK,EACL,QAAQ,EACT,EAAE;IACD,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;CACjB,GAAG,MAAM,CAWT;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,EAC1B,OAAO,EACP,IAAI,EACL,EAAE;IACD,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,IAAI,EAAE,MAAM,EAAE,EAAE,CAAA;CACjB,GAAG,MAAM,CA0BT"}
package/dist/table.js ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Minimal column-aligned table rendering for CLI list/show output.
3
+ *
4
+ * Deliberately hand-rolled (padEnd-based, no dependencies) and deterministic:
5
+ * column widths are computed from the content and optional per-column caps,
6
+ * never from the terminal width, so output is stable for tests and pipes.
7
+ */
8
+ const ELLIPSIS = '...';
9
+ /**
10
+ * Truncate a value to `maxWidth` characters by removing the middle and
11
+ * inserting an ellipsis, keeping both ends -- the informative parts of
12
+ * fingerprints and DIDs.
13
+ *
14
+ * @param options {object}
15
+ * @param options.value {string}
16
+ * @param options.maxWidth {number}
17
+ * @returns {string}
18
+ */
19
+ export function truncateMiddle({ value, maxWidth }) {
20
+ if (value.length <= maxWidth) {
21
+ return value;
22
+ }
23
+ if (maxWidth <= ELLIPSIS.length) {
24
+ return value.slice(0, maxWidth);
25
+ }
26
+ const visible = maxWidth - ELLIPSIS.length;
27
+ const headLength = Math.ceil(visible / 2);
28
+ const tailLength = visible - headLength;
29
+ return value.slice(0, headLength) + ELLIPSIS + value.slice(-tailLength);
30
+ }
31
+ /**
32
+ * Render a padEnd-aligned table: a header row, a dash separator, then one
33
+ * line per row. Each column's width is the widest of its header and cells,
34
+ * capped at the column's `maxWidth` (cells beyond the cap are
35
+ * middle-truncated). Cells are joined with two spaces; missing values render
36
+ * as empty cells. Trailing whitespace is trimmed from every line.
37
+ *
38
+ * @param options {object}
39
+ * @param options.columns {Column[]}
40
+ * @param options.rows {string[][]}
41
+ * @returns {string}
42
+ */
43
+ export function renderTable({ columns, rows }) {
44
+ const cells = rows.map(row => columns.map((column, columnIndex) => {
45
+ const value = row[columnIndex] ?? '';
46
+ return column.maxWidth
47
+ ? truncateMiddle({ value, maxWidth: column.maxWidth })
48
+ : value;
49
+ }));
50
+ const widths = columns.map((column, columnIndex) => {
51
+ const widest = Math.max(column.header.length, ...cells.map(row => row[columnIndex].length));
52
+ return column.maxWidth ? Math.min(widest, column.maxWidth) : widest;
53
+ });
54
+ const lines = [
55
+ columns.map((column, columnIndex) => column.header.padEnd(widths[columnIndex])),
56
+ widths.map(width => '-'.repeat(width)),
57
+ ...cells.map(row => row.map((value, columnIndex) => value.padEnd(widths[columnIndex])))
58
+ ];
59
+ return lines.map(line => line.join(' ').trimEnd()).join('\n');
60
+ }
61
+ //# sourceMappingURL=table.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.js","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,MAAM,QAAQ,GAAG,KAAK,CAAA;AAEtB;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,KAAK,EACL,QAAQ,EAIT;IACC,IAAI,KAAK,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;IACjC,CAAC;IACD,MAAM,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAA;IAC1C,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,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,MAAM,CAAC,WAAW,CAAC,CAAC,CAC1C;QACD,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACtC,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CACjB,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CACnE;KACF,CAAA;IACD,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAChE,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * A sample Verifiable Credential used as test input. Copied from freewallet's
3
+ * `welcomeCredential` fixture. The `proof` block is retained for reference but
4
+ * sign tests strip it and override `issuer` with a freshly generated test DID.
5
+ */
6
+ export declare const welcomeCredential: {
7
+ '@context': string[];
8
+ type: string[];
9
+ issuer: string;
10
+ name: string;
11
+ credentialSubject: {
12
+ description: string;
13
+ };
14
+ proof: {
15
+ type: string;
16
+ created: string;
17
+ verificationMethod: string;
18
+ proofPurpose: string;
19
+ proofValue: string;
20
+ };
21
+ };
22
+ //# sourceMappingURL=welcomeCredential.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"welcomeCredential.d.ts","sourceRoot":"","sources":["../../../src/vc/fixtures/welcomeCredential.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;CAoB7B,CAAA"}