@xylabs/eth-address 2.12.24 β 2.12.26
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.
- package/README.md +25 -31
- package/dist/browser/EthAddress.d.cts +4 -2
- package/dist/browser/EthAddress.d.cts.map +1 -1
- package/dist/browser/EthAddress.d.mts +4 -2
- package/dist/browser/EthAddress.d.mts.map +1 -1
- package/dist/browser/EthAddress.d.ts +4 -2
- package/dist/browser/EthAddress.d.ts.map +1 -1
- package/dist/browser/index.cjs +27 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +17 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/EthAddress.d.cts +4 -2
- package/dist/node/EthAddress.d.cts.map +1 -1
- package/dist/node/EthAddress.d.mts +4 -2
- package/dist/node/EthAddress.d.mts.map +1 -1
- package/dist/node/EthAddress.d.ts +4 -2
- package/dist/node/EthAddress.d.ts.map +1 -1
- package/dist/node/index.cjs +27 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +17 -1
- package/dist/node/index.js.map +1 -1
- package/package.json +7 -9
- package/src/EthAddress.ts +22 -2
package/README.md
CHANGED
|
@@ -1,50 +1,49 @@
|
|
|
1
1
|
[![logo][]](https://xylabs.com)
|
|
2
2
|
|
|
3
|
-
# @xylabs/
|
|
3
|
+
# @xylabs/eth-address
|
|
4
4
|
|
|
5
|
-
[![main-build][]][main-build-link]
|
|
6
|
-
[![beta-build][]][beta-build-link]
|
|
7
5
|
[![npm-badge][]][npm-link]
|
|
8
|
-
[![
|
|
9
|
-
[![
|
|
10
|
-
[![
|
|
11
|
-
|
|
6
|
+
[![npm-downloads-badge][]][npm-link]
|
|
7
|
+
[![jsdelivr-badge][]][jsdelivr-link]
|
|
8
|
+
[![npm-license-badge][]](LICENSE)
|
|
9
|
+
|
|
10
|
+
> XY Labs generalized Javascript library
|
|
12
11
|
|
|
13
12
|
## Table of Contents
|
|
14
13
|
|
|
15
14
|
- [Description](#description)
|
|
16
15
|
- [Install](#install)
|
|
17
|
-
- [Scripts](#scripts)
|
|
18
16
|
- [Maintainers](#maintainers)
|
|
19
17
|
- [License](#license)
|
|
20
18
|
- [Credits](#credits)
|
|
21
19
|
|
|
22
20
|
## Description
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
Common Javascript code that is used throughout XYO projects that use React.
|
|
25
23
|
|
|
26
24
|
## Install
|
|
27
25
|
|
|
28
26
|
Using npm:
|
|
29
27
|
|
|
30
28
|
```sh
|
|
31
|
-
npm i --save @xylabs/
|
|
29
|
+
npm i --save @xylabs/eth-address
|
|
32
30
|
```
|
|
33
31
|
|
|
34
32
|
Using yarn:
|
|
35
33
|
|
|
36
34
|
```sh
|
|
37
|
-
yarn add @xylabs/
|
|
35
|
+
yarn add @xylabs/eth-address
|
|
38
36
|
```
|
|
39
37
|
|
|
40
|
-
##
|
|
41
|
-
|
|
42
|
-
See [ts-scripts](https://github.com/xylabs/ts-scripts/blob/main/README.md)
|
|
43
|
-
for list of shared scripts.
|
|
38
|
+
## Documentation
|
|
39
|
+
[Developer Reference](https://xylabs.github.io/sdk-js)
|
|
44
40
|
|
|
45
41
|
## Maintainers
|
|
46
42
|
|
|
47
|
-
- [Arie Trouw](https://github.com/arietrouw) (
|
|
43
|
+
- [Arie Trouw](https://github.com/arietrouw) ([arietrouw.com](https://arietrouw.com))
|
|
44
|
+
- [Joel Carter](https://github.com/JoelBCarter)
|
|
45
|
+
- [Matt Jones](https://github.com/jonesmac)
|
|
46
|
+
- [Jordan Trouw](https://github.com/jordantrouw)
|
|
48
47
|
|
|
49
48
|
## License
|
|
50
49
|
|
|
@@ -52,20 +51,15 @@ See the [LICENSE](LICENSE) file for license details
|
|
|
52
51
|
|
|
53
52
|
## Credits
|
|
54
53
|
|
|
55
|
-
[Made with π₯
|
|
54
|
+
[Made with π₯and βοΈ by XY Labs](https://xylabs.com)
|
|
56
55
|
|
|
57
56
|
[logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
|
|
58
|
-
|
|
59
|
-
[
|
|
60
|
-
[
|
|
61
|
-
|
|
62
|
-
[npm-badge]: https://img.shields.io/npm/
|
|
63
|
-
[npm-
|
|
64
|
-
|
|
65
|
-
[
|
|
66
|
-
[
|
|
67
|
-
[codacy-link]: https://www.codacy.com/gh/xylabs/sdk-js/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xylabs/sdk-js&utm_campaign=Badge_Grade
|
|
68
|
-
[codeclimate-badge]: https://api.codeclimate.com/v1/badges/c5eb068f806f0b047ea7/maintainability
|
|
69
|
-
[codeclimate-link]: https://codeclimate.com/github/xylabs/sdk-js/maintainability
|
|
70
|
-
[snyk-badge]: https://snyk.io/test/github/xylabs/sdk-js/badge.svg?targetFile=package.json
|
|
71
|
-
[snyk-link]: https://snyk.io/test/github/xylabs/sdk-js?targetFile=package.json
|
|
57
|
+
|
|
58
|
+
[npm-badge]: https://img.shields.io/npm/v/@xylabs/eth-address.svg
|
|
59
|
+
[npm-link]: https://www.npmjs.com/package/@xylabs/eth-address
|
|
60
|
+
|
|
61
|
+
[npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/eth-address
|
|
62
|
+
[npm-license-badge]: https://img.shields.io/npm/l/@xylabs/eth-address
|
|
63
|
+
|
|
64
|
+
[jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/eth-address/badge
|
|
65
|
+
[jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/eth-address
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
package/dist/browser/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -37,6 +47,7 @@ var ellipsize = (value, length = 2) => {
|
|
|
37
47
|
// src/EthAddress.ts
|
|
38
48
|
var import_assert = require("@xylabs/assert");
|
|
39
49
|
var import_bignumber = require("@xylabs/bignumber");
|
|
50
|
+
var import_keccak256 = __toESM(require("keccak256"), 1);
|
|
40
51
|
|
|
41
52
|
// src/padHex.ts
|
|
42
53
|
var padHex = (hex, byteCount) => {
|
|
@@ -72,6 +83,9 @@ var EthAddress = class _EthAddress {
|
|
|
72
83
|
return this.fromString(value, base);
|
|
73
84
|
}
|
|
74
85
|
}
|
|
86
|
+
static validate(address) {
|
|
87
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address);
|
|
88
|
+
}
|
|
75
89
|
equals(address) {
|
|
76
90
|
if (address) {
|
|
77
91
|
let inAddress;
|
|
@@ -99,8 +113,20 @@ var EthAddress = class _EthAddress {
|
|
|
99
113
|
toShortString(length = 2) {
|
|
100
114
|
return `0x${ellipsize(this.toHex(), length)}`;
|
|
101
115
|
}
|
|
102
|
-
toString() {
|
|
116
|
+
toString(checksum, chainId) {
|
|
117
|
+
if (checksum) {
|
|
118
|
+
const strippedAddress = this.toHex();
|
|
119
|
+
const keccakHash = (0, import_keccak256.default)(chainId !== void 0 ? `${chainId}0x${strippedAddress}` : strippedAddress).toString("hex");
|
|
120
|
+
let checksumAddress = "0x";
|
|
121
|
+
for (let i = 0; i < strippedAddress.length; i++) {
|
|
122
|
+
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i];
|
|
123
|
+
}
|
|
124
|
+
return checksumAddress;
|
|
125
|
+
}
|
|
103
126
|
return `0x${this.toHex()}`;
|
|
104
127
|
}
|
|
128
|
+
validate() {
|
|
129
|
+
return _EthAddress.validate(this.toString());
|
|
130
|
+
}
|
|
105
131
|
};
|
|
106
132
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export * from './ellipsize'\nexport * from './EthAddress'\nexport * from './padHex'\n","export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString() {\n return `0x${this.toHex()}`\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export * from './ellipsize'\nexport * from './EthAddress'\nexport * from './padHex'\n","export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\nimport keccak256 from 'keccak256'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static readonly type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n static validate(address: string) {\n return /^(0x)?[0-9a-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString(checksum?: boolean, chainId?: string) {\n if (checksum) {\n const strippedAddress = this.toHex()\n const keccakHash = keccak256(chainId !== undefined ? `${chainId}0x${strippedAddress}` : strippedAddress).toString('hex')\n let checksumAddress = '0x'\n\n for (let i = 0; i < strippedAddress.length; i++) {\n checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i]\n }\n\n return checksumAddress\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,oBAAyB;AACzB,uBAA0B;AAC1B,uBAAsB;;;ACFtB,IAAM,SAAS,CAAC,KAAa,cAAuB;AAClD,MAAI,SAAS;AACb,MAAI,IAAI,SAAS,MAAM,GAAG;AACxB,aAAS,IAAI,GAAG;AAAA,EAClB;AACA,MAAI,WAAW;AACb,WAAO,OAAO,SAAS,IAAI,WAAW;AACpC,eAAS,KAAK,MAAM;AAAA,IACtB;AAAA,EACF;AACA,S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AAAA,EAC7C;AAAA,EAEA,SAAS,UAAoB,SAAkB;AAC7C,QAAI,UAAU;AACZ,YAAM,kBAAkB,KAAK,MAAM;AACnC,YAAM,iBAAa,iBAAAA,SAAU,YAAY,SAAY,GAAG,OAAO,KAAK,eAAe,KAAK,eAAe,EAAE,SAAS,KAAK;AACvH,UAAI,kBAAkB;AAEtB,eAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;AAC/C,2BAAmB,SAAS,WAAW,CAAC,GAAG,EAAE,KAAK,IAAI,gBAAgB,CAAC,EAAE,YAAY,IAAI,gBAAgB,CAAC;AAAA,MAC5G;AAEA,aAAO;AAAA,IACT;AACA,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,EAEA,WAAW;AACT,WAAO,YAAW,SAAS,KAAK,SAAS,CAAC;AAAA,EAC5C;AACF;","names":["keccak256"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var ellipsize = (value, length = 2) => {
|
|
|
8
8
|
// src/EthAddress.ts
|
|
9
9
|
import { assertEx } from "@xylabs/assert";
|
|
10
10
|
import { BigNumber } from "@xylabs/bignumber";
|
|
11
|
+
import keccak256 from "keccak256";
|
|
11
12
|
|
|
12
13
|
// src/padHex.ts
|
|
13
14
|
var padHex = (hex, byteCount) => {
|
|
@@ -43,6 +44,9 @@ var EthAddress = class _EthAddress {
|
|
|
43
44
|
return this.fromString(value, base);
|
|
44
45
|
}
|
|
45
46
|
}
|
|
47
|
+
static validate(address) {
|
|
48
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address);
|
|
49
|
+
}
|
|
46
50
|
equals(address) {
|
|
47
51
|
if (address) {
|
|
48
52
|
let inAddress;
|
|
@@ -70,9 +74,21 @@ var EthAddress = class _EthAddress {
|
|
|
70
74
|
toShortString(length = 2) {
|
|
71
75
|
return `0x${ellipsize(this.toHex(), length)}`;
|
|
72
76
|
}
|
|
73
|
-
toString() {
|
|
77
|
+
toString(checksum, chainId) {
|
|
78
|
+
if (checksum) {
|
|
79
|
+
const strippedAddress = this.toHex();
|
|
80
|
+
const keccakHash = keccak256(chainId !== void 0 ? `${chainId}0x${strippedAddress}` : strippedAddress).toString("hex");
|
|
81
|
+
let checksumAddress = "0x";
|
|
82
|
+
for (let i = 0; i < strippedAddress.length; i++) {
|
|
83
|
+
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i];
|
|
84
|
+
}
|
|
85
|
+
return checksumAddress;
|
|
86
|
+
}
|
|
74
87
|
return `0x${this.toHex()}`;
|
|
75
88
|
}
|
|
89
|
+
validate() {
|
|
90
|
+
return _EthAddress.validate(this.toString());
|
|
91
|
+
}
|
|
76
92
|
};
|
|
77
93
|
export {
|
|
78
94
|
EthAddress,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString() {\n return `0x${this.toHex()}`\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";AAAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;;;
|
|
1
|
+
{"version":3,"sources":["../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\nimport keccak256 from 'keccak256'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static readonly type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n static validate(address: string) {\n return /^(0x)?[0-9a-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString(checksum?: boolean, chainId?: string) {\n if (checksum) {\n const strippedAddress = this.toHex()\n const keccakHash = keccak256(chainId !== undefined ? `${chainId}0x${strippedAddress}` : strippedAddress).toString('hex')\n let checksumAddress = '0x'\n\n for (let i = 0; i < strippedAddress.length; i++) {\n checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i]\n }\n\n return checksumAddress\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";AAAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,OAAO,eAAe;;;ACFtB,IAAM,SAAS,CAAC,KAAa,cAAuB;AAClD,MAAI,SAAS;AACb,MAAI,IAAI,SAAS,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AA,EAEA,oBAAoB;AAClB,WAAO,KAAK,SAAS,EAAE,YAAY;AAAA,EACrC;AAAA,EAEA,cAAc,SAAS,GAAG;AACxB,WAAO,KAAK,UAAU,KAAK,MAAM,GAAG,MAAM,CAAC;AAAA,EAC7C;AAAA,EAEA,SAAS,UAAoB,SAAkB;AAC7C,QAAI,UAAU;AACZ,YAAM,kBAAkB,KAAK,MAAM;AACnC,YAAM,aAAa,UAAU,YAAY,SAAY,GAAG,OAAO,KAAK,eAAe,KAAK,eAAe,EAAE,SAAS,KAAK;AACvH,UAAI,kBAAkB;AAEtB,eAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;AAC/C,2BAAmB,SAAS,WAAW,CAAC,GAAG,EAAE,KAAK,IAAI,gBAAgB,CAAC,EAAE,YAAY,IAAI,gBAAgB,CAAC;AAAA,MAC5G;AAEA,aAAO;AAAA,IACT;AACA,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,EAEA,WAAW;AACT,WAAO,YAAW,SAAS,KAAK,SAAS,CAAC;AAAA,EAC5C;AACF;","names":[]}
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
|
@@ -3,18 +3,20 @@ export declare const isEthAddress: (obj: {
|
|
|
3
3
|
type: string;
|
|
4
4
|
}) => boolean;
|
|
5
5
|
export declare class EthAddress {
|
|
6
|
-
static type
|
|
6
|
+
static readonly type = "EthAddress";
|
|
7
7
|
type: string;
|
|
8
8
|
private address;
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromString(value?: string, base?: number): EthAddress | undefined;
|
|
11
11
|
static parse(value: unknown, base?: number): EthAddress | undefined;
|
|
12
|
+
static validate(address: string): boolean;
|
|
12
13
|
equals(address?: EthAddress | string | null): boolean;
|
|
13
14
|
toBigNumber(): BigNumber;
|
|
14
15
|
toHex(): string;
|
|
15
16
|
toJSON(): string;
|
|
16
17
|
toLowerCaseString(): string;
|
|
17
18
|
toShortString(length?: number): string;
|
|
18
|
-
toString(): string;
|
|
19
|
+
toString(checksum?: boolean, chainId?: string): string;
|
|
20
|
+
validate(): boolean;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAM7C,eAAO,MAAM,YAAY,QAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,YAAkC,CAAA;AAEpF,qBAAa,UAAU;IACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAe;IAEnC,IAAI,SAAkB;IAEtB,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO;IAIP,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAK;IAO3C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM;IAM1C,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAI/B,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAarD,WAAW;IAIX,KAAK;IAIL,MAAM,IAAI,MAAM;IAIhB,iBAAiB;IAIjB,aAAa,CAAC,MAAM,SAAI;IAIxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM;IAe7C,QAAQ;CAGT"}
|
package/dist/node/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -37,6 +47,7 @@ var ellipsize = (value, length = 2) => {
|
|
|
37
47
|
// src/EthAddress.ts
|
|
38
48
|
var import_assert = require("@xylabs/assert");
|
|
39
49
|
var import_bignumber = require("@xylabs/bignumber");
|
|
50
|
+
var import_keccak256 = __toESM(require("keccak256"), 1);
|
|
40
51
|
|
|
41
52
|
// src/padHex.ts
|
|
42
53
|
var padHex = (hex, byteCount) => {
|
|
@@ -72,6 +83,9 @@ var EthAddress = class _EthAddress {
|
|
|
72
83
|
return this.fromString(value, base);
|
|
73
84
|
}
|
|
74
85
|
}
|
|
86
|
+
static validate(address) {
|
|
87
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address);
|
|
88
|
+
}
|
|
75
89
|
equals(address) {
|
|
76
90
|
if (address) {
|
|
77
91
|
let inAddress;
|
|
@@ -99,9 +113,21 @@ var EthAddress = class _EthAddress {
|
|
|
99
113
|
toShortString(length = 2) {
|
|
100
114
|
return `0x${ellipsize(this.toHex(), length)}`;
|
|
101
115
|
}
|
|
102
|
-
toString() {
|
|
116
|
+
toString(checksum, chainId) {
|
|
117
|
+
if (checksum) {
|
|
118
|
+
const strippedAddress = this.toHex();
|
|
119
|
+
const keccakHash = (0, import_keccak256.default)(chainId !== void 0 ? `${chainId}0x${strippedAddress}` : strippedAddress).toString("hex");
|
|
120
|
+
let checksumAddress = "0x";
|
|
121
|
+
for (let i = 0; i < strippedAddress.length; i++) {
|
|
122
|
+
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i];
|
|
123
|
+
}
|
|
124
|
+
return checksumAddress;
|
|
125
|
+
}
|
|
103
126
|
return `0x${this.toHex()}`;
|
|
104
127
|
}
|
|
128
|
+
validate() {
|
|
129
|
+
return _EthAddress.validate(this.toString());
|
|
130
|
+
}
|
|
105
131
|
};
|
|
106
132
|
// Annotate the CommonJS export names for ESM import in node:
|
|
107
133
|
0 && (module.exports = {
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export * from './ellipsize'\nexport * from './EthAddress'\nexport * from './padHex'\n","export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString() {\n return `0x${this.toHex()}`\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export * from './ellipsize'\nexport * from './EthAddress'\nexport * from './padHex'\n","export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\nimport keccak256 from 'keccak256'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static readonly type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n static validate(address: string) {\n return /^(0x)?[0-9a-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString(checksum?: boolean, chainId?: string) {\n if (checksum) {\n const strippedAddress = this.toHex()\n const keccakHash = keccak256(chainId !== undefined ? `${chainId}0x${strippedAddress}` : strippedAddress).toString('hex')\n let checksumAddress = '0x'\n\n for (let i = 0; i < strippedAddress.length; i++) {\n checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i]\n }\n\n return checksumAddress\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,oBAAyB;AACzB,uBAA0B;AAC1B,uBAAsB;;;ACFtB,IAAM,SAAS,CAAC,KAAa,cAAuB;AAClD,MAAI,SAAS;AACb,MAAI,IAAI,SAAS,MAAM,GAAG;AACxB,aAAS,IAAI,GAAG;AAAA,EAClB;AACA,MAAI,WAAW;AACb,WAAO,OAAO,SAAS,IAAI,WAAW;AACpC,eAAS,KAAK,MAAM;AAAA,IACtB;AAAA,EACF;AACA,SAAO;AACT;;;ADJO,IAAM,eAAe,CAAC,SAA0B,2BAAK,UAAS,WAAW;AAEzE,IAAM,aAAN,MAAM,YAAW;AAAA,EACtB,OAAgB,OAAO;AAAA,EAEvB,OAAO,YAAW;AAAA,EAEV;AAAA,EAEA,YAAY,SAAoB;AACtC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SAAS,KAAK;AACvH,UAAI,kBAAkB;AAEtB,eAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;AAC/C,2BAAmB,SAAS,WAAW,CAAC,GAAG,EAAE,KAAK,IAAI,gBAAgB,CAAC,EAAE,YAAY,IAAI,gBAAgB,CAAC;AAAA,MAC5G;AAEA,aAAO;AAAA,IACT;AACA,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,EAEA,WAAW;AACT,WAAO,YAAW,SAAS,KAAK,SAAS,CAAC;AAAA,EAC5C;AACF;","names":["keccak256"]}
|
package/dist/node/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var ellipsize = (value, length = 2) => {
|
|
|
8
8
|
// src/EthAddress.ts
|
|
9
9
|
import { assertEx } from "@xylabs/assert";
|
|
10
10
|
import { BigNumber } from "@xylabs/bignumber";
|
|
11
|
+
import keccak256 from "keccak256";
|
|
11
12
|
|
|
12
13
|
// src/padHex.ts
|
|
13
14
|
var padHex = (hex, byteCount) => {
|
|
@@ -43,6 +44,9 @@ var EthAddress = class _EthAddress {
|
|
|
43
44
|
return this.fromString(value, base);
|
|
44
45
|
}
|
|
45
46
|
}
|
|
47
|
+
static validate(address) {
|
|
48
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address);
|
|
49
|
+
}
|
|
46
50
|
equals(address) {
|
|
47
51
|
if (address) {
|
|
48
52
|
let inAddress;
|
|
@@ -70,9 +74,21 @@ var EthAddress = class _EthAddress {
|
|
|
70
74
|
toShortString(length = 2) {
|
|
71
75
|
return `0x${ellipsize(this.toHex(), length)}`;
|
|
72
76
|
}
|
|
73
|
-
toString() {
|
|
77
|
+
toString(checksum, chainId) {
|
|
78
|
+
if (checksum) {
|
|
79
|
+
const strippedAddress = this.toHex();
|
|
80
|
+
const keccakHash = keccak256(chainId !== void 0 ? `${chainId}0x${strippedAddress}` : strippedAddress).toString("hex");
|
|
81
|
+
let checksumAddress = "0x";
|
|
82
|
+
for (let i = 0; i < strippedAddress.length; i++) {
|
|
83
|
+
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i];
|
|
84
|
+
}
|
|
85
|
+
return checksumAddress;
|
|
86
|
+
}
|
|
74
87
|
return `0x${this.toHex()}`;
|
|
75
88
|
}
|
|
89
|
+
validate() {
|
|
90
|
+
return _EthAddress.validate(this.toString());
|
|
91
|
+
}
|
|
76
92
|
};
|
|
77
93
|
export {
|
|
78
94
|
EthAddress,
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString() {\n return `0x${this.toHex()}`\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";AAAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;;;
|
|
1
|
+
{"version":3,"sources":["../../src/ellipsize.ts","../../src/EthAddress.ts","../../src/padHex.ts"],"sourcesContent":["export const ellipsize = (value: string, length = 2) => {\n const part1 = value.slice(0, length)\n const part2 = value.slice(value.length - length, value.length)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { BigNumber } from '@xylabs/bignumber'\nimport keccak256 from 'keccak256'\n\nimport { ellipsize } from './ellipsize'\nimport { padHex } from './padHex'\n\nexport const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type\n\nexport class EthAddress {\n static readonly type = 'EthAddress'\n\n type = EthAddress.type\n\n private address: BigNumber\n\n private constructor(address: BigNumber) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bn = new BigNumber(value.startsWith('0x') ? value.substring(2) : value, base)\n return new EthAddress(bn)\n }\n }\n\n static parse(value: unknown, base?: number) {\n if (typeof value === 'string') {\n return this.fromString(value, base)\n }\n }\n\n static validate(address: string) {\n return /^(0x)?[0-9a-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n let inAddress: EthAddress\n if (typeof address === 'string') {\n inAddress = assertEx(EthAddress.fromString(address), 'Bad Address')\n } else {\n inAddress = address\n }\n return this.address.eq(inAddress.address)\n }\n return false\n }\n\n toBigNumber() {\n return this.address\n }\n\n toHex() {\n return padHex(this.address.toString(16), 20)\n }\n\n toJSON(): string {\n return `0x${this.toHex()}`\n }\n\n toLowerCaseString() {\n return this.toString().toLowerCase()\n }\n\n toShortString(length = 2) {\n return `0x${ellipsize(this.toHex(), length)}`\n }\n\n toString(checksum?: boolean, chainId?: string) {\n if (checksum) {\n const strippedAddress = this.toHex()\n const keccakHash = keccak256(chainId !== undefined ? `${chainId}0x${strippedAddress}` : strippedAddress).toString('hex')\n let checksumAddress = '0x'\n\n for (let i = 0; i < strippedAddress.length; i++) {\n checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i]\n }\n\n return checksumAddress\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount?: number) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n if (byteCount) {\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n }\n return result\n}\n\nexport { padHex }\n"],"mappings":";AAAO,IAAM,YAAY,CAAC,OAAe,SAAS,MAAM;AACtD,QAAM,QAAQ,MAAM,MAAM,GAAG,MAAM;AACnC,QAAM,QAAQ,MAAM,MAAM,MAAM,SAAS,QAAQ,MAAM,MAAM;AAC7D,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,OAAO,eAAe;;;ACFtB,IAAM,SAAS,CAAC,KAAa,cAAuB;AAClD,MAAI,SAAS;AACb,MAAI,IAAI,SAAS,MAAM,GAAG;AACxB,aAAS,IAAI,GAAG;AAAA,EAClB;AACA,MAAI,WAAW;AACb,WAAO,OAAO,SAAS,IAAI,WAAW;AACpC,eAAS,KAAK,MAAM;AAAA,IACtB;AAAA,EACF;AACA,SAAO;AACT;;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;AAAA,EAEA,SAAS,UAAoB,SAAkB;AAC7C,QAAI,UAAU;AACZ,YAAM,kBAAkB,KAAK,MAAM;AACnC,YAAM,aAAa,UAAU,YAAY,SAAY,GAAG,OAAO,KAAK,eAAe,KAAK,eAAe,EAAE,SAAS,KAAK;AACvH,UAAI,kBAAkB;AAEtB,eAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;AAC/C,2BAAmB,SAAS,WAAW,CAAC,GAAG,EAAE,KAAK,IAAI,gBAAgB,CAAC,EAAE,YAAY,IAAI,gBAAgB,CAAC;AAAA,MAC5G;AAEA,aAAO;AAAA,IACT;AACA,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,EAEA,WAAW;AACT,WAAO,YAAW,SAAS,KAAK,SAAS,CAAC;AAAA,EAC5C;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -26,9 +26,6 @@
|
|
|
26
26
|
"default": "./dist/node/index.js"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
-
"./docs": {
|
|
30
|
-
"default": "./dist/docs.json"
|
|
31
|
-
},
|
|
32
29
|
"./package.json": "./package.json"
|
|
33
30
|
},
|
|
34
31
|
"main": "./dist/node/index.cjs",
|
|
@@ -42,13 +39,14 @@
|
|
|
42
39
|
"esm"
|
|
43
40
|
],
|
|
44
41
|
"dependencies": {
|
|
45
|
-
"@xylabs/assert": "~2.12.
|
|
46
|
-
"@xylabs/bignumber": "~2.12.
|
|
42
|
+
"@xylabs/assert": "~2.12.26",
|
|
43
|
+
"@xylabs/bignumber": "~2.12.26",
|
|
44
|
+
"keccak256": "^1.0.6"
|
|
47
45
|
},
|
|
48
46
|
"devDependencies": {
|
|
49
|
-
"@xylabs/ts-scripts-yarn3": "^3.1.
|
|
50
|
-
"@xylabs/tsconfig": "^3.1.
|
|
51
|
-
"@xylabs/tsconfig-jest": "^3.1.
|
|
47
|
+
"@xylabs/ts-scripts-yarn3": "^3.1.11",
|
|
48
|
+
"@xylabs/tsconfig": "^3.1.11",
|
|
49
|
+
"@xylabs/tsconfig-jest": "^3.1.11",
|
|
52
50
|
"typescript": "^5.2.2"
|
|
53
51
|
},
|
|
54
52
|
"publishConfig": {
|
|
@@ -59,6 +57,6 @@
|
|
|
59
57
|
"url": "https://github.com/xylabs/sdk-js.git"
|
|
60
58
|
},
|
|
61
59
|
"sideEffects": false,
|
|
62
|
-
"version": "2.12.
|
|
60
|
+
"version": "2.12.26",
|
|
63
61
|
"type": "module"
|
|
64
62
|
}
|
package/src/EthAddress.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
2
|
import { BigNumber } from '@xylabs/bignumber'
|
|
3
|
+
import keccak256 from 'keccak256'
|
|
3
4
|
|
|
4
5
|
import { ellipsize } from './ellipsize'
|
|
5
6
|
import { padHex } from './padHex'
|
|
@@ -7,7 +8,7 @@ import { padHex } from './padHex'
|
|
|
7
8
|
export const isEthAddress = (obj: { type: string }) => obj?.type === EthAddress.type
|
|
8
9
|
|
|
9
10
|
export class EthAddress {
|
|
10
|
-
static type = 'EthAddress'
|
|
11
|
+
static readonly type = 'EthAddress'
|
|
11
12
|
|
|
12
13
|
type = EthAddress.type
|
|
13
14
|
|
|
@@ -30,6 +31,10 @@ export class EthAddress {
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
static validate(address: string) {
|
|
35
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address)
|
|
36
|
+
}
|
|
37
|
+
|
|
33
38
|
equals(address?: EthAddress | string | null): boolean {
|
|
34
39
|
if (address) {
|
|
35
40
|
let inAddress: EthAddress
|
|
@@ -63,7 +68,22 @@ export class EthAddress {
|
|
|
63
68
|
return `0x${ellipsize(this.toHex(), length)}`
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
toString() {
|
|
71
|
+
toString(checksum?: boolean, chainId?: string) {
|
|
72
|
+
if (checksum) {
|
|
73
|
+
const strippedAddress = this.toHex()
|
|
74
|
+
const keccakHash = keccak256(chainId !== undefined ? `${chainId}0x${strippedAddress}` : strippedAddress).toString('hex')
|
|
75
|
+
let checksumAddress = '0x'
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < strippedAddress.length; i++) {
|
|
78
|
+
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? strippedAddress[i].toUpperCase() : strippedAddress[i]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return checksumAddress
|
|
82
|
+
}
|
|
67
83
|
return `0x${this.toHex()}`
|
|
68
84
|
}
|
|
85
|
+
|
|
86
|
+
validate() {
|
|
87
|
+
return EthAddress.validate(this.toString())
|
|
88
|
+
}
|
|
69
89
|
}
|