mod registration;
pub use crate::wallet::registration::{
GeneralTransactionMetadataInfo, JsonConversionError, RegistrationTransactionBuilder,
METADATUM_1, METADATUM_2, METADATUM_3, METADATUM_4, REGISTRATION_METADATA_IDX,
REGISTRATION_METADATA_LABEL, REGISTRATION_METADATA_SIGNATURE_LABEL,
REGISTRATION_SIGNATURE_METADATA_IDX,
};
use cardano_serialization_lib::address::{
BaseAddress, NetworkInfo, RewardAddress, StakeCredential,
};
use cardano_serialization_lib::crypto::{PrivateKey, PublicKey};
use cardano_serialization_lib::Transaction;
use chain_addr::Discrimination;
use jormungandr_lib::crypto::account::Identifier;
use jormungandr_lib::interfaces::Address;
use rand::{CryptoRng, RngCore};
use snapshot_lib::registration::Delegations;
pub struct CardanoWallet {
catalyst: thor::Wallet,
stake_key: PrivateKey,
payment_key: PrivateKey,
network: NetworkInfo,
stake: u64,
}
impl CardanoWallet {
#[must_use]
pub fn new(stake: u64) -> Self {
let rng = rand::thread_rng();
Self::new_with_rng(stake, rng)
}
#[must_use]
pub fn new_with_rng<T: RngCore + CryptoRng>(stake: u64, mut rng: T) -> Self {
Self {
catalyst: thor::Wallet::new_account(&mut rng, Discrimination::Production),
stake_key: PrivateKey::generate_ed25519extended().unwrap(),
payment_key: PrivateKey::generate_ed25519extended().unwrap(),
network: NetworkInfo::mainnet(),
stake,
}
}
#[must_use]
pub fn reward_address(&self) -> RewardAddress {
RewardAddress::new(self.network.network_id(), &self.stake_credential())
}
#[must_use]
pub fn stake_credential(&self) -> StakeCredential {
StakeCredential::from_keyhash(&self.stake_key.to_public().hash())
}
#[must_use]
pub fn payment_credential(&self) -> StakeCredential {
StakeCredential::from_keyhash(&self.payment_key.to_public().hash())
}
#[must_use]
pub fn stake_public_key(&self) -> PublicKey {
self.stake_key.to_public()
}
#[must_use]
pub fn catalyst_secret_key(&self) -> chain_crypto::SecretKey<chain_crypto::Ed25519Extended> {
self.catalyst.secret_key()
}
#[must_use]
pub fn catalyst_public_key(&self) -> Identifier {
self.catalyst.secret_key().to_public().into()
}
#[must_use]
pub fn address(&self) -> BaseAddress {
BaseAddress::new(
self.network.network_id(),
&self.payment_credential(),
&self.stake_credential(),
)
}
#[must_use]
pub fn catalyst_address(&self) -> Address {
self.catalyst.address()
}
#[must_use]
pub fn generate_voting_registration(
&self,
delegations: Delegations,
slot_no: u64,
) -> Transaction {
RegistrationTransactionBuilder::new(self)
.to(delegations)
.on(slot_no)
.build()
}
#[must_use]
pub fn generate_delegated_voting_registration(
&self,
delegations: Vec<(Identifier, u32)>,
slot_no: u64,
) -> Transaction {
self.generate_voting_registration(Delegations::New(delegations), slot_no)
}
#[must_use]
pub fn generate_direct_voting_registration(&self, slot_no: u64) -> Transaction {
self.generate_voting_registration(
Delegations::Legacy(self.catalyst.identifier().into()),
slot_no,
)
}
#[must_use]
pub fn stake(&self) -> u64 {
self.stake
}
#[must_use]
pub fn payment_key(&self) -> &PrivateKey {
&self.payment_key
}
#[must_use]
pub fn stake_key(&self) -> &PrivateKey {
&self.stake_key
}
}