New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@zilliqa-js/crypto

Package Overview
Dependencies
Maintainers
3
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zilliqa-js/crypto - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

25

dist/bech32.js

@@ -86,3 +86,3 @@ "use strict";

}
exports.encode = function (hrp, data) {
var encode = function (hrp, data) {
var combined = Buffer.concat([data, createChecksum(hrp, data)]);

@@ -95,3 +95,4 @@ var ret = hrp + '1';

};
exports.decode = function (bechString) {
exports.encode = encode;
var decode = function (bechString) {
var p;

@@ -133,2 +134,3 @@ var hasLower = false;

};
exports.decode = decode;
// HRP is the human-readable part of zilliqa bech32 addresses

@@ -151,3 +153,3 @@ exports.HRP = 'zil';

*/
exports.convertBits = function (data, fromWidth, toWidth, pad) {
var convertBits = function (data, fromWidth, toWidth, pad) {
if (pad === void 0) { pad = true; }

@@ -180,2 +182,3 @@ var acc = 0;

};
exports.convertBits = convertBits;
/**

@@ -193,12 +196,13 @@ * toBech32Address

*/
exports.toBech32Address = function (address) {
var toBech32Address = function (address) {
if (!util_1.validation.isAddress(address)) {
throw new Error('Invalid address format.');
}
var addrBz = exports.convertBits(Buffer.from(address.replace('0x', ''), 'hex'), 8, 5);
var addrBz = (0, exports.convertBits)(Buffer.from(address.replace('0x', ''), 'hex'), 8, 5);
if (addrBz === null) {
throw new Error('Could not convert byte Buffer to 5-bit Buffer');
}
return exports.encode(exports.HRP, addrBz);
return (0, exports.encode)(exports.HRP, addrBz);
};
exports.toBech32Address = toBech32Address;
/**

@@ -210,4 +214,4 @@ * fromBech32Address

*/
exports.fromBech32Address = function (address) {
var res = exports.decode(address);
var fromBech32Address = function (address) {
var res = (0, exports.decode)(address);
if (res === null) {

@@ -221,8 +225,9 @@ throw new Error('Invalid bech32 address');

}
var buf = exports.convertBits(data, 5, 8, false);
var buf = (0, exports.convertBits)(data, 5, 8, false);
if (buf === null) {
throw new Error('Could not convert buffer to bytes');
}
return util_2.toChecksumAddress(buf.toString('hex'));
return (0, util_2.toChecksumAddress)(buf.toString('hex'));
};
exports.fromBech32Address = fromBech32Address;
//# sourceMappingURL=bech32.js.map

4

dist/index.d.ts
/// <reference types="node" />
import Signature from 'elliptic/lib/elliptic/ec/signature';
import * as schnorr from './schnorr';

@@ -11,3 +12,3 @@ /**

export declare const sign: (msg: Buffer, privateKey: string, pubKey: string) => string;
export { schnorr };
export { schnorr, Signature };
export * from './util';

@@ -17,4 +18,3 @@ export * from './keystore';

export * from './types';
export * from './signature';
export * from './bech32';
//# sourceMappingURL=index.d.ts.map

@@ -19,5 +19,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.schnorr = exports.sign = void 0;
exports.Signature = exports.schnorr = exports.sign = void 0;
var tslib_1 = require("tslib");
var schnorr = tslib_1.__importStar(require("./schnorr"));
var signature_1 = (0, tslib_1.__importDefault)(require("elliptic/lib/elliptic/ec/signature"));
exports.Signature = signature_1.default;
var schnorr = (0, tslib_1.__importStar)(require("./schnorr"));
exports.schnorr = schnorr;

@@ -31,3 +33,3 @@ /**

*/
exports.sign = function (msg, privateKey, pubKey) {
var sign = function (msg, privateKey, pubKey) {
var sig = schnorr.sign(msg, Buffer.from(privateKey, 'hex'), Buffer.from(pubKey, 'hex'));

@@ -44,8 +46,8 @@ var r = sig.r.toString('hex');

};
tslib_1.__exportStar(require("./util"), exports);
tslib_1.__exportStar(require("./keystore"), exports);
tslib_1.__exportStar(require("./random"), exports);
tslib_1.__exportStar(require("./types"), exports);
tslib_1.__exportStar(require("./signature"), exports);
tslib_1.__exportStar(require("./bech32"), exports);
exports.sign = sign;
(0, tslib_1.__exportStar)(require("./util"), exports);
(0, tslib_1.__exportStar)(require("./keystore"), exports);
(0, tslib_1.__exportStar)(require("./random"), exports);
(0, tslib_1.__exportStar)(require("./types"), exports);
(0, tslib_1.__exportStar)(require("./bech32"), exports);
//# sourceMappingURL=index.js.map

@@ -21,7 +21,7 @@ "use strict";

var tslib_1 = require("tslib");
var aes_js_1 = tslib_1.__importDefault(require("aes-js"));
var hash_js_1 = tslib_1.__importDefault(require("hash.js"));
var aes_js_1 = (0, tslib_1.__importDefault)(require("aes-js"));
var hash_js_1 = (0, tslib_1.__importDefault)(require("hash.js"));
var pbkdf2_1 = require("pbkdf2");
var scrypt_js_1 = tslib_1.__importDefault(require("scrypt-js"));
var uuid_1 = tslib_1.__importDefault(require("uuid"));
var scrypt_js_1 = (0, tslib_1.__importDefault)(require("scrypt-js"));
var uuid_1 = (0, tslib_1.__importDefault)(require("uuid"));
var util_1 = require("@zilliqa-js/util");

@@ -43,9 +43,9 @@ var random_1 = require("./random");

function getDerivedKey(key, kdf, params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var salt, _a, c, dklen, _b, n, r, p, dklen, derivedKeyInt8Array;
return tslib_1.__generator(this, function (_c) {
return (0, tslib_1.__generator)(this, function (_c) {
salt = Buffer.from(params.salt, 'hex');
if (kdf === 'pbkdf2') {
_a = params, c = _a.c, dklen = _a.dklen;
return [2 /*return*/, pbkdf2_1.pbkdf2Sync(key, salt, c, dklen, 'sha256')];
return [2 /*return*/, (0, pbkdf2_1.pbkdf2Sync)(key, salt, c, dklen, 'sha256')];
}

@@ -77,10 +77,10 @@ if (kdf === 'scrypt') {

*/
exports.encryptPrivateKey = function (kdf, privateKey, passphrase) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var encryptPrivateKey = function (kdf, privateKey, passphrase) { return (0, tslib_1.__awaiter)(void 0, void 0, void 0, function () {
var address, salt, iv, kdfparams, derivedKey, cipher, ciphertext;
return tslib_1.__generator(this, function (_a) {
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
address = util_2.getAddressFromPrivateKey(privateKey);
salt = random_1.randomBytes(32);
iv = Buffer.from(random_1.randomBytes(16), 'hex');
address = (0, util_2.getAddressFromPrivateKey)(privateKey);
salt = (0, random_1.randomBytes)(32);
iv = Buffer.from((0, random_1.randomBytes)(16), 'hex');
kdfparams = {

@@ -120,3 +120,3 @@ salt: salt,

},
id: uuid_1.default.v4({ random: util_1.bytes.hexToIntArray(random_1.randomBytes(16)) }),
id: uuid_1.default.v4({ random: util_1.bytes.hexToIntArray((0, random_1.randomBytes)(16)) }),
version: 3,

@@ -127,2 +127,3 @@ })];

}); };
exports.encryptPrivateKey = encryptPrivateKey;
/**

@@ -137,5 +138,5 @@ * decryptPrivateKey

*/
exports.decryptPrivateKey = function (passphrase, keystore) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var decryptPrivateKey = function (passphrase, keystore) { return (0, tslib_1.__awaiter)(void 0, void 0, void 0, function () {
var ciphertext, iv, kdfparams, derivedKey, mac, cipher;
return tslib_1.__generator(this, function (_a) {
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {

@@ -169,2 +170,3 @@ case 0:

}); };
exports.decryptPrivateKey = decryptPrivateKey;
//# sourceMappingURL=keystore.js.map

@@ -29,3 +29,3 @@ "use strict";

*/
exports.randomBytes = function (bytes) {
var randomBytes = function (bytes) {
var _a;

@@ -68,2 +68,3 @@ var b = Buffer.allocUnsafe(bytes);

};
exports.randomBytes = randomBytes;
//# sourceMappingURL=random.js.map
/// <reference types="node" />
/// <reference types="bn.js" />
import { BN } from '@zilliqa-js/util';
import { Signature } from './signature';
import Signature from 'elliptic/lib/elliptic/ec/signature';
/**

@@ -6,0 +6,0 @@ * generatePrivateKey

@@ -21,8 +21,8 @@ "use strict";

var tslib_1 = require("tslib");
var elliptic_1 = tslib_1.__importDefault(require("elliptic"));
var hash_js_1 = tslib_1.__importDefault(require("hash.js"));
var hmac_drbg_1 = tslib_1.__importDefault(require("hmac-drbg"));
var elliptic_1 = (0, tslib_1.__importDefault)(require("elliptic"));
var hash_js_1 = (0, tslib_1.__importDefault)(require("hash.js"));
var hmac_drbg_1 = (0, tslib_1.__importDefault)(require("hmac-drbg"));
var util_1 = require("@zilliqa-js/util");
var random_1 = require("./random");
var signature_1 = require("./signature");
var signature_1 = (0, tslib_1.__importDefault)(require("elliptic/lib/elliptic/ec/signature"));
var secp256k1 = elliptic_1.default.ec('secp256k1');

@@ -49,6 +49,6 @@ var curve = secp256k1.curve;

*/
exports.generatePrivateKey = function () {
var generatePrivateKey = function () {
return secp256k1
.genKeyPair({
entropy: random_1.randomBytes(secp256k1.curve.n.byteLength()),
entropy: (0, random_1.randomBytes)(secp256k1.curve.n.byteLength()),
entropyEnc: HEX_ENC,

@@ -60,2 +60,3 @@ pers: 'zilliqajs+secp256k1+SHA256',

};
exports.generatePrivateKey = generatePrivateKey;
/**

@@ -68,3 +69,3 @@ * Hash (r | M).

*/
exports.hash = function (q, pubkey, msg) {
var hash = function (q, pubkey, msg) {
var sha256 = hash_js_1.default.sha256();

@@ -79,2 +80,3 @@ var totalLength = PUBKEY_COMPRESSED_SIZE_BYTES * 2 + msg.byteLength; // 33 q + 33 pubkey + variable msgLen

};
exports.hash = hash;
/**

@@ -89,3 +91,3 @@ * sign

*/
exports.sign = function (msg, privKey, pubKey) {
var sign = function (msg, privKey, pubKey) {
var prv = new util_1.BN(privKey);

@@ -97,6 +99,7 @@ var drbg = getDRBG(msg);

var k = new util_1.BN(drbg.generate(len));
sig = exports.trySign(msg, k, prv, pubKey);
sig = (0, exports.trySign)(msg, k, prv, pubKey);
}
return sig;
};
exports.sign = sign;
/**

@@ -112,3 +115,3 @@ * trySign

*/
exports.trySign = function (msg, k, privKey, pubKey) {
var trySign = function (msg, k, privKey, pubKey) {
if (privKey.isZero()) {

@@ -134,3 +137,3 @@ throw new Error('Bad private key.');

// mod reduce the r value by the order of secp256k1, n
var r = exports.hash(compressedQ, pubKey, msg).umod(curve.n);
var r = (0, exports.hash)(compressedQ, pubKey, msg).umod(curve.n);
var h = r.clone();

@@ -148,4 +151,5 @@ if (h.isZero()) {

}
return new signature_1.Signature({ r: r, s: s });
return new signature_1.default({ r: r, s: s });
};
exports.trySign = trySign;
/**

@@ -166,4 +170,4 @@ * Verify signature.

*/
exports.verify = function (msg, signature, key) {
var sig = new signature_1.Signature(signature);
var verify = function (msg, signature, key) {
var sig = new signature_1.default(signature);
if (sig.s.isZero() || sig.r.isZero()) {

@@ -189,3 +193,3 @@ throw new Error('Invalid signature');

var compressedQ = new util_1.BN(Q.encodeCompressed());
var r1 = exports.hash(compressedQ, key, msg).umod(curve.n);
var r1 = (0, exports.hash)(compressedQ, key, msg).umod(curve.n);
if (r1.isZero()) {

@@ -196,7 +200,9 @@ throw new Error('Invalid hash.');

};
exports.toSignature = function (serialised) {
exports.verify = verify;
var toSignature = function (serialised) {
var r = serialised.slice(0, 64);
var s = serialised.slice(64);
return new signature_1.Signature({ r: r, s: s });
return new signature_1.default({ r: r, s: s });
};
exports.toSignature = toSignature;
/**

@@ -210,5 +216,5 @@ * Instantiate an HMAC-DRBG.

var getDRBG = function (msg) {
var entropy = random_1.randomBytes(ENT_LEN);
var entropy = (0, random_1.randomBytes)(ENT_LEN);
var pers = Buffer.allocUnsafe(ALG_LEN + ENT_LEN);
Buffer.from(random_1.randomBytes(ENT_LEN)).copy(pers, 0);
Buffer.from((0, random_1.randomBytes)(ENT_LEN)).copy(pers, 0);
ALG.copy(pers, ENT_LEN);

@@ -215,0 +221,0 @@ return new hmac_drbg_1.default({

@@ -21,4 +21,4 @@ "use strict";

var tslib_1 = require("tslib");
var elliptic_1 = tslib_1.__importDefault(require("elliptic"));
var hash_js_1 = tslib_1.__importDefault(require("hash.js"));
var elliptic_1 = (0, tslib_1.__importDefault)(require("elliptic"));
var hash_js_1 = (0, tslib_1.__importDefault)(require("hash.js"));
var util_1 = require("@zilliqa-js/util");

@@ -36,8 +36,9 @@ var bech32_1 = require("./bech32");

*/
exports.getAddressFromPrivateKey = function (privateKey) {
var normalizedPrviateKey = exports.normalizePrivateKey(privateKey);
var getAddressFromPrivateKey = function (privateKey) {
var normalizedPrviateKey = (0, exports.normalizePrivateKey)(privateKey);
var keyPair = secp256k1.keyFromPrivate(normalizedPrviateKey, 'hex');
var pub = keyPair.getPublic(true, 'hex');
return exports.toChecksumAddress(hash_js_1.default.sha256().update(pub, 'hex').digest('hex').slice(24));
return (0, exports.toChecksumAddress)(hash_js_1.default.sha256().update(pub, 'hex').digest('hex').slice(24));
};
exports.getAddressFromPrivateKey = getAddressFromPrivateKey;
/**

@@ -52,7 +53,8 @@ * getPubKeyFromPrivateKey

*/
exports.getPubKeyFromPrivateKey = function (privateKey) {
var normalizedPrviateKey = exports.normalizePrivateKey(privateKey);
var getPubKeyFromPrivateKey = function (privateKey) {
var normalizedPrviateKey = (0, exports.normalizePrivateKey)(privateKey);
var keyPair = secp256k1.keyFromPrivate(normalizedPrviateKey, 'hex');
return keyPair.getPublic(true, 'hex');
};
exports.getPubKeyFromPrivateKey = getPubKeyFromPrivateKey;
/**

@@ -65,8 +67,8 @@ * getAccountFrom0xPrivateKey

*/
exports.getAccountFrom0xPrivateKey = function (privateKeyWith0x) {
var privateKeyWithout0x = exports.normalizePrivateKey(privateKeyWith0x);
var getAccountFrom0xPrivateKey = function (privateKeyWith0x) {
var privateKeyWithout0x = (0, exports.normalizePrivateKey)(privateKeyWith0x);
var keyPair = secp256k1.keyFromPrivate(privateKeyWith0x, 'hex');
var publicKeyWith0x = keyPair.getPublic(true, 'hex');
var addressWith0x = exports.getAddressFromPublicKey(publicKeyWith0x);
var bech32With0x = bech32_1.toBech32Address(addressWith0x);
var addressWith0x = (0, exports.getAddressFromPublicKey)(publicKeyWith0x);
var bech32With0x = (0, bech32_1.toBech32Address)(addressWith0x);
var with0x = {

@@ -80,4 +82,4 @@ prv: privateKeyWith0x,

var publicKeyWithout0x = keyPair2.getPublic(true, 'hex');
var addressWithout0x = exports.getAddressFromPublicKey(publicKeyWithout0x);
var bech32Without0x = bech32_1.toBech32Address(addressWithout0x);
var addressWithout0x = (0, exports.getAddressFromPublicKey)(publicKeyWithout0x);
var bech32Without0x = (0, bech32_1.toBech32Address)(addressWithout0x);
var without0x = {

@@ -91,4 +93,4 @@ prv: privateKeyWithout0x,

var publicKeyAfterChange = keyPair.getPublic(true, 'hex');
var addressAfterChange = exports.getAddressFromPublicKey(publicKeyAfterChange);
var bech32AfterChange = bech32_1.toBech32Address(addressAfterChange);
var addressAfterChange = (0, exports.getAddressFromPublicKey)(publicKeyAfterChange);
var bech32AfterChange = (0, bech32_1.toBech32Address)(addressAfterChange);
var changed = {

@@ -106,2 +108,3 @@ prv: privateKeyAfterChange,

};
exports.getAccountFrom0xPrivateKey = getAccountFrom0xPrivateKey;
/**

@@ -114,5 +117,6 @@ * compressPublicKey

*/
exports.compressPublicKey = function (publicKey) {
var compressPublicKey = function (publicKey) {
return secp256k1.keyFromPublic(publicKey, 'hex').getPublic(true, 'hex');
};
exports.compressPublicKey = compressPublicKey;
/**

@@ -126,6 +130,7 @@ * getAddressFromPublicKey

*/
exports.getAddressFromPublicKey = function (publicKey) {
var getAddressFromPublicKey = function (publicKey) {
var normalized = publicKey.toLowerCase().replace('0x', '');
return exports.toChecksumAddress(hash_js_1.default.sha256().update(normalized, 'hex').digest('hex').slice(24));
return (0, exports.toChecksumAddress)(hash_js_1.default.sha256().update(normalized, 'hex').digest('hex').slice(24));
};
exports.getAddressFromPublicKey = getAddressFromPublicKey;
/**

@@ -139,3 +144,3 @@ * toChecksumAddress

*/
exports.toChecksumAddress = function (address) {
var toChecksumAddress = function (address) {
if (!util_1.validation.isAddress(address)) {

@@ -160,2 +165,3 @@ throw new Error(address + " is not a valid base 16 address");

};
exports.toChecksumAddress = toChecksumAddress;
/**

@@ -169,6 +175,7 @@ * isValidChecksumAddress

*/
exports.isValidChecksumAddress = function (address) {
var isValidChecksumAddress = function (address) {
return (util_1.validation.isAddress(address.replace('0x', '')) &&
exports.toChecksumAddress(address) === address);
(0, exports.toChecksumAddress)(address) === address);
};
exports.isValidChecksumAddress = isValidChecksumAddress;
/**

@@ -183,7 +190,7 @@ * normaliseAddress

*/
exports.normaliseAddress = function (address) {
var normaliseAddress = function (address) {
if (util_1.validation.isBech32(address)) {
return bech32_1.fromBech32Address(address);
return (0, bech32_1.fromBech32Address)(address);
}
if (!exports.isValidChecksumAddress(address)) {
if (!(0, exports.isValidChecksumAddress)(address)) {
throw Error('Wrong address format, should be either bech32 or checksummed address');

@@ -193,2 +200,3 @@ }

};
exports.normaliseAddress = normaliseAddress;
/**

@@ -201,3 +209,3 @@ * encodeBase58 - may be required for DID public key

*/
exports.encodeBase58 = function (hex) {
var encodeBase58 = function (hex) {
var clean = hex.toLowerCase().replace('0x', '');

@@ -228,2 +236,3 @@ var tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

};
exports.encodeBase58 = encodeBase58;
/**

@@ -236,3 +245,3 @@ * decodeBase58 - may be required for DID public key

*/
exports.decodeBase58 = function (raw) {
var decodeBase58 = function (raw) {
var tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

@@ -268,2 +277,3 @@ var base = new util_1.BN(58);

};
exports.decodeBase58 = decodeBase58;
/**

@@ -275,3 +285,3 @@ * verifyPrivateKey

*/
exports.verifyPrivateKey = function (privateKey) {
var verifyPrivateKey = function (privateKey) {
var keyPair = secp256k1.keyFromPrivate(privateKey, 'hex');

@@ -281,2 +291,3 @@ var result = keyPair.validate().result;

};
exports.verifyPrivateKey = verifyPrivateKey;
/**

@@ -288,3 +299,3 @@ * normalizePrivateKey : normalise private key from 0x or without 0x prefix

*/
exports.normalizePrivateKey = function (privateKey) {
var normalizePrivateKey = function (privateKey) {
try {

@@ -295,3 +306,3 @@ if (!util_1.validation.isPrivateKey(privateKey)) {

var normalized = privateKey.toLowerCase().replace('0x', '');
if (!exports.verifyPrivateKey(normalized)) {
if (!(0, exports.verifyPrivateKey)(normalized)) {
throw new Error('Private key is not correct');

@@ -305,2 +316,3 @@ }

};
exports.normalizePrivateKey = normalizePrivateKey;
//# sourceMappingURL=util.js.map
{
"name": "@zilliqa-js/crypto",
"version": "3.1.0",
"version": "3.2.0",
"description": "Core crypto utilities for signing/verification/hashing Zilliqa transactions.",

@@ -25,6 +25,6 @@ "author": "Ian Tan (https://github.com/iantanwx)",

"scripts": {
"clean": "rimraf ./dist"
"clean": "rimraf ./dist ./tsconfig.tsbuildinfo"
},
"dependencies": {
"@zilliqa-js/util": "3.1.0",
"@zilliqa-js/util": "3.2.0",
"aes-js": "^3.1.1",

@@ -50,3 +50,3 @@ "bsert": "^0.0.4",

],
"gitHead": "5812a8d2199d8f4ff584dfd343b91cac3ee0dbbc"
"gitHead": "76b5911234aeb9b928ca81ebace202aa5e6ed815"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc