@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
106 lines • 4.49 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var interface_exports = {};
__export(interface_exports, {
ECIESError: () => ECIESError,
deserializeECIES: () => deserializeECIES,
isECIESEncrypted: () => isECIESEncrypted,
serializeECIES: () => serializeECIES
});
module.exports = __toCommonJS(interface_exports);
var import_constants = require("./constants");
var import_viem = require("viem");
class ECIESError extends Error {
constructor(message, code, cause) {
super(message);
this.code = code;
this.cause = cause;
this.name = "ECIESError";
}
code;
cause;
}
function isECIESEncrypted(obj) {
if (!obj || typeof obj !== "object") return false;
const enc = obj;
const isUint8Array = (value) => {
return value instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(value);
};
return isUint8Array(enc.iv) && enc.iv.length === import_constants.CIPHER.IV_LENGTH && isUint8Array(enc.ephemPublicKey) && (enc.ephemPublicKey.length === import_constants.CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH || enc.ephemPublicKey.length === import_constants.CURVE.COMPRESSED_PUBLIC_KEY_LENGTH) && isUint8Array(enc.ciphertext) && enc.ciphertext.length > 0 && isUint8Array(enc.mac) && enc.mac.length === import_constants.MAC.LENGTH;
}
function serializeECIES(encrypted) {
const combined = new Uint8Array(
encrypted.iv.length + encrypted.ephemPublicKey.length + encrypted.ciphertext.length + encrypted.mac.length
);
let offset = 0;
combined.set(encrypted.iv, offset);
offset += encrypted.iv.length;
combined.set(encrypted.ephemPublicKey, offset);
offset += encrypted.ephemPublicKey.length;
combined.set(encrypted.ciphertext, offset);
offset += encrypted.ciphertext.length;
combined.set(encrypted.mac, offset);
return (0, import_viem.toHex)(combined).slice(2);
}
function deserializeECIES(hex) {
const hexWithPrefix = hex.startsWith("0x") ? hex : `0x${hex}`;
const bytes = (0, import_viem.fromHex)(hexWithPrefix, "bytes");
const absoluteMinLength = import_constants.FORMAT.IV_LENGTH + 1 + import_constants.MAC.LENGTH + 1;
if (bytes.length < absoluteMinLength) {
throw new ECIESError(
`Invalid ECIES data: too short (${bytes.length} bytes, minimum ${absoluteMinLength} bytes required)`,
"DECRYPTION_FAILED"
);
}
const prefix = bytes[import_constants.FORMAT.EPHEMERAL_KEY_OFFSET];
if (prefix !== import_constants.CURVE.PREFIX.UNCOMPRESSED) {
throw new ECIESError(
`Invalid ephemeral public key: must be uncompressed format (0x04 prefix), got 0x${prefix.toString(16).padStart(2, "0")}`,
"DECRYPTION_FAILED"
);
}
const ephemKeySize = import_constants.CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH;
const minLength = import_constants.FORMAT.IV_LENGTH + ephemKeySize + import_constants.MAC.LENGTH + 1;
if (bytes.length < minLength) {
throw new ECIESError(
`Invalid ECIES data: too short (${bytes.length} bytes, minimum ${minLength} bytes required)`,
"DECRYPTION_FAILED"
);
}
return {
iv: bytes.subarray(import_constants.FORMAT.IV_OFFSET, import_constants.FORMAT.IV_OFFSET + import_constants.FORMAT.IV_LENGTH),
ephemPublicKey: bytes.subarray(
import_constants.FORMAT.EPHEMERAL_KEY_OFFSET,
import_constants.FORMAT.EPHEMERAL_KEY_OFFSET + ephemKeySize
),
ciphertext: bytes.subarray(
import_constants.FORMAT.EPHEMERAL_KEY_OFFSET + ephemKeySize,
bytes.length - import_constants.MAC.LENGTH
),
mac: bytes.subarray(bytes.length - import_constants.MAC.LENGTH)
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ECIESError,
deserializeECIES,
isECIESEncrypted,
serializeECIES
});
//# sourceMappingURL=interface.cjs.map