2016-04-13 15:28:44 -07:00
|
|
|
var assert = require('assert')
|
|
|
|
var sinon = require('sinon')
|
|
|
|
const ethUtil = require('ethereumjs-util')
|
|
|
|
|
|
|
|
var path = require('path')
|
2016-04-18 11:41:29 -07:00
|
|
|
var util = require(path.join(__dirname, '..', '..', 'ui', 'app', 'util.js'))
|
2016-04-13 15:28:44 -07:00
|
|
|
|
|
|
|
describe('util', function() {
|
|
|
|
var ethInWei = '1'
|
|
|
|
for (var i = 0; i < 18; i++ ) { ethInWei += '0' }
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
this.sinon = sinon.sandbox.create()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
this.sinon.restore()
|
|
|
|
})
|
|
|
|
|
2016-07-11 13:57:47 -07:00
|
|
|
describe('#parseBalance', function() {
|
2016-05-25 18:29:03 -07:00
|
|
|
it('should render 0.01 eth correctly', function() {
|
|
|
|
const input = '0x2386F26FC10000'
|
|
|
|
const output = util.parseBalance(input)
|
|
|
|
assert.deepEqual(output, ['0', '01'])
|
|
|
|
})
|
2016-07-11 13:57:47 -07:00
|
|
|
|
2016-06-20 15:46:29 -07:00
|
|
|
it('should render 12.023 eth correctly', function() {
|
2016-05-25 18:29:03 -07:00
|
|
|
const input = 'A6DA46CCA6858000'
|
|
|
|
const output = util.parseBalance(input)
|
|
|
|
assert.deepEqual(output, ['12', '023'])
|
|
|
|
})
|
2016-07-11 13:57:47 -07:00
|
|
|
|
2016-06-20 15:46:29 -07:00
|
|
|
it('should render 0.0000000342422 eth correctly', function() {
|
|
|
|
const input = '0x7F8FE81C0'
|
|
|
|
const output = util.parseBalance(input)
|
|
|
|
assert.deepEqual(output, ['0', '0000000342422'])
|
|
|
|
})
|
2016-07-11 13:57:47 -07:00
|
|
|
|
2016-06-20 15:46:29 -07:00
|
|
|
it('should render 0 eth correctly', function() {
|
|
|
|
const input = '0x0'
|
|
|
|
const output = util.parseBalance(input)
|
|
|
|
assert.deepEqual(output, ['0', '0'])
|
|
|
|
})
|
|
|
|
})
|
2016-05-25 18:29:03 -07:00
|
|
|
|
2016-07-11 13:57:47 -07:00
|
|
|
describe('#addressSummary', function() {
|
2016-05-19 12:06:45 -07:00
|
|
|
it('should add case-sensitive checksum', function() {
|
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
|
|
|
var result = util.addressSummary(address)
|
|
|
|
assert.equal(result, '0xFDEa65C8...b825')
|
|
|
|
})
|
2016-07-06 17:58:46 -07:00
|
|
|
|
|
|
|
it('should accept arguments for firstseg, lastseg, and keepPrefix', function() {
|
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
|
|
|
var result = util.addressSummary(address, 4, 4, false)
|
|
|
|
assert.equal(result, 'FDEa...b825')
|
|
|
|
})
|
2016-05-19 12:06:45 -07:00
|
|
|
})
|
|
|
|
|
2016-07-11 13:57:47 -07:00
|
|
|
describe('#isValidAddress', function() {
|
2016-05-19 12:06:45 -07:00
|
|
|
it('should allow 40-char non-prefixed hex', function() {
|
|
|
|
var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(result)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow 42-char non-prefixed hex', function() {
|
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(result)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not allow less non hex-prefixed', function() {
|
|
|
|
var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(!result)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not allow less hex-prefixed', function() {
|
|
|
|
var address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(!result)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should recognize correct capitalized checksum', function() {
|
|
|
|
var address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(result)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should recognize incorrect capitalized checksum', function() {
|
|
|
|
var address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825'
|
|
|
|
var result = util.isValidAddress(address)
|
|
|
|
assert.ok(!result)
|
|
|
|
})
|
2016-05-19 14:21:35 -07:00
|
|
|
|
2016-06-01 16:14:49 -07:00
|
|
|
it('should recognize this sample hashed address', function() {
|
2016-06-02 18:42:09 -07:00
|
|
|
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0'
|
2016-06-01 16:14:49 -07:00
|
|
|
const result = util.isValidAddress(address)
|
2016-06-02 18:42:09 -07:00
|
|
|
const hashed = ethUtil.toChecksumAddress(address.toLowerCase())
|
2016-06-01 16:14:49 -07:00
|
|
|
assert.equal(hashed, address, 'example is hashed correctly')
|
2016-06-02 18:42:09 -07:00
|
|
|
assert.ok(result, 'is valid by our check')
|
2016-06-01 16:14:49 -07:00
|
|
|
})
|
2016-05-19 12:06:45 -07:00
|
|
|
})
|
|
|
|
|
2016-07-11 13:57:47 -07:00
|
|
|
describe('#numericBalance', function() {
|
2016-04-13 15:28:44 -07:00
|
|
|
|
|
|
|
it('should return a BN 0 if given nothing', function() {
|
|
|
|
var result = util.numericBalance()
|
|
|
|
assert.equal(result.toString(10), 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work with hex prefix', function() {
|
|
|
|
var result = util.numericBalance('0x012')
|
|
|
|
assert.equal(result.toString(10), '18')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work with no hex prefix', function() {
|
|
|
|
var result = util.numericBalance('012')
|
|
|
|
assert.equal(result.toString(10), '18')
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#formatBalance', function() {
|
|
|
|
|
|
|
|
it('when given nothing', function() {
|
|
|
|
var result = util.formatBalance()
|
2016-07-07 11:20:02 -07:00
|
|
|
assert.equal(result, 'None', 'should return "None"')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return eth as string followed by ETH', function() {
|
|
|
|
var input = new ethUtil.BN(ethInWei, 10).toJSON()
|
|
|
|
var result = util.formatBalance(input, 4)
|
|
|
|
assert.equal(result, '1.0000 ETH')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return eth as string followed by ETH', function() {
|
|
|
|
var input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON()
|
|
|
|
var result = util.formatBalance(input, 3)
|
|
|
|
assert.equal(result, '0.500 ETH')
|
2016-04-14 13:20:19 -07:00
|
|
|
})
|
|
|
|
|
2016-05-25 18:29:03 -07:00
|
|
|
it('should display specified decimal points', function() {
|
|
|
|
var input = "0x128dfa6a90b28000"
|
|
|
|
var result = util.formatBalance(input, 2)
|
2016-07-07 11:20:02 -07:00
|
|
|
assert.equal(result, '1.33 ETH')
|
2016-05-25 18:29:03 -07:00
|
|
|
})
|
2016-07-07 11:20:02 -07:00
|
|
|
it('should default to 3 decimal points', function() {
|
2016-04-14 13:20:19 -07:00
|
|
|
var input = "0x128dfa6a90b28000"
|
|
|
|
var result = util.formatBalance(input)
|
2016-07-07 11:20:02 -07:00
|
|
|
assert.equal(result, '1.337 ETH')
|
2016-05-25 18:29:03 -07:00
|
|
|
})
|
|
|
|
it('should show 2 significant digits for tiny balances', function() {
|
|
|
|
var input = "0x1230fa6a90b28"
|
|
|
|
var result = util.formatBalance(input)
|
2016-07-07 11:20:02 -07:00
|
|
|
assert.equal(result, '0.00032 ETH')
|
2016-04-13 15:28:44 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-04-19 18:56:22 -07:00
|
|
|
describe('normalizing values', function() {
|
|
|
|
|
|
|
|
describe('#normalizeToWei', function() {
|
|
|
|
it('should convert an eth to the appropriate equivalent values', function() {
|
|
|
|
var valueTable = {
|
|
|
|
wei: '1000000000000000000',
|
|
|
|
kwei: '1000000000000000',
|
|
|
|
mwei: '1000000000000',
|
|
|
|
gwei: '1000000000',
|
|
|
|
szabo: '1000000',
|
|
|
|
finney:'1000',
|
|
|
|
ether: '1',
|
|
|
|
// kether:'0.001',
|
|
|
|
// mether:'0.000001',
|
|
|
|
// AUDIT: We're getting BN numbers on these ones.
|
|
|
|
// I think they're big enough to ignore for now.
|
|
|
|
// gether:'0.000000001',
|
|
|
|
// tether:'0.000000000001',
|
|
|
|
}
|
|
|
|
var oneEthBn = new ethUtil.BN(ethInWei, 10)
|
|
|
|
|
|
|
|
for(var currency in valueTable) {
|
|
|
|
|
|
|
|
var value = new ethUtil.BN(valueTable[currency], 10)
|
|
|
|
var output = util.normalizeToWei(value, currency)
|
|
|
|
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`)
|
|
|
|
}
|
|
|
|
})
|
2016-04-13 15:28:44 -07:00
|
|
|
})
|
|
|
|
|
2016-07-11 13:57:47 -07:00
|
|
|
describe('#normalizeEthStringToWei', function() {
|
2016-05-19 14:46:50 -07:00
|
|
|
it('should convert decimal eth to pure wei BN', function() {
|
|
|
|
var input = '1.23456789'
|
|
|
|
var output = util.normalizeEthStringToWei(input)
|
|
|
|
assert.equal(output.toString(10), '1234567890000000000')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should convert 1 to expected wei', function() {
|
|
|
|
var input = '1'
|
|
|
|
var output = util.normalizeEthStringToWei(input)
|
|
|
|
assert.equal(output.toString(10), ethInWei)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-04-19 18:56:22 -07:00
|
|
|
describe('#normalizeNumberToWei', function() {
|
|
|
|
|
2016-05-19 14:21:35 -07:00
|
|
|
it('should handle a simple use case', function() {
|
|
|
|
var input = 0.0002
|
|
|
|
var output = util.normalizeNumberToWei(input, 'ether')
|
|
|
|
var str = output.toString(10)
|
|
|
|
assert.equal(str, '200000000000000')
|
|
|
|
})
|
|
|
|
|
2016-04-19 18:56:22 -07:00
|
|
|
it('should convert a kwei number to the appropriate equivalent wei', function() {
|
|
|
|
var result = util.normalizeNumberToWei(1.111, 'kwei')
|
|
|
|
assert.equal(result.toString(10), '1111', 'accepts decimals')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should convert a ether number to the appropriate equivalent wei', function() {
|
|
|
|
var result = util.normalizeNumberToWei(1.111, 'ether')
|
|
|
|
assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-04-13 15:28:44 -07:00
|
|
|
})
|