|
| 1 | +import { DeriveKeyProvider } from "@elizaos/plugin-tee"; |
| 2 | +import * as crypto from "crypto"; |
| 3 | + |
| 4 | +export class DeriveProvider { |
| 5 | + private provider: DeriveKeyProvider; |
| 6 | + |
| 7 | + constructor(teeModel: string) { |
| 8 | + this.provider = new DeriveKeyProvider(teeModel); |
| 9 | + } |
| 10 | + |
| 11 | + async deriveKeyPair(params: { |
| 12 | + agentId: string; |
| 13 | + bizModel: string; |
| 14 | + }): Promise<Buffer> { |
| 15 | + const keyPath = `/${params.agentId}/tee/keypair/${params.bizModel}`; |
| 16 | + const seed = await this.provider.rawDeriveKey(keyPath, params.agentId); |
| 17 | + // 从 PEM 格式解析私钥 |
| 18 | + const privateKey = crypto.createPrivateKey({ |
| 19 | + key: seed.key, |
| 20 | + format: "pem", |
| 21 | + }); |
| 22 | + // 导出私钥为 DER 格式 |
| 23 | + const privateKeyDer = privateKey.export({ |
| 24 | + format: "der", |
| 25 | + type: "pkcs8", |
| 26 | + }); |
| 27 | + // 使用 SHA-256 对私钥进行哈希,派生出对称加密密钥 |
| 28 | + return crypto.createHash("sha256").update(privateKeyDer).digest(); |
| 29 | + } |
| 30 | + |
| 31 | + async encryptAgentData( |
| 32 | + params: { |
| 33 | + agentId: string; |
| 34 | + bizModel: string; |
| 35 | + }, |
| 36 | + plainText: string |
| 37 | + ): Promise<{ |
| 38 | + success: boolean; |
| 39 | + errorMsg: string; |
| 40 | + ivHex: string; |
| 41 | + encryptedData: string; |
| 42 | + }> { |
| 43 | + try { |
| 44 | + const rawKey = await this.deriveKeyPair(params); |
| 45 | + const { ivHex, encrypted } = this.encrypt(plainText, rawKey); |
| 46 | + |
| 47 | + return { |
| 48 | + success: true, |
| 49 | + errorMsg: "", |
| 50 | + ivHex: ivHex, |
| 51 | + encryptedData: encrypted, |
| 52 | + }; |
| 53 | + } catch (error) { |
| 54 | + return { |
| 55 | + success: true, |
| 56 | + errorMsg: "encryptAgentData failed:" + error, |
| 57 | + ivHex: "", |
| 58 | + encryptedData: "", |
| 59 | + }; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + async decryptAgentData( |
| 64 | + params: { |
| 65 | + agentId: string; |
| 66 | + bizModel: string; |
| 67 | + }, |
| 68 | + ivHex: string, |
| 69 | + encryptedData: string |
| 70 | + ): Promise<{ |
| 71 | + success: boolean; |
| 72 | + errorMsg: string; |
| 73 | + plainText: string; |
| 74 | + }> { |
| 75 | + try { |
| 76 | + const rawKey = await this.deriveKeyPair(params); |
| 77 | + const plainText = this.decrypt(encryptedData, ivHex, rawKey); |
| 78 | + return { |
| 79 | + success: true, |
| 80 | + errorMsg: "", |
| 81 | + plainText: plainText, |
| 82 | + }; |
| 83 | + } catch (error) { |
| 84 | + return { |
| 85 | + success: false, |
| 86 | + errorMsg: "decryptAgentData failed: " + error, |
| 87 | + plainText: "", |
| 88 | + }; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // 加密函数 |
| 93 | + private encrypt( |
| 94 | + text: string, |
| 95 | + key: Buffer |
| 96 | + ): { ivHex: string; encrypted: string } { |
| 97 | + // 生成随机初始化向量(IV) |
| 98 | + const iv = crypto.randomBytes(16); |
| 99 | + |
| 100 | + // 创建 cipher 对象 |
| 101 | + const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); |
| 102 | + |
| 103 | + // 加密 |
| 104 | + let encrypted = cipher.update(text, "utf8", "hex"); |
| 105 | + encrypted += cipher.final("hex"); |
| 106 | + |
| 107 | + // 返回 IV 和加密后的数据(IV 需要在解密时使用) |
| 108 | + return { ivHex: iv.toString("hex"), encrypted: encrypted }; |
| 109 | + } |
| 110 | + |
| 111 | + // 解密函数 |
| 112 | + private decrypt(encryptedData: string, ivHex: string, key: Buffer): string { |
| 113 | + const decipher = crypto.createDecipheriv( |
| 114 | + "aes-256-cbc", |
| 115 | + key, |
| 116 | + Buffer.from(ivHex, "hex") |
| 117 | + ); |
| 118 | + let decrypted = decipher.update(encryptedData, "hex", "utf8"); |
| 119 | + decrypted += decipher.final("utf8"); |
| 120 | + return decrypted; |
| 121 | + } |
| 122 | +} |
0 commit comments