@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
131 lines • 3.67 kB
JavaScript
import * as secp256k1 from "@noble/secp256k1";
import { BaseECIESUint8 } from "./base.js";
import { toHex } from "viem";
import { hmac } from "@noble/hashes/hmac";
import { sha256, sha512 as nobleSha512 } from "@noble/hashes/sha2";
class BrowserECIESUint8Provider extends 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 nobleSha512(data);
}
hmacSha256(key, data) {
return hmac(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 ${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 {
BrowserECIESUint8Provider
};
//# sourceMappingURL=browser.js.map