|
| 1 | +/** |
| 2 | + * Hedera HCS policy document anchor. |
| 3 | + * Publishes a PolicyGrant's policy document (or its hash) to a Hedera HCS topic |
| 4 | + * for tamper-evident, third-party-auditable policy trails. |
| 5 | + * |
| 6 | + * Default submitMode is "hash-only" — only the policyHash is published. |
| 7 | + * This is GDPR-safe and sufficient for most audit use cases. |
| 8 | + * |
| 9 | + * Environment variables: |
| 10 | + * MPCP_HCS_POLICY_TOPIC_ID — Target HCS topic for policy anchoring |
| 11 | + * MPCP_HCS_OPERATOR_ID — Hedera operator account ID |
| 12 | + * MPCP_HCS_OPERATOR_KEY — Hedera operator private key |
| 13 | + * HEDERA_NETWORK — testnet | mainnet (default: testnet) |
| 14 | + */ |
| 15 | + |
| 16 | +import { createHash } from "node:crypto"; |
| 17 | +import type { |
| 18 | + PolicyAnchorResult, |
| 19 | + PolicyAnchorSubmitMode, |
| 20 | + PolicyAnchorEncryptionOptions, |
| 21 | +} from "./types.js"; |
| 22 | +import { encryptPolicyDocument } from "./encrypt.js"; |
| 23 | + |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +// HCS message shapes |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +interface HcsPolicyAnchorMessageBase { |
| 29 | + type: "MPCP:PolicyAnchor:1.0"; |
| 30 | + policyHash: string; |
| 31 | + anchoredAt: string; |
| 32 | +} |
| 33 | + |
| 34 | +interface HcsHashOnlyMessage extends HcsPolicyAnchorMessageBase { |
| 35 | + submitMode: "hash-only"; |
| 36 | +} |
| 37 | + |
| 38 | +interface HcsFullDocumentMessage extends HcsPolicyAnchorMessageBase { |
| 39 | + submitMode: "full-document"; |
| 40 | + policyDocument: object; |
| 41 | +} |
| 42 | + |
| 43 | +interface HcsEncryptedMessage extends HcsPolicyAnchorMessageBase { |
| 44 | + submitMode: "encrypted"; |
| 45 | + encryptedDocument: { |
| 46 | + algorithm: "AES-256-GCM"; |
| 47 | + iv: string; |
| 48 | + ciphertext: string; |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +type HcsPolicyAnchorMessage = |
| 53 | + | HcsHashOnlyMessage |
| 54 | + | HcsFullDocumentMessage |
| 55 | + | HcsEncryptedMessage; |
| 56 | + |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | +// Main export |
| 59 | +// --------------------------------------------------------------------------- |
| 60 | + |
| 61 | +/** |
| 62 | + * Anchor a policy document to Hedera HCS. |
| 63 | + * |
| 64 | + * @param policyDocument - The policy document object to anchor |
| 65 | + * @param options - submitMode (default "hash-only"), encryption, and HCS credentials |
| 66 | + * @returns PolicyAnchorResult with anchorRef "hcs:{topicId}:{sequenceNumber}" |
| 67 | + */ |
| 68 | +export async function hederaHcsAnchorPolicyDocument( |
| 69 | + policyDocument: object, |
| 70 | + options?: { |
| 71 | + topicId?: string; |
| 72 | + operatorId?: string; |
| 73 | + operatorKey?: string; |
| 74 | + submitMode?: PolicyAnchorSubmitMode; |
| 75 | + encryption?: PolicyAnchorEncryptionOptions; |
| 76 | + }, |
| 77 | +): Promise<PolicyAnchorResult> { |
| 78 | + const submitMode: PolicyAnchorSubmitMode = options?.submitMode ?? "hash-only"; |
| 79 | + |
| 80 | + if (submitMode === "encrypted" && !options?.encryption) { |
| 81 | + throw new Error( |
| 82 | + "hederaHcsAnchorPolicyDocument: encryption options required when submitMode is 'encrypted'", |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + const accountId = options?.operatorId ?? process.env.MPCP_HCS_OPERATOR_ID; |
| 87 | + const privateKeyStr = options?.operatorKey ?? process.env.MPCP_HCS_OPERATOR_KEY; |
| 88 | + const topicIdStr = options?.topicId ?? process.env.MPCP_HCS_POLICY_TOPIC_ID; |
| 89 | + |
| 90 | + if (!accountId || !privateKeyStr || !topicIdStr) { |
| 91 | + throw new Error( |
| 92 | + "HCS policy anchor requires MPCP_HCS_OPERATOR_ID, MPCP_HCS_OPERATOR_KEY, MPCP_HCS_POLICY_TOPIC_ID", |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + let sdk: typeof import("@hashgraph/sdk"); |
| 97 | + try { |
| 98 | + sdk = await import("@hashgraph/sdk"); |
| 99 | + } catch { |
| 100 | + throw new Error( |
| 101 | + "Hedera HCS requires @hashgraph/sdk. Install with: npm install @hashgraph/sdk", |
| 102 | + ); |
| 103 | + } |
| 104 | + const { Client, TopicMessageSubmitTransaction, PrivateKey, AccountId, TopicId } = sdk; |
| 105 | + |
| 106 | + // Compute SHA-256 of canonical policy document |
| 107 | + const canonicalPolicy = JSON.stringify(policyDocument, Object.keys(policyDocument).sort()); |
| 108 | + const policyHash = createHash("sha256").update(canonicalPolicy).digest("hex"); |
| 109 | + |
| 110 | + const anchoredAt = new Date().toISOString(); |
| 111 | + |
| 112 | + // Build message body based on submitMode |
| 113 | + let message: HcsPolicyAnchorMessage; |
| 114 | + |
| 115 | + if (submitMode === "hash-only") { |
| 116 | + message = { type: "MPCP:PolicyAnchor:1.0", policyHash, anchoredAt, submitMode }; |
| 117 | + } else if (submitMode === "full-document") { |
| 118 | + message = { |
| 119 | + type: "MPCP:PolicyAnchor:1.0", |
| 120 | + policyHash, |
| 121 | + anchoredAt, |
| 122 | + submitMode, |
| 123 | + policyDocument, |
| 124 | + }; |
| 125 | + } else { |
| 126 | + // encrypted |
| 127 | + const encryptedDocument = await encryptPolicyDocument(policyDocument, options!.encryption!); |
| 128 | + message = { |
| 129 | + type: "MPCP:PolicyAnchor:1.0", |
| 130 | + policyHash, |
| 131 | + anchoredAt, |
| 132 | + submitMode, |
| 133 | + encryptedDocument, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + const messageBytes = new TextEncoder().encode(JSON.stringify(message)); |
| 138 | + |
| 139 | + const network = process.env.HEDERA_NETWORK ?? "testnet"; |
| 140 | + const client = Client.forName(network); |
| 141 | + client.setOperator(AccountId.fromString(accountId), PrivateKey.fromString(privateKeyStr)); |
| 142 | + |
| 143 | + const tx = new TopicMessageSubmitTransaction() |
| 144 | + .setTopicId(TopicId.fromString(topicIdStr)) |
| 145 | + .setMessage(messageBytes); |
| 146 | + |
| 147 | + const txResponse = await tx.execute(client); |
| 148 | + const receipt = await txResponse.getReceipt(client); |
| 149 | + |
| 150 | + const sequenceNumber = receipt.topicSequenceNumber?.toString(); |
| 151 | + const consensusTs = receipt.consensusTimestamp; |
| 152 | + const anchoredAtIso = consensusTs ? new Date(consensusTs.toDate()).toISOString() : anchoredAt; |
| 153 | + |
| 154 | + client.close(); |
| 155 | + |
| 156 | + return { |
| 157 | + rail: "hedera-hcs", |
| 158 | + reference: `hcs:${topicIdStr}:${sequenceNumber ?? "unknown"}`, |
| 159 | + anchoredAt: anchoredAtIso, |
| 160 | + policyHash, |
| 161 | + submitMode, |
| 162 | + }; |
| 163 | +} |
0 commit comments