UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

165 lines 5.38 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var browser_exports = {}; __export(browser_exports, { BrowserECIESUint8Provider: () => BrowserECIESUint8Provider }); module.exports = __toCommonJS(browser_exports); var secp256k1 = __toESM(require("@noble/secp256k1"), 1); var import_base = require("./base"); var import_viem = require("viem"); var import_hmac = require("@noble/hashes/hmac"); var import_sha2 = require("@noble/hashes/sha2"); class BrowserECIESUint8Provider extends import_base.BaseECIESUint8 { generateRandomBytes(length) { const bytes = new Uint8Array(length); crypto.getRandomValues(bytes); return bytes; } verifyPrivateKey(privateKey) { try { return secp256k1.utils.isValidPrivateKey(privateKey); } catch { return false; } } createPublicKey(privateKey, compressed) { try { return secp256k1.getPublicKey(privateKey, compressed); } catch { return null; } } validatePublicKey(publicKey) { try { secp256k1.Point.fromHex(publicKey); return true; } catch { return false; } } decompressPublicKey(publicKey) { try { const point = secp256k1.Point.fromHex(publicKey); return point.toRawBytes(false); } catch { return null; } } performECDH(publicKey, privateKey) { try { const sharedPoint = secp256k1.getSharedSecret( privateKey, publicKey, true ); return sharedPoint.slice(1); } catch (error) { throw new Error( `ECDH failed: ${error instanceof Error ? error.message : "Unknown error"}` ); } } sha512(data) { return (0, import_sha2.sha512)(data); } hmacSha256(key, data) { return (0, import_hmac.hmac)(import_sha2.sha256, key, data); } async aesEncrypt(key, iv, plaintext) { const cryptoKey = await crypto.subtle.importKey( "raw", key, { name: "AES-CBC" }, false, ["encrypt"] ); const encrypted = await crypto.subtle.encrypt( { name: "AES-CBC", iv }, cryptoKey, plaintext ); return new Uint8Array(encrypted); } async aesDecrypt(key, iv, ciphertext) { const cryptoKey = await crypto.subtle.importKey( "raw", key, { name: "AES-CBC" }, false, ["decrypt"] ); const decrypted = await crypto.subtle.decrypt( { name: "AES-CBC", iv }, cryptoKey, ciphertext ); return new Uint8Array(decrypted); } /** * Normalizes a public key to uncompressed format (65 bytes with 0x04 prefix). * Handles compressed (33 bytes) and uncompressed (65 bytes) formats only. * * @remarks * Strict policy: Does not accept 64-byte raw coordinates to avoid masking * malformed data. Callers must provide properly formatted keys. * * @param publicKey - The public key to normalize (33 or 65 bytes) * @returns The normalized uncompressed public key (65 bytes) * @throws {Error} When public key format is invalid or decompression fails */ normalizeToUncompressed(publicKey) { const len = publicKey.length; if (len === 65 && publicKey[0] === 4) { return publicKey; } if (len === 33 && (publicKey[0] === 2 || publicKey[0] === 3)) { const decompressed = this.decompressPublicKey(publicKey); if (!decompressed) { throw new Error( `Failed to decompress public key with prefix ${(0, import_viem.toHex)(publicKey[0])}` ); } return decompressed; } if (len === 64) { throw new Error( "Raw public key coordinates (64 bytes) are not accepted. Please provide a properly formatted compressed (33 bytes) or uncompressed (65 bytes) public key." ); } throw new Error( `Invalid public key format: expected compressed (33 bytes) or uncompressed (65 bytes), got ${len} bytes` ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BrowserECIESUint8Provider }); //# sourceMappingURL=browser.cjs.map