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
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt.  If not, see <http://www.gnu.org/licenses/>.

//! Extrinsic decoder

use crate::{
    metadata::{
        env_types::{
            self,
            CustomTypeDecoder,
            EnvTypesTranscoder,
        },
        MetadataPalletCalls,
        PathKey,
    },
    u8_map::U8Map,
    Error,
};
use codec::{
    Compact,
    Decode,
};
use frame_metadata::SignedExtensionMetadata;
use scale_info::{
    form::PortableForm,
    TypeInfo,
};
use scale_value::Value;
use serde::Serialize;
use sp_runtime::{
    AccountId32,
    MultiAddress,
    MultiSignature,
};
use std::{
    collections::HashMap,
    fmt::Debug,
};
// use metadata::Metadata;

/// The result of successfully decoding an extrinsic.
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Extrinsic {
    /// Decoded call data and associated type information about the call.
    pub call_data: CallData,
    /// The signature and signed extensions (if any) associated with the extrinsic
    pub signature: Option<ExtrinsicSignature>,
}

/// Decoded call data and associated type information.
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct CallData {
    /// Pallet name
    pub pallet_name: String,
    /// Function
    pub pallet_fn: String,
    /// Arguments
    pub arguments: Vec<Value<scale_value::scale::TypeId>>,
}

/// The signature information embedded in an extrinsic.
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct ExtrinsicSignature {
    /// Address the extrinsic is being sent from
    #[serde(with = "super::util::RemoteAddress")]
    pub address: MultiAddress<AccountId32, u32>,
    /// Signature to prove validity
    pub signature: MultiSignature,
    /// Signed extensions, which can vary by node. Here, we
    /// return the name and value of each.
    pub extensions: Vec<(String, Value<scale_value::scale::TypeId>)>,
}
/// Pallet name
pub struct DecoderBuilder {
    /// Pallet name
    registry: scale_value::scale::PortableRegistry,
    /// Decoders
    decoders: HashMap<u32, Box<dyn CustomTypeDecoder + Send + Sync>>,
    /// Pallet calls by index
    pallet_calls_by_index: U8Map<MetadataPalletCalls>,
    /// Signed extensions
    signed_extensions: Vec<SignedExtensionMetadata<PortableForm>>,
}

impl DecoderBuilder {
    /// New decoder
    pub fn new(
        registry: scale_value::scale::PortableRegistry,
        pallet_calls_by_index: U8Map<MetadataPalletCalls>,
        signed_extensions: Vec<SignedExtensionMetadata<PortableForm>>,
    ) -> Self {
        Self {
            registry,
            pallet_calls_by_index,
            signed_extensions,
            decoders: HashMap::new(),
        }
    }

    /// Register default custom types decoder
    pub fn with_default_custom_type_decodes(self) -> Self {
        self.register_custom_type_decoder::<tidefi_primitives::AccountId, _>(
            env_types::AccountId,
        )
        .register_custom_type_decoder::<tidefi_primitives::Hash, _>(env_types::Hash)
        .register_custom_type_decoder::<tidefi_primitives::CurrencyId, _>(
            env_types::CurrencyId,
        )
    }

    /// Register custom type decoder
    pub fn register_custom_type_decoder<T, U>(mut self, encoder: U) -> Self
    where
        T: TypeInfo + 'static,
        U: CustomTypeDecoder + 'static + Send + Sync,
    {
        let path_key = PathKey::from_type::<T>();

        let types_by_path = self.registry.types().into_iter().find_map(|portable_ty| {
            let pathkey_src = PathKey::from(portable_ty.ty().path());
            if pathkey_src == path_key {
                Some((portable_ty.id(), portable_ty.ty()))
            } else {
                None
            }
        });

        match types_by_path {
            Some((type_id, _)) => {
                let existing = self.decoders.insert(type_id, Box::new(encoder));
                // log::debug!("Registered custom decoder for type `{:?}`", type_id);
                if existing.is_some() {
                    panic!(
                        "Attempted to register decoder with existing type id {:?}",
                        type_id
                    );
                }
            }
            None => {
                // if the type is not present in the registry, it just means it has not been used.
                // log::info!("No matching type in registry for path {:?}.", path_key);
            }
        }
        self
    }

    /// Build decoder
    pub fn build(self) -> Result<Decoder, Error> {
        let env_types_transcoder = EnvTypesTranscoder::new(self.decoders);
        Ok(Decoder::new(
            self.registry,
            env_types_transcoder,
            self.pallet_calls_by_index,
            self.signed_extensions,
        ))
    }
}

/// Decoder
#[derive(Debug)]
pub struct Decoder {
    /// Registry
    registry: scale_value::scale::PortableRegistry,
    /// Custom decoder
    env_types: EnvTypesTranscoder,
    /// Pallet calls by index
    pallet_calls_by_index: U8Map<MetadataPalletCalls>,
    /// Signed extensions
    signed_extensions: Vec<SignedExtensionMetadata<PortableForm>>,
}

impl Decoder {
    /// New decoder
    pub fn new(
        registry: scale_value::scale::PortableRegistry,
        env_types: EnvTypesTranscoder,
        pallet_calls_by_index: U8Map<MetadataPalletCalls>,
        signed_extensions: Vec<SignedExtensionMetadata<PortableForm>>,
    ) -> Self {
        Self {
            registry,
            env_types,
            pallet_calls_by_index,
            signed_extensions,
        }
    }

    /// Decode extrinsic
    pub fn decode_extrinsic(&self, data: &mut &[u8]) -> Result<Extrinsic, Error> {
        if data.is_empty() {
            return Err(Error::Other(
                "unwrapped extrinsic byte length should be > 0".into(),
            ))
        }

        // Ignore the expected extrinsic length here at the moment, since we know it's 1
        let _len = <Compact<u32>>::decode(data)?;

        // V4 extrinsics (the format we can decode here) are laid out roughly as follows:
        //
        // first byte: abbbbbbb (a = 0 for unsigned, 1 for signed, b = version)
        //
        // signature, which is made up of (in order):
        // - sp_runtime::MultiAddress enum (sender)
        // - sp_runtime::MultiSignature enum
        // - For polkadot, these extensions (but can vary by chain, so we decode generically):
        //   - sp_runtime::generic::Era enum
        //   - compact encoded u32 (nonce; prior transaction count)
        //   - compact encoded u128 (tip paid to block producer/treasury)
        //
        // call, which is made up roughly of:
        // - u8 enum pallet index (for pallets variant)
        // - u8 call index (for inner variant)
        // - call args (types can be pulled from metadata for each arg we expect)
        //
        // So, we start by getting the version/signed from the first byte and go from there.
        let is_signed = data[0] & 0b1000_0000 != 0;
        let version = data[0] & 0b0111_1111;
        *data = &data[1..];

        // We only know how to decode V4 extrinsics at the moment
        if version != 4 {
            return Err(Error::Other(format!("Invalid version {}", version)))
        }

        // If the extrinsic is signed, decode the signature next.
        let signature = match is_signed {
            true => Some(self.decode_signature(data)?),
            false => None,
        };

        // Finally, decode the call data.
        let call_data = self.decode_call_data(data)?;

        Ok(Extrinsic {
            call_data,
            signature,
        })
    }

    fn call_variant_by_enum_index(
        &self,
        pallet: u8,
        call: u8,
    ) -> Option<(&str, &scale_info::Variant<PortableForm>)> {
        self.pallet_calls_by_index.get(pallet).and_then(|p| {
            p.calls.as_ref().and_then(|calls| {
                let type_def_variant = self.get_variant(calls.calls_type_id)?;
                let index = *calls.call_variant_indexes.get(call)?;
                let variant = type_def_variant.variants().get(index)?;
                Some((&*p.name, variant))
            })
        })
    }

    /// A helper function to get hold of a Variant given a type ID, or None if it's not found.
    fn get_variant(
        &self,
        ty: scale_info::interner::UntrackedSymbol<std::any::TypeId>,
    ) -> Option<&scale_info::TypeDefVariant<PortableForm>> {
        self.registry.resolve(ty.id()).and_then(|ty| {
            match ty.type_def() {
                scale_info::TypeDef::Variant(variant) => Some(variant),
                _ => None,
            }
        })
    }

    fn decode_call_data(&self, data: &mut &[u8]) -> Result<CallData, Error> {
        // Pluck out the u8's representing the pallet and call enum next.
        if data.len() < 2 {
            return Err(Error::Other(
                "expected at least 2 more bytes for the pallet/call index".into(),
            )
            .into())
        }
        let pallet_index = u8::decode(data)?;
        let call_index = u8::decode(data)?;

        // Work out which call the extrinsic data represents and get type info for it:
        let (pallet_name, variant) =
            match self.call_variant_by_enum_index(pallet_index, call_index) {
                Some(call) => call,
                None => {
                    return Err(Error::Other(format!(
                        "Unable to find call for pallet: {} call: {}",
                        pallet_index, call_index
                    )))
                }
            };

        // Decode each of the argument values in the extrinsic:
        let arguments = variant
            .fields()
            .iter()
            .map(|field| self.decode(field.ty().id(), data))
            .collect::<Result<Vec<_>, Error>>()?;

        Ok(CallData {
            pallet_name: pallet_name.to_owned(),
            pallet_fn: variant.name().to_owned(),
            arguments,
        })
    }

    fn decode_signature(
        &self,
        data: &mut &[u8],
    ) -> Result<ExtrinsicSignature, Error> {
        let address = <MultiAddress<AccountId32, u32>>::decode(data)?;
        let signature = MultiSignature::decode(data)?;
        let extensions = self.decode_signed_extensions(data)?;

        Ok(ExtrinsicSignature {
            address,
            signature,
            extensions,
        })
    }

    fn decode_signed_extensions(
        &self,
        data: &mut &[u8],
    ) -> Result<Vec<(String, Value<scale_value::scale::TypeId>)>, Error> {
        self.signed_extensions
            .iter()
            .map(|ext| {
                let val = self.decode(ext.ty.id(), data)?;
                let name = ext.identifier.to_owned();
                Ok((name, val))
            })
            .collect()
    }

    /// Decode type id to scale value
    pub fn decode(
        &self,
        type_id: u32,
        input: &mut &[u8],
    ) -> Result<Value<scale_value::scale::TypeId>, Error> {
        match self.env_types.try_decode(type_id, input) {
            // Value was decoded with custom decoder for type.
            Ok(Some(value)) => Ok(value),
            // No custom decoder registered so attempt default decoding.
            Ok(None) => {
                scale_value::scale::decode_as_type(input, type_id, &self.registry)
                    .map_err(Into::into)
            }
            Err(e) => Err(e),
        }
    }
}