inputAddressFormatter

This commit is contained in:
debris 2015-08-08 20:34:12 +02:00
parent f20fca01ad
commit 7cf4e78cf4
3 changed files with 56 additions and 1 deletions

View File

@ -23,6 +23,7 @@
var utils = require('../utils/utils');
var config = require('../utils/config');
var Iban = require('./iban');
/**
* Should the format output to a big number
@ -230,10 +231,23 @@ var outputPostFormatter = function(post){
return post;
};
var inputAddressFormatter = function (address) {
var iban = new Iban(address);
if (iban.isValid() && iban.isDirect()) {
return '0x' + iban.address();
} else if (utils.isStrictAddress(address)) {
return address;
} else if (utils.isAddress(address)) {
return '0x' + address;
}
throw 'invalid address';
};
module.exports = {
inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,
inputBlockNumberFormatter: inputBlockNumberFormatter,
inputTransactionFormatter: inputTransactionFormatter,
inputAddressFormatter: inputAddressFormatter,
inputPostFormatter: inputPostFormatter,
outputBigNumberFormatter: outputBigNumberFormatter,
outputTransactionFormatter: outputTransactionFormatter,

View File

@ -157,7 +157,7 @@ Iban.prototype.isValid = function () {
* @returns {Boolean} true if it is, otherwise false
*/
Iban.prototype.isDirect = function () {
return this._iban.length === 34;
return this._iban.length === 34 || this._iban.length === 35;
};
/**

View File

@ -0,0 +1,41 @@
var chai = require('chai');
var assert = chai.assert;
var formatters = require('../lib/web3/formatters.js');
var BigNumber = require('bignumber.js');
var tests = [{
input: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
input: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
input: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8'
}];
var errorTests = [
'0x0c5496aee77c1ba1f0854206a26dda82a81d6d8',
'0x0c5496aee77c1ba1f0854206a26dda82a81d6d8',
'00c5496aee77c1ba1f0854206a26dda82a81d6d',
'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZE',
'0x'
]
describe('formatters', function () {
describe('inputAddressFormatter', function () {
tests.forEach(function(test){
it('should return the correct value', function () {
assert.deepEqual(formatters.inputAddressFormatter(test.input), test.result);
});
});
});
});
describe('formatters', function () {
describe('inputAddressFormatter', function () {
errorTests.forEach(function(test){
it('should throw an exception', function () {
assert.throws(function () {
formatters.inputAddressFormatter(test);
});
});
});
});
});