UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

139 lines 4.1 kB
import { randomBytes, createHash, createHmac, createCipheriv, createDecipheriv } from "crypto"; import secp256k1Import from "secp256k1"; import { BaseECIESUint8 } from "./base.js"; import { toHex } from "viem"; const secp256k1 = secp256k1Import; class NodeECIESUint8Provider extends BaseECIESUint8 { // Identity hash function for ECDH - returns raw X coordinate // CRITICAL: Must handle (x, y, output) signature correctly identityHashFn = (x, _y, output) => { if (output && output.length >= 32) { output.set(x); return output; } return x; }; generateRandomBytes(length) { return new Uint8Array(randomBytes(length)); } verifyPrivateKey(privateKey) { return secp256k1.privateKeyVerify(Buffer.from(privateKey)) === true; } createPublicKey(privateKey, compressed) { try { return new Uint8Array( secp256k1.publicKeyCreate(Buffer.from(privateKey), compressed) ); } catch { return null; } } validatePublicKey(publicKey) { return secp256k1.publicKeyVerify(Buffer.from(publicKey)) === true; } decompressPublicKey(publicKey) { try { return new Uint8Array( secp256k1.publicKeyConvert(Buffer.from(publicKey), false) ); } catch { return null; } } performECDH(publicKey, privateKey) { try { const output = Buffer.alloc(32); secp256k1.ecdh( Buffer.from(publicKey), Buffer.from(privateKey), { hashfn: this.identityHashFn }, output ); return new Uint8Array(output); } catch (error) { throw new Error( `ECDH failed: ${error instanceof Error ? error.message : "Unknown error"}` ); } } sha512(data) { return new Uint8Array( createHash("sha512").update(Buffer.from(data)).digest() ); } hmacSha256(key, data) { return new Uint8Array( createHmac("sha256", Buffer.from(key)).update(Buffer.from(data)).digest() ); } async aesEncrypt(key, iv, plaintext) { const cipher = createCipheriv( "aes-256-cbc", Buffer.from(key), Buffer.from(iv) ); const encrypted = Buffer.concat([ cipher.update(Buffer.from(plaintext)), cipher.final() ]); return new Uint8Array(encrypted); } async aesDecrypt(key, iv, ciphertext) { const decipher = createDecipheriv( "aes-256-cbc", Buffer.from(key), Buffer.from(iv) ); const decrypted = Buffer.concat([ decipher.update(Buffer.from(ciphertext)), decipher.final() ]); return new Uint8Array(decrypted); } // No Buffer compatibility methods - Uint8Array only public API /** * 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 ${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` ); } } export { NodeECIESUint8Provider }; //# sourceMappingURL=node.js.map