-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ic-certification): add ic-certification crate and move HashTree …
…and Certificate types (#393) * feat(ic-types): add ic-types crate and move HashTree and Certificate types * Improve compatibility * Rename to ic-certification Co-authored-by: Linwei Shang <[email protected]>
- Loading branch information
1 parent
4968280
commit c9f9497
Showing
19 changed files
with
194 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.DS_Store | ||
|
||
/.vscode/ | ||
/.idea/ | ||
.idea/ | ||
|
||
# will have compiled files and executables | ||
/target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ members = [ | |
"icx-cert", | ||
"ic-identity-hsm", | ||
"ic-utils", | ||
"ic-certification", | ||
"icx", | ||
"ref-tests" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "ic-certification" | ||
version = "0.22.0" | ||
description = "Types related to the Internet Computer Public Specification." | ||
homepage = "https://docs.rs/ic-certification" | ||
documentation = "https://docs.rs/ic-certification" | ||
repository = "https://github.com/dfinity/agent-rs" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
readme = "README.md" | ||
categories = ["api-bindings", "data-structures", "no-std"] | ||
keywords = ["internet-computer", "agent", "utility", "icp", "dfinity"] | ||
include = ["src", "Cargo.toml", "../LICENSE", "README.md"] | ||
rust-version = "1.60.0" | ||
|
||
[dependencies] | ||
hex = "0.4.3" | ||
sha2 = "0.10.1" | ||
|
||
[dev-dependencies] | ||
serde = { version = "1.0.133", features = ["derive"] } | ||
serde_cbor = "0.11.2" | ||
|
||
[dependencies.serde] | ||
version = "1.0.115" | ||
features = ["derive"] | ||
optional = true | ||
|
||
[dependencies.serde_bytes] | ||
version = "0.11.5" | ||
optional = true | ||
|
||
[features] | ||
# Default features include serde support. | ||
default = ['serde', 'serde_bytes'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# IC Certification | ||
|
||
## Goal | ||
This library contains typings and utility functions dealing with Certification on the Internet Computer. | ||
|
||
## References | ||
See https://internetcomputer.org/docs/current/references/ic-interface-spec/#certification |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::HashTree; | ||
|
||
/// A `Certificate` as defined in <https://internetcomputer.org/docs/current/references/ic-interface-spec/#certificate> | ||
#[derive(Debug, PartialEq, Eq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct Certificate<'a> { | ||
/// The hash tree. | ||
pub tree: HashTree<'a>, | ||
|
||
/// The signature of the root hash in `tree`. | ||
#[cfg_attr(feature = "serde", serde(with = "serde_bytes"))] | ||
pub signature: Vec<u8>, | ||
|
||
/// A delegation from the root key to the key used to sign `signature`, if one exists. | ||
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] | ||
pub delegation: Option<Delegation>, | ||
} | ||
|
||
/// A `Delegation` as defined in <https://internetcomputer.org/docs/current/references/ic-interface-spec/#certification-delegation> | ||
#[derive(Debug, PartialEq, Eq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct Delegation { | ||
#[cfg_attr(feature = "serde", serde(with = "serde_bytes"))] | ||
pub subnet_id: Vec<u8>, | ||
|
||
#[cfg_attr(feature = "serde", serde(with = "serde_bytes"))] | ||
pub certificate: Vec<u8>, | ||
} | ||
|
||
#[cfg(test)] | ||
#[cfg(feature = "serde")] | ||
mod tests { | ||
use super::*; | ||
use crate::hash_tree::{empty, fork, label, leaf}; | ||
|
||
fn create_tree<'a>() -> HashTree<'a> { | ||
fork( | ||
fork( | ||
label( | ||
"a", | ||
fork( | ||
fork(label("x", leaf(b"hello")), empty()), | ||
label("y", leaf(b"world")), | ||
), | ||
), | ||
label("b", leaf(b"good")), | ||
), | ||
fork(label("c", empty()), label("d", leaf(b"morning"))), | ||
) | ||
} | ||
|
||
#[test] | ||
fn serialize_to_cbor() { | ||
let tree = create_tree(); | ||
let signature = vec![1, 2, 3, 4, 5, 6]; | ||
|
||
let certificate = Certificate { | ||
tree, | ||
signature, | ||
delegation: None, | ||
}; | ||
|
||
let cbor_bytes = | ||
serde_cbor::to_vec(&certificate).expect("Failed to encode certificate to cbor"); | ||
let cbor_hex = hex::encode(cbor_bytes); | ||
|
||
assert_eq!(cbor_hex, "a264747265658301830183024161830183018302417882034568656c6c6f810083024179820345776f726c6483024162820344676f6f648301830241638100830241648203476d6f726e696e67697369676e617475726546010203040506"); | ||
} | ||
|
||
#[test] | ||
fn serialize_to_cbor_with_delegation() { | ||
let tree = create_tree(); | ||
let signature = vec![1, 2, 3, 4, 5, 6]; | ||
let delegation = Delegation { | ||
subnet_id: vec![7, 8, 9, 10, 11, 12], | ||
certificate: vec![13, 14, 15, 16, 17, 18], | ||
}; | ||
|
||
let certificate = Certificate { | ||
tree, | ||
signature, | ||
delegation: Some(delegation), | ||
}; | ||
|
||
let cbor_bytes = | ||
serde_cbor::to_vec(&certificate).expect("Failed to encode certificate to cbor"); | ||
let cbor_hex = hex::encode(cbor_bytes); | ||
|
||
assert_eq!(cbor_hex, "a364747265658301830183024161830183018302417882034568656c6c6f810083024179820345776f726c6483024162820344676f6f648301830241638100830241648203476d6f726e696e67697369676e6174757265460102030405066a64656c65676174696f6ea2697375626e65745f6964460708090a0b0c6b6365727469666963617465460d0e0f101112"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.