UNPKG

@zlattice/lattice-js

Version:

Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)

90 lines 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NIST = void 0; const constants_1 = require("../common/constants.js"); const logger_1 = require("../logger.js"); const base58_1 = require("../utils/base58.js"); const string_1 = require("../utils/string.js"); const bytes_1 = require("@ethersproject/bytes"); const secp256k1_1 = require("@noble/curves/secp256k1"); const sha2_1 = require("@noble/hashes/sha2"); const utils_1 = require("@noble/hashes/utils"); class NIST { generateKeyPair() { // Generate a random private key let privateKey; do { privateKey = (0, utils_1.randomBytes)(32); } while (!secp256k1_1.secp256k1.utils.isValidPrivateKey(privateKey)); // Get the corresponding public key const isCompressed = false; const uncompressedPublicKey = secp256k1_1.secp256k1.getPublicKey(privateKey, isCompressed); return { privateKey: Buffer.from(privateKey), publicKey: Buffer.from(uncompressedPublicKey) }; } compressPublicKey(publicKey) { let publicKeyBuffer; if (typeof publicKey === "string") { publicKeyBuffer = Buffer.from(publicKey, "hex"); } else { publicKeyBuffer = publicKey; } if (publicKeyBuffer.length !== 65) { logger_1.log.error("Invalid public key length, expected size is 65, but actual size is %d", publicKeyBuffer.length); throw new Error(`Invalid public key length, expected size is 65, but actual size is ${publicKeyBuffer.length}`); } // calculate x coordinate and y coordinate const x = publicKeyBuffer.subarray(1, 33); const y = BigInt(`0x${publicKeyBuffer.subarray(33, 65).toString("hex")}`); // judge whether the y coordinate is even // `02` represents even, `03` represents odd. let prefix = "02"; if (y % BigInt(2) === BigInt(1)) { prefix = "03"; } return Buffer.from(`${prefix}${x.toString("hex")}`, "hex"); } publicKeyToAddress(publicKey) { let publicKeyBuffer; if (typeof publicKey === "string") { publicKeyBuffer = Buffer.from((0, string_1.stripHexPrefix)(publicKey), "hex"); } else { publicKeyBuffer = publicKey; } if (publicKeyBuffer.length < 64) { throw new Error(`Invalid public key length, expected size greater than or equal to 64, but actual size is ${publicKeyBuffer.length}`); } const bs = this.hash(publicKeyBuffer.subarray(publicKeyBuffer.length - 64)); const base58 = new base58_1.Base58Impl(); const address = base58.checkEncode(bs.subarray(bs.length - constants_1.ADDRESS_BYTES_LENGTH), constants_1.ADDRESS_VERSION); return `${constants_1.ADDRESS_TITLE}_${address}`; } getPublicKeyFromPrivateKey(privateKey, compressed = false) { if (!(0, bytes_1.isHexString)(privateKey)) { throw new Error(`Invalid private key, excepted hex string, but actual is ${privateKey}`); } const publicKey = secp256k1_1.secp256k1.getPublicKey((0, string_1.stripHexPrefix)(privateKey), compressed); return `0x${(0, utils_1.bytesToHex)(publicKey)}`; } hash(data) { return Buffer.from((0, sha2_1.sha256)(data)); } encodeHash(encodeFunc) { const hash = encodeFunc(); return this.hash(hash); } sign(data, privateKey) { const signature = secp256k1_1.secp256k1.sign(data, (0, string_1.stripHexPrefix)(privateKey)); return `0x${signature.toCompactHex()}`; } verify(data, signature, uncompressedPublicKey) { const recoveredSignature = secp256k1_1.secp256k1.Signature.fromCompact((0, string_1.stripHexPrefix)(signature)); return secp256k1_1.secp256k1.verify(recoveredSignature, data, (0, string_1.stripHexPrefix)(uncompressedPublicKey)); } } exports.NIST = NIST; //# sourceMappingURL=crypto-secp256k1.js.map