#![allow(clippy::too_many_arguments)]
pub use cml_core::{
error::{DeserializeError, DeserializeFailure},
ordered_hash_map::OrderedHashMap,
serialization::{Deserialize, LenEncoding, Serialize, StringEncoding},
};
pub use cml_chain::{address::Address, auxdata::Metadata, NetworkId};
use std::convert::From;
pub mod cbor_encodings;
pub mod error;
pub mod serialization;
pub mod utils;
use cbor_encodings::*;
extern crate derivative;
pub type CIP36VotingPubKey = cml_crypto::PublicKey;
pub type CIP36StakingPubKey = cml_crypto::PublicKey;
pub type CIP36LegacyKeyRegistration = CIP36VotingPubKey;
pub type CIP36Nonce = u64;
pub type CIP36StakeCredential = CIP36StakingPubKey;
pub type CIP36StakeWitness = cml_crypto::Ed25519Signature;
pub type CIP36VotingPurpose = u64;
pub type CIP36Weight = u32;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36Delegation {
pub voting_pub_key: CIP36VotingPubKey,
pub weight: CIP36Weight,
#[serde(skip)]
pub encodings: Option<CIP36DelegationEncoding>,
}
impl CIP36Delegation {
pub fn new(voting_pub_key: CIP36VotingPubKey, weight: CIP36Weight) -> Self {
Self {
voting_pub_key,
weight,
encodings: None,
}
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub enum CIP36DelegationDistribution {
Weighted {
delegations: Vec<CIP36Delegation>,
#[serde(skip)]
delegations_encoding: LenEncoding,
},
Legacy {
legacy: CIP36LegacyKeyRegistration,
#[serde(skip)]
legacy_encoding: StringEncoding,
},
}
impl CIP36DelegationDistribution {
pub fn new_weighted(delegations: Vec<CIP36Delegation>) -> Self {
Self::Weighted {
delegations,
delegations_encoding: LenEncoding::default(),
}
}
pub fn new_legacy(legacy: CIP36LegacyKeyRegistration) -> Self {
Self::Legacy {
legacy,
legacy_encoding: StringEncoding::default(),
}
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36DeregistrationCbor {
pub key_deregistration: CIP36KeyDeregistration,
pub deregistration_witness: CIP36DeregistrationWitness,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36DeregistrationWitness {
pub stake_witness: CIP36StakeWitness,
#[serde(skip)]
pub encodings: Option<CIP36DeregistrationWitnessEncoding>,
}
impl CIP36DeregistrationWitness {
pub fn new(stake_witness: CIP36StakeWitness) -> Self {
Self {
stake_witness,
encodings: None,
}
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36KeyDeregistration {
pub stake_credential: CIP36StakeCredential,
pub nonce: CIP36Nonce,
pub voting_purpose: CIP36VotingPurpose,
#[serde(skip)]
pub encodings: Option<CIP36KeyDeregistrationEncoding>,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36KeyRegistration {
pub delegation: CIP36DelegationDistribution,
pub stake_credential: CIP36StakeCredential,
pub payment_address: Address,
pub nonce: CIP36Nonce,
pub voting_purpose: CIP36VotingPurpose,
#[serde(skip)]
pub encodings: Option<CIP36KeyRegistrationEncoding>,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36RegistrationCbor {
pub key_registration: CIP36KeyRegistration,
pub registration_witness: CIP36RegistrationWitness,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CIP36RegistrationWitness {
pub stake_witness: CIP36StakeWitness,
#[serde(skip)]
pub encodings: Option<CIP36RegistrationWitnessEncoding>,
}
impl CIP36RegistrationWitness {
pub fn new(stake_witness: CIP36StakeWitness) -> Self {
Self {
stake_witness,
encodings: None,
}
}
}
#[cfg(test)]
mod tests {
use cml_chain::address::Address;
use cml_crypto::*;
use super::*;
#[test]
fn sign_data() {
let stake_cred = PublicKey::from_raw_bytes(&[
227, 205, 36, 4, 200, 77, 230, 95, 150, 145, 143, 24, 213, 180, 69, 188, 185, 51, 167,
205, 161, 142, 237, 237, 121, 69, 221, 25, 30, 67, 35, 105,
])
.unwrap();
let nonce = 1234;
let legacy_address = Address::from_raw_bytes(&[
224, 114, 182, 23, 101, 120, 129, 227, 10, 209, 124, 70, 228, 1, 12, 156, 179, 235,
178, 68, 6, 83, 163, 77, 50, 33, 156, 131, 233,
])
.unwrap();
let legacy_reg = CIP36KeyRegistration::new(
CIP36DelegationDistribution::new_legacy(
CIP36LegacyKeyRegistration::from_raw_bytes(&[
0, 54, 239, 62, 31, 13, 63, 89, 137, 226, 209, 85, 234, 84, 189, 178, 167, 44,
76, 69, 108, 203, 149, 154, 244, 201, 72, 104, 244, 115, 245, 160,
])
.unwrap(),
),
stake_cred.clone(),
legacy_address,
nonce,
);
let legacy_sign_data_hash = legacy_reg.hash_to_sign(false).unwrap();
assert_eq!(
"9946e71b5f6c16150cf431910a0f7dbb8084a992577847802e60d32becb3d6be",
hex::encode(legacy_sign_data_hash)
);
let new_address = Address::from_raw_bytes(&[
0, 71, 119, 86, 30, 125, 158, 193, 18, 236, 48, 117, 114, 250, 236, 26, 255, 97, 255,
12, 254, 214, 141, 244, 205, 92, 132, 127, 24, 114, 182, 23, 101, 120, 129, 227, 10,
209, 124, 70, 228, 1, 12, 156, 179, 235, 178, 68, 6, 83, 163, 77, 50, 33, 156, 131,
233,
])
.unwrap();
let weighted_reg = CIP36KeyRegistration::new(
CIP36DelegationDistribution::new_weighted(vec![CIP36Delegation::new(
CIP36VotingPubKey::from_raw_bytes(&[
0, 54, 239, 62, 31, 13, 63, 89, 137, 226, 209, 85, 234, 84, 189, 178, 167, 44,
76, 69, 108, 203, 149, 154, 244, 201, 72, 104, 244, 115, 245, 160,
])
.unwrap(),
1,
)]),
stake_cred,
new_address,
nonce,
);
let weighted_sign_data_hash = weighted_reg.hash_to_sign(false).unwrap();
assert_eq!(
"3110fbad72589a80de7fc174310e92dac35bbfece1690c2dce53c2235a9776fa",
hex::encode(weighted_sign_data_hash)
);
}
}