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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
mod convert;
mod jsonrpc;
pub mod method;

use std::fmt;
use std::str::FromStr;

use ckb_index::LiveCellInfo;
use ckb_jsonrpc_types::{BlockView, HeaderView, JsonBytes, Script, Transaction};
use ckb_types::{H160, H256};
use serde_derive::{Deserialize, Serialize};

pub use jsonrpc::{JsonrpcError, JsonrpcRequest, JsonrpcResponse, JSONRPC_VERSION};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PluginConfig {
    pub name: String,
    pub description: String,
    pub daemon: bool,
    pub roles: Vec<PluginRole>,
}

impl PluginConfig {
    pub fn validate(&self) -> Result<(), String> {
        // TODO: validate PluginConfig.name
        if self.roles.is_empty() {
            return Err(String::from("Role list can not be empty"));
        }
        for role in &self.roles {
            role.validate()?;
        }
        Ok(())
    }

    pub fn is_normal_daemon(&self) -> bool {
        if !self.daemon {
            return false;
        }
        for role in &self.roles {
            match role {
                PluginRole::KeyStore { .. } => (),
                PluginRole::Indexer => (),
                _ => {
                    return true;
                }
            }
        }
        false
    }
}

#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "snake_case")]
pub enum PluginRole {
    // The argument is for if keystore need password
    KeyStore { require_password: bool },
    Indexer,
    // The argument is for where the sub-command is injected to.
    SubCommand { name: String },
    // The argument is for the callback function name
    Callback { name: CallbackName },
}

impl PluginRole {
    pub fn validate(&self) -> Result<(), String> {
        match self {
            Self::SubCommand { .. } => {
                // TODO: check sub-command name
                Ok(())
            }
            _ => Ok(()),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PluginRequest {
    // == Send from ckb-cli to plugin
    // Tell a daemon plugin to quit
    Quit,
    GetConfig,
    // Notify all daemon plugins and indexer when rpc url changed
    RpcUrlChanged(String),
    // The plugin need to parse the rest command line arguments
    SubCommand(String),
    Callback(CallbackRequest),
    // == Send from plugin to ckb-cli
    Rpc(RpcRequest),
    ReadPassword(String),
    PrintStdout(String),
    PrintStderr(String),
    // == Can send from both direction
    KeyStore(KeyStoreRequest),
    Indexer {
        genesis_hash: H256,
        request: IndexerRequest,
    },
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", rename_all = "snake_case", content = "content")]
pub enum PluginResponse {
    Error(JsonrpcError),
    Ok,
    // For get_config request
    PluginConfig(PluginConfig),
    JsonValue(serde_json::Value),
    Boolean(bool),
    String(String),
    Integer64(u64),

    H256Opt(Option<H256>),
    H160(H160),
    H160Vec(Vec<H160>),
    HeaderView(Box<HeaderView>),
    HeaderViewOpt(Box<Option<HeaderView>>),
    BlockViewOpt(Box<Option<BlockView>>),
    Bytes(JsonBytes),
    BytesVec(Vec<JsonBytes>),

    Callback(CallbackResponse),
    MasterPrivateKey {
        privkey: JsonBytes,
        chain_code: JsonBytes,
    },
    DerivedKeySet {
        external: Vec<(String, H160)>,
        change: Vec<(String, H160)>,
    },

    LiveCells(Vec<LiveCellInfo>),
    TopN(Vec<(H256, Option<Script>, u64)>),
}

#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CallbackName {
    SendTransaction,
}
impl fmt::Display for CallbackName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let repr = match self {
            CallbackName::SendTransaction => "send_transaction",
        };
        write!(f, "{}", repr)
    }
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum CallbackRequest {
    SendTransaction {
        tx: Transaction,
        // Send in which subcommand: transfer/deposite/withdraw/prepare/tx
        sub_command: String,
    },
    // TODO: add more
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", rename_all = "snake_case", content = "content")]
pub enum CallbackResponse {
    SendTransaction {
        accepted: bool,
        error_message: String,
    },
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", rename_all = "snake_case", content = "content")]
pub enum SignTarget {
    Transaction {
        tx: Transaction,
        inputs: Vec<Transaction>,
        change_path: String,
    },
    AnyMessage(H256),
    AnyString(String),
    AnyData(JsonBytes),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum KeyStoreRequest {
    // return: PluginResponse::Bytes
    ListAccount,
    // return: PluginResponse::Boolean
    HasAccount(H160),
    // return: PluginResponse::H160
    CreateAccount(Option<String>),
    // return: PluginResponse::Ok
    UpdatePassword {
        hash160: H160,
        password: String,
        new_password: String,
    },
    // return: PluginResponse::H160
    Import {
        privkey: [u8; 32],
        chain_code: [u8; 32],
        password: Option<String>,
    },
    // return: PluginResponse::H160
    ImportAccount {
        account_id: JsonBytes,
        password: Option<String>,
    },
    // return: PluginResponse::MasterPrivateKey
    Export {
        hash160: H160,
        password: Option<String>,
    },
    // return: PluginResponse::Bytes
    Sign {
        hash160: H160,
        path: String,
        message: H256,
        target: Box<SignTarget>,
        recoverable: bool,
        password: Option<String>,
    },
    // return: PluginResponse::Bytes
    ExtendedPubkey {
        hash160: H160,
        path: String,
        password: Option<String>,
    },
    // return: PluginResponse::DerivedKeySet
    DerivedKeySet {
        hash160: H160,
        external_max_len: u32,
        change_last: H160,
        change_max_len: u32,
        password: Option<String>,
    },
    // return: PluginResponse::DerivedKeySet
    DerivedKeySetByIndex {
        hash160: H160,
        external_start: u32,
        external_length: u32,
        change_start: u32,
        change_length: u32,
        password: Option<String>,
    },
    // For plugin to use custom keystore
    // return: PluginResponse::JsonValue
    Any(serde_json::Value),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RpcRequest {
    GetBlock { hash: H256 },
    GetBlockByNumber { number: u64 },
    GetBlockHash { number: u64 },
    // TODO: add more
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum LiveCellIndexType {
    LockHash,
    TypeHash,
    // Code hash of type script
    CodeHash,
}
impl fmt::Display for LiveCellIndexType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let repr = match self {
            LiveCellIndexType::LockHash => "lock_hash",
            LiveCellIndexType::TypeHash => "type_hash",
            LiveCellIndexType::CodeHash => "code_hash",
        };
        write!(f, "{}", repr)
    }
}
impl FromStr for LiveCellIndexType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "lock_hash" => Ok(LiveCellIndexType::LockHash),
            "type_hash" => Ok(LiveCellIndexType::TypeHash),
            "code_hash" => Ok(LiveCellIndexType::CodeHash),
            _ => Err(format!("Invalid index type: {}", s)),
        }
    }
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum IndexerRequest {
    TipHeader,
    LastHeader,
    // Get total capacity by lock hash
    GetCapacity(H256),
    LiveCells {
        index: LiveCellIndexType,
        hash: H256,
        from_number: Option<u64>,
        to_number: Option<u64>,
        limit: u64,
    },
    TopN(u64),
    IndexerInfo,
    // For plugin to use custom indexer
    Any(serde_json::Value),
}