Identity SDK Reference
Canonical function reference for:
@oma3/omatrust/identity
Core Types
type Hex = `0x${string}`;
type Did = string;
type Caip10 = string;
Error Handling
Identity functions throw OmaTrustError (extends Error) with a stable code property.
class OmaTrustError extends Error {
code: string;
details?: unknown;
}
| Code | Thrown by | Description |
|---|---|---|
INVALID_INPUT | All functions receiving malformed params | Missing required fields, wrong types |
INVALID_DID | normalizeDid, normalizeDidWeb, normalizeDidPkh, normalizeDidHandle, normalizeDidKey, computeDidHash, didToAddress, validateDidAddress, all get*FromDid* | String is not a valid DID or wrong method |
INVALID_CAIP | parseCaip10, normalizeCaip10, parseCaip2, buildDidPkhFromCaip10 | String is not a valid CAIP-10 or CAIP-2 |
DID Utilities
Most developers should use normalizeDid, didToAddress, and the CAIP builders/parsers. All DID functions are pure local computation — no RPC calls.
Validation
isValidDid(did: string): boolean— Check if a string matches the basic DID format (did:method:identifier). Does not validate method-specific rules.extractDidMethod(did: Did): string— Extract the method from a DID (e.g.,"web"from"did:web:example.com"). Returnsnullif not a valid DID.extractDidIdentifier(did: Did): string— Extract the identifier portion (e.g.,"example.com"from"did:web:example.com"). Returnsnullif not a valid DID.
Normalization
normalizeDid(input: string): Did— Normalize any DID to its canonical form. Routes to the appropriate method-specific normalizer. Bare domains (nodid:prefix) are treated asdid:web. ThrowsINVALID_DIDfor malformed input.normalizeDidWeb(input: string): Did— Normalize adid:webDID. Lowercases the host, preserves path case. ThrowsINVALID_DIDif input is a non-web DID method.normalizeDidPkh(input: string): Did— Normalize adid:pkhDID. Lowercases the address component per CAIP-10 canonical form. Expects formatdid:pkh:namespace:chainId:address. ThrowsINVALID_DID.normalizeDidHandle(input: string): Did— Normalize adid:handleDID. Lowercases the platform, preserves username case (platform-defined). Expects formatdid:handle:platform:username. ThrowsINVALID_DID.normalizeDidKey(input: string): Did— Normalize adid:keyDID. Returns as-is (multibase encoding is case-sensitive). ThrowsINVALID_DID.normalizeDomain(domain: string): string— Normalize a domain name: lowercase, strip trailing dot.
Hashing and Address
computeDidHash(did: Did): Hex— Compute the keccak256 hash of a normalized DID. Normalizes the DID first, then hashes the UTF-8 bytes. Returns a 32-byte hex string. ThrowsINVALID_DID.computeDidAddress(didHash: Hex): Hex— Compute a DID Address from a DID hash by taking the last 20 bytes (simple truncation per OMATrust spec §5.3.2). Returns lowercase0x-prefixed hex (no EIP-55 checksum casing). This is not a real wallet address — it's a derived lookup key for EAS attestation indexing.didToAddress(did: Did): Hex— Convenience: normalize a DID, hash it, and return the DID Address in one call (lowercase0x-prefixed hex). ThrowsINVALID_DID.validateDidAddress(did: Did, address: Hex): boolean— Verify that a DID Address was computed correctly for a given DID. Returnsfalseon any error.
DID Builders
buildDidWeb(domain: string): Did— Build adid:webDID from a domain name (e.g.,"example.com"→"did:web:example.com"). Normalizes the domain.buildDidPkh(namespace: string, chainId: string | number, address: string): Did— Build adid:pkhDID from components (e.g.,"eip155",1,"0x..."→"did:pkh:eip155:1:0x..."). Lowercases the address.buildEvmDidPkh(chainId: string | number, address: string): Did— Convenience: build adid:pkhwitheip155namespace.buildDidPkhFromCaip10(caip10: Caip10): Did— Build adid:pkhfrom a CAIP-10 string (e.g.,"eip155:1:0x..."→"did:pkh:eip155:1:0x..."). ThrowsINVALID_CAIP.
DID Parsers
-
getChainIdFromDidPkh(did: Did): string— Extract the chain ID from adid:pkhDID. Returnsnullif not a validdid:pkh. Returns the CAIP chain reference as astring; parse tonumberonly for EVM (eip155) contexts.const chainRef = getChainIdFromDidPkh(did);
const chainId = Number(chainRef); // EVM-only -
getAddressFromDidPkh(did: Did): string— Extract the address from adid:pkhDID. Returnsnullif not a validdid:pkh. -
getNamespaceFromDidPkh(did: Did): string— Extract the namespace (e.g.,"eip155") from adid:pkhDID. Returnsnullif not a validdid:pkh. -
isEvmDidPkh(did: Did): boolean— Check if adid:pkhDID uses theeip155(EVM) namespace. -
getDomainFromDidWeb(did: Did): string— Extract the domain from adid:webDID (e.g.,"did:web:example.com/path"→"example.com"). Returnsnullif not a validdid:web. -
extractAddressFromDid(identifier: string): string— Extract an Ethereum address from a DID, CAIP-10 string, or raw address. Handlesdid:pkh, CAIP-10, and plain0xaddresses.
CAIP Utilities
CAIP-10 identifies a blockchain account as namespace:reference:address (e.g., eip155:1:0x...). CAIP-2 identifies a chain as namespace:reference (e.g., eip155:1).
parseCaip10(input: string): { namespace: string; reference: string; address: string }— Parse a CAIP-10 string into its three components. ThrowsINVALID_CAIPif the format is invalid.buildCaip10(namespace: string, reference: string, address: string): Caip10— Build a CAIP-10 string from components.normalizeCaip10(input: string): Caip10— Parse, validate, and normalize a CAIP-10 string. Lowercases the address for EVM namespaces. ThrowsINVALID_CAIP.buildCaip2(namespace: string, reference: string): string— Build a CAIP-2 chain identifier string.parseCaip2(caip2: string): { namespace: string; reference: string }— Parse a CAIP-2 string into namespace and reference. ThrowsINVALID_CAIP.
Data Utilities
These functions handle JSON canonicalization (JCS / RFC 8785). They are used in proof seed construction (constructSeed in the reputation module).
canonicalizeJson(obj: unknown): string— JCS canonicalize any JSON-serializable object.canonicalizeForHash(obj: unknown): { jcsJson: string; hash: Hex }— Canonicalize and return both the JCS string and its keccak256 hash.