@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
108 lines • 5.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GM = void 0;
const constants_1 = require("../common/constants.js");
const constants_2 = require("../common/constants.js");
const sm2_1 = require("../crypto/sm2/index.js");
const sm3_1 = __importDefault(require("../crypto/sm3/index.js"));
const logger_1 = require("../logger.js");
const base58_1 = require("../utils/base58.js");
const index_1 = require("../utils/index.js");
const bytes_1 = require("@ethersproject/bytes");
class GM {
generateKeyPair() {
const { privateKey, publicKey: uncompressedPublicKey } = (0, sm2_1.generateKeyPairHex)();
return {
privateKey: Buffer.from(privateKey, "hex"),
publicKey: Buffer.from(uncompressedPublicKey, "hex")
};
}
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 uncompressed public key length, expected size is 65, but actual size is %d", publicKeyBuffer.length);
throw new Error(`Invalid uncompressed public key length, expected size is 65, but actual size is ${publicKeyBuffer.length}`);
}
const compressedPublicKey = (0, sm2_1.compressPublicKeyHex)(publicKey.toString("hex"));
return Buffer.from(compressedPublicKey, "hex");
}
publicKeyToAddress(publicKey) {
let publicKeyBuffer;
if (typeof publicKey === "string") {
publicKeyBuffer = Buffer.from((0, index_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_2.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}`);
}
return (0, sm2_1.getPublicKeyFromPrivateKey)(privateKey.startsWith(constants_1.HEX_PREFIX)
? privateKey.slice(constants_1.HEX_PREFIX.length)
: privateKey, compressed);
}
hash(data) {
const hash = (0, sm3_1.default)(data); // 杂凑
return Buffer.from(hash, "hex");
}
encodeHash(encodeFunc) {
const hash = encodeFunc();
return this.hash(hash);
}
sign(data, privateKey) {
const buffer = Buffer.from(privateKey, "hex");
if (buffer.length !== 32) {
throw new Error(`Invalid private key length, expected size is 32, but actual size is ${buffer.length}`);
}
const signature = (0, sm2_1.doSignature)(data, privateKey, {
hash: true
});
const signatureBuffer = Buffer.alloc(constants_1.SM2P256V1_SIGNATURE_LENGTH);
signatureBuffer.write(signature, 0, 64, "hex"); // r、s
signatureBuffer.write(constants_1.SM2P256V1_SIGNATURE_REMARK, 64, 1, "hex"); // remark
// calculate e point
const publicKey = (0, sm2_1.getPublicKeyFromPrivateKey)(privateKey);
const hashHex = (0, sm2_1.getHash)(data, publicKey);
signatureBuffer.write(hashHex, 65, 32, "hex");
if (signatureBuffer.length !== constants_1.SM2P256V1_SIGNATURE_LENGTH) {
throw new Error(`Invalid signature length, expected size is ${constants_1.SM2P256V1_SIGNATURE_LENGTH}, but actual size is ${signatureBuffer.length}`);
}
return signatureBuffer.toString("hex");
}
verify(data, signature, uncompressedPublicKey) {
const buffer = Buffer.from(uncompressedPublicKey, "hex");
if (buffer.length !== 65) {
throw new Error(`Invalid uncompressed public key length, expected size is 65, but actual size is ${buffer.length}`);
}
let tempSignature = signature;
if (signature.startsWith(constants_1.HEX_PREFIX)) {
tempSignature = signature.slice(constants_1.HEX_PREFIX.length);
}
if (tempSignature.length > 128) {
tempSignature = tempSignature.slice(0, 128);
}
return (0, sm2_1.doVerifySignature)(data, tempSignature, uncompressedPublicKey, {
hash: true
});
}
}
exports.GM = GM;
//# sourceMappingURL=crypto-sm2p256v1.js.map