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
use tracing::{error, instrument, trace};

use bech32::ToBase32;
use std::borrow::Borrow;

use crate::avm::parser::Context;
use crate::pvm::parser::input_parser::SECP256KTransferInput;
use crate::utils::conversion::{pop_i32, pop_i64};
use std::error::Error;

#[derive(Serialize, Deserialize, Debug)]
pub struct Output {
    pub type_id: i32,
    pub stakeable_locked_output: Option<StakeableLockedOutput>,
    pub secp256k1_transfer_output: Option<SECP256KTransferOutput>,
    pub secp256k1_owner_output: Option<SECP256KTransferOutput>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StakeableLockedInput {
    pub locktime: i64,
    pub asset_id: String,
    pub input: SECP256KTransferInput,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StakeableLockedOutput {
    pub locktime: i64,
    pub secp256k_transfer_output: Option<SECP256KTransferOutput>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SECP256KTransferOutput {
    pub type_id: i32,
    pub amount: Option<i64>,
    pub locktime: i64,
    pub threshold: i32,
    pub addresses: Vec<String>,
}

#[instrument(skip(_raw_msg), fields(tx_id = % _context.tx_id))]
pub fn output_parser(_raw_msg: &[u8], _context: &mut Context) -> Result<Output, Box<dyn Error>> {
    // Type Id
    let type_id = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());

    *_context.offset += 4;

    let mut secp256k1_transfer_output = None;
    let mut secp256k1_owner_output = None;
    let mut stakeable_locked_output = None;

    match type_id {
        7 => {
            secp256k1_transfer_output = Some(secp256k1_transfer_output_parser(_raw_msg, _context)?)
        }
        11 => {
            secp256k1_owner_output = Some(secp256k1_output_owner_output_parser(_raw_msg, _context)?)
        }
        22 => stakeable_locked_output = Some(stackeable_lockout_parser(_raw_msg, _context)?),
        _ => {
            error!("{} type_id for output is not valid or not yet supported ! \n offset : {:?}, \n raw message : {:?}", type_id, _context.offset, _raw_msg);
            panic!("NOT SUPPORTED")
        }
    }

    Ok(Output {
        type_id,
        stakeable_locked_output,
        secp256k1_transfer_output,
        secp256k1_owner_output,
    })
}

#[instrument(skip(_raw_msg), fields(tx_id = % _context.tx_id))]
pub fn secp256k1_transfer_output_parser(
    _raw_msg: &[u8],
    _context: &mut Context,
) -> Result<SECP256KTransferOutput, Box<dyn Error>> {
    // Amount
    let amount = pop_i64(_raw_msg[*_context.offset..=(*_context.offset + 7)].borrow());
    trace!("Amount : {:?}", amount);
    *_context.offset += 8;

    // Locktime
    let locktime = pop_i64(_raw_msg[*_context.offset..=(*_context.offset + 7)].borrow());
    trace!("Locktime : {:?}", locktime);
    *_context.offset += 8;

    // Threshold
    let threshold = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());
    trace!("Threshold : {:?}", threshold);
    *_context.offset += 4;

    // Number of addresses
    let number_of_address = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());
    trace!("Number of addresses : {:?}", number_of_address);
    *_context.offset += 4;

    // Addresses
    let mut index = 0;
    let mut addresses = Vec::new();

    while index < number_of_address {
        let address = format!(
            "X-{}",
            bech32::encode(
                _context.network_name.as_str(),
                _raw_msg[*_context.offset..=(*_context.offset + 19)]
                    .to_vec()
                    .to_base32()
            )?
        );
        trace!("Addresses number {} {:?}", index, address);
        addresses.push(address);
        *_context.offset += 20;
        index += 1;
    }

    Ok(SECP256KTransferOutput {
        type_id: 7,
        amount: Some(amount),
        locktime,
        threshold,
        addresses,
    })
}

#[instrument(skip(_raw_msg), fields(tx_id = % _context.tx_id))]
pub fn secp256k1_output_owner_output_parser(
    _raw_msg: &[u8],
    _context: &mut Context,
) -> Result<SECP256KTransferOutput, Box<dyn Error>> {
    // Locktime
    let locktime = pop_i64(_raw_msg[*_context.offset..=(*_context.offset + 7)].borrow());
    trace!("Locktime : {:?}", locktime);
    *_context.offset += 8;

    // Threshold
    let threshold = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());
    trace!("Threshold : {:?}", threshold);
    *_context.offset += 4;

    // Number of addresses
    let number_of_address = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());
    trace!("Number of addresses : {:?}", number_of_address);
    *_context.offset += 4;

    // Addresses
    let mut index = 0;
    let mut addresses = Vec::new();

    while index < number_of_address {
        let address = format!(
            "X-{}",
            bech32::encode(
                _context.network_name.as_str(),
                _raw_msg[*_context.offset..=(*_context.offset + 19)]
                    .to_vec()
                    .to_base32()
            )?
        );
        trace!("Addresses number {} {:?}", index, address);
        addresses.push(address);
        *_context.offset += 20;
        index += 1;
    }

    Ok(SECP256KTransferOutput {
        type_id: 11,
        amount: None,
        locktime,
        threshold,
        addresses,
    })
}

#[instrument(skip(_raw_msg), fields(tx_id = % _context.tx_id))]
pub fn stackeable_lockout_parser(
    _raw_msg: &[u8],
    _context: &mut Context,
) -> Result<StakeableLockedOutput, Box<dyn Error>> {
    // Locktime
    let locktime = pop_i64(_raw_msg[*_context.offset..=(*_context.offset + 7)].borrow());
    trace!("Locktime : {:?}", locktime);
    *_context.offset += 8;

    let type_id = pop_i32(_raw_msg[*_context.offset..=(*_context.offset + 3)].borrow());
    trace!("Type Id : {:?}", type_id);
    *_context.offset += 4;

    let output;

    match type_id {
        7 => output = Some(secp256k1_transfer_output_parser(_raw_msg, _context)?),
        11 => output = Some(secp256k1_output_owner_output_parser(_raw_msg, _context)?),
        _ => panic!(
            "This output type is incorrect or not yet supported {}",
            type_id
        ),
    }

    Ok(StakeableLockedOutput {
        locktime,
        secp256k_transfer_output: output,
    })
}