UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

65 lines 2.36 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var derive_exports = {}; __export(derive_exports, { MASTER_KEY_MESSAGE: () => MASTER_KEY_MESSAGE, deriveMasterKey: () => deriveMasterKey, deriveScopeKey: () => deriveScopeKey, recoverServerOwner: () => recoverServerOwner }); module.exports = __toCommonJS(derive_exports); var import_hkdf = require("@noble/hashes/hkdf"); var import_sha2 = require("@noble/hashes/sha2"); var import_viem = require("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 (0, import_viem.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 (0, import_hkdf.hkdf)(import_sha2.sha256, masterKey, salt, info, 32); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MASTER_KEY_MESSAGE, deriveMasterKey, deriveScopeKey, recoverServerOwner }); //# sourceMappingURL=derive.cjs.map