UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

38 lines 1.16 kB
import { hkdf } from "@noble/hashes/hkdf"; import { sha256 } from "@noble/hashes/sha2"; import { recoverMessageAddress } from "viem"; const MASTER_KEY_MESSAGE = "vana-master-key-v1"; function deriveMasterKey(signature) { const hex = signature.slice(2); if (hex.length !== 130) { throw new Error( `Invalid signature length: expected 130 hex chars (65 bytes), got ${hex.length}` ); } if (!/^[0-9a-fA-F]+$/.test(hex)) { throw new Error("Invalid signature: contains non-hex characters"); } const bytes = new Uint8Array(65); for (let i = 0; i < 65; i++) { bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16); } return bytes; } async function recoverServerOwner(masterKeySignature) { return recoverMessageAddress({ message: MASTER_KEY_MESSAGE, signature: masterKeySignature }); } function deriveScopeKey(masterKey, scope) { const salt = new TextEncoder().encode("vana"); const info = new TextEncoder().encode(`scope:${scope}`); return hkdf(sha256, masterKey, salt, info, 32); } export { MASTER_KEY_MESSAGE, deriveMasterKey, deriveScopeKey, recoverServerOwner }; //# sourceMappingURL=derive.js.map