@xylabs/eth-address 4.8.9 → 4.9.1
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/dist/neutral/index.mjs +5 -7
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/padHex.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/EthAddress.ts +2 -2
- package/src/padHex.ts +5 -5
package/dist/neutral/index.mjs
CHANGED
|
@@ -10,15 +10,13 @@ import { assertEx } from "@xylabs/assert";
|
|
|
10
10
|
import { getAddress } from "ethers";
|
|
11
11
|
|
|
12
12
|
// src/padHex.ts
|
|
13
|
-
var padHex = (hex, byteCount) => {
|
|
13
|
+
var padHex = (hex, byteCount = 0) => {
|
|
14
14
|
let result = hex;
|
|
15
15
|
if (hex.length % 2 !== 0) {
|
|
16
16
|
result = `0${hex}`;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
result = `00${result}`;
|
|
21
|
-
}
|
|
18
|
+
while (result.length / 2 < byteCount) {
|
|
19
|
+
result = `00${result}`;
|
|
22
20
|
}
|
|
23
21
|
return result;
|
|
24
22
|
};
|
|
@@ -33,7 +31,7 @@ var EthAddress = class _EthAddress {
|
|
|
33
31
|
this.address = address;
|
|
34
32
|
}
|
|
35
33
|
static fromString(value, base = 16) {
|
|
36
|
-
if (value) {
|
|
34
|
+
if (value !== void 0) {
|
|
37
35
|
const bi = base === 16 ? BigInt(value.startsWith("0x") ? value : `0x${value}`) : BigInt(value);
|
|
38
36
|
return new _EthAddress(bi);
|
|
39
37
|
}
|
|
@@ -47,7 +45,7 @@ var EthAddress = class _EthAddress {
|
|
|
47
45
|
return /^(0x)?[\da-f]{40}$/i.test(address);
|
|
48
46
|
}
|
|
49
47
|
equals(address) {
|
|
50
|
-
if (address) {
|
|
48
|
+
if (address !== null && address !== void 0) {
|
|
51
49
|
const inAddress = typeof address === "string" ? assertEx(_EthAddress.fromString(address), () => "Bad Address") : address;
|
|
52
50
|
return this.address === inAddress.address;
|
|
53
51
|
}
|
|
@@ -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)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { getAddress } from 'ethers'\n\nimport { ellipsize } from './ellipsize.ts'\nimport { padHex } from './padHex.ts'\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: bigint\n\n private constructor(address: bigint) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value) {\n const bi = base === 16 ? BigInt(value.startsWith('0x') ? value : `0x${value}`) : BigInt(value)\n return new EthAddress(bi)\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)?[\\da-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address) {\n const inAddress = typeof address === 'string' ? assertEx(EthAddress.fromString(address), () => 'Bad Address') : address\n return this.address === 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 return getAddress(chainId === undefined ? `0x${strippedAddress}` : `${chainId}0x${strippedAddress}`)\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount
|
|
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)\n return `${part1}...${part2}`\n}\n","import { assertEx } from '@xylabs/assert'\nimport { getAddress } from 'ethers'\n\nimport { ellipsize } from './ellipsize.ts'\nimport { padHex } from './padHex.ts'\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: bigint\n\n private constructor(address: bigint) {\n this.address = address\n }\n\n static fromString(value?: string, base = 16) {\n if (value !== undefined) {\n const bi = base === 16 ? BigInt(value.startsWith('0x') ? value : `0x${value}`) : BigInt(value)\n return new EthAddress(bi)\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)?[\\da-f]{40}$/i.test(address)\n }\n\n equals(address?: EthAddress | string | null): boolean {\n if (address !== null && address !== undefined) {\n const inAddress = typeof address === 'string' ? assertEx(EthAddress.fromString(address), () => 'Bad Address') : address\n return this.address === 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 return getAddress(chainId === undefined ? `0x${strippedAddress}` : `${chainId}0x${strippedAddress}`)\n }\n return `0x${this.toHex()}`\n }\n\n validate() {\n return EthAddress.validate(this.toString())\n }\n}\n","const padHex = (hex: string, byteCount = 0) => {\n let result = hex\n if (hex.length % 2 !== 0) {\n result = `0${hex}`\n }\n\n while (result.length / 2 < byteCount) {\n result = `00${result}`\n }\n\n return result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AO,GAAG,MAAM,aAAa,IAAI;AAChH,aAAO,KAAK,YAAY,UAAU;AAAA,IACpC;AACA,WAAO;AAAA,EACT;AAAA,EAEA,cAAc;AACZ,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,QAAQ;AACN,WAAO,OAAO,KAAK,QAAQ,SAAS,EAAE,GAAG,EAAE;AAAA,EAC7C;AAAA,EAEA,SAAiB;AACf,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,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,aAAO,WAAW,YAAY,SAAY,KAAK,eAAe,KAAK,GAAG,OAAO,KAAK,eAAe,EAAE;AAAA,IACrG;AACA,WAAO,KAAK,KAAK,MAAM,CAAC;AAAA,EAC1B;AAAA,EAEA,WAAW;AACT,WAAO,YAAW,SAAS,KAAK,SAAS,CAAC;AAAA,EAC5C;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"padHex.d.ts","sourceRoot":"","sources":["../../src/padHex.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,GAAI,KAAK,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"padHex.d.ts","sourceRoot":"","sources":["../../src/padHex.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,GAAI,KAAK,MAAM,EAAE,kBAAa,WAWzC,CAAA;AAED,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/eth-address",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.9.1",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eth",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
"packages/**/*"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@xylabs/assert": "^4.
|
|
44
|
+
"@xylabs/assert": "^4.9.1",
|
|
45
45
|
"ethers": "^6.13.7"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@xylabs/ts-scripts-yarn3": "^6.
|
|
49
|
-
"@xylabs/tsconfig": "^6.
|
|
48
|
+
"@xylabs/ts-scripts-yarn3": "^6.5.5",
|
|
49
|
+
"@xylabs/tsconfig": "^6.5.5",
|
|
50
50
|
"typescript": "^5.8.3",
|
|
51
|
-
"vitest": "^3.1.
|
|
51
|
+
"vitest": "^3.1.3"
|
|
52
52
|
},
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=18"
|
package/src/EthAddress.ts
CHANGED
|
@@ -18,7 +18,7 @@ export class EthAddress {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
static fromString(value?: string, base = 16) {
|
|
21
|
-
if (value) {
|
|
21
|
+
if (value !== undefined) {
|
|
22
22
|
const bi = base === 16 ? BigInt(value.startsWith('0x') ? value : `0x${value}`) : BigInt(value)
|
|
23
23
|
return new EthAddress(bi)
|
|
24
24
|
}
|
|
@@ -35,7 +35,7 @@ export class EthAddress {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
equals(address?: EthAddress | string | null): boolean {
|
|
38
|
-
if (address) {
|
|
38
|
+
if (address !== null && address !== undefined) {
|
|
39
39
|
const inAddress = typeof address === 'string' ? assertEx(EthAddress.fromString(address), () => 'Bad Address') : address
|
|
40
40
|
return this.address === inAddress.address
|
|
41
41
|
}
|
package/src/padHex.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
const padHex = (hex: string, byteCount
|
|
1
|
+
const padHex = (hex: string, byteCount = 0) => {
|
|
2
2
|
let result = hex
|
|
3
3
|
if (hex.length % 2 !== 0) {
|
|
4
4
|
result = `0${hex}`
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
6
|
+
|
|
7
|
+
while (result.length / 2 < byteCount) {
|
|
8
|
+
result = `00${result}`
|
|
10
9
|
}
|
|
10
|
+
|
|
11
11
|
return result
|
|
12
12
|
}
|
|
13
13
|
|