diff --git a/lib/web3/formatters.js b/lib/web3/formatters.js index 1f88d5b..070f859 100644 --- a/lib/web3/formatters.js +++ b/lib/web3/formatters.js @@ -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, diff --git a/lib/web3/iban.js b/lib/web3/iban.js index 5084343..5533ca2 100644 --- a/lib/web3/iban.js +++ b/lib/web3/iban.js @@ -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; }; /** diff --git a/test/formatters.inputAddressFormatter.js b/test/formatters.inputAddressFormatter.js new file mode 100644 index 0000000..9283f15 --- /dev/null +++ b/test/formatters.inputAddressFormatter.js @@ -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); + }); + }); + }); + }); +});