-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccount.mo
58 lines (49 loc) · 1.91 KB
/
Account.mo
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
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Char "mo:base/Char";
import Debug "mo:base/Debug";
import Int "mo:base/Int";
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Nat32 "mo:base/Nat32";
import Nat64 "mo:base/Nat64";
import Option "mo:base/Option";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Text "mo:base/Text";
import Time "mo:base/Time";
import ArrayModule "mo:array/Array";
import Itertools "mo:itertools/Iter";
import AccountTools "mo:account";
import MigrationTypes "/migrations/types";
module {
type Iter<A> = Iter.Iter<A>;
/// Checks if a subaccount is valid
public func validate_subaccount(subaccount : ?MigrationTypes.Current.Subaccount) : Bool {
switch (subaccount) {
case (?bytes) {
bytes.size() == 32;
};
case (_) true;
};
};
/// Checks if an account is valid
public func validate(account : MigrationTypes.Current.Account) : Bool {
let is_anonymous = Principal.isAnonymous(account.owner);
let invalid_size = Principal.toBlob(account.owner).size() > 29;
if (is_anonymous or invalid_size) {
false;
} else {
validate_subaccount(account.subaccount);
};
};
/// Implementation of ICRC1's Textual representation of accounts [Encoding Standard](https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-1#encoding)
public func encodeAccount(account : MigrationTypes.Current.Account) : Text {
AccountTools.toText(account)
};
/// Implementation of ICRC1's Textual representation of accounts [Decoding Standard](https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-1#decoding)
public func decodeAccount(encoded : Text) : Result.Result<MigrationTypes.Current.Account, AccountTools.ParseError> {
AccountTools.fromText(encoded);
};
};