Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 451c43c

Browse files
Feature/web3 eth iban es6 (#3964)
* web3-eth-iban es6 rewrite - replace `var` with `const` and `let` * web3-eth-iban es6 rewrite - replace object prototype with class * web3-eth-iban code style fix Co-authored-by: Loredana <[email protected]>
1 parent 88f59fe commit 451c43c

File tree

1 file changed

+178
-176
lines changed

1 file changed

+178
-176
lines changed

packages/web3-eth-iban/src/index.js

Lines changed: 178 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525

2626
"use strict";
2727

28-
var utils = require('web3-utils');
29-
var BigNumber = require('bn.js');
28+
const utils = require('web3-utils');
29+
const BigNumber = require('bn.js');
3030

3131

32-
var leftPad = function (string, bytes) {
33-
var result = string;
32+
const leftPad = function (string, bytes) {
33+
let result = string;
3434
while (result.length < bytes * 2) {
3535
result = '0' + result;
3636
}
@@ -45,16 +45,16 @@ var leftPad = function (string, bytes) {
4545
* @param {String} iban the IBAN
4646
* @returns {String} the prepared IBAN
4747
*/
48-
var iso13616Prepare = function (iban) {
49-
var A = 'A'.charCodeAt(0);
50-
var Z = 'Z'.charCodeAt(0);
48+
const iso13616Prepare = function (iban) {
49+
const A = 'A'.charCodeAt(0);
50+
const Z = 'Z'.charCodeAt(0);
5151

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

55-
return iban.split('').map(function(n){
56-
var code = n.charCodeAt(0);
57-
if (code >= A && code <= Z){
55+
return iban.split('').map(function(n) {
56+
const code = n.charCodeAt(0);
57+
if (code >= A && code <= Z) {
5858
// A = 10, B = 11, ... Z = 35
5959
return code - A + 10;
6060
} else {
@@ -70,9 +70,9 @@ var iso13616Prepare = function (iban) {
7070
* @param {String} iban
7171
* @returns {Number}
7272
*/
73-
var mod9710 = function (iban) {
74-
var remainder = iban,
75-
block;
73+
const mod9710 = function (iban) {
74+
let remainder = iban;
75+
let block;
7676

7777
while (remainder.length > 2){
7878
block = remainder.slice(0, 9);
@@ -87,181 +87,183 @@ var mod9710 = function (iban) {
8787
*
8888
* @param {String} iban
8989
*/
90-
var Iban = function Iban(iban) {
91-
this._iban = iban;
92-
};
93-
94-
/**
95-
* This method should be used to create an ethereum address from a direct iban address
96-
*
97-
* @method toAddress
98-
* @param {String} iban address
99-
* @return {String} the ethereum address
100-
*/
101-
Iban.toAddress = function (ib) {
102-
ib = new Iban(ib);
103-
104-
if(!ib.isDirect()) {
105-
throw new Error('IBAN is indirect and can\'t be converted');
90+
class Iban {
91+
constructor (iban) {
92+
this._iban = iban;
10693
}
10794

108-
return ib.toAddress();
109-
};
110-
111-
/**
112-
* This method should be used to create iban address from an ethereum address
113-
*
114-
* @method toIban
115-
* @param {String} address
116-
* @return {String} the IBAN address
117-
*/
118-
Iban.toIban = function (address) {
119-
return Iban.fromAddress(address).toString();
120-
};
95+
/**
96+
* This method should be used to create an ethereum address from a direct iban address
97+
*
98+
* @method toAddress
99+
* @param {String} iban address
100+
* @return {String} the ethereum address
101+
*/
102+
static toAddress (ib) {
103+
ib = new Iban(ib);
104+
105+
if(!ib.isDirect()) {
106+
throw new Error('IBAN is indirect and can\'t be converted');
107+
}
121108

122-
/**
123-
* This method should be used to create iban object from an ethereum address
124-
*
125-
* @method fromAddress
126-
* @param {String} address
127-
* @return {Iban} the IBAN object
128-
*/
129-
Iban.fromAddress = function (address) {
130-
if(!utils.isAddress(address)){
131-
throw new Error('Provided address is not a valid address: '+ address);
109+
return ib.toAddress();
132110
}
133111

134-
address = address.replace('0x','').replace('0X','');
135-
136-
var asBn = new BigNumber(address, 16);
137-
var base36 = asBn.toString(36);
138-
var padded = leftPad(base36, 15);
139-
return Iban.fromBban(padded.toUpperCase());
140-
};
141-
142-
/**
143-
* Convert the passed BBAN to an IBAN for this country specification.
144-
* Please note that <i>"generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account"</i>.
145-
* This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits
146-
*
147-
* @method fromBban
148-
* @param {String} bban the BBAN to convert to IBAN
149-
* @returns {Iban} the IBAN object
150-
*/
151-
Iban.fromBban = function (bban) {
152-
var countryCode = 'XE';
153-
154-
var remainder = mod9710(iso13616Prepare(countryCode + '00' + bban));
155-
var checkDigit = ('0' + (98 - remainder)).slice(-2);
156-
157-
return new Iban(countryCode + checkDigit + bban);
158-
};
159-
160-
/**
161-
* Should be used to create IBAN object for given institution and identifier
162-
*
163-
* @method createIndirect
164-
* @param {Object} options, required options are "institution" and "identifier"
165-
* @return {Iban} the IBAN object
166-
*/
167-
Iban.createIndirect = function (options) {
168-
return Iban.fromBban('ETH' + options.institution + options.identifier);
169-
};
170-
171-
/**
172-
* This method should be used to check if given string is valid iban object
173-
*
174-
* @method isValid
175-
* @param {String} iban string
176-
* @return {Boolean} true if it is valid IBAN
177-
*/
178-
Iban.isValid = function (iban) {
179-
var i = new Iban(iban);
180-
return i.isValid();
181-
};
182-
183-
/**
184-
* Should be called to check if iban is correct
185-
*
186-
* @method isValid
187-
* @returns {Boolean} true if it is, otherwise false
188-
*/
189-
Iban.prototype.isValid = function () {
190-
return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30,31})$/.test(this._iban) &&
191-
mod9710(iso13616Prepare(this._iban)) === 1;
192-
};
193-
194-
/**
195-
* Should be called to check if iban number is direct
196-
*
197-
* @method isDirect
198-
* @returns {Boolean} true if it is, otherwise false
199-
*/
200-
Iban.prototype.isDirect = function () {
201-
return this._iban.length === 34 || this._iban.length === 35;
202-
};
112+
/**
113+
* This method should be used to create iban address from an ethereum address
114+
*
115+
* @method toIban
116+
* @param {String} address
117+
* @return {String} the IBAN address
118+
*/
119+
static toIban (address) {
120+
return Iban.fromAddress(address).toString();
121+
}
203122

204-
/**
205-
* Should be called to check if iban number if indirect
206-
*
207-
* @method isIndirect
208-
* @returns {Boolean} true if it is, otherwise false
209-
*/
210-
Iban.prototype.isIndirect = function () {
211-
return this._iban.length === 20;
212-
};
123+
/**
124+
* This method should be used to create iban object from an ethereum address
125+
*
126+
* @method fromAddress
127+
* @param {String} address
128+
* @return {Iban} the IBAN object
129+
*/
130+
static fromAddress (address) {
131+
if(!utils.isAddress(address)){
132+
throw new Error('Provided address is not a valid address: '+ address);
133+
}
213134

214-
/**
215-
* Should be called to get iban checksum
216-
* Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003)
217-
*
218-
* @method checksum
219-
* @returns {String} checksum
220-
*/
221-
Iban.prototype.checksum = function () {
222-
return this._iban.substr(2, 2);
223-
};
135+
address = address.replace('0x','').replace('0X','');
224136

225-
/**
226-
* Should be called to get institution identifier
227-
* eg. XREG
228-
*
229-
* @method institution
230-
* @returns {String} institution identifier
231-
*/
232-
Iban.prototype.institution = function () {
233-
return this.isIndirect() ? this._iban.substr(7, 4) : '';
234-
};
137+
const asBn = new BigNumber(address, 16);
138+
const base36 = asBn.toString(36);
139+
const padded = leftPad(base36, 15);
140+
return Iban.fromBban(padded.toUpperCase());
141+
}
235142

236-
/**
237-
* Should be called to get client identifier within institution
238-
* eg. GAVOFYORK
239-
*
240-
* @method client
241-
* @returns {String} client identifier
242-
*/
243-
Iban.prototype.client = function () {
244-
return this.isIndirect() ? this._iban.substr(11) : '';
245-
};
143+
/**
144+
* Convert the passed BBAN to an IBAN for this country specification.
145+
* Please note that <i>"generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account"</i>.
146+
* This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits
147+
*
148+
* @method fromBban
149+
* @param {String} bban the BBAN to convert to IBAN
150+
* @returns {Iban} the IBAN object
151+
*/
152+
static fromBban (bban) {
153+
const countryCode = 'XE';
154+
155+
const remainder = mod9710(iso13616Prepare(countryCode + '00' + bban));
156+
const checkDigit = ('0' + (98 - remainder)).slice(-2);
157+
158+
return new Iban(countryCode + checkDigit + bban);
159+
}
246160

247-
/**
248-
* Should be called to get client direct address
249-
*
250-
* @method toAddress
251-
* @returns {String} ethereum address
252-
*/
253-
Iban.prototype.toAddress = function () {
254-
if (this.isDirect()) {
255-
var base36 = this._iban.substr(4);
256-
var asBn = new BigNumber(base36, 36);
257-
return utils.toChecksumAddress(asBn.toString(16, 20));
161+
/**
162+
* Should be used to create IBAN object for given institution and identifier
163+
*
164+
* @method createIndirect
165+
* @param {Object} options, required options are "institution" and "identifier"
166+
* @return {Iban} the IBAN object
167+
*/
168+
static createIndirect (options) {
169+
return Iban.fromBban('ETH' + options.institution + options.identifier);
258170
}
259171

260-
return '';
261-
};
172+
/**
173+
* This method should be used to check if given string is valid iban object
174+
*
175+
* @method isValid
176+
* @param {String} iban string
177+
* @return {Boolean} true if it is valid IBAN
178+
*/
179+
static isValid (iban) {
180+
const i = new Iban(iban);
181+
return i.isValid();
182+
};
183+
184+
/**
185+
* Should be called to check if iban is correct
186+
*
187+
* @method isValid
188+
* @returns {Boolean} true if it is, otherwise false
189+
*/
190+
isValid () {
191+
return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30,31})$/.test(this._iban) &&
192+
mod9710(iso13616Prepare(this._iban)) === 1;
193+
};
194+
195+
/**
196+
* Should be called to check if iban number is direct
197+
*
198+
* @method isDirect
199+
* @returns {Boolean} true if it is, otherwise false
200+
*/
201+
isDirect () {
202+
return this._iban.length === 34 || this._iban.length === 35;
203+
};
204+
205+
/**
206+
* Should be called to check if iban number if indirect
207+
*
208+
* @method isIndirect
209+
* @returns {Boolean} true if it is, otherwise false
210+
*/
211+
isIndirect () {
212+
return this._iban.length === 20;
213+
};
214+
215+
/**
216+
* Should be called to get iban checksum
217+
* Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003)
218+
*
219+
* @method checksum
220+
* @returns {String} checksum
221+
*/
222+
checksum () {
223+
return this._iban.substr(2, 2);
224+
};
225+
226+
/**
227+
* Should be called to get institution identifier
228+
* eg. XREG
229+
*
230+
* @method institution
231+
* @returns {String} institution identifier
232+
*/
233+
institution () {
234+
return this.isIndirect() ? this._iban.substr(7, 4) : '';
235+
};
236+
237+
/**
238+
* Should be called to get client identifier within institution
239+
* eg. GAVOFYORK
240+
*
241+
* @method client
242+
* @returns {String} client identifier
243+
*/
244+
client () {
245+
return this.isIndirect() ? this._iban.substr(11) : '';
246+
};
247+
248+
/**
249+
* Should be called to get client direct address
250+
*
251+
* @method toAddress
252+
* @returns {String} ethereum address
253+
*/
254+
toAddress () {
255+
if (this.isDirect()) {
256+
const base36 = this._iban.substr(4);
257+
const asBn = new BigNumber(base36, 36);
258+
return utils.toChecksumAddress(asBn.toString(16, 20));
259+
}
262260

263-
Iban.prototype.toString = function () {
264-
return this._iban;
265-
};
261+
return '';
262+
};
263+
264+
toString () {
265+
return this._iban;
266+
};
267+
}
266268

267269
module.exports = Iban;

0 commit comments

Comments
 (0)