Decentralized Identifiers (DID) JavaScript Library

website·Indexed 31 days ago

https://did.js.org/

A collection of libraries for managing Decentralized Identifiers (DIDs) in web-based environments. It provides tools for DID resolution, session management via did-session, and authentication support for multiple blockchain networks including Ethereum, Solana, Tezos, and Stacks. The ecosystem includes specialized providers for secp256k1, ed25519, and Webcrypto, as well as support for EIP2844, SIWX, and Multidid.

Tokens
13.6K
Snippets
121
Records
138
Agent score
56%

What's inside did.js

  1. Access DIDSession state and properties

    Use the following accessors to retrieve information about the current session state:
    const resources = session.authorizations; // string[]: List of authorized resources
    const cacao = session.cacao;               // Cacao: The session CACAO
    const did = session.did;                   // DID: The DID instance (if authorized)
    const expiry = session.expireInSecs;       // number: Seconds until expiration
    const expired = session.isExpired;          // boolean: True if session is expired
    const sessionId = session.id;               // string: DID string associated with the session
    const active = session.hasSession;          // boolean: Whether a session is active
  2. Understand User Session and Permission Model

    The system implements user sessions tied to existing wallet addresses. It utilizes object capabilities to provide granular permissions for secure data access control. The architecture supports multi-chain environments, meaning accounts and wallets are not limited to Ethereum.
  3. Install @didtools/codecs

    Install the @didtools/codecs package via npm to access common codecs for encoding, decoding, and validating DID-related types.
    npm install @didtools/codecs
  4. Verify did:pkh signed payloads using Stacks and CACAO

    To verify signed payloads using CACAO, use getStacksVerifier() to retrieve the necessary verifiers and register them in the verifiers map passed to the CACAO or DIDS verification methods.
    import { Cacao } from '@didtools/cacao'
    import { getStacksVerifier } from '@didtools/pkh-stacks'
    import { DID } from 'dids'
    
    const verifiers = {
      ...getStacksVerifier(),
    }
    
    // Verify directly with Cacao
    Cacao.verify(cacao, { verifiers, ...opts })
    
    // Verify with DIDS instance
    const dids = // configured dids instance
    await dids.verifyJWS(jws, { capability, verifiers, ...opts })
  5. Install @didtools/pkh-tezos

    Install the Tezos PKH support library via npm to enable Tezos account authentication and verification for did:pkh identifiers.
    npm install --save @didtools/pkh-tezos
  6. Migrate from did-session v0.x.x to v1.x.x

    AuthProviders have been replaced by AuthMethod interfaces. You must now use specific auth libraries (like @didtools/pkh-ethereum) to generate an AuthMethod function to pass into DIDSession.get().
    // Before (v0.x.x)
    import { EthereumAuthProvider } from '@ceramicnetwork/blockchain-utils-linking'
    const authProvider = new EthereumAuthProvider(ethProvider, addresses[0])
    const session = new DIDSession({ authProvider })
    const did = await session.authorize()
    
    // After (v1.x.x)
    import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
    const accountId = await getAccountId(ethProvider, addresses[0])
    const authMethod = await EthereumWebAuth.getAuthMethod(ethProvider, accountId)
    const session = await DIDSession.get(accountId, authMethod, { resources: [...]})
    const did = session.did
  7. Create a SiwxMessage instance

    The SiwxMessage class is used to represent a SIWE message. It implements the SiwxMessageFields interface. You can instantiate it by passing a fields object containing the required SIWE message parameters.
    const message = new SiwxMessage(fields);
  8. Authenticate Stacks accounts in a web environment

    Use StacksWebAuth.getAuthMethod with an injected web3 provider and a CAIP10-compliant AccountId to create an authentication method. This method can then be used with DIDSession.authorize to request access to specific resources.
    import { StacksWebAuth, getAccountIdByNetwork } from '@didtools/pkh-stacks'
    import { AppConfig, UserSession } from '@stacks/connect'
    
    const stacksProvider = window.StacksProvider
    const appConfig = new AppConfig(['store_write'])
    const userSession = new UserSession({ appConfig })
    
    const userData = userSession.loadUserData()
    const address = userData.profile.stxAddress.mainnet
    
    // Get CAIP10 AccountId
    const accountId = await getAccountIdByNetwork('mainnet', address)
    
    // Create the AuthMethod
    const authMethod = await StacksWebAuth.getAuthMethod(stacksProvider, accountId, publicKey)
    
    // Use with did-session
    const session = await DIDSession.authorize(authMethod, { resources: ['ceramic://*'] })