1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{stake_pool::StakePool, wallet::Wallet};
use chain_impl_mockchain::{
    block::BlockDate,
    certificate::{
        PoolId, PoolOwnersSigned, PoolSignature, SignedCertificate, StakeDelegation, VotePlan,
        VotePlanProof,
    },
    transaction::{AccountBindingSignature, SingleAccountBindingSignature, TxBuilder},
};

pub fn signed_delegation_cert(
    wallet: &Wallet,
    valid_until: BlockDate,
    pool_id: PoolId,
) -> SignedCertificate {
    let stake_delegation = StakeDelegation {
        account_id: wallet.stake_key().unwrap(),
        delegation: chain_impl_mockchain::account::DelegationType::Full(pool_id),
    };
    let txb = TxBuilder::new()
        .set_payload(&stake_delegation)
        .set_expiry_date(valid_until)
        .set_ios(&[], &[])
        .set_witnesses(&[]);
    let auth_data = txb.get_auth_data();

    let sig = AccountBindingSignature::new_single(&auth_data, |d| wallet.sign_slice(d.0));
    SignedCertificate::StakeDelegation(stake_delegation, sig)
}

pub fn signed_stake_pool_cert(valid_until: BlockDate, stake_pool: &StakePool) -> SignedCertificate {
    let owner = stake_pool.owner().clone();
    let txb = TxBuilder::new()
        .set_payload(&stake_pool.inner().info())
        .set_expiry_date(valid_until)
        .set_ios(&[], &[])
        .set_witnesses(&[]);

    let auth_data = txb.get_auth_data();
    let sig0 = SingleAccountBindingSignature::new(&auth_data, |d| owner.sign_slice(d.0));
    let owner_signed = PoolOwnersSigned {
        signatures: vec![(0, sig0)],
    };

    SignedCertificate::PoolRegistration(
        stake_pool.inner().info(),
        PoolSignature::Owners(owner_signed),
    )
}

pub fn vote_plan_cert(
    wallet: &Wallet,
    valid_until: BlockDate,
    vote_plan: &VotePlan,
) -> SignedCertificate {
    let txb = TxBuilder::new()
        .set_payload(vote_plan)
        .set_expiry_date(valid_until)
        .set_ios(&[], &[])
        .set_witnesses(&[]);

    let auth_data = txb.get_auth_data();

    let signature = SingleAccountBindingSignature::new(&auth_data, |d| wallet.sign_slice(d.0));

    SignedCertificate::VotePlan(
        vote_plan.clone(),
        VotePlanProof {
            id: wallet.identifier().into_public_key().into(),
            signature,
        },
    )
}