跳到主要內容

入門指南

本指南提供了一個最小化的、逐步的範例,幫助您在 Node.js 應用程式中設定基本的 DID Connect 會話。在本教學結束時,您將擁有一個可以透過用戶的 DID 錢包請求其個人資料資訊的工作伺服器。

1. 安裝

首先,您需要安裝必要的套件。此函式庫設計用於與 Express.js 等 Web 伺服器框架一起使用。您還需要一個儲存適配器來管理會話令牌。

安裝必要的套件

bash
# 使用 npm
npm install express @arcblock/did-connect-js @arcblock/did-connect-storage-nedb @ocap/wallet

# 使用 pnpm
pnpm install express @arcblock/did-connect-js @arcblock/did-connect-storage-nedb @ocap/wallet
  • express:Web 伺服器框架。
  • @arcblock/did-connect-js:處理 DID Connect 會話的主要函式庫。
  • @arcblock/did-connect-storage-nedb:一個用於會話令牌的簡單基於檔案的儲存適配器。也有其他適配器可用。
  • @ocap/wallet:一個用於建立和管理 DID 錢包的函式庫,我們將用它來為我們的應用程式建立一個身份。

2. 初始化伺服器

現在,讓我們在一個 index.js 檔案中設定 DID Connect 的核心組件。您將使用的兩個主要類別是 WalletAuthenticatorWalletHandlers

  • WalletAuthenticator:管理您的應用程式的身份,並負責建立和簽署身份驗證請求。
  • WalletHandlers:提供一組 Express.js 中介軟體來處理整個 DID Connect 會話的生命週期。

以下是初始設定:

index.js

javascript
const express = require('express');
const { fromRandom } = require('@ocap/wallet');
const SimpleStorage = require('@arcblock/did-connect-storage-nedb');
const { WalletAuthenticator, WalletHandlers } = require('@arcblock/did-connect-js');

// 1. 為您的應用程式建立一個錢包
// 這個錢包代表您的應用程式的身份。
// 在實際應用中,您應該持久化並重複使用這個錢包。
const wallet = fromRandom();

// 2. 設定 WalletAuthenticator
const authenticator = new WalletAuthenticator({
  wallet, // 應用程式的錢包
  baseUrl: 'http://localhost:3000', // 您的應用程式的公開 URL
  appInfo: {
    name: '我的第一個 DID Connect 應用',
    description: '一個用來展示 DID Connect 的簡單應用。',
    icon: 'https://arcblock.oss-cn-beijing.aliyuncs.com/images/wallet-round.png', // 您的應用程式 Logo 的 URL
  },
  chainInfo: {
    host: 'https://babel.arcblock.io/api',
    id: 'babel',
  },
});

// 3. 設定 WalletHandlers
const handlers = new WalletHandlers({
  authenticator,
  tokenStorage: new SimpleStorage({ dbPath: './auth.db' }), // 用於儲存會話令牌
});

// 4. 建立一個 Express 應用
const app = express();

// 如果沒有明確設定,這對於動態 baseUrl 推斷是必需的
app.set('trust proxy', true);

// ... 在下一步中附加處理程序

// 5. 啟動伺服器
const port = 3000;
app.listen(port, () => {
  console.log(`伺服器已在 http://localhost:${port} 上啟動`);
  console.log(`應用程式 DID: ${wallet.toAddress()}`);
});

3. 處理連線

在設定好 authenticator 和 handlers 後,最後一步是將 DID Connect 的端點附加到您的 Express 應用程式上。這是透過使用 handlers.attach() 方法來完成的,您可以在其中定義要請求的特定聲明(claims)以及在成功驗證後執行的回呼。

將以下程式碼加到您的 index.js 檔案中 app.listen() 之前:

index.js

javascript
// 附加 DID Connect 會話處理程序
handlers.attach(app, {
  // 此身份驗證操作的唯一名稱
  action: 'profileLogin',

  // 定義您想向用戶請求的資訊
  claims: {
    profile: () => ({
      fields: ['fullName', 'email'],
      description: '請提供您的姓名和電子郵件以登入。',
    }),
  },

  // 在用戶於錢包中批准請求後,此回呼將被執行
  onAuth: async ({ userDid, claims }) => {
    // `userDid` 是已連線用戶的 DID。
    // `claims` 是一個包含用戶提交資訊的陣列。
    const profile = claims.find((x) => x.type === 'profile');
    console.log('登入成功!');
    console.log('用戶 DID:', userDid);
    console.log('用戶個人資料:', profile);
  },
});

4. 完整程式碼

這裡是您的 index.js 檔案的完整、可執行的程式碼。您可以儲存此檔案並直接執行它。

index.js

javascript
const express = require('express');
const { fromRandom } = require('@ocap/wallet');
const SimpleStorage = require('@arcblock/did-connect-storage-nedb');
const { WalletAuthenticator, WalletHandlers } = require('@arcblock/did-connect-js');

// 1. 為您的應用程式建立一個錢包
const wallet = fromRandom();

// 2. 設定 WalletAuthenticator
const authenticator = new WalletAuthenticator({
  wallet,
  baseUrl: 'http://localhost:3000',
  appInfo: {
    name: '我的第一個 DID Connect 應用',
    description: '一個用來展示 DID Connect 的簡單應用。',
    icon: 'https://arcblock.oss-cn-beijing.aliyuncs.com/images/wallet-round.png',
  },
  chainInfo: {
    host: 'https://babel.arcblock.io/api',
    id: 'babel',
  },
});

// 3. 設定 WalletHandlers
const handlers = new WalletHandlers({
  authenticator,
  tokenStorage: new SimpleStorage({ dbPath: './auth.db' }),
});

// 4. 建立一個 Express 應用並附加處理程序
const app = express();
app.set('trust proxy', true);

handlers.attach(app, {
  action: 'profileLogin',
  claims: {
    profile: () => ({
      fields: ['fullName', 'email'],
      description: '請提供您的姓名和電子郵件以登入。',
    }),
  },
  onAuth: async ({ userDid, claims }) => {
    const profile = claims.find((x) => x.type === 'profile');
    console.log('登入成功!');
    console.log('用戶 DID:', userDid);
    console.log('用戶個人資料:', profile);
  },
});

// 5. 啟動伺服器
const port = 3000;
app.listen(port, () => {
  console.log(`伺服器已在 http://localhost:${port} 上啟動`);
  console.log(`應用程式 DID: ${wallet.toAddress()}`);
});

現在,執行應用程式:

bash
node index.js

您的後端現在已準備好處理 DID Connect 會話。要完成整個流程,您需要一個前端來顯示 QR Code。為了快速測試,您可以使用 DID Connect UX 函式庫或將其整合到您的 Web 應用程式中。

後續步驟

恭喜!您已成功設定一個基本的 DID Connect 伺服器。現在您可以探索更多進階功能。