diff options
author | kumavis <kumavis@users.noreply.github.com> | 2016-05-20 08:06:03 +0800 |
---|---|---|
committer | kumavis <kumavis@users.noreply.github.com> | 2016-05-20 08:06:03 +0800 |
commit | 9daf170b98f71f4845fe0c71faad22dc32514d1b (patch) | |
tree | 73cf15ffbffba708d8c32264226b31dc0a8ecb0e /ui/app/util.js | |
parent | f86a1bdcb07e98ce740109f13b130afb82f35b3a (diff) | |
parent | 47540ad0b3c92df8061bb9c73159670ca2bc03ba (diff) | |
download | tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar.gz tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar.bz2 tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar.lz tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar.xz tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.tar.zst tangerine-wallet-browser-9daf170b98f71f4845fe0c71faad22dc32514d1b.zip |
Merge pull request #192 from MetaMask/ZeroValueTxs
Zero value txs
Diffstat (limited to 'ui/app/util.js')
-rw-r--r-- | ui/app/util.js | 37 |
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) } |