How to fetch canister metadata on-chain?

I want to get canister’s metadata sections such as candid:service and motoko:compiler.
Can I access them on-chain from a Motoko canister? If not, is it possible via http outcalls to some endpoint?

1 Like

The metadata is a component of the system state tree.

Getting such information from IC normally involves read_state, certification verification, parsing the state tree, looking for a path on the tree.

It is agents’ job to encapsulate such functionality with convenient API, e.g. agent-rs, agent-js. And agents are normally used outside canister code.

For Rust canisters, you may be able to use agent-rs to fetch canister metadata within a canister. I haven’t tried it before and can’t guarantee it will work.

I don’t know if it’s doable in Motoko.

Theoretically, it’s possible make an HTTP outcall using the IC HTTP interface: The Internet Computer Interface Specification | Internet Computer

But I don’t know if we have libraries to parse and authenticate the state tree in Motoko. It’s best to refactor the code, so that we can fetch the metadata on the client side, rather than on the server side.

It would be convenient if canister metadata was made available by extending the management canister API


Also would be nice to have on-chain access to canister’s wasm hash, controllers…

And list of all canisters)