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

Multi-Chain Connect


This example demonstrates how to configure a single DID Connect endpoint to handle authentication requests for multiple blockchains, such as ArcBlock and Ethereum. The key is to dynamically generate the authPrincipal claim based on a parameter passed in the request, allowing your application to adapt to different blockchain ecosystems without needing separate endpoints.

Example Implementation#

The following code sets up a DID Connect handler that can initiate a session with either an ArcBlock chain or an Ethereum chain based on a URL query parameter.

const env = require('../../libs/env');

/* eslint-disable no-console */
module.exports = {
action: 'multi-chain',
claims: [
{
authPrincipal: ({ extraParams: { type } }) => {
if (type === 'arcblock') {
return {
description: 'Please sign in with your ArcBlock account',
chainInfo: {
host: env.chainHost,
id: env.chainId,
},
};
}

if (type === 'ethereum') {
return {
description: 'Please sign in with your Ethereum account',
chainInfo: {
type: 'ethereum',
id: '5', // Goerli Testnet
},
};
}

throw new Error('Invalid chain type');
},
},
{
profile: () => {
return {
description: 'Please provide your email',
fields: ['email'],
};
},
},
],

onAuth: ({ claims, userDid }) => {
console.log(claims);
return {
successMessage: `Your selected account is ${userDid}`,
};
},
};

How It Works#

The core of this functionality lies in defining the authPrincipal claim as a function instead of a static object.

  1. Dynamic authPrincipal Claim: The authPrincipal claim is a function that receives extraParams as an argument. These parameters are populated from the query string of the request URL.
  2. Chain Selection: The function inspects the type property within extraParams.
    • If a front-end application initiates a DID Connect session by calling an endpoint like /api/connect/multi-chain?type=ethereum, the function will return a chainInfo object configured for Ethereum (in this case, the Goerli testnet with chain ID 5).
    • If the request is /api/connect/multi-chain?type=arcblock, it returns the configuration for the specified ArcBlock chain.
  3. Wallet Interaction: The chainInfo object instructs the user's wallet which network to connect to. For a complete list of chainInfo options, refer to the Auth Principal Claim documentation.
  4. Sequential Claims: After the user successfully connects their wallet on the specified chain, the session proceeds to the next claim, requesting their email via the profile claim.
  5. Session Completion: Once all claims are fulfilled, the onAuth callback is executed with the user's DID and the collected claim data.

Workflow Diagram#

This diagram illustrates the sequence of events when a user initiates a multi-chain connection.


This pattern provides a flexible way to support users from various blockchain communities within a single, unified authentication flow. To explore requesting on-chain actions after authentication, see the Transaction Payment example.