How to change the controller of a canister?

I have a custom canister A, with a function createSubCanister, invoke this function will create a canister B, I assume the controller of canister B is canister A in such scenario, right? Is there a way to set the controller of canister B to another principal? Can I write a setController function inside canister A to do this? If yes, how?

1 Like

createSubCanister should invoke the management canister’s create_canister method to create B. create_canister has this interface definition:

create_canister : (record {
    settings : opt canister_settings

where canister_settings is similarly defined as:

type canister_settings = record {
  controllers : opt vec principal;
  compute_allocation : opt nat;
  memory_allocation : opt nat;
  freezing_threshold : opt nat;

All you need to do is provide a valid Principal(s) for canister_settings’ controllers field and provide that settings to the create_canister call

You can find more info about create_canister here: The Internet Computer Interface Specification :: Internet Computer


Thanks, this solves my problem.

Another way:

dfx canister --network ic --no-wallet call aaaaa-aa update_settings '(record{canister_id = principal "<CANISTER ID>";settings = record {controller = opt principal "<CONTROLLER>"; null; null; null;};})'

Call this from the current controller of a canister.

1 Like