@arcblock/did 1.30.23 → 1.30.25

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/esm/entity.d.mts CHANGED
@@ -15,9 +15,11 @@
15
15
  declare function getEntityId(did: string): string;
16
16
  /**
17
17
  * Check if two DIDs represent the same entity.
18
- * Compares the identifier portion (case-insensitive), ignoring the method prefix.
18
+ * Compares the identifier portion, ignoring the crypto method prefix.
19
19
  *
20
- * For did:name DIDs, performs exact string comparison (names are case-insensitive).
20
+ * - For alias methods (e.g. did:name), requires both to be alias DIDs and compares NFC-normalized case-insensitively.
21
+ * - For hex DIDs (0x...), compares case-insensitively.
22
+ * - For Base58btc DIDs, compares case-sensitively.
21
23
  *
22
24
  * @param a - First DID or address
23
25
  * @param b - Second DID or address
package/esm/entity.mjs CHANGED
@@ -1,4 +1,6 @@
1
- import { extractIdentifier } from "./parse.mjs";
1
+ import { isAliasMethod } from "./method.mjs";
2
+ import { normalizeName } from "./name.mjs";
3
+ import { extractIdentifier, extractMethod } from "./parse.mjs";
2
4
 
3
5
  //#region src/entity.ts
4
6
  /**
@@ -19,9 +21,11 @@ function getEntityId(did) {
19
21
  }
20
22
  /**
21
23
  * Check if two DIDs represent the same entity.
22
- * Compares the identifier portion (case-insensitive), ignoring the method prefix.
24
+ * Compares the identifier portion, ignoring the crypto method prefix.
23
25
  *
24
- * For did:name DIDs, performs exact string comparison (names are case-insensitive).
26
+ * - For alias methods (e.g. did:name), requires both to be alias DIDs and compares NFC-normalized case-insensitively.
27
+ * - For hex DIDs (0x...), compares case-insensitively.
28
+ * - For Base58btc DIDs, compares case-sensitively.
25
29
  *
26
30
  * @param a - First DID or address
27
31
  * @param b - Second DID or address
@@ -29,10 +33,21 @@ function getEntityId(did) {
29
33
  */
30
34
  function isSameEntity(a, b) {
31
35
  if (typeof a !== "string" || typeof b !== "string" || !a || !b) return false;
36
+ const methodA = extractMethod(a);
37
+ const methodB = extractMethod(b);
38
+ const aIsAlias = methodA !== null && isAliasMethod(methodA);
39
+ const bIsAlias = methodB !== null && isAliasMethod(methodB);
40
+ if (aIsAlias !== bIsAlias) return false;
32
41
  const idA = extractIdentifier(a);
33
42
  const idB = extractIdentifier(b);
34
43
  if (!idA || !idB) return false;
35
- return idA.toLowerCase() === idB.toLowerCase();
44
+ if (aIsAlias && bIsAlias) try {
45
+ return normalizeName(idA) === normalizeName(idB);
46
+ } catch {
47
+ return false;
48
+ }
49
+ if (/^0x[0-9a-fA-F]+$/i.test(idA) && /^0x[0-9a-fA-F]+$/i.test(idB)) return idA.toLowerCase() === idB.toLowerCase();
50
+ return idA === idB;
36
51
  }
37
52
 
38
53
  //#endregion
package/esm/index.d.mts CHANGED
@@ -5,7 +5,7 @@ import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod,
5
5
  import { ParsedDID, extractIdentifier, extractMethod, formatDid, parse } from "./parse.mjs";
6
6
  import { isKnownDid } from "./validate.mjs";
7
7
  import { expandShortForm, toShortForm } from "./short-form.mjs";
8
- import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy } from "./name.mjs";
8
+ import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy } from "./name.mjs";
9
9
  import { InMemoryNameRegistry, NameRegistry } from "./name-registry.mjs";
10
10
  import { DIDNameResolver, DIDNameResolverOptions, ResolveTraceResult } from "./name-resolver.mjs";
11
11
  import { types } from "@ocap/mcrypto";
@@ -66,4 +66,4 @@ declare const isFromPublicKey: (did: string, pk: BytesType) => boolean;
66
66
  */
67
67
  declare const isValid: (did: string) => boolean;
68
68
  //#endregion
69
- export { ALIAS_METHODS, ALL_METHODS, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_METHOD, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, type NameHierarchy, type NameRegistry, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
69
+ export { ALIAS_METHODS, ALL_METHODS, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_METHOD, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, type NameHierarchy, type NameRegistry, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, normalizeName, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
package/esm/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.mjs";
2
+ import { getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy } from "./name.mjs";
2
3
  import { extractIdentifier, extractMethod, formatDid, parse } from "./parse.mjs";
3
4
  import { getEntityId, isSameEntity } from "./entity.mjs";
4
5
  import { DID_PREFIX, toBytes, toStrictHex } from "./util.mjs";
5
6
  import { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr } from "./type.mjs";
6
7
  import { isKnownDid } from "./validate.mjs";
7
8
  import { expandShortForm, toShortForm } from "./short-form.mjs";
8
- import { getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy } from "./name.mjs";
9
9
  import { InMemoryNameRegistry } from "./name-registry.mjs";
10
10
  import { DIDNameResolver } from "./name-resolver.mjs";
11
11
  import { getHasher, getSigner, types } from "@ocap/mcrypto";
@@ -95,9 +95,10 @@ const isValid = (did) => {
95
95
  if (isEthereumDid(address)) return true;
96
96
  const hashFn = getHasher(hash);
97
97
  const bytes = toBytes(address);
98
+ if (bytes.length !== 26) return false;
98
99
  const bytesHex = toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(0, 22))).toString("hex"));
99
100
  return toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(22, 26))).toString("hex")) === stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
100
101
  };
101
102
 
102
103
  //#endregion
103
- export { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, DIDNameResolver, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
104
+ export { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, DIDNameResolver, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, normalizeName, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
@@ -1,4 +1,12 @@
1
+ import { normalizeName } from "./name.mjs";
2
+
1
3
  //#region src/name-registry.ts
4
+ /**
5
+ * NameRegistry Interface and InMemoryNameRegistry Implementation
6
+ *
7
+ * Provides a synchronous interface for name→DID registration and lookup.
8
+ * InMemoryNameRegistry is a Map-based implementation for testing.
9
+ */
2
10
  const MAX_NAME_LENGTH = 1024;
3
11
  /**
4
12
  * In-memory name registry implementation.
@@ -9,21 +17,35 @@ var InMemoryNameRegistry = class {
9
17
  this.store = /* @__PURE__ */ new Map();
10
18
  }
11
19
  resolve(name) {
12
- return this.store.get(name) ?? null;
20
+ try {
21
+ const normalized = normalizeName(name);
22
+ return this.store.get(normalized) ?? null;
23
+ } catch {
24
+ return null;
25
+ }
13
26
  }
14
27
  register(name, did) {
15
28
  if (!name || typeof name !== "string") throw new Error("Name must be a non-empty string");
16
29
  if (name.length > MAX_NAME_LENGTH) throw new Error("Name exceeds maximum length");
17
- if (name.startsWith(".") || name.endsWith(".")) throw new Error("Name cannot start or end with a dot");
30
+ const normalized = normalizeName(name);
31
+ if (normalized.length > MAX_NAME_LENGTH) throw new Error("Name exceeds maximum length");
18
32
  if (!did || typeof did !== "string") throw new Error("DID must be a non-empty string");
19
33
  if (!did.startsWith("did:")) throw new Error("DID must have a valid did: prefix");
20
- this.store.set(name, did);
34
+ this.store.set(normalized, did);
21
35
  }
22
36
  unregister(name) {
23
- this.store.delete(name);
37
+ try {
38
+ const normalized = normalizeName(name);
39
+ this.store.delete(normalized);
40
+ } catch {}
24
41
  }
25
42
  has(name) {
26
- return this.store.has(name);
43
+ try {
44
+ const normalized = normalizeName(name);
45
+ return this.store.has(normalized);
46
+ } catch {
47
+ return false;
48
+ }
27
49
  }
28
50
  list() {
29
51
  return [...this.store.keys()];
@@ -1,4 +1,4 @@
1
- import { getResolveRoute } from "./name.mjs";
1
+ import { getResolveRoute, normalizeName } from "./name.mjs";
2
2
 
3
3
  //#region src/name-resolver.ts
4
4
  /**
@@ -36,12 +36,14 @@ var DIDNameResolver = class {
36
36
  */
37
37
  async resolveWithTrace(name) {
38
38
  if (!name || typeof name !== "string") throw new Error("Name must be a non-empty string");
39
- const cleanName = name.replace(/^did:name:/, "");
40
- if (!cleanName) throw new Error("Name cannot be empty after stripping prefix");
39
+ const rawName = name.replace(/^did:name:/i, "");
40
+ if (!rawName) throw new Error("Name cannot be empty after stripping prefix");
41
+ const cleanName = normalizeName(rawName);
41
42
  const visited = /* @__PURE__ */ new Set();
42
43
  return this._resolve(cleanName, visited, [], 0);
43
44
  }
44
- async _resolve(name, visited, trace, depth) {
45
+ async _resolve(rawName, visited, trace, depth) {
46
+ const name = normalizeName(rawName);
45
47
  if (visited.has(name)) {
46
48
  trace.push(`cycle_detected:${name}`);
47
49
  return {
@@ -67,13 +69,7 @@ var DIDNameResolver = class {
67
69
  if (this.global) resolved = this.global.resolve(route.name);
68
70
  } else {
69
71
  trace.push(`delegated:${name}`);
70
- if (this.global) {
71
- resolved = this.global.resolve(name);
72
- if (!resolved) {
73
- const tldResult = this.global.resolve(route.tld);
74
- if (tldResult) resolved = tldResult;
75
- }
76
- }
72
+ if (this.global) resolved = this.global.resolve(name);
77
73
  }
78
74
  if (!resolved) {
79
75
  trace.push(`not_found:${name}`);
@@ -82,8 +78,8 @@ var DIDNameResolver = class {
82
78
  trace
83
79
  };
84
80
  }
85
- if (resolved.startsWith("did:name:")) {
86
- const nextName = resolved.replace(/^did:name:/, "");
81
+ if (/^did:name:/i.test(resolved)) {
82
+ const nextName = resolved.replace(/^did:name:/i, "");
87
83
  return this._resolve(nextName, visited, trace, depth + 1);
88
84
  }
89
85
  return {
package/esm/name.d.mts CHANGED
@@ -30,6 +30,12 @@ type ResolveRoute = {
30
30
  tld: string;
31
31
  subName: string;
32
32
  };
33
+ /**
34
+ * Normalize and validate a DID name identifier using NFKC + lowercase canonicalization.
35
+ * Rejects control characters, zero-width characters, leading/trailing whitespace,
36
+ * and mixed-script or pure-homoglyph confusable labels.
37
+ */
38
+ declare function normalizeName(name: unknown): string;
33
39
  /**
34
40
  * Parse a name identifier into its hierarchy components.
35
41
  *
@@ -59,4 +65,4 @@ declare function isLocalName(name: string): boolean;
59
65
  */
60
66
  declare function isGlobalName(name: string): boolean;
61
67
  //#endregion
62
- export { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy };
68
+ export { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy };
package/esm/name.mjs CHANGED
@@ -1,4 +1,34 @@
1
1
  //#region src/name.ts
2
+ const INVALID_NAME_CHARS = /[\u0000-\u001F\u007F-\u009F\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF]/;
3
+ const CONFUSABLE_HOMOGLYPH_ONLY = /^[\u0430\u0441\u0435\u0456\u0458\u043E\u0440\u0455\u0445\u0443\u03B1\u03B2\u03B5\u03B9\u03BA\u03BD\u03BF\u03C1\u03C4\u03C5\u03C70-9_-]+$/u;
4
+ function validateSegmentScripts(segment) {
5
+ const hasLatin = /[a-z]/i.test(segment);
6
+ const hasCyrillic = /[\u0400-\u04FF]/.test(segment);
7
+ const hasGreek = /[\u0370-\u03FF]/.test(segment);
8
+ if (hasLatin && (hasCyrillic || hasGreek) || hasCyrillic && hasGreek) throw new Error("Name segment contains mixed-script confusable characters");
9
+ if ((hasCyrillic || hasGreek) && CONFUSABLE_HOMOGLYPH_ONLY.test(segment)) throw new Error("Name segment contains confusable homograph characters");
10
+ }
11
+ /**
12
+ * Normalize and validate a DID name identifier using NFKC + lowercase canonicalization.
13
+ * Rejects control characters, zero-width characters, leading/trailing whitespace,
14
+ * and mixed-script or pure-homoglyph confusable labels.
15
+ */
16
+ function normalizeName(name) {
17
+ if (typeof name !== "string") throw new Error("Name must be a string");
18
+ if (!name) throw new Error("Name cannot be empty");
19
+ if (name !== name.trim()) throw new Error("Name cannot have leading or trailing whitespace");
20
+ if (INVALID_NAME_CHARS.test(name)) throw new Error("Name contains invalid control or zero-width characters");
21
+ const normalized = name.normalize("NFKC").toLowerCase();
22
+ if (!normalized) throw new Error("Name cannot be empty");
23
+ if (INVALID_NAME_CHARS.test(normalized)) throw new Error("Name contains invalid control or zero-width characters");
24
+ if (normalized.startsWith(".")) throw new Error("Name cannot start with a dot");
25
+ if (normalized.endsWith(".")) throw new Error("Name cannot end with a dot");
26
+ if (normalized.includes("..")) throw new Error("Name cannot contain consecutive dots");
27
+ const segments = normalized.split(".");
28
+ if (segments.some((s) => !s)) throw new Error("Name contains empty segments");
29
+ for (const segment of segments) validateSegmentScripts(segment);
30
+ return normalized;
31
+ }
2
32
  /**
3
33
  * Parse a name identifier into its hierarchy components.
4
34
  *
@@ -7,13 +37,7 @@
7
37
  * @throws If name is invalid
8
38
  */
9
39
  function parseNameHierarchy(name) {
10
- if (typeof name !== "string") throw new Error("Name must be a string");
11
- if (!name) throw new Error("Name cannot be empty");
12
- if (name.startsWith(".")) throw new Error("Name cannot start with a dot");
13
- if (name.endsWith(".")) throw new Error("Name cannot end with a dot");
14
- if (name.includes("..")) throw new Error("Name cannot contain consecutive dots");
15
- const segments = name.split(".");
16
- if (segments.some((s) => !s)) throw new Error("Name contains empty segments");
40
+ const segments = normalizeName(name).split(".");
17
41
  const tld = segments[segments.length - 1];
18
42
  const isLocal = tld === "local";
19
43
  const isGlobal = segments.length === 1 && !isLocal;
@@ -45,7 +69,7 @@ function getResolveRoute(name) {
45
69
  };
46
70
  if (hierarchy.isGlobal) return {
47
71
  type: "global",
48
- name
72
+ name: hierarchy.segments[0]
49
73
  };
50
74
  const subName = hierarchy.segments.slice(0, -1).join(".");
51
75
  return {
@@ -58,16 +82,24 @@ function getResolveRoute(name) {
58
82
  * Check if a name is a .local name.
59
83
  */
60
84
  function isLocalName(name) {
61
- if (!name || !name.includes(".")) return false;
62
- return name.endsWith(".local");
85
+ if (!name || typeof name !== "string" || !name.includes(".")) return false;
86
+ try {
87
+ return parseNameHierarchy(name).isLocal;
88
+ } catch {
89
+ return false;
90
+ }
63
91
  }
64
92
  /**
65
93
  * Check if a name is a global name (single segment, not .local).
66
94
  */
67
95
  function isGlobalName(name) {
68
- if (!name) return false;
69
- return !name.includes(".");
96
+ if (!name || typeof name !== "string") return false;
97
+ try {
98
+ return parseNameHierarchy(name).isGlobal;
99
+ } catch {
100
+ return false;
101
+ }
70
102
  }
71
103
 
72
104
  //#endregion
73
- export { getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy };
105
+ export { getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy };
package/esm/util.mjs CHANGED
@@ -2,6 +2,7 @@ import { fromBase58, isHexStrict, stripHexPrefix, toBN } from "@ocap/util";
2
2
 
3
3
  //#region src/util.ts
4
4
  const DID_PREFIX = "did:abt:";
5
+ const MAX_DID_INPUT_LENGTH = 128;
5
6
  /**
6
7
  * Convert did to bytes with length of 26
7
8
  *
@@ -10,6 +11,7 @@ const DID_PREFIX = "did:abt:";
10
11
  */
11
12
  const toBytes = (did) => {
12
13
  try {
14
+ if (did.length > MAX_DID_INPUT_LENGTH) throw new Error(`DID string exceeds max length of ${MAX_DID_INPUT_LENGTH} chars (got ${did.length})`);
13
15
  if (isHexStrict(did)) return Buffer.from(stripHexPrefix(did), "hex");
14
16
  let bytes = fromBase58(did.replace(/^did:[a-z0-9]+:/, ""));
15
17
  while (bytes.length < 26) bytes = Buffer.concat([Uint8Array.from([0]), Uint8Array.from(bytes)]);
package/lib/entity.cjs CHANGED
@@ -1,3 +1,5 @@
1
+ const require_method = require('./method.cjs');
2
+ const require_name = require('./name.cjs');
1
3
  const require_parse = require('./parse.cjs');
2
4
 
3
5
  //#region src/entity.ts
@@ -19,9 +21,11 @@ function getEntityId(did) {
19
21
  }
20
22
  /**
21
23
  * Check if two DIDs represent the same entity.
22
- * Compares the identifier portion (case-insensitive), ignoring the method prefix.
24
+ * Compares the identifier portion, ignoring the crypto method prefix.
23
25
  *
24
- * For did:name DIDs, performs exact string comparison (names are case-insensitive).
26
+ * - For alias methods (e.g. did:name), requires both to be alias DIDs and compares NFC-normalized case-insensitively.
27
+ * - For hex DIDs (0x...), compares case-insensitively.
28
+ * - For Base58btc DIDs, compares case-sensitively.
25
29
  *
26
30
  * @param a - First DID or address
27
31
  * @param b - Second DID or address
@@ -29,10 +33,21 @@ function getEntityId(did) {
29
33
  */
30
34
  function isSameEntity(a, b) {
31
35
  if (typeof a !== "string" || typeof b !== "string" || !a || !b) return false;
36
+ const methodA = require_parse.extractMethod(a);
37
+ const methodB = require_parse.extractMethod(b);
38
+ const aIsAlias = methodA !== null && require_method.isAliasMethod(methodA);
39
+ const bIsAlias = methodB !== null && require_method.isAliasMethod(methodB);
40
+ if (aIsAlias !== bIsAlias) return false;
32
41
  const idA = require_parse.extractIdentifier(a);
33
42
  const idB = require_parse.extractIdentifier(b);
34
43
  if (!idA || !idB) return false;
35
- return idA.toLowerCase() === idB.toLowerCase();
44
+ if (aIsAlias && bIsAlias) try {
45
+ return require_name.normalizeName(idA) === require_name.normalizeName(idB);
46
+ } catch {
47
+ return false;
48
+ }
49
+ if (/^0x[0-9a-fA-F]+$/i.test(idA) && /^0x[0-9a-fA-F]+$/i.test(idB)) return idA.toLowerCase() === idB.toLowerCase();
50
+ return idA === idB;
36
51
  }
37
52
 
38
53
  //#endregion
package/lib/entity.d.cts CHANGED
@@ -15,9 +15,11 @@
15
15
  declare function getEntityId(did: string): string;
16
16
  /**
17
17
  * Check if two DIDs represent the same entity.
18
- * Compares the identifier portion (case-insensitive), ignoring the method prefix.
18
+ * Compares the identifier portion, ignoring the crypto method prefix.
19
19
  *
20
- * For did:name DIDs, performs exact string comparison (names are case-insensitive).
20
+ * - For alias methods (e.g. did:name), requires both to be alias DIDs and compares NFC-normalized case-insensitively.
21
+ * - For hex DIDs (0x...), compares case-insensitively.
22
+ * - For Base58btc DIDs, compares case-sensitively.
21
23
  *
22
24
  * @param a - First DID or address
23
25
  * @param b - Second DID or address
package/lib/index.cjs CHANGED
@@ -1,11 +1,11 @@
1
1
  const require_method = require('./method.cjs');
2
+ const require_name = require('./name.cjs');
2
3
  const require_parse = require('./parse.cjs');
3
4
  const require_entity = require('./entity.cjs');
4
5
  const require_util = require('./util.cjs');
5
6
  const require_type = require('./type.cjs');
6
7
  const require_validate = require('./validate.cjs');
7
8
  const require_short_form = require('./short-form.cjs');
8
- const require_name = require('./name.cjs');
9
9
  const require_name_registry = require('./name-registry.cjs');
10
10
  const require_name_resolver = require('./name-resolver.cjs');
11
11
  let _ocap_mcrypto = require("@ocap/mcrypto");
@@ -95,6 +95,7 @@ const isValid = (did) => {
95
95
  if (require_type.isEthereumDid(address)) return true;
96
96
  const hashFn = (0, _ocap_mcrypto.getHasher)(hash);
97
97
  const bytes = require_util.toBytes(address);
98
+ if (bytes.length !== 26) return false;
98
99
  const bytesHex = require_util.toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(0, 22))).toString("hex"));
99
100
  return require_util.toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(22, 26))).toString("hex")) === (0, _ocap_util.stripHexPrefix)(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
100
101
  };
@@ -133,6 +134,7 @@ exports.isKnownMethod = require_method.isKnownMethod;
133
134
  exports.isLocalName = require_name.isLocalName;
134
135
  exports.isSameEntity = require_entity.isSameEntity;
135
136
  exports.isValid = isValid;
137
+ exports.normalizeName = require_name.normalizeName;
136
138
  exports.parse = require_parse.parse;
137
139
  exports.parseNameHierarchy = require_name.parseNameHierarchy;
138
140
  exports.toAddress = _ocap_util.toAddress;
package/lib/index.d.cts CHANGED
@@ -5,7 +5,7 @@ import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod,
5
5
  import { ParsedDID, extractIdentifier, extractMethod, formatDid, parse } from "./parse.cjs";
6
6
  import { isKnownDid } from "./validate.cjs";
7
7
  import { expandShortForm, toShortForm } from "./short-form.cjs";
8
- import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy } from "./name.cjs";
8
+ import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy } from "./name.cjs";
9
9
  import { InMemoryNameRegistry, NameRegistry } from "./name-registry.cjs";
10
10
  import { DIDNameResolver, DIDNameResolverOptions, ResolveTraceResult } from "./name-resolver.cjs";
11
11
  import { types } from "@ocap/mcrypto";
@@ -66,4 +66,4 @@ declare const isFromPublicKey: (did: string, pk: BytesType) => boolean;
66
66
  */
67
67
  declare const isValid: (did: string) => boolean;
68
68
  //#endregion
69
- export { ALIAS_METHODS, ALL_METHODS, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_METHOD, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, type NameHierarchy, type NameRegistry, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
69
+ export { ALIAS_METHODS, ALL_METHODS, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_METHOD, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, type NameHierarchy, type NameRegistry, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, normalizeName, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
@@ -1,5 +1,12 @@
1
+ const require_name = require('./name.cjs');
1
2
 
2
3
  //#region src/name-registry.ts
4
+ /**
5
+ * NameRegistry Interface and InMemoryNameRegistry Implementation
6
+ *
7
+ * Provides a synchronous interface for name→DID registration and lookup.
8
+ * InMemoryNameRegistry is a Map-based implementation for testing.
9
+ */
3
10
  const MAX_NAME_LENGTH = 1024;
4
11
  /**
5
12
  * In-memory name registry implementation.
@@ -10,21 +17,35 @@ var InMemoryNameRegistry = class {
10
17
  this.store = /* @__PURE__ */ new Map();
11
18
  }
12
19
  resolve(name) {
13
- return this.store.get(name) ?? null;
20
+ try {
21
+ const normalized = require_name.normalizeName(name);
22
+ return this.store.get(normalized) ?? null;
23
+ } catch {
24
+ return null;
25
+ }
14
26
  }
15
27
  register(name, did) {
16
28
  if (!name || typeof name !== "string") throw new Error("Name must be a non-empty string");
17
29
  if (name.length > MAX_NAME_LENGTH) throw new Error("Name exceeds maximum length");
18
- if (name.startsWith(".") || name.endsWith(".")) throw new Error("Name cannot start or end with a dot");
30
+ const normalized = require_name.normalizeName(name);
31
+ if (normalized.length > MAX_NAME_LENGTH) throw new Error("Name exceeds maximum length");
19
32
  if (!did || typeof did !== "string") throw new Error("DID must be a non-empty string");
20
33
  if (!did.startsWith("did:")) throw new Error("DID must have a valid did: prefix");
21
- this.store.set(name, did);
34
+ this.store.set(normalized, did);
22
35
  }
23
36
  unregister(name) {
24
- this.store.delete(name);
37
+ try {
38
+ const normalized = require_name.normalizeName(name);
39
+ this.store.delete(normalized);
40
+ } catch {}
25
41
  }
26
42
  has(name) {
27
- return this.store.has(name);
43
+ try {
44
+ const normalized = require_name.normalizeName(name);
45
+ return this.store.has(normalized);
46
+ } catch {
47
+ return false;
48
+ }
28
49
  }
29
50
  list() {
30
51
  return [...this.store.keys()];
@@ -36,12 +36,14 @@ var DIDNameResolver = class {
36
36
  */
37
37
  async resolveWithTrace(name) {
38
38
  if (!name || typeof name !== "string") throw new Error("Name must be a non-empty string");
39
- const cleanName = name.replace(/^did:name:/, "");
40
- if (!cleanName) throw new Error("Name cannot be empty after stripping prefix");
39
+ const rawName = name.replace(/^did:name:/i, "");
40
+ if (!rawName) throw new Error("Name cannot be empty after stripping prefix");
41
+ const cleanName = require_name.normalizeName(rawName);
41
42
  const visited = /* @__PURE__ */ new Set();
42
43
  return this._resolve(cleanName, visited, [], 0);
43
44
  }
44
- async _resolve(name, visited, trace, depth) {
45
+ async _resolve(rawName, visited, trace, depth) {
46
+ const name = require_name.normalizeName(rawName);
45
47
  if (visited.has(name)) {
46
48
  trace.push(`cycle_detected:${name}`);
47
49
  return {
@@ -67,13 +69,7 @@ var DIDNameResolver = class {
67
69
  if (this.global) resolved = this.global.resolve(route.name);
68
70
  } else {
69
71
  trace.push(`delegated:${name}`);
70
- if (this.global) {
71
- resolved = this.global.resolve(name);
72
- if (!resolved) {
73
- const tldResult = this.global.resolve(route.tld);
74
- if (tldResult) resolved = tldResult;
75
- }
76
- }
72
+ if (this.global) resolved = this.global.resolve(name);
77
73
  }
78
74
  if (!resolved) {
79
75
  trace.push(`not_found:${name}`);
@@ -82,8 +78,8 @@ var DIDNameResolver = class {
82
78
  trace
83
79
  };
84
80
  }
85
- if (resolved.startsWith("did:name:")) {
86
- const nextName = resolved.replace(/^did:name:/, "");
81
+ if (/^did:name:/i.test(resolved)) {
82
+ const nextName = resolved.replace(/^did:name:/i, "");
87
83
  return this._resolve(nextName, visited, trace, depth + 1);
88
84
  }
89
85
  return {
package/lib/name.cjs CHANGED
@@ -1,5 +1,35 @@
1
1
 
2
2
  //#region src/name.ts
3
+ const INVALID_NAME_CHARS = /[\u0000-\u001F\u007F-\u009F\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF]/;
4
+ const CONFUSABLE_HOMOGLYPH_ONLY = /^[\u0430\u0441\u0435\u0456\u0458\u043E\u0440\u0455\u0445\u0443\u03B1\u03B2\u03B5\u03B9\u03BA\u03BD\u03BF\u03C1\u03C4\u03C5\u03C70-9_-]+$/u;
5
+ function validateSegmentScripts(segment) {
6
+ const hasLatin = /[a-z]/i.test(segment);
7
+ const hasCyrillic = /[\u0400-\u04FF]/.test(segment);
8
+ const hasGreek = /[\u0370-\u03FF]/.test(segment);
9
+ if (hasLatin && (hasCyrillic || hasGreek) || hasCyrillic && hasGreek) throw new Error("Name segment contains mixed-script confusable characters");
10
+ if ((hasCyrillic || hasGreek) && CONFUSABLE_HOMOGLYPH_ONLY.test(segment)) throw new Error("Name segment contains confusable homograph characters");
11
+ }
12
+ /**
13
+ * Normalize and validate a DID name identifier using NFKC + lowercase canonicalization.
14
+ * Rejects control characters, zero-width characters, leading/trailing whitespace,
15
+ * and mixed-script or pure-homoglyph confusable labels.
16
+ */
17
+ function normalizeName(name) {
18
+ if (typeof name !== "string") throw new Error("Name must be a string");
19
+ if (!name) throw new Error("Name cannot be empty");
20
+ if (name !== name.trim()) throw new Error("Name cannot have leading or trailing whitespace");
21
+ if (INVALID_NAME_CHARS.test(name)) throw new Error("Name contains invalid control or zero-width characters");
22
+ const normalized = name.normalize("NFKC").toLowerCase();
23
+ if (!normalized) throw new Error("Name cannot be empty");
24
+ if (INVALID_NAME_CHARS.test(normalized)) throw new Error("Name contains invalid control or zero-width characters");
25
+ if (normalized.startsWith(".")) throw new Error("Name cannot start with a dot");
26
+ if (normalized.endsWith(".")) throw new Error("Name cannot end with a dot");
27
+ if (normalized.includes("..")) throw new Error("Name cannot contain consecutive dots");
28
+ const segments = normalized.split(".");
29
+ if (segments.some((s) => !s)) throw new Error("Name contains empty segments");
30
+ for (const segment of segments) validateSegmentScripts(segment);
31
+ return normalized;
32
+ }
3
33
  /**
4
34
  * Parse a name identifier into its hierarchy components.
5
35
  *
@@ -8,13 +38,7 @@
8
38
  * @throws If name is invalid
9
39
  */
10
40
  function parseNameHierarchy(name) {
11
- if (typeof name !== "string") throw new Error("Name must be a string");
12
- if (!name) throw new Error("Name cannot be empty");
13
- if (name.startsWith(".")) throw new Error("Name cannot start with a dot");
14
- if (name.endsWith(".")) throw new Error("Name cannot end with a dot");
15
- if (name.includes("..")) throw new Error("Name cannot contain consecutive dots");
16
- const segments = name.split(".");
17
- if (segments.some((s) => !s)) throw new Error("Name contains empty segments");
41
+ const segments = normalizeName(name).split(".");
18
42
  const tld = segments[segments.length - 1];
19
43
  const isLocal = tld === "local";
20
44
  const isGlobal = segments.length === 1 && !isLocal;
@@ -46,7 +70,7 @@ function getResolveRoute(name) {
46
70
  };
47
71
  if (hierarchy.isGlobal) return {
48
72
  type: "global",
49
- name
73
+ name: hierarchy.segments[0]
50
74
  };
51
75
  const subName = hierarchy.segments.slice(0, -1).join(".");
52
76
  return {
@@ -59,19 +83,28 @@ function getResolveRoute(name) {
59
83
  * Check if a name is a .local name.
60
84
  */
61
85
  function isLocalName(name) {
62
- if (!name || !name.includes(".")) return false;
63
- return name.endsWith(".local");
86
+ if (!name || typeof name !== "string" || !name.includes(".")) return false;
87
+ try {
88
+ return parseNameHierarchy(name).isLocal;
89
+ } catch {
90
+ return false;
91
+ }
64
92
  }
65
93
  /**
66
94
  * Check if a name is a global name (single segment, not .local).
67
95
  */
68
96
  function isGlobalName(name) {
69
- if (!name) return false;
70
- return !name.includes(".");
97
+ if (!name || typeof name !== "string") return false;
98
+ try {
99
+ return parseNameHierarchy(name).isGlobal;
100
+ } catch {
101
+ return false;
102
+ }
71
103
  }
72
104
 
73
105
  //#endregion
74
106
  exports.getResolveRoute = getResolveRoute;
75
107
  exports.isGlobalName = isGlobalName;
76
108
  exports.isLocalName = isLocalName;
109
+ exports.normalizeName = normalizeName;
77
110
  exports.parseNameHierarchy = parseNameHierarchy;
package/lib/name.d.cts CHANGED
@@ -30,6 +30,12 @@ type ResolveRoute = {
30
30
  tld: string;
31
31
  subName: string;
32
32
  };
33
+ /**
34
+ * Normalize and validate a DID name identifier using NFKC + lowercase canonicalization.
35
+ * Rejects control characters, zero-width characters, leading/trailing whitespace,
36
+ * and mixed-script or pure-homoglyph confusable labels.
37
+ */
38
+ declare function normalizeName(name: unknown): string;
33
39
  /**
34
40
  * Parse a name identifier into its hierarchy components.
35
41
  *
@@ -59,4 +65,4 @@ declare function isLocalName(name: string): boolean;
59
65
  */
60
66
  declare function isGlobalName(name: string): boolean;
61
67
  //#endregion
62
- export { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy };
68
+ export { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy };
package/lib/util.cjs CHANGED
@@ -2,6 +2,7 @@ let _ocap_util = require("@ocap/util");
2
2
 
3
3
  //#region src/util.ts
4
4
  const DID_PREFIX = "did:abt:";
5
+ const MAX_DID_INPUT_LENGTH = 128;
5
6
  /**
6
7
  * Convert did to bytes with length of 26
7
8
  *
@@ -10,6 +11,7 @@ const DID_PREFIX = "did:abt:";
10
11
  */
11
12
  const toBytes = (did) => {
12
13
  try {
14
+ if (did.length > MAX_DID_INPUT_LENGTH) throw new Error(`DID string exceeds max length of ${MAX_DID_INPUT_LENGTH} chars (got ${did.length})`);
13
15
  if ((0, _ocap_util.isHexStrict)(did)) return Buffer.from((0, _ocap_util.stripHexPrefix)(did), "hex");
14
16
  let bytes = (0, _ocap_util.fromBase58)(did.replace(/^did:[a-z0-9]+:/, ""));
15
17
  while (bytes.length < 26) bytes = Buffer.concat([Uint8Array.from([0]), Uint8Array.from(bytes)]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/did",
3
- "version": "1.30.23",
3
+ "version": "1.30.25",
4
4
  "type": "module",
5
5
  "description": "Javascript lib to work with ArcBlock DID",
6
6
  "keywords": [
@@ -69,8 +69,8 @@
69
69
  "url": "https://github.com/ArcBlock/blockchain/issues"
70
70
  },
71
71
  "dependencies": {
72
- "@ocap/mcrypto": "1.30.23",
73
- "@ocap/util": "1.30.23",
72
+ "@ocap/mcrypto": "1.30.25",
73
+ "@ocap/util": "1.30.25",
74
74
  "debug": "^4.4.3"
75
75
  }
76
76
  }