Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const formatPrimative = (value: PrimativeType) => {
if (isAddress(value)) {
return value as string
} else if (typeof value === 'boolean') {
return toUint(value ? 1 : 0, 1)
} else if (
typeof value === 'number' ||
+value ||
BigNumber.isBigNumber(value)
) {
return toUint(value, 32)
} else {
throw new Error('unsupported type')
}
}
/**
static from(value) {
if (isBoolean(value)) {
if (value === true) {
return new Hex('0x01');
}
return new Hex('0x00');
}
if (isObject(value) && !BigNumber.isBigNumber(value) && !BN.isBN(value)) {
return Hex.fromUTF8(JSON.stringify(value));
}
if (isString(value)) {
if (
value.startsWith('-0x') ||
value.startsWith('-0X') ||
value.startsWith('0x') ||
value.startsWith('0X')
) {
return new Hex(value);
}
if (!isFinite(value)) {
return Hex.fromUTF8(value);
}
const formatPrimative = (value: PrimativeType) => {
if (isAddress(value)) {
return value as string
} else if (typeof value === 'boolean') {
return toUint(value ? 1 : 0, 1)
} else if (
typeof value === 'number' ||
+value ||
BigNumber.isBigNumber(value)
) {
return toUint(value, 32)
} else {
throw new Error('unsupported type')
}
}
static toWei(number, unit) {
unit = Units.getUnitValue(unit);
if (BigNumber.isBigNumber(number) || BN.isBN(number)) {
return BigNumber.from(ethjsUnit.toWei(number, unit).toString(10));
}
if (isString(number)) {
return ethjsUnit.toWei(number, unit).toString(10);
}
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}
}
static fromWei(number, unit) {
unit = Units.getUnitValue(unit);
if (BigNumber.isBigNumber(number) || BN.isBN(number)) {
return BigNumber.from(ethjsUnit.fromWei(number, unit).toString(10));
}
if (isString(number)) {
return ethjsUnit.fromWei(number, unit).toString(10);
}
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}
if (isString(value)) {
if (
value.startsWith('-0x') ||
value.startsWith('-0X') ||
value.startsWith('0x') ||
value.startsWith('0X')
) {
return new Hex(value);
}
if (!isFinite(value)) {
return Hex.fromUTF8(value);
}
}
if (BigNumber.isBigNumber(value)) {
let hex = value.toString(16);
if (value.toHexString) {
hex = value.toHexString();
}
if (hex.startsWith('-')) {
return new Hex('-0x' + hex.slice(1));
}
return new Hex('0x' + value.toString(16));
}
if (BN.isBN(value)) {
const hex = value.toString(16);