@didcid/keymaster 0.3.5 → 0.3.6

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/README.md CHANGED
@@ -198,14 +198,14 @@ keymaster list-ids
198
198
  | `create-response <challenge>` | Respond to a challenge |
199
199
  | `verify-response <response>` | Verify a response |
200
200
 
201
- ##### Names (Aliases)
201
+ ##### Aliases
202
202
 
203
203
  | Command | Description |
204
204
  |---------|-------------|
205
- | `add-name <name> <did>` | Add alias for DID |
206
- | `get-name <name>` | Get DID by alias |
207
- | `remove-name <name>` | Remove alias |
208
- | `list-names` | List all aliases |
205
+ | `add-alias <alias> <did>` | Add alias for DID |
206
+ | `get-alias <alias>` | Get DID by alias |
207
+ | `remove-alias <alias>` | Remove alias |
208
+ | `list-aliases` | List all aliases |
209
209
 
210
210
  ##### Groups
211
211
 
@@ -276,13 +276,13 @@ Many commands support these options:
276
276
 
277
277
  | Option | Description |
278
278
  |--------|-------------|
279
- | `-n, --name <name>` | Assign a name to created DID |
279
+ | `-a, --alias <alias>` | Assign an alias to created DID |
280
280
  | `-r, --registry <registry>` | Specify DID registry |
281
281
 
282
282
  Example:
283
283
  ```bash
284
284
  keymaster create-id MyBot -r hyperswarm
285
- keymaster create-schema schema.json -n my-schema -r local
285
+ keymaster create-schema schema.json -a my-schema -r local
286
286
  ```
287
287
 
288
288
  ### Client
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  function isWalletEncFile(obj) {
4
- return !!obj && obj.version === 1 && typeof obj.enc === 'string' && obj.seed?.mnemonicEnc;
4
+ return !!obj && (obj.version === 1 || obj.version === 2) && typeof obj.enc === 'string' && obj.seed?.mnemonicEnc;
5
5
  }
6
6
  function isWalletFile(obj) {
7
- return !!obj && obj.version === 1 && obj.seed?.mnemonicEnc && !('enc' in obj);
7
+ return !!obj && (obj.version === 1 || obj.version === 2) && obj.seed?.mnemonicEnc && !('enc' in obj);
8
8
  }
9
9
 
10
10
  exports.isWalletEncFile = isWalletEncFile;
@@ -275,36 +275,36 @@ class KeymasterClient {
275
275
  throwError(error);
276
276
  }
277
277
  }
278
- async listNames() {
278
+ async listAliases() {
279
279
  try {
280
- const response = await axios.get(`${this.API}/names`);
281
- return response.data.names;
280
+ const response = await axios.get(`${this.API}/aliases`);
281
+ return response.data.aliases;
282
282
  }
283
283
  catch (error) {
284
284
  throwError(error);
285
285
  }
286
286
  }
287
- async addName(name, did) {
287
+ async addAlias(alias, did) {
288
288
  try {
289
- const response = await axios.post(`${this.API}/names`, { name, did });
289
+ const response = await axios.post(`${this.API}/aliases`, { alias, did });
290
290
  return response.data.ok;
291
291
  }
292
292
  catch (error) {
293
293
  throwError(error);
294
294
  }
295
295
  }
296
- async getName(name) {
296
+ async getAlias(alias) {
297
297
  try {
298
- const response = await axios.get(`${this.API}/names/${name}`);
298
+ const response = await axios.get(`${this.API}/aliases/${alias}`);
299
299
  return response.data.did;
300
300
  }
301
301
  catch (error) {
302
302
  throwError(error);
303
303
  }
304
304
  }
305
- async removeName(name) {
305
+ async removeAlias(alias) {
306
306
  try {
307
- const response = await axios.delete(`${this.API}/names/${name}`);
307
+ const response = await axios.delete(`${this.API}/aliases/${alias}`);
308
308
  return response.data.ok;
309
309
  }
310
310
  catch (error) {
@@ -1126,7 +1126,7 @@ class Keymaster {
1126
1126
  cipher;
1127
1127
  defaultRegistry;
1128
1128
  ephemeralRegistry;
1129
- maxNameLength;
1129
+ maxAliasLength;
1130
1130
  maxDataLength;
1131
1131
  _walletCache;
1132
1132
  _hdkeyCache;
@@ -1149,7 +1149,7 @@ class Keymaster {
1149
1149
  this.cipher = options.cipher;
1150
1150
  this.defaultRegistry = options.defaultRegistry || 'hyperswarm';
1151
1151
  this.ephemeralRegistry = 'hyperswarm';
1152
- this.maxNameLength = options.maxNameLength || 32;
1152
+ this.maxAliasLength = options.maxAliasLength || 32;
1153
1153
  this.maxDataLength = 8 * 1024; // 8 KB max data to store in a JSON object
1154
1154
  }
1155
1155
  async listRegistries() {
@@ -1181,18 +1181,18 @@ class Keymaster {
1181
1181
  if (!stored) {
1182
1182
  stored = await this.newWallet();
1183
1183
  }
1184
- const upgraded = await this.upgradeWallet(stored);
1185
- this._walletCache = await this.decryptWallet(upgraded);
1184
+ const decrypted = await this.decryptWallet(stored);
1185
+ this._walletCache = await this.upgradeWallet(decrypted);
1186
1186
  return this._walletCache;
1187
1187
  }
1188
1188
  async saveWallet(wallet, overwrite = true) {
1189
- let upgraded = await this.upgradeWallet(wallet);
1190
1189
  // Decrypt if encrypted to verify passphrase and get decrypted form
1191
- const decrypted = await this.decryptWallet(upgraded);
1192
- let toStore = await this.encryptWalletForStorage(decrypted);
1190
+ const decrypted = await this.decryptWallet(wallet);
1191
+ const upgraded = await this.upgradeWallet(decrypted);
1192
+ let toStore = await this.encryptWalletForStorage(upgraded);
1193
1193
  const ok = await this.db.saveWallet(toStore, overwrite);
1194
1194
  if (ok) {
1195
- this._walletCache = decrypted;
1195
+ this._walletCache = upgraded;
1196
1196
  }
1197
1197
  return ok;
1198
1198
  }
@@ -1208,7 +1208,7 @@ class Keymaster {
1208
1208
  }
1209
1209
  const mnemonicEnc = await encryption.encMnemonic(mnemonic, this.passphrase);
1210
1210
  const wallet = {
1211
- version: 1,
1211
+ version: 2,
1212
1212
  seed: { mnemonicEnc },
1213
1213
  counter: 0,
1214
1214
  ids: {}
@@ -1275,10 +1275,10 @@ class Keymaster {
1275
1275
  }
1276
1276
  }
1277
1277
  }
1278
- if (wallet.names) {
1279
- for (const name of Object.keys(wallet.names)) {
1278
+ if (wallet.aliases) {
1279
+ for (const alias of Object.keys(wallet.aliases)) {
1280
1280
  try {
1281
- const doc = await this.resolveDID(wallet.names[name]);
1281
+ const doc = await this.resolveDID(wallet.aliases[alias]);
1282
1282
  if (doc.didDocumentMetadata?.deactivated) {
1283
1283
  deleted += 1;
1284
1284
  }
@@ -1295,7 +1295,7 @@ class Keymaster {
1295
1295
  let idsRemoved = 0;
1296
1296
  let ownedRemoved = 0;
1297
1297
  let heldRemoved = 0;
1298
- let namesRemoved = 0;
1298
+ let aliasesRemoved = 0;
1299
1299
  await this.mutateWallet(async (wallet) => {
1300
1300
  for (const name of Object.keys(wallet.ids)) {
1301
1301
  let remove = false;
@@ -1353,11 +1353,11 @@ class Keymaster {
1353
1353
  }
1354
1354
  }
1355
1355
  }
1356
- if (wallet.names) {
1357
- for (const name of Object.keys(wallet.names)) {
1356
+ if (wallet.aliases) {
1357
+ for (const alias of Object.keys(wallet.aliases)) {
1358
1358
  let remove = false;
1359
1359
  try {
1360
- const doc = await this.resolveDID(wallet.names[name]);
1360
+ const doc = await this.resolveDID(wallet.aliases[alias]);
1361
1361
  if (doc.didDocumentMetadata?.deactivated) {
1362
1362
  remove = true;
1363
1363
  }
@@ -1366,13 +1366,13 @@ class Keymaster {
1366
1366
  remove = true;
1367
1367
  }
1368
1368
  if (remove) {
1369
- delete wallet.names[name];
1370
- namesRemoved++;
1369
+ delete wallet.aliases[alias];
1370
+ aliasesRemoved++;
1371
1371
  }
1372
1372
  }
1373
1373
  }
1374
1374
  });
1375
- return { idsRemoved, ownedRemoved, heldRemoved, namesRemoved };
1375
+ return { idsRemoved, ownedRemoved, heldRemoved, aliasesRemoved };
1376
1376
  }
1377
1377
  async resolveSeedBank() {
1378
1378
  const keypair = await this.hdKeyPair();
@@ -1502,10 +1502,10 @@ class Keymaster {
1502
1502
  for (const k in current) {
1503
1503
  delete current[k];
1504
1504
  }
1505
- // Upgrade the recovered wallet to the latest version if needed
1506
- wallet = await this.upgradeWallet(wallet);
1507
1505
  // Decrypt the wallet if needed
1508
1506
  wallet = db_typeGuards.isWalletEncFile(wallet) ? await this.decryptWalletFromStorage(wallet) : wallet;
1507
+ // Upgrade the recovered wallet to the latest version if needed
1508
+ wallet = await this.upgradeWallet(wallet);
1509
1509
  // Copy all properties from the recovered wallet into the cleared current wallet
1510
1510
  // This effectively replaces the current wallet with the recovered one
1511
1511
  Object.assign(current, wallet);
@@ -1608,16 +1608,16 @@ class Keymaster {
1608
1608
  return null;
1609
1609
  }
1610
1610
  async createAsset(data, options = {}) {
1611
- let { registry = this.defaultRegistry, controller, validUntil, name } = options;
1611
+ let { registry = this.defaultRegistry, controller, validUntil, alias } = options;
1612
1612
  if (validUntil) {
1613
1613
  const validate = new Date(validUntil);
1614
1614
  if (isNaN(validate.getTime())) {
1615
1615
  throw new InvalidParameterError('options.validUntil');
1616
1616
  }
1617
1617
  }
1618
- if (name) {
1618
+ if (alias) {
1619
1619
  const wallet = await this.loadWallet();
1620
- this.validateName(name, wallet);
1620
+ this.validateAlias(alias, wallet, 'alias');
1621
1621
  }
1622
1622
  if (!data) {
1623
1623
  throw new InvalidParameterError('data');
@@ -1644,8 +1644,8 @@ class Keymaster {
1644
1644
  if (!validUntil) {
1645
1645
  await this.addToOwned(did);
1646
1646
  }
1647
- if (name) {
1648
- await this.addName(name, did);
1647
+ if (alias) {
1648
+ await this.addAlias(alias, did);
1649
1649
  }
1650
1650
  return did;
1651
1651
  }
@@ -1989,8 +1989,8 @@ class Keymaster {
1989
1989
  throw new InvalidDIDError();
1990
1990
  }
1991
1991
  const wallet = await this.loadWallet();
1992
- if (wallet.names && name in wallet.names) {
1993
- return wallet.names[name];
1992
+ if (wallet.aliases && name in wallet.aliases) {
1993
+ return wallet.aliases[name];
1994
1994
  }
1995
1995
  if (wallet.ids && name in wallet.ids) {
1996
1996
  return wallet.ids[name].did;
@@ -2080,25 +2080,25 @@ class Keymaster {
2080
2080
  const id = await this.fetchIdInfo(owner);
2081
2081
  return id.owned || [];
2082
2082
  }
2083
- validateName(name, wallet) {
2084
- if (typeof name !== 'string' || !name.trim()) {
2085
- throw new InvalidParameterError('name must be a non-empty string');
2083
+ validateAlias(alias, wallet, label = 'name') {
2084
+ if (typeof alias !== 'string' || !alias.trim()) {
2085
+ throw new InvalidParameterError(`${label} must be a non-empty string`);
2086
2086
  }
2087
- name = name.trim(); // Remove leading/trailing whitespace
2088
- if (name.length > this.maxNameLength) {
2089
- throw new InvalidParameterError(`name too long`);
2087
+ alias = alias.trim(); // Remove leading/trailing whitespace
2088
+ if (alias.length > this.maxAliasLength) {
2089
+ throw new InvalidParameterError(`${label} too long`);
2090
2090
  }
2091
- if (/[^\P{Cc}]/u.test(name)) {
2092
- throw new InvalidParameterError('name contains unprintable characters');
2091
+ if (/[^\P{Cc}]/u.test(alias)) {
2092
+ throw new InvalidParameterError(`${label} contains unprintable characters`);
2093
2093
  }
2094
- const alreadyUsedError = 'name already used';
2095
- if (wallet && wallet.names && name in wallet.names) {
2094
+ const alreadyUsedError = `${label} already used`;
2095
+ if (wallet && wallet.aliases && alias in wallet.aliases) {
2096
2096
  throw new InvalidParameterError(alreadyUsedError);
2097
2097
  }
2098
- if (wallet && wallet.ids && name in wallet.ids) {
2098
+ if (wallet && wallet.ids && alias in wallet.ids) {
2099
2099
  throw new InvalidParameterError(alreadyUsedError);
2100
2100
  }
2101
- return name;
2101
+ return alias;
2102
2102
  }
2103
2103
  async createId(name, options = {}) {
2104
2104
  let did = '';
@@ -2116,7 +2116,7 @@ class Keymaster {
2116
2116
  async createIdOperation(name, account = 0, options = {}) {
2117
2117
  const { registry = this.defaultRegistry } = options;
2118
2118
  const wallet = await this.loadWallet();
2119
- name = this.validateName(name, wallet);
2119
+ name = this.validateAlias(name, wallet);
2120
2120
  const hdkey = await this.getHDKeyFromCacheOrMnemonic(wallet);
2121
2121
  const path = `m/44'/0'/${account}'/0/0`;
2122
2122
  const didkey = hdkey.derive(path);
@@ -2162,7 +2162,7 @@ class Keymaster {
2162
2162
  }
2163
2163
  async renameId(id, name) {
2164
2164
  await this.mutateWallet((wallet) => {
2165
- name = this.validateName(name);
2165
+ name = this.validateAlias(name);
2166
2166
  if (!(id in wallet.ids)) {
2167
2167
  throw new UnknownIDError();
2168
2168
  }
@@ -2271,40 +2271,40 @@ class Keymaster {
2271
2271
  });
2272
2272
  return ok;
2273
2273
  }
2274
- async listNames(options = {}) {
2274
+ async listAliases(options = {}) {
2275
2275
  const { includeIDs = false } = options;
2276
2276
  const wallet = await this.loadWallet();
2277
- const names = { ...(wallet.names || {}) };
2277
+ const aliases = { ...(wallet.aliases || {}) };
2278
2278
  if (includeIDs) {
2279
2279
  for (const [name, id] of Object.entries(wallet.ids || {})) {
2280
- names[name] = id.did;
2280
+ aliases[name] = id.did;
2281
2281
  }
2282
2282
  }
2283
- return names;
2283
+ return aliases;
2284
2284
  }
2285
- async addName(name, did) {
2285
+ async addAlias(alias, did) {
2286
2286
  await this.mutateWallet((wallet) => {
2287
- if (!wallet.names) {
2288
- wallet.names = {};
2287
+ if (!wallet.aliases) {
2288
+ wallet.aliases = {};
2289
2289
  }
2290
- name = this.validateName(name, wallet);
2291
- wallet.names[name] = did;
2290
+ alias = this.validateAlias(alias, wallet, 'alias');
2291
+ wallet.aliases[alias] = did;
2292
2292
  });
2293
2293
  return true;
2294
2294
  }
2295
- async getName(name) {
2295
+ async getAlias(alias) {
2296
2296
  const wallet = await this.loadWallet();
2297
- if (wallet.names && name in wallet.names) {
2298
- return wallet.names[name];
2297
+ if (wallet.aliases && alias in wallet.aliases) {
2298
+ return wallet.aliases[alias];
2299
2299
  }
2300
2300
  return null;
2301
2301
  }
2302
- async removeName(name) {
2302
+ async removeAlias(alias) {
2303
2303
  await this.mutateWallet((wallet) => {
2304
- if (!wallet.names || !(name in wallet.names)) {
2304
+ if (!wallet.aliases || !(alias in wallet.aliases)) {
2305
2305
  return;
2306
2306
  }
2307
- delete wallet.names[name];
2307
+ delete wallet.aliases[alias];
2308
2308
  });
2309
2309
  return true;
2310
2310
  }
@@ -3378,7 +3378,7 @@ class Keymaster {
3378
3378
  await this.checkVaultOwner(vaultId);
3379
3379
  const vault = await this.getVault(vaultId);
3380
3380
  const { privateJwk, items } = await this.decryptVault(vault);
3381
- const validName = this.validateName(name);
3381
+ const validName = this.validateAlias(name);
3382
3382
  const encryptedData = this.cipher.encryptBytes(vault.publicJwk, privateJwk, buffer);
3383
3383
  const cid = await this.gatekeeper.addText(encryptedData);
3384
3384
  const sha256 = this.cipher.hashMessage(buffer);
@@ -3428,7 +3428,7 @@ class Keymaster {
3428
3428
  const id = await this.fetchIdInfo(undefined, wallet);
3429
3429
  const list = id.dmail || {};
3430
3430
  const dmailList = {};
3431
- const nameList = await this.listNames({ includeIDs: true });
3431
+ const nameList = await this.listAliases({ includeIDs: true });
3432
3432
  const didToName = Object.entries(nameList).reduce((acc, [name, did]) => {
3433
3433
  acc[did] = name;
3434
3434
  return acc;
@@ -3466,7 +3466,7 @@ class Keymaster {
3466
3466
  const tagSet = new Set();
3467
3467
  for (const tag of tags) {
3468
3468
  try {
3469
- tagSet.add(this.validateName(tag));
3469
+ tagSet.add(this.validateAlias(tag));
3470
3470
  }
3471
3471
  catch (error) {
3472
3472
  throw new InvalidParameterError(`Invalid tag: '${tag}'`);
@@ -3499,7 +3499,7 @@ class Keymaster {
3499
3499
  if (!Array.isArray(list)) {
3500
3500
  throw new InvalidParameterError('list');
3501
3501
  }
3502
- const nameList = await this.listNames({ includeIDs: true });
3502
+ const nameList = await this.listAliases({ includeIDs: true });
3503
3503
  let newList = [];
3504
3504
  for (const id of list) {
3505
3505
  if (typeof id !== 'string') {
@@ -3702,9 +3702,9 @@ class Keymaster {
3702
3702
  }
3703
3703
  const poll = await this.getPoll(noticeDID);
3704
3704
  if (poll) {
3705
- const names = await this.listNames();
3705
+ const names = await this.listAliases();
3706
3706
  if (!Object.values(names).includes(noticeDID)) {
3707
- await this.addUnnamedPoll(noticeDID);
3707
+ await this.addUnaliasedPoll(noticeDID);
3708
3708
  }
3709
3709
  await this.addToNotices(did, [exports.NoticeTags.POLL]);
3710
3710
  continue;
@@ -3788,10 +3788,10 @@ class Keymaster {
3788
3788
  }
3789
3789
  return payload && typeof payload.poll === "string" && typeof payload.vote === "number";
3790
3790
  }
3791
- async addUnnamedPoll(did) {
3791
+ async addUnaliasedPoll(did) {
3792
3792
  const fallbackName = did.slice(-32);
3793
3793
  try {
3794
- await this.addName(fallbackName, did);
3794
+ await this.addAlias(fallbackName, did);
3795
3795
  }
3796
3796
  catch { }
3797
3797
  }
@@ -3841,7 +3841,14 @@ class Keymaster {
3841
3841
  return wallet;
3842
3842
  }
3843
3843
  async upgradeWallet(wallet) {
3844
- if (wallet.version !== 1) {
3844
+ if (wallet.version === 1) {
3845
+ if (wallet.names && !wallet.aliases) {
3846
+ wallet.aliases = wallet.names;
3847
+ delete wallet.names;
3848
+ }
3849
+ wallet.version = 2;
3850
+ }
3851
+ if (wallet.version !== 2) {
3845
3852
  throw new KeymasterError("Unsupported wallet version.");
3846
3853
  }
3847
3854
  return wallet;