meru-stablecoin-lib
Version:
A comprehensive TypeScript library for stablecoin operations including encryption utilities, error handling, and validation tools
42 lines • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptionUtil = void 0;
const crypto_1 = __importDefault(require("crypto"));
const config_1 = __importDefault(require("./config"));
class EncryptionUtil {
static createSignature(payload, secretKey) {
return crypto_1.default
.createHmac(EncryptionUtil.HMAC_ALGORITHM, secretKey)
.update(payload)
.digest("hex");
}
static verifySignature(secretKey, encryptedPrivateKey) {
const expected = EncryptionUtil.createSignature(secretKey, EncryptionUtil.ENCRYPTION_KEY);
return crypto_1.default.timingSafeEqual(Buffer.from(encryptedPrivateKey), Buffer.from(expected));
}
static encrypt(text) {
const iv = crypto_1.default.randomBytes(16);
const key = crypto_1.default
.createHash("sha256")
.update(EncryptionUtil.ENCRYPTION_KEY)
.digest();
const cipher = crypto_1.default.createCipheriv("aes-256-cbc", key, iv);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
return `${iv.toString("hex")}:${encrypted}`;
}
}
exports.EncryptionUtil = EncryptionUtil;
EncryptionUtil.HMAC_ALGORITHM = "sha256";
EncryptionUtil.ENCRYPTION_KEY = config_1.default.api_keys.privateEncryptionKey ||
"default-key-please-user-kms-in-production";
EncryptionUtil.generateApiKeys = () => {
const publicKey = `pk_${crypto_1.default.randomBytes(16).toString("hex")}`;
const secretKey = `sk_${crypto_1.default.randomBytes(32).toString("hex")}`;
const hashedSecret = EncryptionUtil.createSignature(secretKey, EncryptionUtil.ENCRYPTION_KEY);
return { publicKey, secretKey, hashedSecret };
};
//# sourceMappingURL=encryption.util.js.map