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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use super::element::TransactionSignDataHash;
use crate::account;
use crate::chaintypes::HeaderId;
use crate::key::{
    deserialize_public_key, deserialize_signature, serialize_public_key, serialize_signature,
    SpendingSignature,
};
use crate::multisig;
use chain_core::{
    packer::Codec,
    property::{Deserialize, DeserializeFromSlice, ReadError, Serialize, WriteError},
};
use chain_crypto::{Ed25519, PublicKey, Signature};

/// Structure that proofs that certain user agrees with
/// some data. This structure is used to sign `Transaction`
/// and get `SignedTransaction` out.
///
/// It's important that witness works with opaque structures
/// and may not know the contents of the internal transaction.
#[derive(Debug, Clone)]
pub enum Witness {
    Utxo(SpendingSignature<WitnessUtxoData>),
    Account(account::SpendingCounter, account::Witness),
    OldUtxo(
        PublicKey<Ed25519>,
        [u8; 32],
        Signature<WitnessUtxoData, Ed25519>,
    ),
    Multisig(account::SpendingCounter, multisig::Witness),
}

impl PartialEq for Witness {
    fn eq(&self, rhs: &Self) -> bool {
        match (self, rhs) {
            (Witness::Utxo(s1), Witness::Utxo(s2)) => s1.as_ref() == s2.as_ref(),
            (Witness::Account(n1, s1), Witness::Account(n2, s2)) => {
                n1 == n2 && s1.as_ref() == s2.as_ref()
            }
            (Witness::Multisig(n1, s1), Witness::Multisig(n2, s2)) => n1 == n2 && s1 == s2,
            (Witness::OldUtxo(p1, c1, s1), Witness::OldUtxo(p2, c2, s2)) => {
                s1.as_ref() == s2.as_ref() && c1 == c2 && p1 == p2
            }
            (_, _) => false,
        }
    }
}
impl Eq for Witness {}

impl std::fmt::Display for Witness {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Witness::Utxo(_) => write!(f, "UTxO Witness"),
            Witness::Account(_, _) => write!(f, "Account Witness"),
            Witness::OldUtxo(..) => write!(f, "Old UTxO Witness"),
            Witness::Multisig(_, _) => write!(f, "Multisig Witness"),
        }
    }
}

pub struct WitnessUtxoData(Vec<u8>);

#[derive(Debug, Clone, Copy)]
pub enum WitnessUtxoVersion {
    Legacy,
    Normal,
}

fn witness_data_common(
    data: &mut Vec<u8>,
    tag: u8,
    block0: &HeaderId,
    transaction_id: &TransactionSignDataHash,
) {
    data.push(tag);
    data.extend_from_slice(block0.as_ref());
    data.extend_from_slice(transaction_id.as_ref());
}

impl WitnessUtxoData {
    pub fn new(
        block0: &HeaderId,
        transaction_id: &TransactionSignDataHash,
        utxo_version: WitnessUtxoVersion,
    ) -> Self {
        let mut v = Vec::with_capacity(65);
        let tag = match utxo_version {
            WitnessUtxoVersion::Legacy => WITNESS_TAG_OLDUTXO,
            WitnessUtxoVersion::Normal => WITNESS_TAG_UTXO,
        };
        witness_data_common(&mut v, tag, block0, transaction_id);
        WitnessUtxoData(v)
    }
}

impl AsRef<[u8]> for WitnessUtxoData {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

pub struct WitnessAccountData(Vec<u8>);

impl WitnessAccountData {
    pub fn new(
        block0: &HeaderId,
        transaction_id: &TransactionSignDataHash,
        spending_counter: account::SpendingCounter,
    ) -> Self {
        let mut v = Vec::with_capacity(69);
        witness_data_common(&mut v, WITNESS_TAG_ACCOUNT, block0, transaction_id);
        v.extend_from_slice(&spending_counter.to_bytes());
        WitnessAccountData(v)
    }
}

impl AsRef<[u8]> for WitnessAccountData {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

pub struct WitnessMultisigData(Vec<u8>);

impl WitnessMultisigData {
    pub fn new(
        block0: &HeaderId,
        transaction_id: &TransactionSignDataHash,
        spending_counter: account::SpendingCounter,
    ) -> Self {
        let mut v = Vec::with_capacity(69);
        witness_data_common(&mut v, WITNESS_TAG_MULTISIG, block0, transaction_id);
        v.extend_from_slice(&spending_counter.to_bytes());
        Self(v)
    }
}

impl AsRef<[u8]> for WitnessMultisigData {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl Witness {
    /// Creates new `Witness` value.

    pub fn new_utxo_data(
        block0: &HeaderId,
        sign_data_hash: &TransactionSignDataHash,
    ) -> WitnessUtxoData {
        WitnessUtxoData::new(block0, sign_data_hash, WitnessUtxoVersion::Normal)
    }

    pub fn new_utxo<F>(block0: &HeaderId, sign_data_hash: &TransactionSignDataHash, sign: F) -> Self
    where
        F: FnOnce(&WitnessUtxoData) -> Signature<WitnessUtxoData, Ed25519>,
    {
        let wud = WitnessUtxoData::new(block0, sign_data_hash, WitnessUtxoVersion::Normal);
        Witness::Utxo(sign(&wud))
    }

    pub fn new_old_utxo_data(
        block0: &HeaderId,
        sign_data_hash: &TransactionSignDataHash,
    ) -> WitnessUtxoData {
        WitnessUtxoData::new(block0, sign_data_hash, WitnessUtxoVersion::Legacy)
    }

    pub fn new_old_utxo<F>(
        block0: &HeaderId,
        sign_data_hash: &TransactionSignDataHash,
        sign: F,
        some_bytes: &[u8; 32],
    ) -> Self
    where
        F: FnOnce(&WitnessUtxoData) -> (PublicKey<Ed25519>, Signature<WitnessUtxoData, Ed25519>),
    {
        let wud = WitnessUtxoData::new(block0, sign_data_hash, WitnessUtxoVersion::Legacy);
        let (pk, sig) = sign(&wud);
        Witness::OldUtxo(pk, *some_bytes, sig)
    }

    pub fn new_account_data(
        block0: &HeaderId,
        sign_data_hash: &TransactionSignDataHash,
        spending_counter: account::SpendingCounter,
    ) -> WitnessAccountData {
        WitnessAccountData::new(block0, sign_data_hash, spending_counter)
    }

    pub fn new_account<F>(
        block0: &HeaderId,
        sign_data_hash: &TransactionSignDataHash,
        spending_counter: account::SpendingCounter,
        sign: F,
    ) -> Self
    where
        F: FnOnce(&WitnessAccountData) -> account::Witness,
    {
        let wud = WitnessAccountData::new(block0, sign_data_hash, spending_counter);
        let sig = sign(&wud);
        Witness::Account(spending_counter, sig)
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.serialize_as_vec()
            .expect("memory serialize is expected to just work")
    }
}

const WITNESS_TAG_OLDUTXO: u8 = 0u8;
const WITNESS_TAG_UTXO: u8 = 1u8;
const WITNESS_TAG_ACCOUNT: u8 = 2u8;
const WITNESS_TAG_MULTISIG: u8 = 3u8;

impl Serialize for Witness {
    fn serialized_size(&self) -> usize {
        match self {
            Witness::OldUtxo(pk, cc, sig) => {
                Codec::u8_size() + pk.as_ref().len() + cc.serialized_size() + sig.as_ref().len()
            }
            Witness::Utxo(sig) => Codec::u8_size() + sig.as_ref().len(),
            Witness::Account(_, sig) => Codec::u8_size() + Codec::u32_size() + sig.as_ref().len(),
            Witness::Multisig(_, msig) => {
                Codec::u8_size() + Codec::u32_size() + msig.serialized_size()
            }
        }
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        match self {
            Witness::OldUtxo(pk, cc, sig) => {
                codec.put_u8(WITNESS_TAG_OLDUTXO)?;
                serialize_public_key(pk, codec)?;
                codec.put_bytes(cc)?;
                serialize_signature(sig, codec)
            }
            Witness::Utxo(sig) => {
                codec.put_u8(WITNESS_TAG_UTXO)?;
                serialize_signature(sig, codec)
            }
            Witness::Account(nonce, sig) => {
                codec.put_u8(WITNESS_TAG_ACCOUNT)?;
                codec.put_be_u32((*nonce).into())?;
                serialize_signature(sig, codec)
            }
            Witness::Multisig(nonce, msig) => {
                codec.put_u8(WITNESS_TAG_MULTISIG)?;
                codec.put_be_u32((*nonce).into())?;
                msig.serialize(codec)
            }
        }
    }
}

impl DeserializeFromSlice for Witness {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        match codec.get_u8()? {
            WITNESS_TAG_OLDUTXO => {
                let pk = deserialize_public_key(codec)?;
                let some_bytes = <[u8; 32]>::deserialize(codec)?;
                let sig = deserialize_signature(codec)?;
                Ok(Witness::OldUtxo(pk, some_bytes, sig))
            }
            WITNESS_TAG_UTXO => deserialize_signature(codec).map(Witness::Utxo),
            WITNESS_TAG_ACCOUNT => {
                let nonce = codec.get_be_u32()?.into();
                let sig = deserialize_signature(codec)?;
                Ok(Witness::Account(nonce, sig))
            }
            WITNESS_TAG_MULTISIG => {
                let nonce = codec.get_be_u32()?.into();
                let msig = multisig::Witness::deserialize_from_slice(codec)?;
                Ok(Witness::Multisig(nonce, msig))
            }
            i => Err(ReadError::UnknownTag(i as u32)),
        }
    }
}