-
Notifications
You must be signed in to change notification settings - Fork 2
/
address.ts
67 lines (53 loc) · 1.42 KB
/
address.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: Apache-2.0
"use strict";
import { utils } from "ethers";
import { jsonObject } from "#erdstall/export/typedjson";
import { equalArray } from "#erdstall/utils/arrays";
import { ABIValue, customJSON } from "#erdstall/api/util";
/**
* This class implements an address representation and is used within the SDK
* wherever an address is required.
*/
@jsonObject
export class Address implements ABIValue {
private value: Uint8Array;
constructor(value: Uint8Array) {
this.value = value;
}
static fromJSON(val: any): Address {
return new Address(utils.arrayify(val));
}
static toJSON(me: Address) {
return utils.hexlify(me.value);
}
static fromString(addr: string): Address {
return Address.fromJSON(addr);
}
static ensure(addr: string | Address): Address {
if (addr === undefined) return addr;
if (addr instanceof Address) return addr;
return Address.fromString(addr);
}
toString(): string {
return utils.getAddress(utils.hexlify(this.value));
}
get key(): string {
return Address.toJSON(this);
}
asABI(): any {
return this.toString();
}
ABIType(): string {
return "address";
}
isZero(): boolean {
return this.value.every((x) => x === 0);
}
equals(other: Address): boolean {
return equalArray(this.value, other.value);
}
}
customJSON(Address);
export function addressKey(addr: Address | string): string {
return addr instanceof Address ? addr.key : addr.toLowerCase();
}