@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
167 lines • 5.81 kB
JavaScript
;
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 node_exports = {};
__export(node_exports, {
NodeECIESUint8Provider: () => NodeECIESUint8Provider
});
module.exports = __toCommonJS(node_exports);
var import_crypto = require("crypto");
var import_secp256k1 = __toESM(require("secp256k1"), 1);
var import_base = require("./base");
var import_viem = require("viem");
const secp256k1 = import_secp256k1.default;
class NodeECIESUint8Provider extends import_base.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((0, import_crypto.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(
(0, import_crypto.createHash)("sha512").update(Buffer.from(data)).digest()
);
}
hmacSha256(key, data) {
return new Uint8Array(
(0, import_crypto.createHmac)("sha256", Buffer.from(key)).update(Buffer.from(data)).digest()
);
}
async aesEncrypt(key, iv, plaintext) {
const cipher = (0, import_crypto.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 = (0, import_crypto.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 ${(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 = {
NodeECIESUint8Provider
});
//# sourceMappingURL=node.cjs.map