did-sdk-js
Version:
js sdk for did and vc according to mcps did spec
110 lines • 4.11 kB
JavaScript
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.Crypto = void 0;
const bip32 = require("bip32");
const bip39 = require("bip39");
const errors_1 = require("../errors");
const utils_1 = require("../utils");
/**
* Crypto Utils
* @hidden
*/
class Crypto {
/**
* Generates mnemonic phrase words using random entropy.
*
* @returns Mnemonic
*/
static generateMnemonic() {
return bip39.generateMnemonic(Crypto.MNEMONIC_LEN);
}
static generateKey() {
return { privateKey: "", publicKey: "" };
}
static generateMnemonicAndKey() {
return { mnemonic: "", privateKey: "", publicKey: "" };
}
/**
* Gets an address from a public key hex.
* @param publicKeyHex The public key hexstring
* @param prefix The address prefix
*
* @returns The address
*/
static getBech32AddressFromPublicKey(publicKeyHex, prefix = '') {
let hash = utils_1.Utils.sha256ripemd160(publicKeyHex);
return utils_1.AddressUtils.convertAndEncode('', Buffer.from(hash, 'hex'));
}
/**
* Gets a private key from mnemonic words.
* @param mnemonic The mnemonic phrase words
* @param derive Derive a private key using the default HD path (default: true)
* @param index The bip44 address index (default: 0)
* @param password A passphrase for generating the salt, according to bip39
* @returns hexstring
*/
static getPrivateKeyFromMnemonic(mnemonic, index = 0, derive = true, password = '') {
if (!bip39.validateMnemonic(mnemonic)) {
throw new errors_1.SdkError('wrong mnemonic format');
}
const seed = bip39.mnemonicToSeedSync(mnemonic, password);
if (derive) {
const master = bip32.fromSeed(seed);
const child = master.derivePath(Crypto.HDPATH + index);
if (typeof child === 'undefined' ||
typeof child.privateKey === 'undefined') {
throw new errors_1.SdkError('error getting private key from mnemonic');
}
return child.privateKey.toString('hex');
}
return seed.toString('hex');
}
static sign(signMsg, privateKeyHex) {
return __awaiter(this, void 0, void 0, function* () {
// return base64 encode value
return "";
});
}
static signVerify(signMsg, // base64 encode
sigValue, publicKeyHex) {
return __awaiter(this, void 0, void 0, function* () {
return false;
});
}
// to hex str
static encrypt(msg, publicKeyHex) {
return __awaiter(this, void 0, void 0, function* () {
return "";
});
}
// hex编码
static decrypt(encryptDataHex, privateKeyHex) {
return __awaiter(this, void 0, void 0, function* () {
return "";
});
}
}
exports.Crypto = Crypto;
Crypto.PRIVKEY_LEN = 32;
Crypto.MNEMONIC_LEN = 256;
Crypto.DECODED_ADDRESS_LEN = 20;
Crypto.COMPRESSED_PUBKEY_LEN = 33;
Crypto.UNCOMPRESSED_PUBKEY_LEN = 65;
//hdpath
Crypto.HDPATH = "44'/118'/0'/0/";
/**
* Validates mnemonic phrase words.
* @param mnemonic The mnemonic phrase words
* @returns Validation result
*/
Crypto.validateMnemonic = bip39.validateMnemonic;
//# sourceMappingURL=crypto.js.map
;