@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
79 lines • 3.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addressesAreEqual = exports.stripHexPrefix = exports.hashBytes = exports.hashString = exports.addressToBytes32 = exports.argToBytes = exports.toBytesNoPadding = exports.toBytes = exports.bytesToHexString = exports.hexStringToBytes = void 0;
const ethers_1 = require("ethers");
const web3_1 = __importDefault(require("web3"));
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function hexStringToBytes(hex) {
if (!hex || hex === '0x') {
return [];
}
return web3_1.default.utils.hexToBytes(hex).map(x => [x]);
}
exports.hexStringToBytes = hexStringToBytes;
function bytesToHexString(input) {
return ethers_1.ethers.utils.hexlify(input.map(x => new bignumber_js_1.default(x[0]).toNumber()));
}
exports.bytesToHexString = bytesToHexString;
function toBytes(...args) {
const result = args.reduce((acc, val) => acc.concat(argToBytes(val)), []);
return result.map((a) => [a]);
}
exports.toBytes = toBytes;
function toBytesNoPadding(...args) {
const result = args.reduce((acc, val) => acc.concat(argToBytes(val, false)), []);
return result.map((a) => [a]);
}
exports.toBytesNoPadding = toBytesNoPadding;
function argToBytes(val, shouldPad = true) {
let v = val;
if (typeof val === 'boolean') {
v = val ? '1' : '0';
}
if (typeof val === 'number') {
v = val.toString();
}
if (val instanceof bignumber_js_1.default) {
v = val.toFixed(0);
}
return web3_1.default.utils.hexToBytes(shouldPad ? web3_1.default.utils.padLeft(web3_1.default.utils.toHex(v), 64, '0') : web3_1.default.utils.toHex(v));
}
exports.argToBytes = argToBytes;
function addressToBytes32(input) {
return `0x000000000000000000000000${stripHexPrefix(input)}`;
}
exports.addressToBytes32 = addressToBytes32;
function hashString(input) {
return web3_1.default.utils.soliditySha3({ t: 'string', v: input });
}
exports.hashString = hashString;
function hashBytes(input) {
// javascript soliditySha3 has a problem with empty bytes arrays, so manually return the same
// value that solidity does for keccak256 of an empty bytes array
if (!stripHexPrefix(input)) {
return '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
}
return web3_1.default.utils.soliditySha3({
t: 'bytes',
v: `0x${stripHexPrefix(input)}`,
});
}
exports.hashBytes = hashBytes;
function stripHexPrefix(input) {
if (input.indexOf('0x') === 0) {
return input.substr(2);
}
return input;
}
exports.stripHexPrefix = stripHexPrefix;
function addressesAreEqual(addressOne, addressTwo) {
return (addressOne
&& addressTwo
&& stripHexPrefix(addressOne).toLowerCase()
=== stripHexPrefix(addressTwo).toLowerCase());
}
exports.addressesAreEqual = addressesAreEqual;
//# sourceMappingURL=BytesHelper.js.map