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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use codec::{Compact, Decode, Encode, Output};

#[cfg(all(feature = "std", feature = "type_info"))]
use scale_info::TypeInfo;

#[cfg(not(feature = "std"))]
use alloc::{format, string::String};
use sp_core::{hashing::blake2_256, H256};
use sp_runtime::{ConsensusEngineId, MultiSignature};
use sp_std::prelude::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::basic_types::{AccountId, GenericAddress};
use crate::*;

pub type TxHash = H256;
pub type BlockHash = H256;
pub type BlockNumber = u32;

#[cfg(feature = "serde")]
pub mod block_number {
  use super::BlockNumber;
  use sp_core::U256;

  pub fn serialize<S>(num: &BlockNumber, s: S) -> Result<S::Ok, S::Error>
  where
    S: serde::Serializer,
  {
    let num = U256::from(*num);
    serde::Serialize::serialize(&num, s)
  }

  pub fn deserialize<'de, D>(d: D) -> Result<BlockNumber, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    let num: U256 = serde::Deserialize::deserialize(d)?;
    Ok(num.as_u32())
  }
}

#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Header {
  pub parent_hash: BlockHash,
  #[cfg_attr(feature = "serde", serde(with = "block_number"))]
  #[codec(compact)]
  pub number: BlockNumber,
  pub state_root: BlockHash,
  pub extrinsics_root: BlockHash,
  pub digest: Digest,
}

impl Header {
  pub fn hash(&self) -> BlockHash {
    H256(self.using_encoded(blake2_256))
  }
}

impl From<Header> for sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256> {
  fn from(header: Header) -> Self {
    let logs = header
      .digest
      .logs
      .into_iter()
      .map(|item| item.into())
      .collect();
    Self {
      parent_hash: header.parent_hash,
      number: header.number,
      state_root: header.state_root,
      extrinsics_root: header.extrinsics_root,
      digest: sp_runtime::generic::Digest { logs },
    }
  }
}

#[derive(Clone, Debug, Default, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Digest {
  pub logs: Vec<DigestItem>,
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "RawDigestItem"))]
#[cfg_attr(feature = "serde", serde(into = "RawDigestItem"))]
pub enum DigestItem {
  PreRuntime(ConsensusEngineId, Vec<u8>),
  Consensus(ConsensusEngineId, Vec<u8>),
  Seal(ConsensusEngineId, Vec<u8>),
  Other(Vec<u8>),
  RuntimeEnvironmentUpdated,
}

impl Encode for DigestItem {
  fn encode_to<T: Output + ?Sized>(&self, output: &mut T) {
    let runtime_era: sp_runtime::generic::DigestItem = self.clone().into();
    runtime_era.encode_to(output)
  }
}

impl Decode for DigestItem {
  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
    let runtime_era = sp_runtime::generic::DigestItem::decode(input)?;
    Ok(runtime_era.into())
  }
}

impl From<sp_runtime::generic::DigestItem> for DigestItem {
  fn from(r_item: sp_runtime::generic::DigestItem) -> Self {
    use sp_runtime::generic::DigestItem::*;
    match r_item {
      PreRuntime(id, data) => Self::PreRuntime(id, data),
      Consensus(id, data) => Self::Consensus(id, data),
      Seal(id, data) => Self::Seal(id, data),
      Other(data) => Self::Other(data),
      RuntimeEnvironmentUpdated => Self::RuntimeEnvironmentUpdated,
    }
  }
}

impl From<DigestItem> for sp_runtime::generic::DigestItem {
  fn from(item: DigestItem) -> Self {
    match item {
      DigestItem::PreRuntime(id, data) => Self::PreRuntime(id, data),
      DigestItem::Consensus(id, data) => Self::Consensus(id, data),
      DigestItem::Seal(id, data) => Self::Seal(id, data),
      DigestItem::Other(data) => Self::Other(data),
      DigestItem::RuntimeEnvironmentUpdated => Self::RuntimeEnvironmentUpdated,
    }
  }
}

impl TryFrom<RawDigestItem> for DigestItem {
  type Error = crate::Error;

  fn try_from(raw: RawDigestItem) -> Result<Self, Self::Error> {
    let item = DigestItem::decode(&mut &raw.0[..])?;
    Ok(item.into())
  }
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RawDigestItem(
  #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec<u8>,
);

impl From<DigestItem> for RawDigestItem {
  fn from(item: DigestItem) -> Self {
    Self(item.encode())
  }
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StorageData(
  #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec<u8>,
);

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StorageKey(
  #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec<u8>,
);

#[derive(Clone, Debug, Default, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AdditionalSigned {
  pub spec_version: u32,
  pub tx_version: u32,
  pub genesis_hash: BlockHash,
  pub current_hash: BlockHash,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(all(feature = "std", feature = "type_info"), derive(TypeInfo))]
pub enum Era {
  Immortal,
  Mortal(u64, u64),
}

impl Era {
  pub fn mortal(current: BlockNumber, period: Option<u64>) -> Self {
    let period = period.unwrap_or(64);
    sp_runtime::generic::Era::mortal(period, current.into()).into()
  }

  pub fn immortal() -> Self {
    Self::Immortal
  }
}

impl Encode for Era {
  fn encode_to<T: Output + ?Sized>(&self, output: &mut T) {
    let runtime_era: sp_runtime::generic::Era = self.clone().into();
    runtime_era.encode_to(output)
  }
}

impl Decode for Era {
  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
    let runtime_era = sp_runtime::generic::Era::decode(input)?;
    Ok(runtime_era.into())
  }
}

impl From<sp_runtime::generic::Era> for Era {
  fn from(e: sp_runtime::generic::Era) -> Self {
    match e {
      sp_runtime::generic::Era::Immortal => Self::Immortal,
      sp_runtime::generic::Era::Mortal(period, phase) => Self::Mortal(period, phase),
    }
  }
}

impl From<Era> for sp_runtime::generic::Era {
  fn from(e: Era) -> Self {
    match e {
      Era::Immortal => Self::Immortal,
      Era::Mortal(period, phase) => Self::Mortal(period, phase),
    }
  }
}

#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Extra {
  era: sp_runtime::generic::Era,
  nonce: Compact<u32>,
  tip: Compact<u128>,
}

impl Extra {
  pub fn new(era: Era, nonce: u32) -> Self {
    Self {
      era: era.into(),
      nonce: nonce.into(),
      tip: 0u128.into(),
    }
  }

  pub fn nonce(&self) -> u32 {
    self.nonce.0
  }

  pub fn tip(&self) -> u128 {
    self.tip.0
  }
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Encoded(
  #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec<u8>,
);

impl Encoded {
  pub fn decode_as<T: Decode>(&self) -> Result<T> {
    Ok(T::decode(&mut &self.0[..])?)
  }
}

impl<T: Encode> From<&T> for Encoded {
  fn from(other: &T) -> Self {
    Self(other.encode())
  }
}

impl Encode for Encoded {
  fn size_hint(&self) -> usize {
    self.0.len()
  }
  fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
    dest.write(&self.0);
  }
}

impl Decode for Encoded {
  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
    if let Some(len) = input.remaining_len()? {
      let mut data = vec![0u8; len];
      input.read(&mut data.as_mut_slice())?;
      Ok(Self(data))
    } else {
      let mut data = Vec::new();
      while let Ok(b) = input.read_byte() {
        data.push(b);
      }
      Ok(Self(data))
    }
  }
}

pub struct SignedPayload<'a>((&'a Encoded, &'a Extra, AdditionalSigned));

impl<'a> SignedPayload<'a> {
  pub fn new(call: &'a Encoded, extra: &'a Extra, additional: AdditionalSigned) -> Self {
    Self((call, extra, additional))
  }
}

impl<'a> Encode for SignedPayload<'a> {
  fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
    self.0.using_encoded(|payload| {
      if payload.len() > 256 {
        f(&blake2_256(payload)[..])
      } else {
        f(payload)
      }
    })
  }
}

/// PreparedTransaction holds all data needed to sign a transaction.
///
/// This can be used for offline signers.
#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PreparedTransaction {
  pub call: Encoded,
  pub extra: Extra,
  pub additional: AdditionalSigned,
  pub account: Option<AccountId>,
}

impl PreparedTransaction {
  pub fn new(
    account: AccountId,
    additional: AdditionalSigned,
    extra: Extra,
    call: Encoded,
  ) -> Self {
    Self {
      account: Some(account),
      additional,
      extra,
      call,
    }
  }

  /// Decode from a `SignedPayload` with optional `AccountId`.
  pub fn decode_signed_payload<Api: ChainApi, I: codec::Input>(input: &mut I) -> Result<Self> {
    let call = Api::RuntimeCall::decode(input)?;
    let extra = Decode::decode(input)?;
    let additional = Decode::decode(input)?;
    // Try decode optional `AccountId`.
    let account = match input.remaining_len()? {
      Some(33) => Decode::decode(input)?,
      _ => None,
    };
    Ok(Self {
      call: Encoded(call.encode()),
      extra,
      additional,
      account,
    })
  }

  pub async fn sign(self, signer: &mut impl Signer) -> Result<ExtrinsicV4> {
    let account = signer.account();
    if let Some(tx_account) = &self.account {
      // Ensure the signer's account matches the transaction.
      if account != *tx_account {
        use sp_core::crypto::Ss58Codec;
        let version = 12u16.into(); // Polymesh
        let a1 = account.to_ss58check_with_version(version);
        let a2 = tx_account.to_ss58check_with_version(version);
        return Err(Error::WrongSignerAccount(a1, a2));
      }
    }
    let payload = SignedPayload::new(&self.call, &self.extra, self.additional);
    let payload = payload.encode();
    let sig = signer.sign(&payload[..]).await?;

    let xt = ExtrinsicV4::signed(account, sig, self.extra, self.call);
    Ok(xt)
  }
}

#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExtrinsicSignature {
  pub account: GenericAddress,
  pub signature: MultiSignature,
  pub extra: Extra,
}

/// Current version of the `UncheckedExtrinsic` format.
pub const EXTRINSIC_VERSION: u8 = 4;

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExtrinsicV4 {
  pub signature: Option<ExtrinsicSignature>,
  pub call: Encoded,
}

impl ExtrinsicV4 {
  pub fn tx_hash(tx: &[u8]) -> TxHash {
    H256(blake2_256(tx))
  }

  pub fn signed(account: AccountId, sig: MultiSignature, extra: Extra, call: Encoded) -> Self {
    Self {
      signature: Some(ExtrinsicSignature {
        account: GenericAddress::from(account),
        signature: sig,
        extra,
      }),
      call,
    }
  }

  pub fn unsigned(call: Encoded) -> Self {
    Self {
      signature: None,
      call,
    }
  }

  pub fn as_hex_and_hash(&self) -> (String, TxHash) {
    let tx = self.encode();
    let tx_hash = Self::tx_hash(tx.as_slice());
    let mut tx_hex = hex::encode(tx);
    tx_hex.insert_str(0, "0x");
    (tx_hex, tx_hash)
  }

  pub fn to_hex(&self) -> String {
    let mut hex = hex::encode(self.encode());
    hex.insert_str(0, "0x");
    hex
  }
}

impl Encode for ExtrinsicV4 {
  fn encode(&self) -> Vec<u8> {
    let mut buf = Vec::with_capacity(512);

    // 1 byte version id and signature if signed.
    match &self.signature {
      Some(sig) => {
        buf.push(EXTRINSIC_VERSION | 0b1000_0000);
        sig.encode_to(&mut buf);
      }
      None => {
        buf.push(EXTRINSIC_VERSION & 0b0111_1111);
      }
    }
    self.call.encode_to(&mut buf);

    buf.encode()
  }
}

impl Decode for ExtrinsicV4 {
  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
    // Decode Vec length.
    let _len: Compact<u32> = Decode::decode(input)?;
    // Version and signed flag.
    let version = input.read_byte()?;
    let is_signed = version & 0b1000_0000 != 0;
    if (version & 0b0111_1111) != EXTRINSIC_VERSION {
      Err("Invalid EXTRINSIC_VERSION")?;
    }

    let signature = if is_signed {
      Some(ExtrinsicSignature::decode(input)?)
    } else {
      None
    };

    Ok(Self {
      signature,
      call: Decode::decode(input)?,
    })
  }
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub struct AccountInfo {
  pub nonce: u32,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum TransactionStatus {
  Future,
  Ready,
  Broadcast(Vec<String>),
  InBlock(BlockHash),
  Retracted(BlockHash),
  FinalityTimeout(BlockHash),
  Finalized(BlockHash),
  Usurped(TxHash),
  Dropped,
  Invalid,
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct SignedBlock {
  pub block: Block,
  // TODO: Add Justifications field.
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Block {
  extrinsics: Vec<Encoded>,
  header: Header,
}

impl Block {
  pub fn find_extrinsic(&self, xt_hash: TxHash) -> Option<usize> {
    // TODO: Add caching of blocks with extrinsic hashes.
    self
      .extrinsics
      .iter()
      .position(|xt| ExtrinsicV4::tx_hash(xt.0.as_slice()) == xt_hash)
  }

  pub fn extrinsics(&self) -> &[Encoded] {
    self.extrinsics.as_slice()
  }

  pub fn parent(&self) -> BlockHash {
    self.header.parent_hash
  }

  pub fn state_root(&self) -> BlockHash {
    self.header.state_root
  }

  pub fn extrinsics_root(&self) -> BlockHash {
    self.header.extrinsics_root
  }

  pub fn block_number(&self) -> BlockNumber {
    self.header.number
  }

  pub fn to_string(&self) -> String {
    format!("{:?}", self)
  }
}

#[derive(Clone, Debug, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Phase {
  ApplyExtrinsic(u32),
  Finalization,
  Initialization,
}

#[derive(Clone, Debug, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EventRecord<Event> {
  pub phase: Phase,
  pub event: Event,
  pub topics: Vec<BlockHash>,
}

impl<Event: RuntimeEnumTraits> EventRecord<Event> {
  pub fn name(&self) -> &'static str {
    self.event.as_name()
  }

  pub fn short_doc(&self) -> &'static str {
    self.event.as_short_doc()
  }

  pub fn docs(&self) -> &'static [&'static str] {
    self.event.as_docs()
  }

  pub fn to_string(&self) -> String {
    format!("{:#?}", self)
  }
}

#[derive(Clone, Debug, Decode, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EventRecords<Event>(pub Vec<EventRecord<Event>>);

impl<Event: RuntimeEnumTraits> EventRecords<Event> {
  pub fn from_vec(mut events: Vec<EventRecord<Event>>, filter: Option<Phase>) -> Self {
    if let Some(filter) = filter {
      events.retain(|ev| ev.phase == filter);
    }
    Self(events)
  }

  pub fn to_string(&self) -> String {
    format!("{:#?}", self.0)
  }
}