@xylabs/eth-address 4.15.0 → 4.15.2
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.
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
export declare const isEthAddressWrapper: (obj: {
|
|
2
|
+
type: string;
|
|
3
|
+
}) => obj is {
|
|
4
|
+
type: string;
|
|
5
|
+
} & EthAddressWrapper;
|
|
6
|
+
/** @deprecated use isEthAddressWrapper */
|
|
1
7
|
export declare const isEthAddress: (obj: {
|
|
2
8
|
type: string;
|
|
3
|
-
}) =>
|
|
4
|
-
export declare class EthAddress {
|
|
5
|
-
static readonly type = "EthAddress";
|
|
9
|
+
}) => obj is {
|
|
6
10
|
type: string;
|
|
11
|
+
} & EthAddressWrapper;
|
|
12
|
+
export declare class EthAddressWrapper {
|
|
7
13
|
private address;
|
|
8
|
-
|
|
9
|
-
static fromString(value?: string, base?: number):
|
|
10
|
-
static parse(value: unknown, base?: number):
|
|
14
|
+
protected constructor(address: bigint);
|
|
15
|
+
static fromString(value?: string, base?: number): EthAddressWrapper | undefined;
|
|
16
|
+
static parse(value: unknown, base?: number): EthAddressWrapper | undefined;
|
|
11
17
|
static validate(address: string): boolean;
|
|
12
|
-
equals(address?:
|
|
18
|
+
equals(address?: EthAddressWrapper | string | null): boolean;
|
|
13
19
|
toBigNumber(): bigint;
|
|
14
20
|
toHex(): string;
|
|
15
21
|
toJSON(): string;
|
|
@@ -18,4 +24,7 @@ export declare class EthAddress {
|
|
|
18
24
|
toString(checksum?: boolean, chainId?: string): string;
|
|
19
25
|
validate(): boolean;
|
|
20
26
|
}
|
|
27
|
+
/** @deprecated use EthAddressWrapper */
|
|
28
|
+
export declare class EthAddress extends EthAddressWrapper {
|
|
29
|
+
}
|
|
21
30
|
//# sourceMappingURL=EthAddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"EthAddress.d.ts","sourceRoot":"","sources":["../../src/EthAddress.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,mBAAmB,GAAI,KAAK;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;UAAR,MAAM;qBAAuC,CAAA;AAE9F,0CAA0C;AAC1C,eAAO,MAAM,YAAY,QAHgB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;UAAR,MAAM;qBAGR,CAAA;AAE/C,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAQ;IAEvB,SAAS,aAAa,OAAO,EAAE,MAAM;IAIrC,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,iBAAiB,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAQ5D,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;IAQ7C,QAAQ;CAGT;AAED,wCAAwC;AACxC,qBAAa,UAAW,SAAQ,iBAAiB;CAAG"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -22,10 +22,9 @@ var padHex = (hex, byteCount = 0) => {
|
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
// src/EthAddress.ts
|
|
25
|
-
var
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
type = _EthAddress.type;
|
|
25
|
+
var isEthAddressWrapper = (obj) => obj instanceof EthAddressWrapper;
|
|
26
|
+
var isEthAddress = isEthAddressWrapper;
|
|
27
|
+
var EthAddressWrapper = class _EthAddressWrapper {
|
|
29
28
|
address;
|
|
30
29
|
constructor(address) {
|
|
31
30
|
this.address = address;
|
|
@@ -33,7 +32,7 @@ var EthAddress = class _EthAddress {
|
|
|
33
32
|
static fromString(value, base = 16) {
|
|
34
33
|
if (value !== void 0) {
|
|
35
34
|
const bi = base === 16 ? BigInt(value.startsWith("0x") ? value : `0x${value}`) : BigInt(value);
|
|
36
|
-
return new
|
|
35
|
+
return new _EthAddressWrapper(bi);
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
38
|
static parse(value, base) {
|
|
@@ -46,7 +45,7 @@ var EthAddress = class _EthAddress {
|
|
|
46
45
|
}
|
|
47
46
|
equals(address) {
|
|
48
47
|
if (address !== null && address !== void 0) {
|
|
49
|
-
const inAddress = typeof address === "string" ? assertEx(
|
|
48
|
+
const inAddress = typeof address === "string" ? assertEx(_EthAddressWrapper.fromString(address), () => "Bad Address") : address;
|
|
50
49
|
return this.address === inAddress.address;
|
|
51
50
|
}
|
|
52
51
|
return false;
|
|
@@ -74,13 +73,17 @@ var EthAddress = class _EthAddress {
|
|
|
74
73
|
return `0x${this.toHex()}`;
|
|
75
74
|
}
|
|
76
75
|
validate() {
|
|
77
|
-
return
|
|
76
|
+
return _EthAddressWrapper.validate(this.toString());
|
|
78
77
|
}
|
|
79
78
|
};
|
|
79
|
+
var EthAddress = class extends EthAddressWrapper {
|
|
80
|
+
};
|
|
80
81
|
export {
|
|
81
82
|
EthAddress,
|
|
83
|
+
EthAddressWrapper,
|
|
82
84
|
ellipsize,
|
|
83
85
|
isEthAddress,
|
|
86
|
+
isEthAddressWrapper,
|
|
84
87
|
padHex
|
|
85
88
|
};
|
|
86
89
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -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
|
|
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 isEthAddressWrapper = (obj: { type: string }) => obj instanceof EthAddressWrapper\n\n/** @deprecated use isEthAddressWrapper */\nexport const isEthAddress = isEthAddressWrapper\n\nexport class EthAddressWrapper {\n private address: bigint\n\n protected 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 EthAddressWrapper(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?: EthAddressWrapper | string | null): boolean {\n if (address !== null && address !== undefined) {\n const inAddress = typeof address === 'string' ? assertEx(EthAddressWrapper.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 EthAddressWrapper.validate(this.toString())\n }\n}\n\n/** @deprecated use EthAddressWrapper */\nexport class EthAddress extends EthAddressWrapper {}\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\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,MAAM;AAC/C,SAAO,GAAG,KAAK,MAAM,KAAK;AAC5B;;;ACJA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;;;ACD3B,IAAM,SAAS,CAAC,KAAa,YAAY,MAAM;AAC7C,MAAI,SAAS;AACb,MAAI,IAAI,SAAS,MAAM,GAAG;AACxB,aAAS,IAAI,GAAG;AAAA,EAClB;AAEA,SAAO,OAAO,SAAS,IAAI,WAAW;AACpC,aAAS,KAAK,MAAM;AAAA,EACtB;AAEA,SAAO;AACT;;;ADLO,IAAM,sBAAsB,CAAC,QAA0B,eAAe;AAGtE,IAAM,eAAe;AAErB,IAAM,oBAAN,MAAM,mBAAkB;AAAA,EACrB;AAAA,EAEE,YAAY,SAAiB;AACrC,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,OAAO,WAAW,OAAgB,OAAO,IAAI;AAC3C,QAAI,UAAU,QAAW;AACvB,YAAM,KAAK,SAAS,KAAK,OAAO,MAAM,WAAW,IAAI,IAAI,QAAQ,KAAK,KAAK,EAAE,IAAI,OAAO,KAAK;AAC7F,aAAO,IAAI,mBAAkB,EAAE;AAAA,IACjC;AAAA,EACF;AAAA,EAEA,OAAO,MAAM,OAAgB,MAAe;AAC1C,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAO,KAAK,WAAW,OAAO,IAAI;AAAA,IACpC;AAAA,EACF;AAAA,EAEA,OAAO,SAAS,SAAiB;AAC/B,WAAO,sBAAsB,KAAK,OAAO;AAAA,EAC3C;AAAA,EAEA,OAAO,SAAsD;AAC3D,QAAI,YAAY,QAAQ,YAAY,QAAW;AAC7C,YAAM,YAAY,OAAO,YAAY,WAAW,SAAS,mBAAkB,WAAW,OAAO,GAAG,MAAM,aAAa,IAAI;AACvH,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,mBAAkB,SAAS,KAAK,SAAS,CAAC;AAAA,EACnD;AACF;AAGO,IAAM,aAAN,cAAyB,kBAAkB;AAAC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/eth-address",
|
|
3
|
-
"version": "4.15.
|
|
3
|
+
"version": "4.15.2",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eth",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"src"
|
|
45
45
|
],
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@xylabs/assert": "^4.15.
|
|
47
|
+
"@xylabs/assert": "^4.15.2",
|
|
48
48
|
"ethers": "^6.15.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
package/src/EthAddress.ts
CHANGED
|
@@ -4,23 +4,22 @@ import { getAddress } from 'ethers'
|
|
|
4
4
|
import { ellipsize } from './ellipsize.ts'
|
|
5
5
|
import { padHex } from './padHex.ts'
|
|
6
6
|
|
|
7
|
-
export const
|
|
7
|
+
export const isEthAddressWrapper = (obj: { type: string }) => obj instanceof EthAddressWrapper
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type = EthAddress.type
|
|
9
|
+
/** @deprecated use isEthAddressWrapper */
|
|
10
|
+
export const isEthAddress = isEthAddressWrapper
|
|
13
11
|
|
|
12
|
+
export class EthAddressWrapper {
|
|
14
13
|
private address: bigint
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
protected constructor(address: bigint) {
|
|
17
16
|
this.address = address
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
static fromString(value?: string, base = 16) {
|
|
21
20
|
if (value !== undefined) {
|
|
22
21
|
const bi = base === 16 ? BigInt(value.startsWith('0x') ? value : `0x${value}`) : BigInt(value)
|
|
23
|
-
return new
|
|
22
|
+
return new EthAddressWrapper(bi)
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
|
|
@@ -34,9 +33,9 @@ export class EthAddress {
|
|
|
34
33
|
return /^(0x)?[\da-f]{40}$/i.test(address)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
equals(address?:
|
|
36
|
+
equals(address?: EthAddressWrapper | string | null): boolean {
|
|
38
37
|
if (address !== null && address !== undefined) {
|
|
39
|
-
const inAddress = typeof address === 'string' ? assertEx(
|
|
38
|
+
const inAddress = typeof address === 'string' ? assertEx(EthAddressWrapper.fromString(address), () => 'Bad Address') : address
|
|
40
39
|
return this.address === inAddress.address
|
|
41
40
|
}
|
|
42
41
|
return false
|
|
@@ -71,6 +70,9 @@ export class EthAddress {
|
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
validate() {
|
|
74
|
-
return
|
|
73
|
+
return EthAddressWrapper.validate(this.toString())
|
|
75
74
|
}
|
|
76
75
|
}
|
|
76
|
+
|
|
77
|
+
/** @deprecated use EthAddressWrapper */
|
|
78
|
+
export class EthAddress extends EthAddressWrapper {}
|
|
@@ -2,17 +2,17 @@ import {
|
|
|
2
2
|
describe, expect, test,
|
|
3
3
|
} from 'vitest'
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { EthAddressWrapper } from '../EthAddress.ts'
|
|
6
6
|
|
|
7
7
|
describe('EthAddress', () => {
|
|
8
8
|
test('checking happy path', () => {
|
|
9
9
|
const addressString = '7284b6A4233B8B05910F2CbF7dBf6715325F6fCb'.toLowerCase()
|
|
10
|
-
const address =
|
|
10
|
+
const address = EthAddressWrapper.fromString(addressString, 16)
|
|
11
11
|
expect(address?.toString()).toBe(`0x${addressString}`)
|
|
12
12
|
expect(address?.toShortString()).toBe('0x72...cb')
|
|
13
13
|
})
|
|
14
14
|
test('checksum', () => {
|
|
15
|
-
const address =
|
|
15
|
+
const address = EthAddressWrapper.fromString('0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1')
|
|
16
16
|
expect(address?.toString(true)).toBe('0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1')
|
|
17
17
|
})
|
|
18
18
|
})
|