@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
76 lines • 2.09 kB
JavaScript
import { fromHex } from "viem";
function processWalletPublicKey(publicKey) {
return typeof publicKey === "string" ? fromHex(
publicKey.startsWith("0x") ? publicKey : `0x${publicKey}`,
"bytes"
) : publicKey;
}
function processWalletPrivateKey(privateKey) {
return typeof privateKey === "string" ? fromHex(
privateKey.startsWith("0x") ? privateKey : `0x${privateKey}`,
"bytes"
) : privateKey;
}
function parseEncryptedDataBuffer(encryptedBuffer) {
return {
iv: encryptedBuffer.slice(0, 16),
ephemPublicKey: encryptedBuffer.slice(16, 81),
// 65 bytes for uncompressed public key
ciphertext: encryptedBuffer.slice(81, -32),
mac: encryptedBuffer.slice(-32)
};
}
function generateSeed(message) {
const encoder = new TextEncoder();
return encoder.encode(message);
}
function bytesEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
function copyBytes(bytes) {
return new Uint8Array(bytes);
}
function isValidPublicKeyFormat(publicKey) {
const len = publicKey.length;
if (len === 33) {
return publicKey[0] === 2 || publicKey[0] === 3;
}
if (len === 65) {
return publicKey[0] === 4;
}
if (len === 64) {
return true;
}
return false;
}
function isValidPrivateKeyFormat(privateKey) {
return privateKey.length === 32;
}
function assertUncompressedPublicKey(publicKey) {
if (publicKey.length !== 65) {
throw new Error(
`Public key must be uncompressed (65 bytes), got ${publicKey.length} bytes. Use provider.normalizeToUncompressed() to convert compressed keys.`
);
}
if (publicKey[0] !== 4) {
throw new Error(
`Uncompressed public key must start with 0x04 prefix, got 0x${publicKey[0].toString(16).padStart(2, "0")}`
);
}
}
export {
assertUncompressedPublicKey,
bytesEqual,
copyBytes,
generateSeed,
isValidPrivateKeyFormat,
isValidPublicKeyFormat,
parseEncryptedDataBuffer,
processWalletPrivateKey,
processWalletPublicKey
};
//# sourceMappingURL=crypto-utils.js.map