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.
- Dynamic
authPrincipal
Claim: TheauthPrincipal
claim is a function that receivesextraParams
as an argument. These parameters are populated from the query string of the request URL. - Chain Selection: The function inspects the
type
property withinextraParams
. - 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 achainInfo
object configured for Ethereum (in this case, the Goerli testnet with chain ID5
). - If the request is
/api/connect/multi-chain?type=arcblock
, it returns the configuration for the specified ArcBlock chain.
- If a front-end application initiates a DID Connect session by calling an endpoint like
- Wallet Interaction: The
chainInfo
object instructs the user's wallet which network to connect to. For a complete list ofchainInfo
options, refer to the Auth Principal Claim documentation. - 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. - 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.