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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

//! Some basic versioned types used throughout

use crate::version_bytes;
use bs58::encode::EncodeBuilder;
use derive_more::{From, Into};
use mina_serialization_types_macros::AutoFrom;
use num::Integer;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use versioned::*;

/// 32 bytes representing a hash of some kind (v1)
pub type HashV1 = Versioned<[u8; 32], 1>;

/// 32 bytes representing a hash of some kind (v1) with extra version byte
pub type Hash2V1 = Versioned<HashV1, 1>;

/// u64 representing a token ID (v1)
pub type TokenIdV1 = Versioned<Versioned<Versioned<u64, 1>, 1>, 1>;
impl_from_for_newtype!(U64Json, TokenIdV1);

/// u64 representing a block time (v1)
pub type BlockTimeV1 = Versioned<Versioned<u64, 1>, 1>;

/// u64 representing an account nonce (v1) // This should also be an extendedu32
pub type AccountNonceV1 = Versioned<Versioned<u32, 1>, 1>;

/// u32 wrapper (json)
/// Note that integers are represented as string in mina json
#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)]
pub struct U32Json(pub u32);

impl Serialize for U32Json {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = format!("{}", self.0);
        serializer.serialize_str(&s)
    }
}

impl<'de> Deserialize<'de> for U32Json {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;

        Ok(Self(
            s.parse().map_err(<D::Error as serde::de::Error>::custom)?,
        ))
    }
}

/// u64 wrapper (json)
/// Note that integers are represented as string in mina json
#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)]
pub struct U64Json(pub u64);

impl Serialize for U64Json {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = format!("{}", self.0);
        serializer.serialize_str(&s)
    }
}

impl<'de> Deserialize<'de> for U64Json {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;

        Ok(Self(
            s.parse().map_err(<D::Error as serde::de::Error>::custom)?,
        ))
    }
}

/// i64 wrapper (json)
#[derive(
    Clone, Debug, Eq, PartialEq, Serialize, Deserialize, derive_more::From, derive_more::Into,
)]
pub struct I64(pub i64);

/// u64 wrapper (json)
/// Note that integers are represented as string in mina json
#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)]
pub struct DecimalJson(pub u64);

const MINA_PRECISION: u64 = 1000000000;
impl Serialize for DecimalJson {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let (q, r) = self.0.div_rem(&MINA_PRECISION);
        let s = format!("{q}.{:0>9}", r);
        serializer.serialize_str(s.trim_end_matches('0').trim_end_matches('.'))
    }
}

impl<'de> Deserialize<'de> for DecimalJson {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let mut iter = s.split('.');
        let q: u64 = iter
            .next()
            .ok_or_else(|| <D::Error as serde::de::Error>::custom(format!("Invalid string: {s}")))?
            .parse()
            .map_err(<D::Error as serde::de::Error>::custom)?;
        let r_str = iter.next().unwrap_or("0");
        let r: u64 = format!("{:0<9}", r_str)
            .parse()
            .map_err(<D::Error as serde::de::Error>::custom)?;
        if iter.next().is_none() {
            // ensure there isn't more to parse as that is undefined
            Ok(Self(r + MINA_PRECISION * q))
        } else {
            Err(<D::Error as serde::de::Error>::custom(format!(
                "Invalid string: {s}"
            )))
        }
    }
}

/// u32 representing a length (v1)
pub type LengthV1 = Versioned<Versioned<u32, 1>, 1>;
impl_from_for_newtype!(U32Json, LengthV1);

/// u32 representing a delta (i.e. difference) (v1)
pub type DeltaV1 = Versioned<Versioned<u32, 1>, 1>;

/// u32 representing a slot number (v1)
pub type GlobalSlotNumberV1 = Versioned<Versioned<u32, 1>, 1>;

/// u64 representing an amount of currency (v1)
pub type AmountV1 = Versioned<Versioned<u64, 1>, 1>;
impl_from_for_newtype!(U64Json, AmountV1);
impl_from_for_newtype!(DecimalJson, AmountV1);

// FIXME: 255 255 cannot be deserialized to u32, use i32 for now
// Note: Extended_Uint32 is not defined in bin_prot, but comes from mina
// Block path: t/staged_ledger_diff/t/diff/t/0/t/t/commands/0/t/data/t/t/t/t/payload/t/t/common/t/t/t/valid_until
/// u32 wrapped in 1 version byte
pub type ExtendedU32 = Versioned<Versioned<i32, 1>, 1>;
impl From<U32Json> for ExtendedU32 {
    fn from(t: U32Json) -> Self {
        (t.0 as i32).into()
    }
}

impl From<ExtendedU32> for U32Json {
    fn from(t: ExtendedU32) -> Self {
        Self(t.t.t as u32)
    }
}

/// u64 wrapped in 1 version byte
pub type ExtendedU64 = Versioned<u64, 1>;

/// u64 wrapped in 2 version bytes
pub type ExtendedU64_2 = Versioned<ExtendedU64, 1>;

/// u64 wrapped in 3 version bytes
pub type ExtendedU64_3 = Versioned<ExtendedU64_2, 1>;

/// Versioned 64 bytes
pub type Hex64V1 = Versioned<i64, 1>;
impl_from_for_newtype!(I64, Hex64V1);

/// char (v1)
pub type CharV1 = Versioned<u8, 1>;
impl_from_for_newtype!(CharJson, CharV1);

/// char (json)
#[derive(Debug, Clone, Eq, PartialEq, From, Into)]
pub struct CharJson(pub u8);

impl Serialize for CharJson {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = unsafe { String::from_utf8_unchecked(vec![self.0]) };
        serializer.serialize_str(&s)
    }
}

impl<'de> Deserialize<'de> for CharJson {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Ok(s.as_bytes()[0].into())
    }
}

/// 32 bytes representing a BigInt256
pub type BigInt256 = [u8; 32];

/// Wrapper of Vec<u8>
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, derive_more::From)]
pub struct ByteVec(pub Vec<u8>);

impl_from_versioned!(ByteVec);

/// Wrapper of Vec<u8> (json)
#[derive(Debug, Clone, Eq, PartialEq, From, Into, AutoFrom)]
#[auto_from(ByteVec)]
pub struct ByteVecJson(pub Vec<u8>);

impl Serialize for ByteVecJson {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = unsafe { String::from_utf8_unchecked(self.0.clone()) };
        serializer.serialize_str(&s)
    }
}

impl<'de> Deserialize<'de> for ByteVecJson {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let v: Vec<u8> = s.as_bytes().to_vec();
        Ok(Self(v))
    }
}

/// Vector of bytes with a version number. Also encodes its own length when encoded using bin-prot
pub type ByteVecV1 = Versioned<ByteVec, 1>;

/// A wrapper of versioned type that is base58 encodable
#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)]
pub struct Base58EncodableType<const VERSION_BYTE: u8, T>(pub T);

impl<const VERSION_BYTE: u8, T> Base58EncodableType<VERSION_BYTE, T>
where
    T: From<Vec<u8>>,
{
    /// Decode input base58 encoded bytes into [Base58EncodableType]
    pub fn from_base58(input: impl AsRef<[u8]>) -> Result<Self, crate::errors::Error> {
        let bytes: Vec<u8> = bs58::decode(input)
            .with_check(Some(VERSION_BYTE))
            .into_vec()
            .map_err(crate::errors::Error::Base58DecodeError)?;
        // skip the version check byte
        let mut v: Vec<u8> = Vec::with_capacity(bytes.len() - 1);
        v.extend_from_slice(&bytes[1..]);
        Ok(Self(v.into()))
    }
}

impl<const VERSION_BYTE: u8, T> Base58EncodableType<VERSION_BYTE, T>
where
    T: Serialize + AsRef<[u8]>,
{
    /// Encode inner data with version check byte into [String]
    pub fn to_base58_string(&self) -> Result<String, crate::errors::Error> {
        Ok(self.to_base58_builder().into_string())
    }

    /// Encode inner data with version check byte into [EncodeBuilder]
    pub fn to_base58_builder(&self) -> EncodeBuilder<'static, &[u8]> {
        let bytes: &[u8] = self.0.as_ref();
        bs58::encode(bytes).with_check_version(VERSION_BYTE)
    }
}

impl<const VERSION_BYTE: u8, T> From<Base58EncodableType<VERSION_BYTE, T>> for (T,) {
    fn from(i: Base58EncodableType<VERSION_BYTE, T>) -> Self {
        (i.0,)
    }
}

impl<const VERSION_BYTE: u8, T> Serialize for Base58EncodableType<VERSION_BYTE, T>
where
    T: Serialize + AsRef<[u8]>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = self
            .to_base58_string()
            .map_err(<S::Error as serde::ser::Error>::custom)?;
        serializer.serialize_str(&s)
    }
}

impl<'de, const VERSION_BYTE: u8, T> Deserialize<'de> for Base58EncodableType<VERSION_BYTE, T>
where
    T: Deserialize<'de> + From<Vec<u8>>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s =
            String::deserialize(deserializer).map_err(<D::Error as serde::de::Error>::custom)?;
        Self::from_base58(s).map_err(<D::Error as serde::de::Error>::custom)
    }
}

/// A wrapper of versioned type that is base58 encodable with an version byte
#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)]
pub struct Base58EncodableVersionedType<const VERSION_BYTE: u8, T>(pub T);

impl<'de, const VERSION_BYTE: u8, T> Base58EncodableVersionedType<VERSION_BYTE, T>
where
    T: Deserialize<'de>,
{
    /// Decode input base58 encoded bytes into [Base58EncodableVersionedType]
    pub fn from_base58(input: impl AsRef<[u8]>) -> Result<Self, crate::errors::Error> {
        let bytes: Vec<u8> = bs58::decode(input)
            .with_check(Some(VERSION_BYTE))
            .into_vec()
            .map_err(crate::errors::Error::Base58DecodeError)?;
        // skip the version check byte
        let data: T = bin_prot::from_reader_strict(&bytes[1..])
            .map_err(crate::errors::Error::BinProtError)?;
        Ok(Self(data))
    }
}

impl<const VERSION_BYTE: u8, T> Base58EncodableVersionedType<VERSION_BYTE, T>
where
    T: Serialize,
{
    /// Encode inner data with version check byte into [String]
    pub fn to_base58_string(&self) -> Result<String, crate::errors::Error> {
        let builder = self.to_base58_builder()?;
        Ok(builder.into_string())
    }

    /// Encode inner data with version check byte into [EncodeBuilder]
    pub fn to_base58_builder(
        &self,
    ) -> Result<EncodeBuilder<'static, Vec<u8>>, bin_prot::error::Error> {
        let mut buf = Vec::new();
        bin_prot::to_writer(&mut buf, &self.0)?;
        Ok(bs58::encode(buf).with_check_version(VERSION_BYTE))
    }
}

impl<const VERSION_BYTE: u8, T> From<Base58EncodableVersionedType<VERSION_BYTE, T>> for (T,) {
    fn from(i: Base58EncodableVersionedType<VERSION_BYTE, T>) -> Self {
        (i.0,)
    }
}

impl<const VERSION_BYTE: u8, T> Serialize for Base58EncodableVersionedType<VERSION_BYTE, T>
where
    T: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = self
            .to_base58_string()
            .map_err(<S::Error as serde::ser::Error>::custom)?;
        serializer.serialize_str(&s)
    }
}

impl<'de, const VERSION_BYTE: u8, T> Deserialize<'de>
    for Base58EncodableVersionedType<VERSION_BYTE, T>
where
    T: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s =
            String::deserialize(deserializer).map_err(<D::Error as serde::de::Error>::custom)?;
        Self::from_base58(s).map_err(<D::Error as serde::de::Error>::custom)
    }
}

/// base58 string representation of a hash
pub type HashV1Json<const VERSION_BYTE: u8> = Base58EncodableVersionedType<VERSION_BYTE, HashV1>;

impl<const VERSION_BYTE: u8> From<HashV1Json<VERSION_BYTE>> for HashV1 {
    fn from(i: HashV1Json<VERSION_BYTE>) -> Self {
        let (h,) = i.into();
        h
    }
}

impl<const VERSION_BYTE: u8> From<HashV1Json<VERSION_BYTE>> for Hash2V1 {
    fn from(i: HashV1Json<VERSION_BYTE>) -> Self {
        let v1: HashV1 = i.into();
        v1.into()
    }
}

impl<const VERSION_BYTE: u8> From<Hash2V1> for HashV1Json<VERSION_BYTE> {
    fn from(i: Hash2V1) -> Self {
        i.t.into()
    }
}

/// base58 string representation of a ledger hash
pub type LedgerHashV1Json = HashV1Json<{ version_bytes::LEDGER_HASH }>;

/// base58 string representation of a chain hash
pub type ChainHashV1Json = LedgerHashV1Json;

/// base58 string representation of a coinbase hash
pub type CoinBaseHashV1Json = HashV1Json<{ version_bytes::COINBASE_HASH }>;

/// base58 string representation of a coinbase hash
pub type EpochSeedHashV1Json = HashV1Json<{ version_bytes::EPOCH_SEED }>;

/// base58 string representation of a state hash
pub type StateHashV1Json = HashV1Json<{ version_bytes::STATE_HASH }>;

/// base58 string representation of a state hash
pub type StateBodyHashV1Json = HashV1Json<{ version_bytes::STATE_BODY_HASH }>;

/// base58 string representation of a coinbase stack data hash
pub type CoinBaseStackDataV1Json = HashV1Json<{ version_bytes::COINBASE_STACK_DATA }>;

/// base58 string representation of a coinbase stack hash
pub type CoinBaseStackHashV1Json = HashV1Json<{ version_bytes::COINBASE_STACK_HASH }>;

/// base58 string representation of a vrf output hash
pub type VrfOutputHashV1Json = HashV1Json<{ version_bytes::VRF_TRUNCATED_OUTPUT }>;

/// base58 string representation of a aux hash
pub type AuxHashJson = Base58EncodableType<{ version_bytes::STAGED_LEDGER_HASH_AUX_HASH }, Vec<u8>>;

/// base58 string representation of a pending coinbase aux hash
pub type PendingCoinbaseAuxHashJson =
    Base58EncodableType<{ version_bytes::STAGED_LEDGER_HASH_PENDING_COINBASE_AUX }, Vec<u8>>;

impl<const VERSION_BYTE: u8> From<Base58EncodableType<VERSION_BYTE, Vec<u8>>> for ByteVecV1 {
    fn from(i: Base58EncodableType<VERSION_BYTE, Vec<u8>>) -> Self {
        let bv: ByteVec = i.0.into();
        bv.into()
    }
}

impl<const VERSION_BYTE: u8> From<ByteVecV1> for Base58EncodableType<VERSION_BYTE, Vec<u8>> {
    fn from(i: ByteVecV1) -> Self {
        let bv: ByteVec = i.into();
        bv.0.into()
    }
}