why-we-open-sour-c1f013bf.webp
alt: 构建数字身份工具 - 为什么我们开源了 SSI SDK

relative: false

自我主权身份(Self-Sovereign Identity,简称 SSI)是一种框架,它允许个人和组织掌控自己的数字身份,并在无需依赖中央权威机构的情况下共享经过验证的凭证。这种范式转变赋予用户更大的隐私权和对个人数据的控制权,同时也提供了强大的机制来验证凭证的真实性。

什么是自我主权身份(SSI)?

SSI 围绕去中心化标识符(DIDs)和可验证凭证的概念构建。DIDs 是由其所代表的实体控制的唯一标识符,使他们能够管理自己的身份数据。可验证凭证是由一方颁发并可由另一方验证的数字断言,确保共享信息的真实性和完整性。

为什么我们开源了 SSI SDK?

开源 SSI SDK 是一个战略决策,受到多个因素驱动。首先,促进社区内的创新对于推进数字身份领域至关重要。通过向所有人提供我们的 SDK,我们鼓励协作和实验,从而产生新想法和改进。

其次,促进透明度对于在数字身份系统中建立信任至关重要。开源项目允许他人检查代码库、理解其工作原理并识别潜在漏洞。这种透明度有助于建立对 SDK 安全性和可靠性的信心。

最后,使更广泛的社区能够贡献并受益于安全的数字身份解决方案,这符合我们将这些技术民主化访问的使命。通过降低入门门槛,我们希望赋能更多开发者和组织采用并改进我们的工作。

SSI SDK 的主要功能有哪些?

SSI SDK 提供了一套全面的工具,用于构建数字身份应用。以下是其主要功能:

  • 去中心化标识符(DID)管理:使用各种方法创建、解析和管理 DIDs,包括基于区块链的解决方案。
  • 可验证凭证的颁发和验证:使用加密保障颁发和验证凭证,确保数据完整性和真实性。
  • 区块链集成:在区块链网络上存储和检索凭证,利用其不可变性和安全特性。
  • 可扩展架构:将 SDK 设计为模块化和可扩展的,允许开发者集成自定义组件和协议。
  • 跨平台兼容性:确保 SDK 在不同的操作系统和编程语言上运行,为多样化的用例提供灵活性。

安全注意事项

安全在任何数字身份系统中都至关重要。以下是使用 SSI SDK 时的一些关键注意事项:

  • 加密操作:确保所有加密操作正确且安全地执行。使用成熟的库并遵循密钥管理的最佳实践。
  • 私钥保护:切勿暴露私钥。安全地存储它们,最好使用硬件安全模块(HSMs)或安全飞地。
  • 凭证验证:验证所有凭证和签名,以防止伪造和篡改。实施强大的验证流程以确保数据完整性。
  • 定期审计:定期进行安全审计和漏洞评估,以及时识别和解决潜在问题。

⚠️ 警告: 始终保持 SDK 和依赖项更新,以防范已知漏洞。

如何使用 SSI SDK 实现可验证凭证?

实现可验证凭证涉及多个步骤,从创建 DIDs 到颁发和验证凭证。以下是帮助您入门的逐步指南:

步骤 1:设置您的环境

在开始之前,请确保已安装必要的工具和依赖项。SSI SDK 通常需要 Node.js 和 npm(Node Package Manager)。

# Install Node.js and npm
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Verify installation
node -v
npm -v

Enter fullscreen mode Exit fullscreen mode

步骤 2:安装 SSI SDK

使用 npm 安装 SSI SDK。您可以在 官方 GitHub 仓库 上找到最新版本。

# Install the SSI SDK
npm install @yourorg/ssi-sdk

Enter fullscreen mode Exit fullscreen mode

步骤 3:创建去中心化标识符(DID)

使用 SDK 创建 DID。此标识符将成为您数字身份的基础。

const { DID } = require('@yourorg/ssi-sdk');

// Create a new DID
const did = await DID.create();
console.log('Generated DID:', did.didString);

Enter fullscreen mode Exit fullscreen mode

步骤 4:颁发可验证凭证

拥有 DID 后,您可以颁发可验证凭证。这些凭证经过数字签名,可以与他人共享。

const { Credential } = require('@yourorg/ssi-sdk');

// Define the credential payload
const credentialPayload = {
  '@context': ['https://www.w3.org/2018/credentials/v1'],
  type: ['VerifiableCredential', 'UniversityDegreeCredential'],
  issuer: did.didString,
  issuanceDate: new Date().toISOString(),
  credentialSubject: {
    id: 'did:example:123',
    degree: {
      type: 'BachelorDegree',
      name: 'Bachelor of Science in Computer Science'
    }
  }
};

// Issue the credential
const credential = await Credential.issue(credentialPayload, did.privateKey);
console.log('Issued Credential:', JSON.stringify(credential));

Enter fullscreen mode Exit fullscreen mode

步骤 5:验证可验证凭证

为确保凭证的真实性,验证其签名和其他属性。

// Verify the credential
const isValid = await Credential.verify(credential);
console.log('Credential is valid:', isValid);

Enter fullscreen mode Exit fullscreen mode

步骤 6:存储和检索凭证

您可以将凭证存储在区块链网络或其他安全存储解决方案上。SDK 提供用于与各种区块链平台交互的实用程序。

const { BlockchainStorage } = require('@yourorg/ssi-sdk');

// Initialize blockchain storage
const storage = new BlockchainStorage('https://your-blockchain-node.com');

// Store the credential
await storage.storeCredential(credential);

// Retrieve the credential
const storedCredential = await storage.getCredential(credential.id);
console.log('Stored Credential:', JSON.stringify(storedCredential));

Enter fullscreen mode Exit fullscreen mode

🎯 关键要点

  • 创建 DIDs 以管理数字身份。
  • 使用加密签名颁发和验证可验证凭证。
  • 在区块链网络上安全地存储和检索凭证。
  • 遵循安全和密钥管理的最佳实践。

SSI SDK 与其他身份解决方案的比较

方法 优点 缺点 适用场景
SSI SDK 去中心化、安全、灵活 需要技术专业知识 构建自定义身份解决方案
集中式 ID 提供商 易于集成、广泛支持 缺乏用户控制、隐私问题 快速实施、现有生态系统
传统 PKI 成熟、可信的基础设施 集中式、灵活性较低 传统系统、受监管环境

快速参考

📋 快速参考

  • DID.create() - 生成新的去中心化标识符。
  • Credential.issue(payload, privateKey) - 颁发可验证凭证。
  • Credential.verify(credential) - 验证可验证凭证。
  • BlockchainStorage.storeCredential(credential) - 将凭证存储在区块链上。
  • BlockchainStorage.getCredential(id) - 从区块链检索凭证。

真实案例

让我们通过一个真实案例,了解如何使用 SSI SDK 为大学毕业生创建数字身份并颁发可验证学位凭证。

步骤 1:为毕业生生成 DID

const graduateDID = await DID.create();
console.log('Graduate DID:', graduateDID.didString);

Enter fullscreen mode Exit fullscreen mode

步骤 2:颁发学位凭证

const degreeCredentialPayload = {
  '@context': ['https://www.w3.org/2018/credentials/v1'],
  type: ['VerifiableCredential', 'UniversityDegreeCredential'],
  issuer: 'did:example:university',
  issuanceDate: new Date().toISOString(),
  credentialSubject: {
    id: graduateDID.didString,
    degree: {
      type: 'BachelorDegree',
      name: 'Bachelor of Science in Computer Science'
    }
  }
};

const degreeCredential = await Credential.issue(degreeCredentialPayload, 'universityPrivateKey');
console.log('Degree Credential:', JSON.stringify(degreeCredential));

Enter fullscreen mode Exit fullscreen mode

步骤 3:验证凭证

const isDegreeValid = await Credential.verify(degreeCredential);
console.log('Degree Credential is valid:', isDegreeValid);

Enter fullscreen mode Exit fullscreen mode

步骤 4:将凭证存储在区块链上

await storage.storeCredential(degreeCredential);
console.log('Degree Credential stored on blockchain.');

Enter fullscreen mode Exit fullscreen mode

步骤 5:检索并验证存储的凭证

const retrievedDegreeCredential = await storage.getCredential(degreeCredential.id);
console.log('Retrieved Degree Credential:', JSON.stringify(retrievedDegreeCredential));

const isRetrievedDegreeValid = await Credential.verify(retrievedDegreeCredential);
console.log('Retrieved Degree Credential is valid:', isRetrievedDegreeValid);

Enter fullscreen mode Exit fullscreen mode

最佳实践: 检索后始终验证凭证以确保其真实性。

常见问题排查

以下是使用 SSI SDK 时可能遇到的一些常见问题及其解决方法:

问题:无效签名错误

症状: 验证凭证时收到“无效签名”错误。

解决方案: 确保用于签署凭证的私钥与颁发者 DID 关联的公钥匹配。仔细检查密钥管理流程以避免不匹配。

问题:区块链存储失败

症状: 将凭证存储在区块链上时因网络错误而失败。

解决方案: 验证区块链节点 URL 是否正确且网络可访问。检查是否存在网络连接问题或可能阻止连接的防火墙规则。

问题:DID 解析失败

症状: 解析 DID 时返回错误,表明找不到该 DID。

解决方案: 确保 DID 解析器配置正确且 DID 已正确注册。检查 DID 方法和网络设置以确认兼容性。

结论

通过开源我们的 SSI SDK,我们旨在赋能开发者和组织构建安全、去中心化的数字身份解决方案。SDK 提供了一套强大的工具,用于管理 DIDs、颁发和验证可验证凭证,以及与区块链网络集成。遵循安全和密钥管理的最佳实践,可确保数字身份的完整性和真实性。

就是这样。简单、安全、有效。深入了解 SDK 文档,今天就开始构建您自己的数字身份工具吧。

💜 专业提示: 加入社区论坛并参与讨论,分享您的经验并向他人学习。