Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Utilities

DID


The DID utilities within the DID Spaces SDK provide essential functions for manipulating and validating Decentralized Identifiers (DIDs). These utilities are crucial for ensuring DIDs conform to ArcBlock's specific formats and for robust error handling in your applications.

For an overview of all available utilities, refer to the Utilities section.

toABTDid#

This function converts a standard Decentralized Identifier (DID) into an ArcBlock DID (ABT DID) by prepending the did:abt: prefix. It ensures the input DID is valid before conversion.

Parameters

Name

Type

Description

did

string

The standard Decentralized Identifier (DID) to convert.

Returns

Name

Type

Description

string

string

The converted ArcBlock DID, formatted as did:abt:{did}.

Example

import { toABTDid } from '@arcblock/did-spaces-client/libs/did';

async function convertToAbtDid() {
const standardDid = 'did:test:abcde12345';
try {
const abtDid = toABTDid(standardDid);
console.log(`Converted to ABT DID: ${abtDid}`);
// Expected output: Converted to ABT DID: did:abt:did:test:abcde12345

// Example with an invalid DID
const invalidDid = 'invalid-did';
toABTDid(invalidDid); // This will throw an error
} catch (error) {
console.error(`Error converting DID: ${error.message}`);
// Expected output for invalid-did: Error converting DID: did(invalid-did) is not a valid did
}
}

convertToAbtDid();

This example demonstrates how to use toABTDid to convert a valid standard DID into its ArcBlock equivalent. It also shows the error handling for invalid input DIDs.

toDid#

This function converts an ArcBlock DID (ABT DID) back into its standard DID format by removing the did:abt: prefix. It includes logic to handle cases where the input DID might already be a valid standard DID without the ABT prefix, returning it as is. It performs validation at each step.

Parameters

Name

Type

Description

did

string

The ArcBlock DID string or a standard DID string to convert.

Returns

Name

Type

Description

string

string

The standard DID (without the did:abt: prefix).

Example

import { toDid } from '@arcblock/did-spaces-client/libs/did';

async function convertFromAbtDid() {
const abtDid = 'did:abt:did:test:abcde12345';
try {
const standardDid = toDid(abtDid);
console.log(`Converted from ABT DID: ${standardDid}`);
// Expected output: Converted from ABT DID: did:test:abcde12345

// Example with an already standard DID
const alreadyStandardDid = 'did:test:anotherdid';
const result = toDid(alreadyStandardDid);
console.log(`Already standard DID: ${result}`);
// Expected output: Already standard DID: did:test:anotherdid

// Example with an invalid format
const malformedAbtDid = 'did:abt:invalid-part';
toDid(malformedAbtDid); // This will throw an error
} catch (error) {
console.error(`Error converting DID: ${error.message}`);
// Expected output for malformedAbtDid: Error converting DID: did(invalid-part) is not a valid did

// Example with missing prefix
const missingPrefix = 'did:test:invalid'; // This is a valid general DID but not an ABT DID, so it will pass through
try {
const result = toDid(missingPrefix);
console.log(`Missing prefix, but valid DID: ${result}`);
} catch (e) {
console.error(`Error: ${e.message}`);
}

const completelyInvalid = 'not-a-did';
try {
toDid(completelyInvalid);
} catch (e) {
console.error(`Error: ${e.message}`);
// Expected output: Error: The format of did(not-a-did) should be: did:abt:{did}
}
}
}

convertFromAbtDid();

This example showcases the toDid function's ability to extract the standard DID from an ABT DID. It also demonstrates its behavior when handling DIDs that are already in a standard format and various error conditions.


These DID utilities simplify working with DIDs within the ArcBlock ecosystem, ensuring proper formatting and validation. Continue exploring the SDK's capabilities in the HTTP Clients section for network communication utilities.