motoko-base
motoko-base copied to clipboard
Function to check if a string is a valid principal id
Do we have a function exposed to easily check if a string is a valid principal id in Motoko? We can always try creating an actor instance and it will trap if the id is invalid, but is this internal check available in Motoko so we can do so without trapping?
No such check is available at the moment. If the string was passed in via a frontend, it may anyways make sense to do all validation an parsing there, and use the pricipal
type in your service API (e.g. in the Candid interface). Of course, then you need to do the parsing in the frontend code… :-)
Maybe Principal implement
public func fromText(t : Text) : ?Principal
is a good choice.
Developers can judge whether the text is valid based on the return value.
I have try it like this:
try {
let pid = Principal.fromText("test");
throw (Prim.error("Error"));
Prim.debugPrint "unreachable";
}
catch e { Prim.debugPrint "2"};
return true;
};
When the code runs to Principal.fromText it is interrupted.
try can not catch Principal.fromText("test")'s error.
I have implemented it here
Maybe it can meet your needs
Do we have a function exposed to easily check if a string is a valid principal id in Motoko? We can always try creating an actor instance and it will trap if the id is invalid, but is this internal check available in Motoko so we can do so without trapping?
@chenyan-dfinity Hi Yan, could you take a look at this PR? Can it be merged?
What's the use case for string to principal conversion? From the backend perspective, principal is an opaque object. The frontend should just pass in the principal instead of a string.
Dfinity has two different addresses, AccountId (Principal + Subaccount) and Principal. DFT transfer support AccountId and Principal. We hope that when developers use this method, they don’t need to pay attention to whether the address is AccountId or Principal, making it easier for developers to use.In this usage scenario, there is the possibility of passing the Principal in the invalid format, but Motoko Principal.fromText(" invalid principal format") will let the canister raise trap, developer cannot continue to process the program logic when the format is invalid
Oh, it linked automatically. The use-case came up again with coincidentally enough fungible tokens--as the ICRC1 standard uses a canonical text representation that can be just a principal (or more) but will trap if the developer doesn't check the text is acceptable to be parsed as Principal before converting from text.
It specifically came up as an issue for me in creating unit tests; and it is very conceivable front-end UI would accept the string as is, and as the ICRC1 text representation was specifically designed to be easy to use (iirc), this could often be problematic without an "official" method of validation (particularly for developers and/or users new to the IC interested in ICRC1 and its possibilities).
I'm just going to bump this up....We have a use case where principals are defined in config files and thus they end up getting passed into to contracts. We really don't want the conversion to fail inelegantly. If this code works, can it make it's way into base?
https://github.com/Deland-Labs/fungible-token-standard/blob/06ad30ea8a8c27b6eaada9c214f39e8f50eb8d4b/canisters/dft_motoko/utils/PrincipalExt.mo#L17