create-bap-identity

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Create BAP Identity

创建BAP身份

Create and manage BAP (Bitcoin Attestation Protocol) identities using the
bsv-bap
library.
使用
bsv-bap
库创建和管理BAP(Bitcoin Attestation Protocol)身份。

Installation

安装

bash
bun add bsv-bap @bsv/sdk
bash
bun add bsv-bap @bsv/sdk

Creating an Identity

创建身份

typescript
import { BAP } from "bsv-bap";
import { PrivateKey } from "@bsv/sdk";

// Create BAP instance with new key
const privateKey = PrivateKey.fromRandom();
const bap = new BAP({ rootPk: privateKey.toWif() });

// Create identity
const identity = bap.newId("Alice Smith");

console.log("Identity Key:", identity.getIdentityKey());
console.log("Root Address:", identity.rootAddress);
console.log("Signing Address:", identity.getCurrentAddress());
typescript
import { BAP } from "bsv-bap";
import { PrivateKey } from "@bsv/sdk";

// 使用新密钥创建BAP实例
const privateKey = PrivateKey.fromRandom();
const bap = new BAP({ rootPk: privateKey.toWif() });

// 创建身份
const identity = bap.newId("Alice Smith");

console.log("身份密钥:", identity.getIdentityKey());
console.log("根地址:", identity.rootAddress);
console.log("签名地址:", identity.getCurrentAddress());

Key Derivation

密钥派生

BAP uses Type42 (BRC-42) key derivation with BRC-43 invoice numbers:
PurposeInvoice NumberSecurity Level
Signing key
1-bap-identity
1 (public protocol)
Friend encryption
2-friend-{sha256(friendBapId)}
2 (user-approved)
BAP使用带有BRC-43发票编号的Type42(BRC-42)密钥派生方式:
用途发票编号安全级别
签名密钥
1-bap-identity
1(公开协议)
好友加密
2-friend-{sha256(friendBapId)}
2(用户授权)

Signing Messages

签名消息

typescript
import { Utils } from "@bsv/sdk";
const { toArray } = Utils;

// Sign a message
const message = toArray("Hello World", "utf8");
const { address, signature } = identity.signMessage(message);

// Verify (on any BAP instance)
const isValid = bap.verifySignature("Hello World", address, signature);
typescript
import { Utils } from "@bsv/sdk";
const { toArray } = Utils;

// 签名消息
const message = toArray("Hello World", "utf8");
const { address, signature } = identity.signMessage(message);

// 验证(在任意BAP实例上)
const isValid = bap.verifySignature("Hello World", address, signature);

Friend Encryption

好友加密

Derive friend-specific encryption keys for private communication:
typescript
// Get encryption pubkey for a friend (share in friend requests)
const friendPubKey = identity.getEncryptionPublicKeyWithSeed(friendBapId);

// Encrypt data for friend
const ciphertext = identity.encryptWithSeed("secret message", friendBapId);

// Decrypt data from friend
const plaintext = identity.decryptWithSeed(ciphertext, friendBapId);
派生特定于好友的加密密钥以进行私密通信:
typescript
// 获取好友的加密公钥(在好友请求中分享)
const friendPubKey = identity.getEncryptionPublicKeyWithSeed(friendBapId);

// 为好友加密数据
const ciphertext = identity.encryptWithSeed("secret message", friendBapId);

// 解密来自好友的数据
const plaintext = identity.decryptWithSeed(ciphertext, friendBapId);

Export/Import

导出/导入

typescript
// Export for backup
const backup = bap.exportForBackup("My Identity");
// { ids: "...", createdAt: "...", rootPk: "..." }

// Import from backup
const bap2 = new BAP({ rootPk: backup.rootPk });
bap2.importIds(backup.ids);
typescript
// 导出用于备份
const backup = bap.exportForBackup("My Identity");
// { ids: "...", createdAt: "...", rootPk: "..." }

// 从备份导入
const bap2 = new BAP({ rootPk: backup.rootPk });
bap2.importIds(backup.ids);

CLI Option

CLI选项

For quick operations, the
bsv-bap
package includes a CLI:
bash
npm install -g bsv-bap

bap create --name "Alice"     # Create identity (~/.bap/identity.json)
bap sign "Hello World"        # Sign message
bap verify "msg" "sig" "addr" # Verify signature
bap info                      # Show identity info
bap friend-pubkey <bapId>     # Get friend encryption pubkey
bap encrypt <data> <bapId>    # Encrypt for friend
bap decrypt <text> <bapId>    # Decrypt from friend
bap export                    # Export backup JSON
bap import <file>             # Import from backup
为了快速操作,
bsv-bap
包包含一个CLI:
bash
npm install -g bsv-bap

bap create --name "Alice"     # 创建身份(~/.bap/identity.json)
bap sign "Hello World"        # 签名消息
bap verify "msg" "sig" "addr" # 验证签名
bap info                      # 显示身份信息
bap friend-pubkey <bapId>     # 获取好友加密公钥
bap encrypt <data> <bapId>    # 为好友加密数据
bap decrypt <text> <bapId>    # 解密来自好友的数据
bap export                    # 导出备份JSON
bap import <file>             # 从文件导入

Next Steps

后续步骤

After creating an identity:
  1. Sign messages for authentication
  2. Share encryption pubkeys in friend requests
  3. Publish identity to blockchain for on-chain reputation
  4. Integrate with Sigma Identity for OAuth (
    @sigma-auth/better-auth-plugin
    )
创建身份后:
  1. 签名消息以进行身份验证
  2. 在好友请求中分享加密公钥
  3. 将身份发布到区块链以获取链上声誉
  4. 与Sigma Identity集成以实现OAuth(
    @sigma-auth/better-auth-plugin

Related Skills

相关技能

  • key-derivation
    - Type42 and BRC-43 key derivation patterns
  • message-signing
    - BSM, BRC-77, and Sigma signing protocols
  • encrypt-decrypt-backup
    - bitcoin-backup CLI for .bep encrypted backups
  • key-derivation
    - Type42和BRC-43密钥派生模式
  • message-signing
    - BSM、BRC-77和Sigma签名协议
  • encrypt-decrypt-backup
    - 用于.bep加密备份的bitcoin-backup CLI