/
Address.ts
190 lines (175 loc) · 6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2018 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AddressDto } from 'catbuffer-typescript';
import { Convert, RawAddress } from '../../core/format';
import { NetworkType } from '../network/NetworkType';
/**
* The address structure describes an address with its network
*/
export class Address {
/**
* Create from private key
* @param publicKey - The account public key.
* @param networkType - The NEM network type.
* @returns {Address}
*/
public static createFromPublicKey(publicKey: string, networkType: NetworkType): Address {
const publicKeyUint8 = Convert.hexToUint8(publicKey);
const address = RawAddress.addressToString(RawAddress.publicKeyToAddress(publicKeyUint8, networkType));
return new Address(address, networkType);
}
/**
* Create an Address from a given raw address.
* @param rawAddress - Address in string format.
* ex: SB3KUBHATFCPV7UZQLWAQ2EUR6SIHBSBEOEDDDF3 or SB3KUB-HATFCP-V7UZQL-WAQ2EU-R6SIHB-SBEOED-DDF3
* @returns {Address}
*/
public static createFromRawAddress(rawAddress: string): Address {
let networkType: NetworkType;
const addressTrimAndUpperCase: string = rawAddress.trim().toUpperCase().replace(/-/g, '');
if (addressTrimAndUpperCase.length !== 39) {
throw new Error('Address ' + addressTrimAndUpperCase + ' has to be 39 characters long');
}
if (addressTrimAndUpperCase.charAt(0) === 'T') {
networkType = NetworkType.TEST_NET;
} else if (addressTrimAndUpperCase.charAt(0) === 'N') {
networkType = NetworkType.MAIN_NET;
} else {
throw new Error('Address Network unsupported');
}
return new Address(addressTrimAndUpperCase, networkType);
}
/**
* Create an Address from a given encoded address.
* @param {string} encoded address. Expected format: 9085215E4620D383C2DF70235B9EF7607F6A28EF6D16FD7B9C.
* @return {Address}
*/
public static createFromEncoded(encoded: string): Address {
return Address.createFromRawAddress(RawAddress.addressToString(Convert.hexToUint8(encoded)));
}
/**
* Determines the validity of an raw address string.
* @param {string} rawAddress The raw address string. Expected format TATNE7Q5BITMUTRRN6IB4I7FLSDRDWZA37JGO5Q
* @returns {boolean} true if the raw address string is valid, false otherwise.
*/
public static isValidRawAddress = (rawAddress: string): boolean => {
if (!['A', 'I', 'Q', 'Y'].includes(rawAddress.slice(-1).toUpperCase())) {
return false;
}
try {
return RawAddress.isValidAddress(RawAddress.stringToAddress(rawAddress));
} catch (err) {
return false;
}
};
/**
* Determines the validity of an encoded address string.
* @param {string} encoded The encoded address string. Expected format: 6823BB7C3C089D996585466380EDBDC19D4959184893E38C
* @returns {boolean} true if the encoded address string is valid, false otherwise.
*/
public static isValidEncodedAddress = (encoded: string): boolean => {
try {
return RawAddress.isValidAddress(Convert.hexToUint8(encoded));
} catch (err) {
return false;
}
};
/**
* @internal
* @param address
* @param networkType
*/
private constructor(
/**
* The address value.
*/
private readonly address: string,
/**
* The NEM network type.
*/
public readonly networkType: NetworkType,
) {}
/**
* Get address in plain format ex: SB3KUBHATFCPV7UZQLWAQ2EUR6SIHBSBEOEDDDF3.
* @returns {string}
*/
public plain(): string {
return this.address;
}
/**
* Get address in the encoded format ex: NAR3W7B4BCOZSZMFIZRYB3N5YGOUSWIYJCJ6HDFH.
* @returns {string}
*/
public encoded(): string {
return Convert.uint8ToHex(RawAddress.stringToAddress(this.address));
}
/**
* Get address in pretty format ex: SB3KUB-HATFCP-V7UZQL-WAQ2EU-R6SIHB-SBEOED-DDF3.
* @returns {string}
*/
public pretty(): string {
return this.address.match(/.{1,6}/g)!.join('-');
}
/**
* Compares addresses for equality
* @param address - Address to compare
* @returns {boolean}
*/
public equals(address: any): boolean {
return address && this.address == address.address && this.networkType === address.networkType;
}
/**
* Create DTO object
*/
public toDTO(): any {
return {
address: this.address,
networkType: this.networkType,
};
}
/**
* Create Builder object
*/
public toBuilder(): AddressDto {
return new AddressDto(Convert.hexToUint8(this.encoded()));
}
/**
* Encoded address or namespace id. Note that namespace id get the hex reversed and
* zero padded.
* @returns {Uint8Array}
*/
public encodeUnresolvedAddress(): Uint8Array {
return Convert.hexToUint8(this.encoded());
}
/**
* returns that this instance is a resolved, not an alias, address.
*/
public isNamespaceId(): boolean {
return false;
}
/**
* returns that the instance is an address
*/
public isAddress(): boolean {
return true;
}
/**
* returns that the instance is not a mosaic id
*/
public isMosaicId(): boolean {
return false;
}
}