Hey devs. I’m trying to get my Motoko canister to dynamically create a canister and then install wasm code in the same function to the newly generated canister (via the IC management canister). But when I call the methods install_code and uninstall_code in particular, I always get a {"err":{"Unauthorized":null}} response.
I have made sure that when creating the canister, both the parent canister and the caller are set as controllers, but it doesn’t seem to matter:
I’ve tested both on my local network and on the IC with the same error. I can’t find the public source code for the management canister to troubleshoot what logic might be throwing the error. What am I doing wrong?
What is the description of your type Management @Severin ?
In my interface description, create_canister takes in 4 parameters in the settings object and the install_code.arg parameter is type [Nat8] not Blob. Even then, I don’t see what the error code “Unauthorized” has to do with any of this
The documentation is very terse at the moment, with no separate tutorial style page yet:
Our tests, which could form the basis of worked examples, are here:
Simple uses:
More involved: a basic distributed map, upgrading its bucket actors using a protocol after upgrade.
Note these are not polished, but maybe you’ll still find them useful.
Part of the reason this is not yet described in an accessible tutorial is that it is still unsafe -
I had plans to add some more safety checks, trapping an upgrade if the upgrade is incompatible by doing a dynamic check.
Stupid question, but are the calls that are failing actually Motoko calls, or dfx calls? I don’t think you can call the management canister from dfx directly.
Can you try create_canister with a null setting, e.g. await managementCanister.create_canister({ settings = null });. This ensures the parent canister to be the controller of the child canister.
Regarding the type of create_canister, [Nat8] and Blob has the same Candid type, but Blob is more efficient and recommended.
Thank you for the troubleshooting everyone. I figured out the error was completely something I overlooked in my code. I was in fact calling my method from an unauthorized canister/principal which after the fact sounds really stupid to have to admit on the forum.
But I will now try to look into motoko sugar syntax pointed out by @skilesare to see if it is a possible alternative to calling the management canister: