1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::{multihash::MultihashDigest, Cid, Code::Identity, RAW};

pub const SYSTEM_ACTOR_CODE_ID_NAME: &str = "fil/6/system";
pub const INIT_ACTOR_CODE_ID_NAME: &str = "fil/6/init";
pub const CRON_ACTOR_CODE_ID_NAME: &str = "fil/6/cron";
pub const ACCOUNT_ACTOR_CODE_ID_NAME: &str = "fil/6/account";
pub const POWER_ACTOR_CODE_ID_NAME: &str = "fil/6/storagepower";
pub const MINER_ACTOR_CODE_ID_NAME: &str = "fil/6/storageminer";
pub const MARKET_ACTOR_CODE_ID_NAME: &str = "fil/6/storagemarket";
pub const PAYCH_ACTOR_CODE_ID_NAME: &str = "fil/6/paymentchannel";
pub const MULTISIG_ACTOR_CODE_ID_NAME: &str = "fil/6/multisig";
pub const REWARD_ACTOR_CODE_ID_NAME: &str = "fil/6/reward";
pub const VERIFREG_ACTOR_CODE_ID_NAME: &str = "fil/6/verifiedregistry";
pub const CHAOS_ACTOR_CODE_ID_NAME: &str = "fil/6/chaos";

lazy_static! {
    pub static ref SYSTEM_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/system");
    pub static ref INIT_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/init");
    pub static ref CRON_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/cron");
    pub static ref ACCOUNT_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/account");
    pub static ref POWER_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/storagepower");
    pub static ref MINER_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/storageminer");
    pub static ref MARKET_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/storagemarket");
    pub static ref PAYCH_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/paymentchannel");
    pub static ref MULTISIG_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/multisig");
    pub static ref REWARD_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/reward");
    pub static ref VERIFREG_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/verifiedregistry");
    pub static ref CHAOS_ACTOR_CODE_ID: Cid = make_builtin(b"fil/6/chaos");

    /// Set of actor code types that can represent external signing parties.
    pub static ref CALLER_TYPES_SIGNABLE: [Cid; 2] =
        [*ACCOUNT_ACTOR_CODE_ID, *MULTISIG_ACTOR_CODE_ID];
}

fn make_builtin(bz: &[u8]) -> Cid {
    Cid::new_v1(RAW, Identity.digest(bz))
}

/// Returns true if the code `Cid` belongs to a builtin actor.
pub fn is_builtin_actor(code: &Cid) -> bool {
    code == &*SYSTEM_ACTOR_CODE_ID
        || code == &*INIT_ACTOR_CODE_ID
        || code == &*CRON_ACTOR_CODE_ID
        || code == &*ACCOUNT_ACTOR_CODE_ID
        || code == &*POWER_ACTOR_CODE_ID
        || code == &*MINER_ACTOR_CODE_ID
        || code == &*MARKET_ACTOR_CODE_ID
        || code == &*PAYCH_ACTOR_CODE_ID
        || code == &*MULTISIG_ACTOR_CODE_ID
        || code == &*REWARD_ACTOR_CODE_ID
        || code == &*VERIFREG_ACTOR_CODE_ID
}

/// Returns true if the code belongs to a singleton actor.
pub fn is_singleton_actor(code: &Cid) -> bool {
    code == &*SYSTEM_ACTOR_CODE_ID
        || code == &*INIT_ACTOR_CODE_ID
        || code == &*REWARD_ACTOR_CODE_ID
        || code == &*CRON_ACTOR_CODE_ID
        || code == &*POWER_ACTOR_CODE_ID
        || code == &*MARKET_ACTOR_CODE_ID
        || code == &*VERIFREG_ACTOR_CODE_ID
}

/// Returns true if the code belongs to an account actor.
pub fn is_account_actor(code: &Cid) -> bool {
    code == &*ACCOUNT_ACTOR_CODE_ID
}

/// Tests whether a code CID represents an actor that can be an external principal: i.e. an account or multisig.
pub fn is_principal(code: &Cid) -> bool {
    CALLER_TYPES_SIGNABLE.iter().any(|c| c == code)
}

/// Given an actor code Cid, returns the name of the actor.
pub fn actor_name_by_code(code: &Cid) -> Result<&str, String> {
    match code {
        x if x == &*SYSTEM_ACTOR_CODE_ID => Ok(SYSTEM_ACTOR_CODE_ID_NAME),
        x if x == &*INIT_ACTOR_CODE_ID => Ok(INIT_ACTOR_CODE_ID_NAME),
        x if x == &*CRON_ACTOR_CODE_ID => Ok(CRON_ACTOR_CODE_ID_NAME),
        x if x == &*ACCOUNT_ACTOR_CODE_ID => Ok(ACCOUNT_ACTOR_CODE_ID_NAME),
        x if x == &*POWER_ACTOR_CODE_ID => Ok(POWER_ACTOR_CODE_ID_NAME),
        x if x == &*MINER_ACTOR_CODE_ID => Ok(MINER_ACTOR_CODE_ID_NAME),
        x if x == &*MARKET_ACTOR_CODE_ID => Ok(MARKET_ACTOR_CODE_ID_NAME),
        x if x == &*PAYCH_ACTOR_CODE_ID => Ok(PAYCH_ACTOR_CODE_ID_NAME),
        x if x == &*MULTISIG_ACTOR_CODE_ID => Ok(MULTISIG_ACTOR_CODE_ID_NAME),
        x if x == &*REWARD_ACTOR_CODE_ID => Ok(REWARD_ACTOR_CODE_ID_NAME),
        x if x == &*VERIFREG_ACTOR_CODE_ID => Ok(VERIFREG_ACTOR_CODE_ID_NAME),
        x if x == &*CHAOS_ACTOR_CODE_ID => Ok(CHAOS_ACTOR_CODE_ID_NAME),
        _ => Err(format!("{} is not a valid code", code)),
    }
}