name: create-bap-identity description: 当用户要求"创建BAP身份"、“新BAP”、“Type42身份”、“Legacy BAP身份”、“生成BAP”、“设置BAP身份”、“初始化BAP”,或需要使用bap CLI创建Bitcoin Attestation Protocol身份时,应使用此技能。
创建BAP身份
使用 bsv-bap 库创建和管理 BAP(Bitcoin Attestation Protocol)身份。
安装
bun add bsv-bap @bsv/sdk
创建身份
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());
密钥派生
BAP使用Type42(BRC-42)密钥派生,带BRC-43发票号:
| 用途 | 发票号 | 安全级别 |
|---|---|---|
| 签名密钥 | 1-bap-identity |
1(公共协议) |
| 朋友加密 | 2-friend-{sha256(friendBapId)} |
2(用户批准) |
签名消息
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);
朋友加密
派生朋友特定的加密密钥用于私人通信:
// 获取朋友的加密公钥(在朋友请求中分享)
const friendPubKey = identity.getEncryptionPublicKeyWithSeed(friendBapId);
// 为朋友加密数据
const ciphertext = identity.encryptWithSeed("secret message", friendBapId);
// 解密来自朋友的数据
const plaintext = identity.decryptWithSeed(ciphertext, friendBapId);
导出/导入
// 导出备份
const backup = bap.exportForBackup("My Identity");
// { ids: "...", createdAt: "...", rootPk: "..." }
// 从备份导入
const bap2 = new BAP({ rootPk: backup.rootPk });
bap2.importIds(backup.ids);
CLI选项
对于快速操作,bsv-bap 包包括一个CLI:
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> # 从备份导入
后续步骤
创建身份后:
- 签名消息用于身份验证
- 在朋友请求中分享加密公钥
- 将身份发布到区块链以获取链上声誉
- 集成Sigma Identity以支持OAuth(
@sigma-auth/better-auth-plugin)
相关技能
key-derivation- Type42和BRC-43密钥派生模式message-signing- BSM、BRC-77和Sigma签名协议encrypt-decrypt-backup- bitcoin-backup CLI用于.bep加密备份