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
use crate::{Address, AddressReadable, Discrimination, Kind, KindType};
use chain_crypto::{Ed25519, KeyPair, PublicKey};
use quickcheck::{Arbitrary, Gen};

impl Arbitrary for Discrimination {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        match u8::arbitrary(g) % 2 {
            0 => Discrimination::Production,
            1 => Discrimination::Test,
            _ => unreachable!(),
        }
    }
}

impl Arbitrary for KindType {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        match u8::arbitrary(g) % 5 {
            0 => KindType::Single,
            1 => KindType::Group,
            2 => KindType::Account,
            3 => KindType::Multisig,
            4 => KindType::Script,
            _ => unreachable!(),
        }
    }
}

const ARBITRARY_PREFIX: &str = "qaddr";

impl Arbitrary for AddressReadable {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        AddressReadable::from_address(ARBITRARY_PREFIX, &Arbitrary::arbitrary(g))
    }
}

fn arbitrary_public_key<G: Gen>(g: &mut G) -> PublicKey<Ed25519> {
    let kp: KeyPair<Ed25519> = Arbitrary::arbitrary(g);
    kp.into_keys().1
}

fn arbitrary_32bytes<G: Gen>(g: &mut G) -> [u8; 32] {
    let mut h = [0u8; 32];
    for i in h.iter_mut() {
        *i = Arbitrary::arbitrary(g)
    }
    h
}

impl Arbitrary for Address {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        let discrimination = Discrimination::arbitrary(g);
        let kind = Kind::arbitrary(g);
        Self(discrimination, kind)
    }
}

impl Arbitrary for Kind {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        match u8::arbitrary(g) % 5 {
            0 => Kind::Single(arbitrary_public_key(g)),
            1 => Kind::Group(arbitrary_public_key(g), arbitrary_public_key(g)),
            2 => Kind::Account(arbitrary_public_key(g)),
            3 => {
                let h = arbitrary_32bytes(g);
                Kind::Multisig(h)
            }
            4 => Kind::Script(arbitrary_32bytes(g)),
            _ => unreachable!(),
        }
    }
}