@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
1 lines • 6.91 kB
Source Map (JSON)
{"version":3,"sources":["../../src/utils/crypto-utils.ts"],"sourcesContent":["/**\n * Provides platform-agnostic cryptographic utility functions.\n *\n * @remarks\n * This module contains utility functions for cryptographic operations that work\n * consistently across Node.js and browser environments. All functions use `Uint8Array`\n * exclusively for binary data to ensure cross-platform compatibility.\n *\n * @category Cryptography\n */\n\nimport { fromHex } from \"viem\";\n\n/**\n * Processes a wallet public key for cryptographic operations.\n *\n * @remarks\n * Converts hex string public keys to Uint8Array format.\n * For normalization to uncompressed format, use the crypto provider's\n * normalizeToUncompressed method.\n *\n * @param publicKey - The wallet public key as hex string or byte array.\n * @returns The public key as a Uint8Array.\n *\n * @example\n * ```typescript\n * const keyBytes = processWalletPublicKey(\"0x04...\");\n * const normalized = provider.normalizeToUncompressed(keyBytes);\n * ```\n */\nexport function processWalletPublicKey(\n publicKey: string | Uint8Array,\n): Uint8Array {\n // Convert to bytes if hex string\n return typeof publicKey === \"string\"\n ? fromHex(\n (publicKey.startsWith(\"0x\")\n ? publicKey\n : `0x${publicKey}`) as `0x${string}`,\n \"bytes\",\n )\n : publicKey;\n}\n\n/**\n * Processes a wallet private key for cryptographic operations.\n *\n * @param privateKey - The wallet private key as hex string or byte array.\n * @returns The private key as a Uint8Array.\n *\n * @example\n * ```typescript\n * const key = processWalletPrivateKey(\"0x...\");\n * console.log(key.length); // 32 (secp256k1 private key)\n * ```\n */\nexport function processWalletPrivateKey(\n privateKey: string | Uint8Array,\n): Uint8Array {\n // Convert to bytes\n return typeof privateKey === \"string\"\n ? fromHex(\n (privateKey.startsWith(\"0x\")\n ? privateKey\n : `0x${privateKey}`) as `0x${string}`,\n \"bytes\",\n )\n : privateKey;\n}\n\n/**\n * Parses legacy eccrypto-format encrypted data buffer.\n * Format: [iv(16)][ephemPublicKey(65)][ciphertext(variable)][mac(32)]\n *\n * @param encryptedBuffer - Buffer containing encrypted data in eccrypto format\n * @returns Parsed encrypted data components\n */\nexport function parseEncryptedDataBuffer(encryptedBuffer: Uint8Array): {\n iv: Uint8Array;\n ephemPublicKey: Uint8Array;\n ciphertext: Uint8Array;\n mac: Uint8Array;\n} {\n return {\n iv: encryptedBuffer.slice(0, 16),\n ephemPublicKey: encryptedBuffer.slice(16, 81), // 65 bytes for uncompressed public key\n ciphertext: encryptedBuffer.slice(81, -32),\n mac: encryptedBuffer.slice(-32),\n };\n}\n\n/**\n * Generates a deterministic seed from a message for key derivation\n *\n * @param message - Message to derive seed from\n * @returns Seed as Uint8Array\n */\nexport function generateSeed(message: string): Uint8Array {\n // Use encoding utils for consistent string-to-bytes conversion\n const encoder = new TextEncoder();\n return encoder.encode(message);\n}\n\n/**\n * Compares two Uint8Arrays for equality\n *\n * @param a - First array\n * @param b - Second array\n * @returns True if arrays are equal\n */\nexport function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n return true;\n}\n\n/**\n * Creates a copy of a Uint8Array\n *\n * @param bytes - Array to copy\n * @returns New array with same contents\n */\nexport function copyBytes(bytes: Uint8Array): Uint8Array {\n return new Uint8Array(bytes);\n}\n\n/**\n * Validates a secp256k1 public key format\n *\n * @param publicKey - Public key to validate\n * @returns True if valid format (33, 65, or 64 bytes)\n */\nexport function isValidPublicKeyFormat(publicKey: Uint8Array): boolean {\n const len = publicKey.length;\n\n // Compressed (33 bytes)\n if (len === 33) {\n return publicKey[0] === 0x02 || publicKey[0] === 0x03;\n }\n\n // Uncompressed (65 bytes)\n if (len === 65) {\n return publicKey[0] === 0x04;\n }\n\n // Raw coordinates (64 bytes - no prefix)\n if (len === 64) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Validates a secp256k1 private key format\n *\n * @param privateKey - Private key to validate\n * @returns True if valid format (32 bytes)\n */\nexport function isValidPrivateKeyFormat(privateKey: Uint8Array): boolean {\n return privateKey.length === 32;\n}\n\n/**\n * Asserts that a public key is in uncompressed format (65 bytes with 0x04 prefix).\n * This validation function only checks format, it does not transform keys.\n * For key normalization (including decompression), use the crypto provider's\n * normalizeToUncompressed method.\n *\n * @param publicKey - Public key to validate\n * @throws {Error} When public key is not in uncompressed format\n */\nexport function assertUncompressedPublicKey(publicKey: Uint8Array): void {\n if (publicKey.length !== 65) {\n throw new Error(\n `Public key must be uncompressed (65 bytes), got ${publicKey.length} bytes. ` +\n `Use provider.normalizeToUncompressed() to convert compressed keys.`,\n );\n }\n\n if (publicKey[0] !== 0x04) {\n throw new Error(\n `Uncompressed public key must start with 0x04 prefix, got 0x${publicKey[0].toString(16).padStart(2, \"0\")}`,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,kBAAwB;AAmBjB,SAAS,uBACd,WACY;AAEZ,SAAO,OAAO,cAAc,eACxB;AAAA,IACG,UAAU,WAAW,IAAI,IACtB,YACA,KAAK,SAAS;AAAA,IAClB;AAAA,EACF,IACA;AACN;AAcO,SAAS,wBACd,YACY;AAEZ,SAAO,OAAO,eAAe,eACzB;AAAA,IACG,WAAW,WAAW,IAAI,IACvB,aACA,KAAK,UAAU;AAAA,IACnB;AAAA,EACF,IACA;AACN;AASO,SAAS,yBAAyB,iBAKvC;AACA,SAAO;AAAA,IACL,IAAI,gBAAgB,MAAM,GAAG,EAAE;AAAA,IAC/B,gBAAgB,gBAAgB,MAAM,IAAI,EAAE;AAAA;AAAA,IAC5C,YAAY,gBAAgB,MAAM,IAAI,GAAG;AAAA,IACzC,KAAK,gBAAgB,MAAM,GAAG;AAAA,EAChC;AACF;AAQO,SAAS,aAAa,SAA6B;AAExD,QAAM,UAAU,IAAI,YAAY;AAChC,SAAO,QAAQ,OAAO,OAAO;AAC/B;AASO,SAAS,WAAW,GAAe,GAAwB;AAChE,MAAI,EAAE,WAAW,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