Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@harmony-js/crypto

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harmony-js/crypto - npm Package Compare versions

Comparing version 0.0.22 to 0.0.23

0

dist/address.d.ts

@@ -0,0 +0,0 @@ export declare class HarmonyAddress {

@@ -0,0 +0,0 @@ /// <reference types="node" />

@@ -0,0 +0,0 @@ export declare type Arrayish = string | ArrayLike<number>;

@@ -0,0 +0,0 @@ export declare const UNKNOWN_ERROR = "UNKNOWN_ERROR";

247

dist/index.cjs.js

@@ -1031,2 +1031,206 @@ /**

// This code is taken from https://github.com/sipa/bech32/tree/bdc264f84014c234e908d72026b7b780122be11f/ref/javascript
// Copyright (c) 2017 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
var polymod = function (values) {
var chk = 1;
// tslint:disable-next-line
for (var p = 0; p < values.length; ++p) {
var top_1 = chk >> 25;
chk = ((chk & 0x1ffffff) << 5) ^ values[p];
for (var i = 0; i < 5; ++i) {
if ((top_1 >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
};
var hrpExpand = function (hrp) {
var ret = [];
var p;
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) >> 5);
}
ret.push(0);
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) & 31);
}
return Buffer.from(ret);
};
function verifyChecksum(hrp, data) {
return polymod(Buffer.concat([hrpExpand(hrp), data])) === 1;
}
function createChecksum(hrp, data) {
var values = Buffer.concat([
Buffer.from(hrpExpand(hrp)),
data,
Buffer.from([0, 0, 0, 0, 0, 0]),
]);
// var values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
var mod = polymod(values) ^ 1;
var ret = [];
for (var p = 0; p < 6; ++p) {
ret.push((mod >> (5 * (5 - p))) & 31);
}
return Buffer.from(ret);
}
var bech32Encode = function (hrp, data) {
var combined = Buffer.concat([data, createChecksum(hrp, data)]);
var ret = hrp + '1';
// tslint:disable-next-line
for (var p = 0; p < combined.length; ++p) {
ret += CHARSET.charAt(combined[p]);
}
return ret;
};
var bech32Decode = function (bechString) {
var p;
var hasLower = false;
var hasUpper = false;
for (p = 0; p < bechString.length; ++p) {
if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) {
return null;
}
if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) {
hasLower = true;
}
if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) {
hasUpper = true;
}
}
if (hasLower && hasUpper) {
return null;
}
bechString = bechString.toLowerCase();
var pos = bechString.lastIndexOf('1');
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
return null;
}
var hrp = bechString.substring(0, pos);
var data = [];
for (p = pos + 1; p < bechString.length; ++p) {
var d = CHARSET.indexOf(bechString.charAt(p));
if (d === -1) {
return null;
}
data.push(d);
}
if (!verifyChecksum(hrp, Buffer.from(data))) {
return null;
}
return { hrp: hrp, data: Buffer.from(data.slice(0, data.length - 6)) };
};
// HRP is the human-readable part of zilliqa bech32 addresses
var HRP = 'hmy';
var tHRP = 'thmy';
/**
* convertBits
*
* groups buffers of a certain width to buffers of the desired width.
*
* For example, converts byte buffers to buffers of maximum 5 bit numbers,
* padding those numbers as necessary. Necessary for encoding Ethereum-style
* addresses as bech32 ones.
*
* @param {Buffer} data
* @param {number} fromWidth
* @param {number} toWidth
* @param {boolean} pad
* @returns {Buffer|null}
*/
var convertBits = function (data, fromWidth, toWidth, pad) {
if (pad === void 0) { pad = true; }
var acc = 0;
var bits = 0;
var ret = [];
var maxv = (1 << toWidth) - 1;
// tslint:disable-next-line
for (var p = 0; p < data.length; ++p) {
var value = data[p];
if (value < 0 || value >> fromWidth !== 0) {
return null;
}
acc = (acc << fromWidth) | value;
bits += fromWidth;
while (bits >= toWidth) {
bits -= toWidth;
ret.push((acc >> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
ret.push((acc << (toWidth - bits)) & maxv);
}
}
else if (bits >= fromWidth || (acc << (toWidth - bits)) & maxv) {
return null;
}
return Buffer.from(ret);
};
/**
* toBech32Address
*
* bech32Encodes a canonical 20-byte Ethereum-style address as a bech32 zilliqa
* address.
*
* The expected format is zil1<address><checksum> where address and checksum
* are the result of bech32 encoding a Buffer containing the address bytes.
*
* @param {string} 20 byte canonical address
* @returns {string} 38 char bech32 bech32Encoded zilliqa address
*/
var toBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
if (!utils.isAddress(address)) {
throw new Error('Invalid address format.');
}
var addrBz = 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 bech32Encode(useHRP, addrBz);
};
/**
* fromBech32Address
*
* @param {string} address - a valid Zilliqa bech32 address
* @returns {string} a canonical 20-byte Ethereum-style address
*/
var fromBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
var res = bech32Decode(address);
if (res === null) {
throw new Error('Invalid bech32 address');
}
var hrp = res.hrp, data = res.data;
if (hrp !== useHRP) {
throw new Error("Expected hrp to be " + useHRP + " but got " + hrp);
}
var buf = convertBits(data, 5, 8, false);
if (buf === null) {
throw new Error('Could not convert buffer to bytes');
}
return toChecksumAddress('0x' + buf.toString('hex'));
};
var HarmonyAddress = /** @class */ (function () {

@@ -1047,2 +1251,12 @@ function HarmonyAddress(raw) {

};
// static validator
HarmonyAddress.isValidBech32 = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32;
};
// static validator
HarmonyAddress.isValidBech32TestNet = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32TestNet;
};
Object.defineProperty(HarmonyAddress.prototype, "basicHex", {

@@ -1057,3 +1271,3 @@ get: function () {

get: function () {
return toChecksumAddress(this.basic);
return toChecksumAddress("0x" + this.basic);
},

@@ -1063,7 +1277,31 @@ enumerable: true,

});
Object.defineProperty(HarmonyAddress.prototype, "bech32", {
get: function () {
return toBech32(this.basic, HRP);
},
enumerable: true,
configurable: true
});
Object.defineProperty(HarmonyAddress.prototype, "bech32TestNet", {
get: function () {
return toBech32(this.basic, tHRP);
},
enumerable: true,
configurable: true
});
HarmonyAddress.prototype.getBasic = function (addr) {
var basicBool = utils.isAddress(addr);
var bech32Bool = utils.isBech32Address(addr);
var bech32TestNetBool = utils.isBech32TestNetAddress(addr);
if (basicBool) {
return addr.replace('0x', '').toLowerCase();
}
if (bech32Bool) {
var fromB32 = fromBech32(addr, HRP);
return fromB32.replace('0x', '').toLowerCase();
}
if (bech32TestNetBool) {
var fromB32TestNet = fromBech32(addr, tHRP);
return fromB32TestNet.replace('0x', '').toLowerCase();
}
throw new Error(addr + " is valid address format");

@@ -1143,4 +1381,11 @@ };

exports.info = info;
exports.bech32Encode = bech32Encode;
exports.bech32Decode = bech32Decode;
exports.HRP = HRP;
exports.tHRP = tHRP;
exports.convertBits = convertBits;
exports.toBech32 = toBech32;
exports.fromBech32 = fromBech32;
exports.HarmonyAddress = HarmonyAddress;
exports.getAddress = getAddress;
//# sourceMappingURL=index.cjs.js.map

1

dist/index.d.ts

@@ -11,3 +11,2 @@ import hdkey from 'hdkey';

export * from './errors';
export * from './base58';
export * from './bech32';

@@ -14,0 +13,0 @@ export * from './types';

@@ -7,3 +7,3 @@ /**

import elliptic from 'elliptic';
import { isPrivateKey, strip0x, isAddress } from '@harmony-js/utils';
import { isPrivateKey, strip0x, isAddress, isBech32Address, isBech32TestNetAddress } from '@harmony-js/utils';
import aes from 'aes-js';

@@ -1029,2 +1029,206 @@ import scrypt from 'scrypt.js';

// This code is taken from https://github.com/sipa/bech32/tree/bdc264f84014c234e908d72026b7b780122be11f/ref/javascript
// Copyright (c) 2017 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
var polymod = function (values) {
var chk = 1;
// tslint:disable-next-line
for (var p = 0; p < values.length; ++p) {
var top_1 = chk >> 25;
chk = ((chk & 0x1ffffff) << 5) ^ values[p];
for (var i = 0; i < 5; ++i) {
if ((top_1 >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
};
var hrpExpand = function (hrp) {
var ret = [];
var p;
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) >> 5);
}
ret.push(0);
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) & 31);
}
return Buffer.from(ret);
};
function verifyChecksum(hrp, data) {
return polymod(Buffer.concat([hrpExpand(hrp), data])) === 1;
}
function createChecksum(hrp, data) {
var values = Buffer.concat([
Buffer.from(hrpExpand(hrp)),
data,
Buffer.from([0, 0, 0, 0, 0, 0]),
]);
// var values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
var mod = polymod(values) ^ 1;
var ret = [];
for (var p = 0; p < 6; ++p) {
ret.push((mod >> (5 * (5 - p))) & 31);
}
return Buffer.from(ret);
}
var bech32Encode = function (hrp, data) {
var combined = Buffer.concat([data, createChecksum(hrp, data)]);
var ret = hrp + '1';
// tslint:disable-next-line
for (var p = 0; p < combined.length; ++p) {
ret += CHARSET.charAt(combined[p]);
}
return ret;
};
var bech32Decode = function (bechString) {
var p;
var hasLower = false;
var hasUpper = false;
for (p = 0; p < bechString.length; ++p) {
if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) {
return null;
}
if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) {
hasLower = true;
}
if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) {
hasUpper = true;
}
}
if (hasLower && hasUpper) {
return null;
}
bechString = bechString.toLowerCase();
var pos = bechString.lastIndexOf('1');
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
return null;
}
var hrp = bechString.substring(0, pos);
var data = [];
for (p = pos + 1; p < bechString.length; ++p) {
var d = CHARSET.indexOf(bechString.charAt(p));
if (d === -1) {
return null;
}
data.push(d);
}
if (!verifyChecksum(hrp, Buffer.from(data))) {
return null;
}
return { hrp: hrp, data: Buffer.from(data.slice(0, data.length - 6)) };
};
// HRP is the human-readable part of zilliqa bech32 addresses
var HRP = 'hmy';
var tHRP = 'thmy';
/**
* convertBits
*
* groups buffers of a certain width to buffers of the desired width.
*
* For example, converts byte buffers to buffers of maximum 5 bit numbers,
* padding those numbers as necessary. Necessary for encoding Ethereum-style
* addresses as bech32 ones.
*
* @param {Buffer} data
* @param {number} fromWidth
* @param {number} toWidth
* @param {boolean} pad
* @returns {Buffer|null}
*/
var convertBits = function (data, fromWidth, toWidth, pad) {
if (pad === void 0) { pad = true; }
var acc = 0;
var bits = 0;
var ret = [];
var maxv = (1 << toWidth) - 1;
// tslint:disable-next-line
for (var p = 0; p < data.length; ++p) {
var value = data[p];
if (value < 0 || value >> fromWidth !== 0) {
return null;
}
acc = (acc << fromWidth) | value;
bits += fromWidth;
while (bits >= toWidth) {
bits -= toWidth;
ret.push((acc >> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
ret.push((acc << (toWidth - bits)) & maxv);
}
}
else if (bits >= fromWidth || (acc << (toWidth - bits)) & maxv) {
return null;
}
return Buffer.from(ret);
};
/**
* toBech32Address
*
* bech32Encodes a canonical 20-byte Ethereum-style address as a bech32 zilliqa
* address.
*
* The expected format is zil1<address><checksum> where address and checksum
* are the result of bech32 encoding a Buffer containing the address bytes.
*
* @param {string} 20 byte canonical address
* @returns {string} 38 char bech32 bech32Encoded zilliqa address
*/
var toBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
if (!isAddress(address)) {
throw new Error('Invalid address format.');
}
var addrBz = 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 bech32Encode(useHRP, addrBz);
};
/**
* fromBech32Address
*
* @param {string} address - a valid Zilliqa bech32 address
* @returns {string} a canonical 20-byte Ethereum-style address
*/
var fromBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
var res = bech32Decode(address);
if (res === null) {
throw new Error('Invalid bech32 address');
}
var hrp = res.hrp, data = res.data;
if (hrp !== useHRP) {
throw new Error("Expected hrp to be " + useHRP + " but got " + hrp);
}
var buf = convertBits(data, 5, 8, false);
if (buf === null) {
throw new Error('Could not convert buffer to bytes');
}
return toChecksumAddress('0x' + buf.toString('hex'));
};
var HarmonyAddress = /** @class */ (function () {

@@ -1045,2 +1249,12 @@ function HarmonyAddress(raw) {

};
// static validator
HarmonyAddress.isValidBech32 = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32;
};
// static validator
HarmonyAddress.isValidBech32TestNet = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32TestNet;
};
Object.defineProperty(HarmonyAddress.prototype, "basicHex", {

@@ -1055,3 +1269,3 @@ get: function () {

get: function () {
return toChecksumAddress(this.basic);
return toChecksumAddress("0x" + this.basic);
},

@@ -1061,7 +1275,31 @@ enumerable: true,

});
Object.defineProperty(HarmonyAddress.prototype, "bech32", {
get: function () {
return toBech32(this.basic, HRP);
},
enumerable: true,
configurable: true
});
Object.defineProperty(HarmonyAddress.prototype, "bech32TestNet", {
get: function () {
return toBech32(this.basic, tHRP);
},
enumerable: true,
configurable: true
});
HarmonyAddress.prototype.getBasic = function (addr) {
var basicBool = isAddress(addr);
var bech32Bool = isBech32Address(addr);
var bech32TestNetBool = isBech32TestNetAddress(addr);
if (basicBool) {
return addr.replace('0x', '').toLowerCase();
}
if (bech32Bool) {
var fromB32 = fromBech32(addr, HRP);
return fromB32.replace('0x', '').toLowerCase();
}
if (bech32TestNetBool) {
var fromB32TestNet = fromBech32(addr, tHRP);
return fromB32TestNet.replace('0x', '').toLowerCase();
}
throw new Error(addr + " is valid address format");

@@ -1080,3 +1318,3 @@ };

export { randomBytes, generatePrivateKey, getPubkeyFromPrivateKey, getAddressFromPrivateKey, getPublic, getAddressFromPublicKey, toChecksumAddress, sign, getContractAddress, verifySignature, recoverPublicKey, recoverAddress, isValidChecksumAddress, encrypt, decrypt, isHexable, isArrayish, arrayify, concat, stripZeros, padZeros, isHexString, hexlify, hexDataLength, hexDataSlice, hexStripZeros, hexZeroPad, bytesPadLeft, bytesPadRight, isSignature, splitSignature, joinSignature, hexToByteArray, hexToIntArray, isHex, encode, decode, keccak256, UNKNOWN_ERROR, NOT_IMPLEMENTED, MISSING_NEW, CALL_EXCEPTION, INVALID_ARGUMENT, MISSING_ARGUMENT, UNEXPECTED_ARGUMENT, NUMERIC_FAULT, INSUFFICIENT_FUNDS, NONCE_EXPIRED, REPLACEMENT_UNDERPRICED, UNSUPPORTED_OPERATION, throwError, checkNew, checkArgumentCount, setCensorship, checkNormalize, setLogLevel, warn, info, HarmonyAddress, getAddress };
export { randomBytes, generatePrivateKey, getPubkeyFromPrivateKey, getAddressFromPrivateKey, getPublic, getAddressFromPublicKey, toChecksumAddress, sign, getContractAddress, verifySignature, recoverPublicKey, recoverAddress, isValidChecksumAddress, encrypt, decrypt, isHexable, isArrayish, arrayify, concat, stripZeros, padZeros, isHexString, hexlify, hexDataLength, hexDataSlice, hexStripZeros, hexZeroPad, bytesPadLeft, bytesPadRight, isSignature, splitSignature, joinSignature, hexToByteArray, hexToIntArray, isHex, encode, decode, keccak256, UNKNOWN_ERROR, NOT_IMPLEMENTED, MISSING_NEW, CALL_EXCEPTION, INVALID_ARGUMENT, MISSING_ARGUMENT, UNEXPECTED_ARGUMENT, NUMERIC_FAULT, INSUFFICIENT_FUNDS, NONCE_EXPIRED, REPLACEMENT_UNDERPRICED, UNSUPPORTED_OPERATION, throwError, checkNew, checkArgumentCount, setCensorship, checkNormalize, setLogLevel, warn, info, bech32Encode, bech32Decode, HRP, tHRP, convertBits, toBech32, fromBech32, HarmonyAddress, getAddress };
//# sourceMappingURL=index.esm.js.map

@@ -17,5 +17,4 @@ "use strict";

tslib_1.__exportStar(require("./errors"), exports);
tslib_1.__exportStar(require("./base58"), exports);
tslib_1.__exportStar(require("./bech32"), exports);
tslib_1.__exportStar(require("./address"), exports);
//# sourceMappingURL=index.js.map

@@ -1028,2 +1028,206 @@ /**

// This code is taken from https://github.com/sipa/bech32/tree/bdc264f84014c234e908d72026b7b780122be11f/ref/javascript
// Copyright (c) 2017 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
var polymod = function (values) {
var chk = 1;
// tslint:disable-next-line
for (var p = 0; p < values.length; ++p) {
var top_1 = chk >> 25;
chk = ((chk & 0x1ffffff) << 5) ^ values[p];
for (var i = 0; i < 5; ++i) {
if ((top_1 >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
};
var hrpExpand = function (hrp) {
var ret = [];
var p;
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) >> 5);
}
ret.push(0);
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) & 31);
}
return Buffer.from(ret);
};
function verifyChecksum(hrp, data) {
return polymod(Buffer.concat([hrpExpand(hrp), data])) === 1;
}
function createChecksum(hrp, data) {
var values = Buffer.concat([
Buffer.from(hrpExpand(hrp)),
data,
Buffer.from([0, 0, 0, 0, 0, 0]),
]);
// var values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
var mod = polymod(values) ^ 1;
var ret = [];
for (var p = 0; p < 6; ++p) {
ret.push((mod >> (5 * (5 - p))) & 31);
}
return Buffer.from(ret);
}
var bech32Encode = function (hrp, data) {
var combined = Buffer.concat([data, createChecksum(hrp, data)]);
var ret = hrp + '1';
// tslint:disable-next-line
for (var p = 0; p < combined.length; ++p) {
ret += CHARSET.charAt(combined[p]);
}
return ret;
};
var bech32Decode = function (bechString) {
var p;
var hasLower = false;
var hasUpper = false;
for (p = 0; p < bechString.length; ++p) {
if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) {
return null;
}
if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) {
hasLower = true;
}
if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) {
hasUpper = true;
}
}
if (hasLower && hasUpper) {
return null;
}
bechString = bechString.toLowerCase();
var pos = bechString.lastIndexOf('1');
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
return null;
}
var hrp = bechString.substring(0, pos);
var data = [];
for (p = pos + 1; p < bechString.length; ++p) {
var d = CHARSET.indexOf(bechString.charAt(p));
if (d === -1) {
return null;
}
data.push(d);
}
if (!verifyChecksum(hrp, Buffer.from(data))) {
return null;
}
return { hrp: hrp, data: Buffer.from(data.slice(0, data.length - 6)) };
};
// HRP is the human-readable part of zilliqa bech32 addresses
var HRP = 'hmy';
var tHRP = 'thmy';
/**
* convertBits
*
* groups buffers of a certain width to buffers of the desired width.
*
* For example, converts byte buffers to buffers of maximum 5 bit numbers,
* padding those numbers as necessary. Necessary for encoding Ethereum-style
* addresses as bech32 ones.
*
* @param {Buffer} data
* @param {number} fromWidth
* @param {number} toWidth
* @param {boolean} pad
* @returns {Buffer|null}
*/
var convertBits = function (data, fromWidth, toWidth, pad) {
if (pad === void 0) { pad = true; }
var acc = 0;
var bits = 0;
var ret = [];
var maxv = (1 << toWidth) - 1;
// tslint:disable-next-line
for (var p = 0; p < data.length; ++p) {
var value = data[p];
if (value < 0 || value >> fromWidth !== 0) {
return null;
}
acc = (acc << fromWidth) | value;
bits += fromWidth;
while (bits >= toWidth) {
bits -= toWidth;
ret.push((acc >> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
ret.push((acc << (toWidth - bits)) & maxv);
}
}
else if (bits >= fromWidth || (acc << (toWidth - bits)) & maxv) {
return null;
}
return Buffer.from(ret);
};
/**
* toBech32Address
*
* bech32Encodes a canonical 20-byte Ethereum-style address as a bech32 zilliqa
* address.
*
* The expected format is zil1<address><checksum> where address and checksum
* are the result of bech32 encoding a Buffer containing the address bytes.
*
* @param {string} 20 byte canonical address
* @returns {string} 38 char bech32 bech32Encoded zilliqa address
*/
var toBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
if (!utils.isAddress(address)) {
throw new Error('Invalid address format.');
}
var addrBz = 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 bech32Encode(useHRP, addrBz);
};
/**
* fromBech32Address
*
* @param {string} address - a valid Zilliqa bech32 address
* @returns {string} a canonical 20-byte Ethereum-style address
*/
var fromBech32 = function (address, useHRP) {
if (useHRP === void 0) { useHRP = HRP; }
var res = bech32Decode(address);
if (res === null) {
throw new Error('Invalid bech32 address');
}
var hrp = res.hrp, data = res.data;
if (hrp !== useHRP) {
throw new Error("Expected hrp to be " + useHRP + " but got " + hrp);
}
var buf = convertBits(data, 5, 8, false);
if (buf === null) {
throw new Error('Could not convert buffer to bytes');
}
return toChecksumAddress('0x' + buf.toString('hex'));
};
var HarmonyAddress = /** @class */ (function () {

@@ -1044,2 +1248,12 @@ function HarmonyAddress(raw) {

};
// static validator
HarmonyAddress.isValidBech32 = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32;
};
// static validator
HarmonyAddress.isValidBech32TestNet = function (str) {
var toTest = new HarmonyAddress(str);
return toTest.raw === toTest.bech32TestNet;
};
Object.defineProperty(HarmonyAddress.prototype, "basicHex", {

@@ -1054,3 +1268,3 @@ get: function () {

get: function () {
return toChecksumAddress(this.basic);
return toChecksumAddress("0x" + this.basic);
},

@@ -1060,7 +1274,31 @@ enumerable: true,

});
Object.defineProperty(HarmonyAddress.prototype, "bech32", {
get: function () {
return toBech32(this.basic, HRP);
},
enumerable: true,
configurable: true
});
Object.defineProperty(HarmonyAddress.prototype, "bech32TestNet", {
get: function () {
return toBech32(this.basic, tHRP);
},
enumerable: true,
configurable: true
});
HarmonyAddress.prototype.getBasic = function (addr) {
var basicBool = utils.isAddress(addr);
var bech32Bool = utils.isBech32Address(addr);
var bech32TestNetBool = utils.isBech32TestNetAddress(addr);
if (basicBool) {
return addr.replace('0x', '').toLowerCase();
}
if (bech32Bool) {
var fromB32 = fromBech32(addr, HRP);
return fromB32.replace('0x', '').toLowerCase();
}
if (bech32TestNetBool) {
var fromB32TestNet = fromBech32(addr, tHRP);
return fromB32TestNet.replace('0x', '').toLowerCase();
}
throw new Error(addr + " is valid address format");

@@ -1140,2 +1378,9 @@ };

exports.info = info;
exports.bech32Encode = bech32Encode;
exports.bech32Decode = bech32Decode;
exports.HRP = HRP;
exports.tHRP = tHRP;
exports.convertBits = convertBits;
exports.toBech32 = toBech32;
exports.fromBech32 = fromBech32;
exports.HarmonyAddress = HarmonyAddress;

@@ -1142,0 +1387,0 @@ exports.getAddress = getAddress;

import { Arrayish } from './bytes';
export declare function keccak256(data: Arrayish): string;
//# sourceMappingURL=keccak256.d.ts.map

@@ -0,0 +0,0 @@ import { EncryptOptions, Keystore } from './types';

@@ -0,0 +0,0 @@ import * as bytes from './bytes';

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ import { Arrayish } from './bytes';

@@ -0,0 +0,0 @@ export declare type KDF = 'pbkdf2' | 'scrypt';

{
"name": "@harmony-js/crypto",
"version": "0.0.22",
"version": "0.0.23",
"description": "crypto libraries for harmony",

@@ -21,5 +21,4 @@ "main": "dist/index.js",

"dependencies": {
"@harmony-js/utils": "0.0.22",
"@harmony-js/utils": "0.0.23",
"aes-js": "^3.1.2",
"base-x": "^3.0.5",
"bip39": "^2.5.0",

@@ -35,3 +34,3 @@ "bn.js": "^4.11.8",

},
"gitHead": "5c53415c333ab12d727fc1808be2e603d40489e0"
"gitHead": "2b54d4deba2169372508da8f0a1816575e25a47c"
}

@@ -12,3 +12,2 @@ import hdkey from 'hdkey';

export * from './errors';
export * from './base58';
export * from './bech32';

@@ -15,0 +14,0 @@

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

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

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