DID Module

The DID module provides W3C-compatible Decentralized Identifiers (did:bsv:) backed by BSV identity keys. It includes the standalone DID utility class and wallet-integrated methods.

Source: src/modules/did.ts

DID Class

The DID class is a standalone utility — no wallet instance required.

import { DID } from '@bsv/simple/browser'

DID.fromIdentityKey()

static fromIdentityKey(identityKey: string): DIDDocument

Generate a W3C DID Document from a compressed public key.

Parameter
Type
Description

identityKey

string

66-character hex compressed public key

Returns: DIDDocument

Throws: DIDError if the identity key is not a valid 66-character hex string.

const doc = DID.fromIdentityKey('02a1b2c3...')
// {
//   '@context': ['https://www.w3.org/ns/did/v1'],
//   id: 'did:bsv:02a1b2c3...',
//   controller: 'did:bsv:02a1b2c3...',
//   verificationMethod: [{
//     id: 'did:bsv:02a1b2c3...#key-1',
//     type: 'EcdsaSecp256k1VerificationKey2019',
//     controller: 'did:bsv:02a1b2c3...',
//     publicKeyHex: '02a1b2c3...'
//   }],
//   authentication: ['did:bsv:02a1b2c3...#key-1'],
//   assertionMethod: ['did:bsv:02a1b2c3...#key-1']
// }

DID.parse()

Parse a did:bsv: string and extract the identity key.

Parameter
Type
Description

didString

string

A did:bsv: DID string

Returns:

Throws: DIDError if the format is invalid.

DID.isValid()

Validate a did:bsv: string format.

Parameter
Type
Description

didString

string

String to validate

Returns: true if the string is a valid did:bsv: DID, false otherwise.

DID.getCertificateType()

Get the base64-encoded certificate type used for DID persistence.

Returns: Base64 encoding of 'did:bsv'.

Wallet Methods

getDID()

Get this wallet's DID Document. Synchronous — builds the document from the identity key.

resolveDID()

Resolve any did:bsv: string to its DID Document. Synchronous.

Parameter
Type
Description

didString

string

A did:bsv: DID string

Throws: DIDError if the DID format is invalid.

registerDID()

Persist this wallet's DID as a BSV certificate.

Parameter
Type
Default
Description

options.persist

boolean

true

If false, returns the DID Document without persisting

What happens:

  1. Builds the DID Document from the identity key

  2. Creates an ephemeral Certifier with certificateType: DID.getCertificateType()

  3. Issues a certificate containing:

    • didId: The full DID string

    • didType: 'identity'

    • version: '1.0'

    • created: ISO timestamp

    • isDID: 'true'

Throws: DIDError if registration fails.

Last updated