Selector

A Selector is a 32-bit unsigned integer (u32) that identifies an underlying asset of an object. It is calculated from the function signatures of asset functions within a kind contract.

u32::MIN (0) and u32::MAX (2^32 - 1) are not considered valid selectors.

Calculation

The calculation process follows the same method used in Ethereum:

#![allow(unused)]
fn main() {
sel = bytes4(keccak256(sig))
}

In this context, the function signatures (sig) are determined differently: since asset functions do not have arguments, the signature is simply the function name without parentheses.

For example, given the following kind contract:

@kind
class Hat {
    // ...

    meta(): Json {
        // ...
    }

    picture(): Image {
        // ...
    }
}

The selectors are calculated as follows:

#![allow(unused)]
fn main() {
sel_meta    = bytes4(keccak256("meta"))    = 0x0144b03a
sel_picture = bytes4(keccak256("picture")) = 0xeb0568a6
}