# All files / web3-eth-iban/src index.js

92.96% Statements 66/71
72.22% Branches 13/18
78.95% Functions 15/19
92.96% Lines 66/71

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268

    2x
    2x

    2x
    13x
    13x
    4x

    13x

    2x
    43x
    43x

    43x
    43x

    43x
    1346x
    1346x

    933x

    413x

    2x
    43x

    43x
    323x
    323x

    43x

    2x
    498x

    2x
    5x

    5x
    1x

    4x

    2x
    9x

    2x
    14x
    1x

    13x

    13x
    13x
    13x
    13x

    2x
    14x

    14x
    14x

    14x

    2x
    1x

    2x
    18x
    18x

    2x
    472x

    2x
    50x

    2x

    2x

    2x

    2x

    2x
    22x
    22x
    22x
    22x

    2x
    8x

    2x
     /*
        This file is part of web3.js.

        web3.js is free software: you can redistribute it and/or modify
        it under the terms of the GNU Lesser General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        web3.js is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Lesser General Public License for more details.

        You should have received a copy of the GNU Lesser General Public License
        along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
    */
    /**
     * @file iban.js
     *
     * Details: https://github.com/ethereum/wiki/wiki/ICAP:-Inter-exchange-Client-Address-Protocol
     *
     * @author Marek Kotewicz <marek@parity.io>
     * @date 2015
     */

    "use strict";

    var utils = require('web3-utils');
    var BigNumber = require('bn.js');

    var leftPad = function (string, bytes) {
        var result = string;
        while (result.length < bytes * 2) {
            result = '0' + result;
        }
        return result;
    };

    /**
     * Prepare an IBAN for mod 97 computation by moving the first 4 chars to the end and transforming the letters to
     * numbers (A = 10, B = 11, ..., Z = 35), as specified in ISO13616.
     *
     * @method iso13616Prepare
     * @param {String} iban the IBAN
     * @returns {String} the prepared IBAN
     */
    var iso13616Prepare = function (iban) {
        var A = 'A'.charCodeAt(0);
        var Z = 'Z'.charCodeAt(0);

        iban = iban.toUpperCase();
        iban = iban.substr(4) + iban.substr(0,4);

        return iban.split('').map(function(n){
            var code = n.charCodeAt(0);
            if (code >= A && code <= Z){
                // A = 10, B = 11, ... Z = 35
                return code - A + 10;
            } else {
                return n;
            }
        }).join('');
    };

    /**
     * Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064.
     *
     * @method mod9710
     * @param {String} iban
     * @returns {Number}
     */
    var mod9710 = function (iban) {
        var remainder = iban,
            block;

        while (remainder.length > 2){
            block = remainder.slice(0, 9);
            remainder = parseInt(block, 10) % 97 + remainder.slice(block.length);
        }

        return parseInt(remainder, 10) % 97;
    };

    /**
     * This prototype should be used to create iban object from iban correct string
     *
     * @param {String} iban
     */
    var Iban = function Iban(iban) {
        this._iban = iban;
    };

    /**
     * This method should be used to create an ethereum address from a direct iban address
     *
     * @method toAddress
     * @param {String} iban address
     * @return {String} the ethereum address
     */
    Iban.toAddress = function (ib) {
        ib = new Iban(ib);

        if(!ib.isDirect()) {
            throw new Error('IBAN is indirect and can\'t be converted');
        }

        return ib.toAddress();
    };

    /**
     * This method should be used to create iban address from an ethereum address
     *
     * @method toIban
     * @param {String} address
     * @return {String} the IBAN address
     */
    Iban.toIban = function (address) {
        return Iban.fromAddress(address).toString();
    };

    /**
     * This method should be used to create iban object from an ethereum address
     *
     * @method fromAddress
     * @param {String} address
     * @return {Iban} the IBAN object
     */
    Iban.fromAddress = function (address) {
        if(!utils.isAddress(address)){
            throw new Error('Provided address is not a valid address: '+ address);
        }

        address = address.replace('0x','').replace('0X','');

        var asBn = new BigNumber(address, 16);
        var base36 = asBn.toString(36);
        var padded = leftPad(base36, 15);
        return Iban.fromBban(padded.toUpperCase());
    };

    /**
     * Convert the passed BBAN to an IBAN for this country specification.
     * Please note that <i>"generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account"</i>.
     * This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits
     *
     * @method fromBban
     * @param {String} bban the BBAN to convert to IBAN
     * @returns {Iban} the IBAN object
     */
    Iban.fromBban = function (bban) {
        var countryCode = 'XE';

        var remainder = mod9710(iso13616Prepare(countryCode + '00' + bban));
        var checkDigit = ('0' + (98 - remainder)).slice(-2);

        return new Iban(countryCode + checkDigit + bban);
    };

    /**
     * Should be used to create IBAN object for given institution and identifier
     *
     * @method createIndirect
     * @param {Object} options, required options are "institution" and "identifier"
     * @return {Iban} the IBAN object
     */
    Iban.createIndirect = function (options) {
        return Iban.fromBban('ETH' + options.institution + options.identifier);
    };

    /**
     * This method should be used to check if given string is valid iban object
     *
     * @method isValid
     * @param {String} iban string
     * @return {Boolean} true if it is valid IBAN
     */
    Iban.isValid = function (iban) {
        var i = new Iban(iban);
        return i.isValid();
    };

    /**
     * Should be called to check if iban is correct
     *
     * @method isValid
     * @returns {Boolean} true if it is, otherwise false
     */
    Iban.prototype.isValid = function () {
        return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30,31})$/.test(this._iban) &&
            mod9710(iso13616Prepare(this._iban)) === 1;
    };

    /**
     * Should be called to check if iban number is direct
     *
     * @method isDirect
     * @returns {Boolean} true if it is, otherwise false
     */
    Iban.prototype.isDirect = function () {
        return this._iban.length === 34 || this._iban.length === 35;
    };

    /**
     * Should be called to check if iban number if indirect
     *
     * @method isIndirect
     * @returns {Boolean} true if it is, otherwise false
     */
    Iban.prototype.isIndirect = function () {
        return this._iban.length === 20;
    };

    /**
     * Should be called to get iban checksum
     * Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003)
     *
     * @method checksum
     * @returns {String} checksum
     */
    Iban.prototype.checksum = function () {
        return this._iban.substr(2, 2);
    };

    /**
     * Should be called to get institution identifier
     * eg. XREG
     *
     * @method institution
     * @returns {String} institution identifier
     */
    Iban.prototype.institution = function () {
        return this.isIndirect() ? this._iban.substr(7, 4) : '';
    };

    /**
     * Should be called to get client identifier within institution
     * eg. GAVOFYORK
     *
     * @method client
     * @returns {String} client identifier
     */
    Iban.prototype.client = function () {
        return this.isIndirect() ? this._iban.substr(11) : '';
    };

    /**
     * Should be called to get client direct address
     *
     * @method toAddress
     * @returns {String} ethereum address
     */
    Iban.prototype.toAddress = function () {
        Eif (this.isDirect()) {
            var base36 = this._iban.substr(4);
            var asBn = new BigNumber(base36, 36);
            return utils.toChecksumAddress(asBn.toString(16, 20));
        }

        return '';
    };

    Iban.prototype.toString = function () {
        return this._iban;
    };

    module.exports = Iban;