aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/app/util.js')
-rw-r--r--ui/app/util.js37
1 files changed, 34 insertions, 3 deletions
diff --git a/ui/app/util.js b/ui/app/util.js
index 0f3f191aa..81a029350 100644
--- a/ui/app/util.js
+++ b/ui/app/util.js
@@ -21,6 +21,8 @@ for (var currency in valueTable) {
module.exports = {
valuesFor: valuesFor,
addressSummary: addressSummary,
+ isAllOneCase: isAllOneCase,
+ isValidAddress: isValidAddress,
numericBalance: numericBalance,
parseBalance: parseBalance,
formatBalance: formatBalance,
@@ -29,6 +31,7 @@ module.exports = {
ethToWei: ethToWei,
weiToEth: weiToEth,
normalizeToWei: normalizeToWei,
+ normalizeEthStringToWei: normalizeEthStringToWei,
normalizeNumberToWei: normalizeNumberToWei,
valueTable: valueTable,
bnTable: bnTable,
@@ -42,7 +45,21 @@ function valuesFor(obj) {
}
function addressSummary(address) {
- return address ? address.slice(0,2+8)+'...'+address.slice(-4) : '...'
+ if (!address) return ''
+ var checked = ethUtil.toChecksumAddress(address)
+ return checked ? checked.slice(0,2+8)+'...'+checked.slice(-4) : '...'
+}
+
+function isValidAddress(address) {
+ var prefixed = ethUtil.addHexPrefix(address)
+ return isAllOneCase(prefixed) && ethUtil.isValidAddress(prefixed) || ethUtil.isValidChecksumAddress(prefixed)
+}
+
+function isAllOneCase(address) {
+ if (!address) return true
+ var lower = address.toLowerCase()
+ var upper = address.toUpperCase()
+ return address === lower || address === upper
}
// Takes wei Hex, returns wei BN, even if input is null
@@ -106,9 +123,23 @@ function normalizeToWei(amount, currency) {
return amount
}
-var multiple = new ethUtil.BN('1000', 10)
+function normalizeEthStringToWei(str) {
+ const parts = str.split('.')
+ let eth = new ethUtil.BN(parts[0], 10).mul(bnTable.wei)
+ if (parts[1]) {
+ var decimal = parts[1]
+ while(decimal.length < 18) {
+ decimal += '0'
+ }
+ const decimalBN = new ethUtil.BN(decimal, 10)
+ eth = eth.add(decimalBN)
+ }
+ return eth
+}
+
+var multiple = new ethUtil.BN('10000', 10)
function normalizeNumberToWei(n, currency) {
- var enlarged = n * 1000
+ var enlarged = n * 10000
var amount = new ethUtil.BN(String(enlarged), 10)
return normalizeToWei(amount, currency).div(multiple)
}