-
Notifications
You must be signed in to change notification settings - Fork 18
Create a dedicated ssv_types
crate.
#58
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
834891b
move to separate files and pull in lighthouse types
Zacholme7 0f0ef38
types according to spec
Zacholme7 3676efe
update comments
Zacholme7 c8bdf73
doc comments
Zacholme7 5b02a70
spelling
Zacholme7 1712f7b
toml update
Zacholme7 376cdaa
rename to ssv_types to prevent conflict with lighthouse types
Zacholme7 d53b83c
format
Zacholme7 80b71c1
dep bump
Zacholme7 4a4dfb5
newtypes & rsa support
Zacholme7 fd0a225
ValidatorIndex newtype
Zacholme7 7b1efc6
doc comment
Zacholme7 de080c5
cargo update to fix CI
dknopik c5b5954
fix msrv
dknopik 54ce852
small fixes from db integration
Zacholme7 3c3b4b7
Merge branch 'types' of github.com:Zacholme7/anchor into types
Zacholme7 d4cc839
revert alloy-primitives version to supported
Zacholme7 0d9890b
cargo fmt
Zacholme7 42d991b
Merge branch 'unstable' into types
Zacholme7 0286957
fix clippy
jking-aus 3cf5dec
fix clippy
jking-aus 64c22b4
merge
Zacholme7 8793324
Merge branch 'unstable' into types
jking-aus 6da30c8
Merge branch 'unstable' into types
jking-aus 1fa7b3b
Update cli.rs
jking-aus 3328c03
Update Cargo.toml
jking-aus 1cb71c8
change rsa crate to openssl
jking-aus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,11 @@ | ||
[package] | ||
name = "ssv_types" | ||
version = "0.1.0" | ||
edition = { workspace = true } | ||
authors = ["Sigma Prime <[email protected]>"] | ||
|
||
[dependencies] | ||
types = { workspace = true } | ||
openssl = { workspace = true } | ||
derive_more = { workspace = true } | ||
base64 = { workspace = true } |
This file contains hidden or 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,63 @@ | ||
use crate::util::parse_rsa; | ||
use crate::{Operator, OperatorId}; | ||
use derive_more::{Deref, From}; | ||
use openssl::pkey::Public; | ||
use openssl::rsa::Rsa; | ||
use types::Domain; | ||
|
||
/// Unique identifier for a committee. | ||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, From, Deref)] | ||
pub struct CommitteeId(pub u64); | ||
|
||
/// Member of a SSV Committee. A CommitteeMember is just an operator that is part of the committee | ||
/// a validator has chosen to distribute its keyshares to. | ||
#[derive(Debug, Clone)] | ||
pub struct CommitteeMember { | ||
/// Unique identifier for the operator | ||
pub operator_id: OperatorId, | ||
/// Unique identifier for the committee this member is a part of | ||
pub committee_id: CommitteeId, | ||
/// Base-64 encoded PEM RSA public key of the operator | ||
pub operator_public_key: Rsa<Public>, | ||
/// Number of nodes that are faulty/malicious in the committee | ||
pub faulty: u64, | ||
/// All of the operators that are a part of this committee | ||
pub members: Vec<Operator>, | ||
/// Signature domain | ||
pub domain: Domain, | ||
} | ||
|
||
impl CommitteeMember { | ||
/// Creates a new committee member from a PEM-encoded public key string | ||
pub fn new( | ||
pem_data: &str, | ||
operator_id: OperatorId, | ||
committee_id: CommitteeId, | ||
domain: Domain, | ||
) -> Result<Self, String> { | ||
let rsa_pubkey = parse_rsa(pem_data)?; | ||
Ok(Self::new_with_pubkey( | ||
rsa_pubkey, | ||
operator_id, | ||
committee_id, | ||
domain, | ||
)) | ||
} | ||
|
||
/// Creates a new committee member from an existing RSA public key | ||
pub fn new_with_pubkey( | ||
rsa_pubkey: Rsa<Public>, | ||
operator_id: OperatorId, | ||
committee_id: CommitteeId, | ||
domain: Domain, | ||
) -> Self { | ||
Self { | ||
operator_id, | ||
committee_id, | ||
operator_public_key: rsa_pubkey, | ||
faulty: 0, | ||
members: Vec::new(), | ||
domain, | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
pub use committee::{CommitteeId, CommitteeMember}; | ||
pub use operator::{Operator, OperatorId}; | ||
pub use share::{SSVShare, Share, ShareMember, ValidatorIndex}; | ||
mod committee; | ||
mod operator; | ||
mod share; | ||
mod util; |
This file contains hidden or 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,57 @@ | ||
use crate::util::parse_rsa; | ||
use derive_more::{Deref, From}; | ||
use openssl::rsa::Rsa; | ||
use std::cmp::Eq; | ||
use std::fmt::Debug; | ||
use std::hash::Hash; | ||
|
||
/// Unique identifier for an Operator. | ||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, From, Deref)] | ||
pub struct OperatorId(u64); | ||
|
||
/// Client responsible for maintaining the overall health of the network. | ||
#[derive(Debug, Clone)] | ||
pub struct Operator { | ||
/// ID to uniquely identify this operator | ||
pub id: OperatorId, | ||
/// Base-64 encoded PEM RSA public key | ||
pub public_key: Rsa<openssl::pkey::Public>, | ||
} | ||
|
||
impl Operator { | ||
/// Creates a new operator from its OperatorId and PEM-encoded public key string | ||
pub fn new(pem_data: &str, operator_id: OperatorId) -> Result<Self, String> { | ||
let rsa_pubkey = parse_rsa(pem_data)?; | ||
Ok(Self::new_with_pubkey(rsa_pubkey, operator_id)) | ||
} | ||
|
||
// Creates a new operator from an existing RSA public key and OperatorId | ||
pub fn new_with_pubkey( | ||
rsa_pubkey: Rsa<openssl::pkey::Public>, | ||
operator_id: OperatorId, | ||
) -> Self { | ||
Self { | ||
id: operator_id, | ||
public_key: rsa_pubkey, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod operator_tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn operator_from_pubkey_and_id() { | ||
// Random valid operator public key and id: https://explorer.ssv.network/operators/1141 | ||
let pem_data = "LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBbFFmQVIzMEd4bFpacEwrNDByU0IKTEpSYlkwY2laZDBVMXhtTlp1bFB0NzZKQXJ5d2lia0Y4SFlQV2xkM3dERVdWZXZjRzRGVVBSZ0hDM1MrTHNuMwpVVC9TS280eE9nNFlnZ0xqbVVXQysyU3ZGRFhXYVFvdFRXYW5UU0drSEllNGFnTVNEYlUzOWhSMWdOSTJhY2NNCkVCcjU2eXpWcFMvKytkSk5xU002S1FQM3RnTU5ia2IvbEtlY0piTXM0ZWNRMTNkWUQwY3dFNFQxcEdTYUdhcEkKbFNaZ2lYd0cwSGFNTm5GUkt0OFlkZjNHaTFMRlh3Zlo5NHZFRjJMLzg3RCtidjdkSFVpSGRjRnh0Vm0rVjVvawo3VFptcnpVdXB2NWhKZ3lDVE9zc0xHOW1QSGNORnhEVDJ4NUJKZ2FFOVpJYnMrWVZ5a1k3UTE4VEhRS2lWcDFaCmp3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K"; | ||
let operator_id = 1141; | ||
|
||
let operator = Operator::new(pem_data, operator_id.into()); | ||
assert!(operator.is_ok()); | ||
|
||
if let Ok(op) = operator { | ||
assert_eq!(op.id.0, operator_id); | ||
} | ||
} | ||
} |
This file contains hidden or 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,56 @@ | ||
use crate::OperatorId; | ||
use derive_more::{Deref, From}; | ||
use std::time::SystemTime; | ||
use types::{Address, Domain, Graffiti, PublicKey}; | ||
|
||
/// Index of the validator in the validator registry. | ||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, From, Deref)] | ||
pub struct ValidatorIndex(usize); | ||
|
||
/// Share of a key that a operator owns and its accompanying metadata. | ||
#[derive(Debug, Clone)] | ||
pub struct SSVShare { | ||
// A single share of a validator private key. | ||
pub share: Share, | ||
// Miscellaneous metadata relevant to the share | ||
pub metadata: Metadata, | ||
} | ||
|
||
/// One of N shares of a split validator key. | ||
#[derive(Debug, Clone)] | ||
pub struct Share { | ||
/// Index of the validator | ||
pub validator_index: ValidatorIndex, | ||
/// Public key of the validator | ||
pub validator_pubkey: PublicKey, | ||
/// Public key for this portion of the share | ||
pub share_public_key: PublicKey, | ||
/// All committee members that contain a sibling share | ||
pub committee: Vec<ShareMember>, | ||
/// Identifies the context/purpose of signature | ||
pub domain_type: Domain, | ||
/// Eth1 fee address | ||
pub fee_recipient: Address, | ||
/// Graffiti | ||
pub graffiti: Graffiti, | ||
} | ||
|
||
/// A operator who holds a portion of the share. | ||
#[derive(Debug, Clone)] | ||
pub struct ShareMember { | ||
/// Unique identifier for the operator | ||
pub operator: OperatorId, | ||
/// The public key for this members share | ||
pub share_public_key: PublicKey, | ||
} | ||
|
||
/// General metadata. | ||
#[derive(Debug, Clone)] | ||
pub struct Metadata { | ||
/// The owner of the validator | ||
pub owner: Address, | ||
/// Is the committee this share is a part of currently liquidated | ||
pub liquidated: bool, | ||
/// Track the last time the metadata was updated. | ||
pub last_updated: SystemTime, | ||
} |
This file contains hidden or 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,31 @@ | ||
use base64::prelude::*; | ||
use openssl::pkey::Public; | ||
use openssl::rsa::Rsa; | ||
|
||
// Parse from a RSA public key string into the associated RSA representation | ||
// NOTE: This function handles a legacy system quirk where RSA keys are | ||
// incorrectly formatted with PKCS8 headers but PKCS1 content. | ||
pub fn parse_rsa(pem_data: &str) -> Result<Rsa<Public>, String> { | ||
// First decode the base64 data | ||
let pem_decoded = BASE64_STANDARD | ||
.decode(pem_data) | ||
.map_err(|e| format!("Unable to decode base64 pem data: {}", e))?; | ||
|
||
// Convert the decoded data to a utf8 string | ||
let mut pem_string = String::from_utf8(pem_decoded) | ||
.map_err(|e| format!("Unable to convert decoded pem data into a string: {}", e))?; | ||
|
||
// Fix the header - replace PKCS1 header with PKCS8 header | ||
pem_string = pem_string | ||
.replace( | ||
"-----BEGIN RSA PUBLIC KEY-----", | ||
"-----BEGIN PUBLIC KEY-----", | ||
) | ||
.replace("-----END RSA PUBLIC KEY-----", "-----END PUBLIC KEY-----"); | ||
|
||
// Parse the PEM string into an RSA public key | ||
let rsa_pubkey = Rsa::public_key_from_pem(pem_string.as_bytes()) | ||
.map_err(|e| format!("Failed to parse RSA public key: {}", e))?; | ||
|
||
Ok(rsa_pubkey) | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.