@@ -17,13 +17,11 @@ const _typeOpaque = 1;
17
17
final _emptySubAccount = Uint8List (32 );
18
18
19
19
class Principal {
20
- Principal (
21
- this .principal , {
20
+ const Principal (
21
+ this ._principal , {
22
22
Uint8List ? subAccount,
23
23
}) : assert (subAccount == null || subAccount.length == 32 ),
24
- subAccount = subAccount != null && subAccount.eq (_emptySubAccount)
25
- ? null
26
- : subAccount;
24
+ _subAccount = subAccount;
27
25
28
26
factory Principal .selfAuthenticating (Uint8List publicKey) {
29
27
final sha = sha224Hash (publicKey.buffer);
@@ -41,7 +39,7 @@ class Principal {
41
39
} else if (other is Map <String , dynamic > && other['_isPrincipal' ] == true ) {
42
40
return Principal (other['_arr' ], subAccount: other['_subAccount' ]);
43
41
} else if (other is Principal ) {
44
- return Principal (other.principal , subAccount: other.subAccount);
42
+ return Principal (other._principal , subAccount: other.subAccount);
45
43
}
46
44
throw UnreachableError ();
47
45
}
@@ -131,30 +129,37 @@ class Principal {
131
129
return principal;
132
130
}
133
131
134
- final Uint8List principal;
135
- final Uint8List ? subAccount;
132
+ final Uint8List _principal;
133
+ final Uint8List ? _subAccount;
134
+
135
+ Uint8List ? get subAccount {
136
+ if (_subAccount case final v when v == null || v.eq (_emptySubAccount)) {
137
+ return null ;
138
+ }
139
+ return _subAccount;
140
+ }
136
141
137
142
Principal newSubAccount (Uint8List ? subAccount) {
138
143
if (subAccount == null || subAccount.eq (_emptySubAccount)) {
139
144
return this ;
140
145
}
141
146
if (this .subAccount == null || ! this .subAccount! .eq (subAccount)) {
142
- return Principal (principal , subAccount: subAccount);
147
+ return Principal (_principal , subAccount: subAccount);
143
148
}
144
149
return this ;
145
150
}
146
151
147
152
bool isAnonymous () {
148
- return principal .lengthInBytes == 1 && principal [0 ] == _suffixAnonymous;
153
+ return _principal .lengthInBytes == 1 && _principal [0 ] == _suffixAnonymous;
149
154
}
150
155
151
- Uint8List toUint8List () => principal ;
156
+ Uint8List toUint8List () => _principal ;
152
157
153
- String toHex () => _toHexString (principal ).toUpperCase ();
158
+ String toHex () => _toHexString (_principal ).toUpperCase ();
154
159
155
160
String toText () {
156
- final checksum = _getChecksum (principal .buffer);
157
- final bytes = Uint8List .fromList (principal );
161
+ final checksum = _getChecksum (_principal .buffer);
162
+ final bytes = Uint8List .fromList (_principal );
158
163
final array = Uint8List .fromList ([...checksum, ...bytes]);
159
164
final result = base32Encode (array);
160
165
final reg = RegExp (r'.{1,5}' );
@@ -164,8 +169,9 @@ class Principal {
164
169
throw StateError ('No characters found.' );
165
170
}
166
171
final buffer = StringBuffer (matches.map ((e) => e.group (0 )).join ('-' ));
167
- if (subAccount != null ) {
168
- final subAccountHex = subAccount! .toHex ();
172
+ if (_subAccount case final subAccount?
173
+ when ! subAccount.eq (_emptySubAccount)) {
174
+ final subAccountHex = subAccount.toHex ();
169
175
int nonZeroStart = 0 ;
170
176
while (nonZeroStart < subAccountHex.length) {
171
177
if (subAccountHex[nonZeroStart] != '0' ) {
@@ -175,7 +181,7 @@ class Principal {
175
181
}
176
182
if (nonZeroStart != subAccountHex.length) {
177
183
final checksum = base32Encode (
178
- _getChecksum (Uint8List .fromList (principal + subAccount! ).buffer),
184
+ _getChecksum (Uint8List .fromList (_principal + subAccount).buffer),
179
185
);
180
186
buffer.write ('-$checksum ' );
181
187
buffer.write ('.' );
@@ -189,7 +195,7 @@ class Principal {
189
195
final hash = SHA224 ();
190
196
hash.update ('\x 0Aaccount-id' .plainToU8a ());
191
197
hash.update (toUint8List ());
192
- hash.update (subAccount ?? Uint8List ( 32 ) );
198
+ hash.update (subAccount ?? _emptySubAccount );
193
199
final data = hash.digest ();
194
200
final view = ByteData (4 );
195
201
view.setUint32 (0 , getCrc32 (data.buffer));
@@ -207,12 +213,12 @@ class Principal {
207
213
bool operator == (Object other) =>
208
214
identical (this , other) ||
209
215
other is Principal &&
210
- principal .eq (other.principal ) &&
211
- (subAccount ? .eq (other.subAccount ?? Uint8List ( 0 ) ) ??
212
- subAccount == null && other.subAccount == null );
216
+ _principal .eq (other._principal ) &&
217
+ (_subAccount ? .eq (other._subAccount ?? _emptySubAccount ) ??
218
+ _subAccount == null && other._subAccount == null );
213
219
214
220
@override
215
- int get hashCode => Object .hash (principal , subAccount);
221
+ int get hashCode => Object .hash (_principal , subAccount);
216
222
}
217
223
218
224
class CanisterId extends Principal {
0 commit comments