Programmatically add canister controller from within canister

Is there any way to programmatically set the canister controller or custodian from within canister itself? I’d like to write a function that transfers ownership of the canister to a principal that’s passed in as an argument.

update_settings maybe?

When I create canisters it’s the one I use to define the controllers (source).

public func updateSettings(canisterId: Principal, manager: Principal): async () {
            let controllers: ?[Principal] = ?[canisterId, manager];

            await ic.update_settings(({canister_id = canisterId; settings = {
                controllers = controllers;
                freezing_threshold = null;
                memory_allocation = null;
                compute_allocation = null;
            }}));
        };
2 Likes

This is exactly what i was needing. Thank you!

1 Like

Cool, my pleasure! Let me know if it works out

Is it working now? I had problems, but once they did not arise.
I can still add another controller via the command:
dfx canister update-settings authorization-rbac --add-controller
But an error appears from the code in motoko
The Replica returned an error: code 4, message: "Only the controllers of the canister wckdt-raaaa-aaaaa-aaatq-cai can control it.