mirror of https://github.com/trezor/trezor-wallet
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
969 B
31 lines
969 B
7 years ago
|
/* @flow */
|
||
|
|
||
6 years ago
|
import BigNumber from 'bignumber.js';
|
||
|
|
||
|
export const decimalToHex = (dec: number): string => new BigNumber(dec).toString(16);
|
||
7 years ago
|
|
||
|
export const hexToDecimal = (hex: number): string => {
|
||
6 years ago
|
const sanitized: ?string = sanitizeHex(hex);
|
||
|
return !sanitized ? 'null' : new BigNumber(sanitized).toString();
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
6 years ago
|
export const sanitizeHex = (hex: number | string): ?string => {
|
||
7 years ago
|
if (typeof hex !== 'string') return null;
|
||
|
hex = hex.substring(0, 2) === '0x' ? hex.substring(2) : hex;
|
||
|
if (hex === '') return '';
|
||
6 years ago
|
return `0x${padLeftEven(hex)}`;
|
||
|
};
|
||
7 years ago
|
|
||
6 years ago
|
export const padLeftEven = (hex: string): string => {
|
||
6 years ago
|
hex = hex.length % 2 != 0 ? `0${hex}` : hex;
|
||
7 years ago
|
return hex;
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
|
export const strip = (str: string): string => {
|
||
|
if (str.indexOf('0x') === 0) {
|
||
6 years ago
|
return padLeftEven(str.substring(2, str.length));
|
||
7 years ago
|
}
|
||
|
return padLeftEven(str);
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
6 years ago
|
export const calcGasPrice = (price: BigNumber, limit: string): string => price.times(limit).toString();
|