UNPKG

did-sdk-js

Version:

js sdk for did and vc according to mcps did spec

99 lines 4.57 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sm2 = void 0; const errors_1 = require("../errors"); const crypto_1 = require("./crypto"); const sm2_ex = require("./lib/sm2"); const SM2 = require('sm-crypto').sm2; class Sm2 extends crypto_1.Crypto { static generateMnemonicAndKey() { let mnemonic = Sm2.generateMnemonic(); let privateKey = Sm2.getPrivateKeyFromMnemonic(mnemonic); let publicKey = Sm2.getPublicKeyFromPrivateKey(privateKey); return { mnemonic, privateKey, publicKey }; } /** * Calculates the public key from a given private key. * @param privateKeyHex The private key hexstring * @param mode Pubkey Type 'compress' or '' * @returns Public key {type:type, value:hexstring} */ static getPublicKeyFromPrivateKey(privateKeyHex, mode = 'compress') { if (!privateKeyHex || privateKeyHex.length !== crypto_1.Crypto.PRIVKEY_LEN * 2) { throw new errors_1.SdkError('invalid privateKey'); } return SM2.getPublicKeyFromPrivateKey(privateKeyHex, mode); // do not use'compress' } static generateKey() { let keypair = SM2.generateKeyPairHex(); return { privateKey: keypair.privateKey, publicKey: keypair.publicKey }; } /** * Generates a signature (base64 string) for a signDocSerialize based on given private key. * @param signMsg from protobuf and tx. * @param privateKeyHex The private key. * @returns Signature. Does not include tx. */ static sign(signMsg, privateKeyHex) { return __awaiter(this, void 0, void 0, function* () { let signature = ''; const sm2Sig = SM2.doSignature(Buffer.from(signMsg), privateKeyHex, { hash: true }); signature = Buffer.from(sm2Sig, 'hex').toString('base64'); if (!signature) { throw Error(' generate Signature error '); } return signature; }); } /** * Verifies a signature (64 byte <r,s>) given the sign bytes and public key. * @param sigValue The signature base64 string. * @param signMsg Unsigned transaction sign msg string. * @param publicKeyHex The public key. * @returns Signature. Does not include tx. */ static signVerify(signMsg, sigValue, publicKeyHex) { return __awaiter(this, void 0, void 0, function* () { if (Buffer.from(publicKeyHex, 'hex').length == crypto_1.Crypto.COMPRESSED_PUBKEY_LEN) { publicKeyHex = sm2_ex.publicDecompress(publicKeyHex); } let signature = Buffer.from(sigValue, 'base64').toString('hex'); return SM2.doVerifySignature(Buffer.from(signMsg), signature, publicKeyHex, { hash: true, }); }); } // hex编码 static encrypt(msg, publicKeyHex) { return __awaiter(this, void 0, void 0, function* () { if (Buffer.from(publicKeyHex, 'hex').length == crypto_1.Crypto.COMPRESSED_PUBKEY_LEN) { publicKeyHex = sm2_ex.publicDecompress(publicKeyHex); } // golang 库,添加了“04”前缀,这里统一标准 return "04" + SM2.doEncrypt(msg, publicKeyHex, 1); }); } // hex编码 static decrypt(encryptDataHex, privateKeyHex) { return __awaiter(this, void 0, void 0, function* () { // golang 库,添加了“04”前缀,这里统一标准 if (encryptDataHex.length > 2) { encryptDataHex = encryptDataHex.substr(2); } return SM2.doDecrypt(encryptDataHex, privateKeyHex, 1); }); } } exports.Sm2 = Sm2; Sm2.PRIVKEY_LEN = 32; Sm2.KEY_TYPE = "sm2"; //# sourceMappingURL=sm2.js.map