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
//! Debug / Display implementations for structures.

use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;

use cadence_json::AddressRef;
use otopr::HasItem;

use crate::access::TransactionResultResponse;
use crate::entities::*;
use crate::transaction::*;

struct Hexes<H>(H);

impl<H> fmt::Debug for Hexes<H>
where
    H: IntoIterator + Copy,
    H::Item: AsRef<[u8]>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.0.into_iter().map(Hex)).finish()
    }
}

struct Hex<H>(H);

impl<H: AsRef<[u8]>> fmt::Debug for Hex<H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for &byte in self.0.as_ref() {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}

struct Addrs<'a>(&'a [Box<[u8]>]);

impl fmt::Debug for Addrs<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(
                self.0
                    .iter()
                    .map(Deref::deref)
                    .map(|data| AddressRef { data }),
            )
            .finish()
    }
}

impl fmt::Debug for Block {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Block")
            .field("id", &Hex(&self.id))
            .field("parent_id", &Hex(&self.parent_id))
            .field("height", &self.height)
            .field("timestamp", &self.timestamp)
            .field("collection_guarantees", &self.collection_guarantees)
            .field("block_seals", &self.block_seals)
            .field("signatures", &Hexes(&*self.signatures))
            .finish()
    }
}

impl fmt::Debug for BlockSeal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BlockSeal")
            .field("block_id", &Hex(&self.block_id))
            .field("execution_receipt_id", &Hex(&self.execution_receipt_id))
            .field(
                "execution_receipt_signatures",
                &Hexes(&*self.execution_receipt_signatures),
            )
            .field(
                "result_approval_signatures",
                &Hexes(&*self.result_approval_signatures),
            )
            .finish()
    }
}

impl fmt::Debug for ProposalKeyD {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProposalKey")
            .field(
                "address",
                &AddressRef {
                    data: &self.address,
                },
            )
            .field("key_id", &self.key_id)
            .field("sequence_number", &self.sequence_number)
            .finish()
    }
}

impl fmt::Debug for SignatureD {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Signature")
            .field(
                "address",
                &AddressRef {
                    data: &self.address,
                },
            )
            .field("key_id", &self.key_id)
            .field("signature", &Hex(&self.signature))
            .finish()
    }
}

impl fmt::Debug for TransactionD {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct Arguments<'a>(&'a [Box<[u8]>]);
        impl fmt::Debug for Arguments<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_list()
                    .entries(self.0.iter().map(|v| String::from_utf8_lossy(v)))
                    .finish()
            }
        }

        f.debug_struct("Transaction")
            .field("script", &String::from_utf8_lossy(&self.script))
            .field("arguments", &Arguments(&self.arguments))
            .field("reference_block_id", &Hex(&self.reference_block_id))
            .field("gas_limit", &self.gas_limit)
            .field("proposal_key", &self.proposal_key)
            .field("payer", &AddressRef { data: &self.payer })
            .field("authorizers", &Addrs(&self.authorizers))
            .field("payload_signatures", &self.payload_signatures)
            .field("envelope_signatures", &self.envelope_signatures)
            .finish()
    }
}

impl fmt::Debug for AccountKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AccountKey")
            .field("index", &self.index)
            .field("public_key", &Hex(&self.public_key))
            .field("sign_algo", &self.sign_algo)
            .field("hash_algo", &self.hash_algo)
            .field("weight", &self.weight)
            .field("sequence_number", &self.sequence_number)
            .field("revoked", &self.revoked)
            .finish()
    }
}

impl fmt::Debug for Account {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct Contracts<'a>(&'a HashMap<String, Box<[u8]>>);

        impl fmt::Debug for Contracts<'_> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_map()
                    .entries(
                        self.0
                            .iter()
                            .map(|(key, value)| (key, String::from_utf8_lossy(value))),
                    )
                    .finish()
            }
        }

        f.debug_struct("Account")
            .field(
                "address",
                &AddressRef {
                    data: &self.address,
                },
            )
            .field("balance", &self.balance)
            .field("code", &self.code)
            .field("keys", &self.keys)
            .field("contracts", &Contracts(&self.contracts))
            .finish()
    }
}

impl fmt::Debug for Collection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Collection")
            .field("id", &Hex(&self.id))
            .field("transactions", &Hexes(&*self.transactions))
            .finish()
    }
}

impl fmt::Debug for CollectionGuarantee {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CollectionGuarantee")
            .field("collection_id", &Hex(&self.collection_id))
            .field("signatures", &Hexes(&*self.signatures))
            .finish()
    }
}

impl fmt::Debug for TransactionResultResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TransactionResultResponse")
            .field("status", &self.status)
            .field("status_code", &self.status_code)
            .field("error_message", &self.error_message)
            .field("events", &self.events)
            .field("block_id", &Hex(&self.block_id))
            .finish()
    }
}

impl fmt::Debug for Event {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut dbg = f.debug_struct("Event");

        dbg.field("ty", &self.ty)
            .field("transaction_id", &Hex(&self.transaction_id))
            .field("transaction_index", &self.transaction_index)
            .field("event_index", &self.event_index);

        if let Ok(v) = self.parse_payload_as_value() {
            dbg.field("payload", &v);
        } else {
            dbg.field("payload", &String::from_utf8_lossy(&self.payload).as_ref());
        }

        dbg.finish()
    }
}

impl<A: AsRef<[u8]>> fmt::Debug for ProposalKeyE<A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProposalKey")
            .field("address", &Hex(self.address.as_ref()))
            .field("key_id", &self.key_id)
            .field("sequence_number", &self.sequence_number)
            .finish()
    }
}

impl<A: AsRef<[u8]>, B: AsRef<[u8]>> fmt::Debug for SignatureE<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Signature")
            .field("address", &Hex(self.address.as_ref()))
            .field("key_id", &self.key_id)
            .field("signature", &Hex(self.signature.as_ref()))
            .finish()
    }
}

struct Arguments<I>(I);

impl<I> fmt::Debug for Arguments<I>
where
    I: IntoIterator + Copy,
    I::Item: AsRef<[u8]>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut f = f.debug_list();

        for e in self.0 {
            f.entry(&String::from_utf8_lossy(e.as_ref()).as_ref());
        }

        f.finish()
    }
}

impl<Args> fmt::Debug for TransactionHeader<Args>
where
    for<'a> &'a Args: IntoIterator,
    for<'a> <&'a Args as IntoIterator>::Item: AsRef<[u8]>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TransactionHeader")
            .field("script", &self.script)
            .field("arguments", &Arguments(&self.arguments))
            .finish()
    }
}

impl<
        Script: AsRef<[u8]>,
        Arguments,
        ReferenceBlockId: AsRef<[u8]>,
        ProposalKeyAddress: AsRef<[u8]>,
        Payer: AsRef<[u8]>,
        Authorizers,
        PayloadSignatures,
        EnvelopeSignatures,
        PayloadSignatureAddress: AsRef<[u8]>,
        PayloadSignature: AsRef<[u8]>,
        EnvelopeSignatureAddress: AsRef<[u8]>,
        EnvelopeSignature: AsRef<[u8]>,
    > fmt::Debug
    for TransactionE<
        Script,
        Arguments,
        ReferenceBlockId,
        ProposalKeyAddress,
        Payer,
        Authorizers,
        PayloadSignatures,
        EnvelopeSignatures,
    >
where
    Arguments: HasItem,
    <Arguments as HasItem>::Item: AsRef<[u8]>,
    for<'a> &'a Arguments: IntoIterator<Item = &'a <Arguments as HasItem>::Item>,

    Authorizers: HasItem,
    <Authorizers as HasItem>::Item: AsRef<[u8]>,
    for<'a> &'a Authorizers: IntoIterator<Item = &'a <Authorizers as HasItem>::Item>,

    PayloadSignatures: HasItem<Item = SignatureE<PayloadSignatureAddress, PayloadSignature>>,
    for<'a> &'a PayloadSignatures:
        IntoIterator<Item = &'a SignatureE<PayloadSignatureAddress, PayloadSignature>>,

    EnvelopeSignatures: HasItem<Item = SignatureE<EnvelopeSignatureAddress, EnvelopeSignature>>,
    for<'a> &'a EnvelopeSignatures:
        IntoIterator<Item = &'a SignatureE<EnvelopeSignatureAddress, EnvelopeSignature>>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        struct Signatures<'a, I>(I, PhantomData<&'a ()>);

        impl<'a, I, SigAddr: AsRef<[u8]> + 'a, Sig: AsRef<[u8]> + 'a> fmt::Debug for Signatures<'a, I>
        where
            I: IntoIterator<Item = &'a SignatureE<SigAddr, Sig>> + Copy,
        {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_list().entries(self.0).finish()
            }
        }

        f.debug_struct("Transaction")
            .field(
                "script",
                &String::from_utf8_lossy(self.script.as_ref()).as_ref(),
            )
            .field("arguments", &Arguments(&self.arguments))
            .field("reference_block_id", &Hex(self.reference_block_id.as_ref()))
            .field("gas_limit", &self.gas_limit)
            .field("proposal_key", &self.proposal_key)
            .field("payer", &Hex(self.payer.as_ref()))
            .field("authorizers", &Hexes(&self.authorizers))
            .field(
                "payload_signatures",
                &Signatures(&self.payload_signatures, PhantomData),
            )
            .field(
                "envelope_signatures",
                &Signatures(&self.envelope_signatures, PhantomData),
            )
            .finish()
    }
}