@xylabs/eth-address 2.6.14

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.
Files changed (41) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +71 -0
  3. package/dist/cjs/EthAddress.d.ts +21 -0
  4. package/dist/cjs/EthAddress.d.ts.map +1 -0
  5. package/dist/cjs/EthAddress.js +60 -0
  6. package/dist/cjs/EthAddress.js.map +1 -0
  7. package/dist/cjs/ellipsize.d.ts +2 -0
  8. package/dist/cjs/ellipsize.d.ts.map +1 -0
  9. package/dist/cjs/ellipsize.js +10 -0
  10. package/dist/cjs/ellipsize.js.map +1 -0
  11. package/dist/cjs/index.d.ts +4 -0
  12. package/dist/cjs/index.d.ts.map +1 -0
  13. package/dist/cjs/index.js +7 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/padHex.d.ts +3 -0
  16. package/dist/cjs/padHex.d.ts.map +1 -0
  17. package/dist/cjs/padHex.js +17 -0
  18. package/dist/cjs/padHex.js.map +1 -0
  19. package/dist/docs.json +773 -0
  20. package/dist/esm/EthAddress.d.ts +21 -0
  21. package/dist/esm/EthAddress.d.ts.map +1 -0
  22. package/dist/esm/EthAddress.js +56 -0
  23. package/dist/esm/EthAddress.js.map +1 -0
  24. package/dist/esm/ellipsize.d.ts +2 -0
  25. package/dist/esm/ellipsize.d.ts.map +1 -0
  26. package/dist/esm/ellipsize.js +6 -0
  27. package/dist/esm/ellipsize.js.map +1 -0
  28. package/dist/esm/index.d.ts +4 -0
  29. package/dist/esm/index.d.ts.map +1 -0
  30. package/dist/esm/index.js +4 -0
  31. package/dist/esm/index.js.map +1 -0
  32. package/dist/esm/padHex.d.ts +3 -0
  33. package/dist/esm/padHex.d.ts.map +1 -0
  34. package/dist/esm/padHex.js +14 -0
  35. package/dist/esm/padHex.js.map +1 -0
  36. package/package.json +65 -0
  37. package/src/EthAddress.spec.ts +10 -0
  38. package/src/EthAddress.ts +69 -0
  39. package/src/ellipsize.ts +5 -0
  40. package/src/index.ts +3 -0
  41. package/src/padHex.ts +14 -0
@@ -0,0 +1,21 @@
1
+ /// <reference types="bn.js" />
2
+ import { BigNumber } from '@xylabs/bignumber';
3
+ export declare const isEthAddress: (obj: {
4
+ type: string;
5
+ }) => boolean;
6
+ export declare class EthAddress {
7
+ type: string;
8
+ private address;
9
+ static type: string;
10
+ private constructor();
11
+ equals(address?: EthAddress | string | null): boolean;
12
+ static fromString(value?: string, base?: number): EthAddress | undefined;
13
+ static parse(value: unknown, base?: number): EthAddress | undefined;
14
+ toBigNumber(): BigNumber;
15
+ toString(): string;
16
+ toLowerCaseString(): string;
17
+ toJSON(): string;
18
+ toHex(): string;
19
+ toShortString(length?: number): string;
20
+ }
21
+ //# sourceMappingURL=EthAddress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAK7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACd,IAAI,SAAkB;IAE7B,OAAO,CAAC,OAAO,CAAW;IAE1B,MAAM,CAAC,IAAI,SAAe;IAE1B,OAAO;IAIA,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAa5D,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAMnC,WAAW;IAIX,QAAQ;IAIR,iBAAiB;IAIjB,MAAM,IAAI,MAAM;IAIhB,KAAK;IAIL,aAAa,CAAC,MAAM,SAAI;CAGhC"}
@@ -0,0 +1,56 @@
1
+ import { assertEx } from '@xylabs/assert';
2
+ import { BigNumber } from '@xylabs/bignumber';
3
+ import { ellipsize } from './ellipsize';
4
+ import { padHex } from './padHex';
5
+ export const isEthAddress = (obj) => obj?.type === EthAddress.type;
6
+ export class EthAddress {
7
+ type = EthAddress.type;
8
+ address;
9
+ static type = 'EthAddress';
10
+ constructor(address) {
11
+ this.address = address;
12
+ }
13
+ equals(address) {
14
+ if (address) {
15
+ let inAddress;
16
+ if (typeof address === 'string') {
17
+ inAddress = assertEx(EthAddress.fromString(address), 'Bad Address');
18
+ }
19
+ else {
20
+ inAddress = address;
21
+ }
22
+ return this.address.eq(inAddress.address);
23
+ }
24
+ return false;
25
+ }
26
+ static fromString(value, base = 16) {
27
+ if (value) {
28
+ const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base);
29
+ return new EthAddress(bn);
30
+ }
31
+ }
32
+ static parse(value, base) {
33
+ if (typeof value === 'string') {
34
+ return this.fromString(value, base);
35
+ }
36
+ }
37
+ toBigNumber() {
38
+ return this.address;
39
+ }
40
+ toString() {
41
+ return `0x${this.toHex()}`;
42
+ }
43
+ toLowerCaseString() {
44
+ return this.toString().toLowerCase();
45
+ }
46
+ toJSON() {
47
+ return `0x${this.toHex()}`;
48
+ }
49
+ toHex() {
50
+ return padHex(this.address.toString(16), 20);
51
+ }
52
+ toShortString(length = 2) {
53
+ return `0x${ellipsize(this.toHex(), length)}`;
54
+ }
55
+ }
56
+ //# sourceMappingURL=EthAddress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EthAddress.js","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAE7C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEjC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAA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OAAO,KAAK,IAAI,CAAC,KAAK,EAAE,EAAE,CAAA;IAC5B,CAAC;IAEM,iBAAiB;QACtB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,CAAA;IACtC,CAAC;IAEM,MAAM;QACX,OAAO,KAAK,IAAI,CAAC,KAAK,EAAE,EAAE,CAAA;IAC5B,CAAC;IAEM,KAAK;QACV,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IAC9C,CAAC;IAEM,aAAa,CAAC,MAAM,GAAG,CAAC;QAC7B,OAAO,KAAK,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,MAAM,CAAC,EAAE,CAAA;IAC/C,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const ellipsize: (value: string, length?: number) => string;
2
+ //# sourceMappingURL=ellipsize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ellipsize.d.ts","sourceRoot":"","sources":["../../src/ellipsize.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,UAAW,MAAM,4BAItC,CAAA"}
@@ -0,0 +1,6 @@
1
+ export const ellipsize = (value, length = 2) => {
2
+ const part1 = value.slice(0, length);
3
+ const part2 = value.slice(value.length - length, value.length);
4
+ return `${part1}...${part2}`;
5
+ };
6
+ //# sourceMappingURL=ellipsize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ellipsize.js","sourceRoot":"","sources":["../../src/ellipsize.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE;IACrD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;IACpC,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;IAC9D,OAAO,GAAG,KAAK,MAAM,KAAK,EAAE,CAAA;AAC9B,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './ellipsize';
2
+ export * from './EthAddress';
3
+ export * from './padHex';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './ellipsize';
2
+ export * from './EthAddress';
3
+ export * from './padHex';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA"}
@@ -0,0 +1,3 @@
1
+ declare const padHex: (hex: string, byteCount?: number) => string;
2
+ export { padHex };
3
+ //# sourceMappingURL=padHex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"padHex.d.ts","sourceRoot":"","sources":["../../src/padHex.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,QAAS,MAAM,cAAc,MAAM,WAW9C,CAAA;AAED,OAAO,EAAE,MAAM,EAAE,CAAA"}
@@ -0,0 +1,14 @@
1
+ const padHex = (hex, byteCount) => {
2
+ let result = hex;
3
+ if (hex.length % 2 !== 0) {
4
+ result = `0${hex}`;
5
+ }
6
+ if (byteCount) {
7
+ while (result.length / 2 < byteCount) {
8
+ result = `00${result}`;
9
+ }
10
+ }
11
+ return result;
12
+ };
13
+ export { padHex };
14
+ //# sourceMappingURL=padHex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"padHex.js","sourceRoot":"","sources":["../../src/padHex.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,SAAkB,EAAE,EAAE;IACjD,IAAI,MAAM,GAAG,GAAG,CAAA;IAChB,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE;QACxB,MAAM,GAAG,IAAI,GAAG,EAAE,CAAA;KACnB;IACD,IAAI,SAAS,EAAE;QACb,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,GAAG,SAAS,EAAE;YACpC,MAAM,GAAG,KAAK,MAAM,EAAE,CAAA;SACvB;KACF;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED,OAAO,EAAE,MAAM,EAAE,CAAA"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "license": "LGPL-3.0",
3
+ "name": "@xylabs/eth-address",
4
+ "author": {
5
+ "email": "support@xylabs.com",
6
+ "name": "XY Labs Development Team",
7
+ "url": "https://xylabs.com"
8
+ },
9
+ "bugs": {
10
+ "email": "support@xylabs.com",
11
+ "url": "https://github.com/xylabs/sdk-js/issues"
12
+ },
13
+ "workspaces": [
14
+ "packages/**/*"
15
+ ],
16
+ "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
17
+ "browser": "dist/esm/index.js",
18
+ "main": "dist/cjs/index.js",
19
+ "module": "dist/esm/index.js",
20
+ "docs": "dist/docs.json",
21
+ "types": "dist/esm/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "node": {
25
+ "import": "./dist/esm/index.js",
26
+ "require": "./dist/cjs/index.js"
27
+ },
28
+ "browser": {
29
+ "import": "./dist/esm/index.js",
30
+ "require": "./dist/cjs/index.js"
31
+ },
32
+ "default": "./dist/esm/index.js"
33
+ },
34
+ "./dist/docs.json": {
35
+ "default": "./dist/docs.json"
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "homepage": "https://xylabs.com",
40
+ "keywords": [
41
+ "xylabs",
42
+ "utility",
43
+ "typescript",
44
+ "esm"
45
+ ],
46
+ "dependencies": {
47
+ "@xylabs/assert": "^2.6.14",
48
+ "@xylabs/bignumber": "^2.6.14"
49
+ },
50
+ "devDependencies": {
51
+ "@xylabs/eslint-config": "2.11.16",
52
+ "@xylabs/ts-scripts-yarn3": "^2.11.16",
53
+ "eslint": "^8.30.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "https://github.com/xylabs/sdk-js.git"
61
+ },
62
+ "sideEffects": false,
63
+ "version": "2.6.14",
64
+ "packageManager": "yarn@3.3.1"
65
+ }
@@ -0,0 +1,10 @@
1
+ import { EthAddress } from './EthAddress'
2
+
3
+ describe('EthAddress', () => {
4
+ test('checking happy path', () => {
5
+ const addressString = '7284b6A4233B8B05910F2CbF7dBf6715325F6fCb'.toLowerCase()
6
+ const address = EthAddress.fromString(addressString, 16)
7
+ expect(address?.toString()).toBe(`0x${addressString}`)
8
+ expect(address?.toShortString()).toBe('0x72...cb')
9
+ })
10
+ })
@@ -0,0 +1,69 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { BigNumber } from '@xylabs/bignumber'
3
+
4
+ import { ellipsize } from './ellipsize'
5
+ import { padHex } from './padHex'
6
+
7
+ export const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type
8
+
9
+ export class EthAddress {
10
+ public type = EthAddress.type
11
+
12
+ private address: BigNumber
13
+
14
+ static type = 'EthAddress'
15
+
16
+ private constructor(address: BigNumber) {
17
+ this.address = address
18
+ }
19
+
20
+ public equals(address?: EthAddress | string | null): boolean {
21
+ if (address) {
22
+ let inAddress: EthAddress
23
+ if (typeof address === 'string') {
24
+ inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')
25
+ } else {
26
+ inAddress = address
27
+ }
28
+ return this.address.eq(inAddress.address)
29
+ }
30
+ return false
31
+ }
32
+
33
+ static fromString(value?: string, base = 16) {
34
+ if (value) {
35
+ const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)
36
+ return new EthAddress(bn)
37
+ }
38
+ }
39
+
40
+ static parse(value: unknown, base?: number) {
41
+ if (typeof value === 'string') {
42
+ return this.fromString(value, base)
43
+ }
44
+ }
45
+
46
+ public toBigNumber() {
47
+ return this.address
48
+ }
49
+
50
+ public toString() {
51
+ return `0x${this.toHex()}`
52
+ }
53
+
54
+ public toLowerCaseString() {
55
+ return this.toString().toLowerCase()
56
+ }
57
+
58
+ public toJSON(): string {
59
+ return `0x${this.toHex()}`
60
+ }
61
+
62
+ public toHex() {
63
+ return padHex(this.address.toString(16), 20)
64
+ }
65
+
66
+ public toShortString(length = 2) {
67
+ return `0x${ellipsize(this.toHex(), length)}`
68
+ }
69
+ }
@@ -0,0 +1,5 @@
1
+ export const ellipsize = (value: string, length = 2) => {
2
+ const part1 = value.slice(0, length)
3
+ const part2 = value.slice(value.length - length, value.length)
4
+ return `${part1}...${part2}`
5
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './ellipsize'
2
+ export * from './EthAddress'
3
+ export * from './padHex'
package/src/padHex.ts ADDED
@@ -0,0 +1,14 @@
1
+ const padHex = (hex: string, byteCount?: number) => {
2
+ let result = hex
3
+ if (hex.length % 2 !== 0) {
4
+ result = `0${hex}`
5
+ }
6
+ if (byteCount) {
7
+ while (result.length / 2 < byteCount) {
8
+ result = `00${result}`
9
+ }
10
+ }
11
+ return result
12
+ }
13
+
14
+ export { padHex }