Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ckb-jsonrpc-types #92

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dyn-clone = "1.0"
ckb-types = "0.111.0"
ckb-dao-utils = "0.111.0"
ckb-traits = "0.111.0"
ckb-jsonrpc-types = "0.111.0"
ckb-hash = "0.111.0"
ckb-resource = "0.111.0"
ckb-crypto = { version = "=0.111.0", features = ["secp"] }
Expand All @@ -41,6 +40,8 @@ bitflags = "1.3.2"
sha3 = "0.10.1"
enum-repr-derive = "0.2.0"

ckb-jsonrpc-types = { package = "ckb-sdk-jsonrpc-types", path = "jsonrpc-types", version = "= 3.0.0" }

# for feature test
rand = { version = "0.7.3", optional = true }
ckb-mock-tx-types = "0.111.0"
Expand All @@ -61,3 +62,9 @@ clap = { version = "4.1.8", features = ["derive"] }
httpmock = "0.6"
async-global-executor = "2.3.1"
hex = "0.4"
ckb-crate-jsonrpc-types = { package = "ckb-jsonrpc-types", version = "0.111.0" }

[workspace]
members = [
"jsonrpc-types"
]
20 changes: 20 additions & 0 deletions jsonrpc-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "ckb-sdk-jsonrpc-types"
version = "3.0.0"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2021"
license = "MIT"
description = "CKB common types for JSON serialization."
homepage = "https://github.com/nervosnetwork/ckb-sdk-rust"
repository = "https://github.com/nervosnetwork/ckb-sdk-rust"

[dependencies]
ckb-types = "0.111.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
faster-hex = "0.6"

[dev-dependencies]
proptest = "1.0"
regex = "1.1"
lazy_static = "1.3"
136 changes: 136 additions & 0 deletions jsonrpc-types/src/alert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::{bytes::JsonBytes, Timestamp, Uint32};
use ckb_types::{packed, prelude::*};
use serde::{Deserialize, Serialize};

/// The alert identifier that is used to filter duplicated alerts.
///
/// This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](type.Uint32.html#examples).
pub type AlertId = Uint32;
/// Alerts are sorted by priority. Greater integers mean higher priorities.
///
/// This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](type.Uint32.html#examples).
pub type AlertPriority = Uint32;

/// An alert is a message about critical problems to be broadcast to all nodes via the p2p network.
///
/// ## Examples
///
/// An example in JSON
///
/// ```
/// # serde_json::from_str::<ckb_sdk_jsonrpc_types::Alert>(r#"
/// {
/// "id": "0x1",
/// "cancel": "0x0",
/// "min_version": "0.1.0",
/// "max_version": "1.0.0",
/// "priority": "0x1",
/// "message": "An example alert message!",
/// "notice_until": "0x24bcca57c00",
/// "signatures": [
/// "0xbd07059aa9a3d057da294c2c4d96fa1e67eeb089837c87b523f124239e18e9fc7d11bb95b720478f7f937d073517d0e4eb9a91d12da5c88a05f750362f4c214dd0",
/// "0x0242ef40bb64fe3189284de91f981b17f4d740c5e24a3fc9b70059db6aa1d198a2e76da4f84ab37549880d116860976e0cf81cd039563c452412076ebffa2e4453"
/// ]
/// }
/// # "#).unwrap();
/// ```
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct Alert {
/// The identifier of the alert. Clients use id to filter duplicated alerts.
pub id: AlertId,
/// Cancel a previous sent alert.
pub cancel: AlertId,
/// Optionally set the minimal version of the target clients.
///
/// See [Semantic Version](https://semver.org/) about how to specify a version.
pub min_version: Option<String>,
/// Optionally set the maximal version of the target clients.
///
/// See [Semantic Version](https://semver.org/) about how to specify a version.
pub max_version: Option<String>,
/// Alerts are sorted by priority, highest first.
pub priority: AlertPriority,
/// The alert is expired after this timestamp.
pub notice_until: Timestamp,
/// Alert message.
pub message: String,
/// The list of required signatures.
pub signatures: Vec<JsonBytes>,
}

/// An alert sent by RPC `send_alert`.
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct AlertMessage {
/// The unique alert ID.
pub id: AlertId,
/// Alerts are sorted by priority, highest first.
pub priority: AlertPriority,
/// The alert is expired after this timestamp.
pub notice_until: Timestamp,
/// Alert message.
pub message: String,
}

impl From<Alert> for packed::Alert {
fn from(json: Alert) -> Self {
let Alert {
id,
cancel,
min_version,
max_version,
priority,
notice_until,
message,
signatures,
} = json;
let raw = packed::RawAlert::new_builder()
.id(id.pack())
.cancel(cancel.pack())
.min_version(min_version.pack())
.max_version(max_version.pack())
.priority(priority.pack())
.notice_until(notice_until.pack())
.message(message.pack())
.build();
packed::Alert::new_builder()
.raw(raw)
.signatures(signatures.into_iter().map(Into::into).pack())
.build()
}
}

impl From<packed::Alert> for Alert {
fn from(input: packed::Alert) -> Self {
let raw = input.raw();
Alert {
id: raw.id().unpack(),
cancel: raw.cancel().unpack(),
min_version: raw
.as_reader()
.min_version()
.to_opt()
.map(|b| unsafe { b.as_utf8_unchecked() }.to_owned()),
max_version: raw
.as_reader()
.max_version()
.to_opt()
.map(|b| unsafe { b.as_utf8_unchecked() }.to_owned()),
priority: raw.priority().unpack(),
notice_until: raw.notice_until().unpack(),
message: unsafe { raw.as_reader().message().as_utf8_unchecked() }.to_owned(),
signatures: input.signatures().into_iter().map(Into::into).collect(),
}
}
}

impl From<packed::Alert> for AlertMessage {
fn from(input: packed::Alert) -> Self {
let raw = input.raw();
AlertMessage {
id: raw.id().unpack(),
priority: raw.priority().unpack(),
notice_until: raw.notice_until().unpack(),
message: unsafe { raw.as_reader().message().as_utf8_unchecked() }.to_owned(),
}
}
}
Loading
Loading