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
use BlockHash;
use TransactionId;
use ScriptPubKey;

use std::time::SystemTime;
use types::arguments::address::Address;

#[derive(Deserialize, Serialize, Debug)]
pub struct SerializedRawTransaction(pub String);

impl SerializedRawTransaction {

    /// This method sets the locktime of a transaction.
    /// A SerializedRawTransaction upon creation does not have a set locktime.
    /// To earn KMD rewards, it must be set to the current time - 777 seconds in little endian hex.
    /// The first 8 chars of the last 38 chars of the hex string is the place to set the locktime.
    /// NOTE: this only applies to KMD, not its assetchains.
    pub fn set_locktime(&mut self) {
        let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); // todo return result
        let hex_time = format!("{:x}", current_time.as_secs() - 120);

        let mut rev_hexified_time = String::new();

        // hex_time must be little endian
        for i in (0..8).rev().step_by(2) {
            rev_hexified_time.push_str(hex_time.get(i-1..i+1).unwrap());
        }

        let hex = self.0.clone();

        let payload = &hex[0..hex.len() - 38];
        let expiry = &hex[hex.len() - 30..hex.len() - 24];

        let mut result = String::new();
        result.push_str(payload);
        result.push_str(rev_hexified_time.as_str());
        result.push_str(expiry);
        result.push_str("000000000000000000000000");

        self.0 = result;
    }

    pub fn from_hex(hex: String) -> Self {
        SerializedRawTransaction(hex.clone())
    }
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Transaction {
    pub amount: f64,
    pub confirmations: u32,
    pub blockhash: Option<BlockHash>,
    /// Unix Timestamp
    pub blockindex: Option<u64>,
    /// Unix Timestamp
    pub blocktime: Option<u64>,
    pub walletconflicts: Vec<TransactionId>,
    pub expiryheight: u32,
    pub txid: TransactionId,
    /// Unix Timestamp
    pub time: u64,
    /// Unix Timestamp
    pub timereceived: u64,
    pub vjoinsplit: Vec<VJoinsplit>,
    //    pub comment: Option<String>,
//    pub to: Option<String>,
//    #[serde(rename = "bip125-replaceable")]
    /// yes|no|unknown: TODO: Create enum if needed
//    pub bip125_replaceable: String,
    pub details: Vec<Detail>,
    pub hex: String,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Detail {
    pub account: String,
//    pub address: Option<Address>,
    pub address: String,
    // send|receive|immature|generate|orphan TODO: Create enum if needed
    pub category: String,
    pub amount: f64,
    pub fee: Option<f64>,
    pub vout: u32,
    #[serde(rename = "involvesWatchonly")]
    pub involves_watchonly: Option<bool>,
    pub abandoned: Option<bool>,
    pub size: u32,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct RawTransaction {
    pub hex: Option<String>,
    pub overwintered: Option<bool>,
    pub txid: String,
    pub size: Option<u32>,
    pub versiongroupid: Option<String>,
    pub version: u32,
    pub locktime: u64,
    pub expiryheight: Option<u32>,
    pub vin: Vec<Vin>,
    pub vout: Vec<Vout>,
    pub vjoinsplit: Vec<VJoinsplit>,
    pub blockhash: Option<BlockHash>,
    pub confirmations: Option<u32>,
    pub time: Option<u64>,
    pub blocktime: Option<u64>
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Vin {
    pub txid: TransactionId,
    pub vout: u32,
    #[serde(rename = "scriptSig")]
    pub script_sig: ScriptSig,
    pub sequence: u64,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ScriptSig {
    pub asm: String,
    pub hex: String
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Vout {
    pub value: f64,
    pub interest: Option<f64>,
    pub n: u32,
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: ScriptPubKey,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct VJoinsplit {
    pub vpub_old: f64,
    pub vpub_new: f64,
    pub anchor: String,
    pub nullifiers: Vec<String>,
    pub commitments: Vec<String>,
    #[serde(rename = "onetimePubKey")]
    pub onetime_pubkey: String,
    #[serde(rename = "randomSeed")]
    pub random_seed: String,
    pub macs: Vec<String>,
    pub proof: String,
    pub ciphertexts: Vec<String>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct SignedRawTransaction {
    pub hex: String,
    pub complete: bool,
    pub errors: Option<Vec<SignRawTransactionError>>,
}

impl SignedRawTransaction {
    pub fn to_string(&self) -> String {
        self.hex.clone()
    }
}

#[derive(Deserialize, Serialize, Debug)]
pub struct SignRawTransactionError {
    pub txid: String,
    pub vout: u16,
    #[serde(rename = "scriptSig")]
    pub script_sig: String,
    pub sequence: u64,
    pub error: String,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct TransactionOutputDetail {
    pub txid: TransactionId,
    pub vout: u32,
    #[serde(rename = "scriptPubKey")]
    pub script_pub_key: String,
    #[serde(rename = "redeemScript")]
    pub redeem_script: Option<String>, // is hex hash
    pub amount: f64,
}

#[derive(Deserialize, Serialize, Debug)]
pub enum SigHashType {
    #[serde(rename = "ALL")]
    All,
    #[serde(rename = "NONE")]
    None,
    #[serde(rename = "SINGLE")]
    Single,
    #[serde(rename = "ALL|ANYONECANPAY")]
    AllAnyoneCanPay,
    #[serde(rename = "NONE|ANYONECANPAY")]
    NoneAnyoneCanPay,
    #[serde(rename = "SINGLE|ANYONECANPAY")]
    SingleAnyoneCanPay,
}

#[derive(Deserialize, Debug)]
pub struct Unspent {
    pub txid: TransactionId,
    pub vout: u8,
    pub generated: bool,
    pub address: Address,
    pub account: Option<String>, //does not exist for an utxo in a change address
    pub amount: f64,
    pub interest: Option<f64>,
    #[serde(rename = "scriptPubKey")]
    pub script_pub_key: String,
    pub rawconfirmations: u64,
    pub confirmations: u64,
    pub spendable: bool,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct LockedUnspent {
    pub txid: TransactionId,
    pub vout: u8
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ReceivedByAddress {
    pub address: Address,
    pub account: Option<String>, //accounts are deprecated
    pub amount: f64,
    pub rawconfirmations: u64,
    pub confirmations: u64,
    pub txids: Vec<TransactionId>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct TxListSinceBlock {
    pub transactions: Vec<ListTransaction>,
    pub lastblock: String,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ListTransaction {
    pub account: String,
    pub address: Address,
    pub category: TxCategory,
    pub amount: f64,
    pub vout: u32,
    pub fee: Option<f64>,
    pub rawconfirmations: u64,
    pub confirmations: u64,
    pub blockhash: String,
    pub blockindex: u64,
    pub blocktime: u64,
    pub expiryheight: u64,
    pub txid: TransactionId,
    pub walletconflicts: Vec<Option<String>>,
    pub time: u64,
    pub timereceived: u64,
    pub vjoinsplit: Vec<Option<VJoinsplit>>,
    pub size: u32,
    pub comment: Option<String>,
    pub to: Option<String>,
}

#[derive(Deserialize, Serialize, Debug)]
pub enum TxCategory {
    #[serde(rename = "send")]
    Send,
    #[serde(rename = "receive")]
    Receive,
    #[serde(rename = "move")]
    Move
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ListTransactions(Vec<ListTransaction>);

#[derive(Deserialize, Serialize, Debug)]
pub struct ResentWalletTransactions(Vec<TransactionId>);

#[derive(Deserialize, Serialize, Debug)]
pub struct SetPubkey {
    pub pubkey: String,
    pub ismine: bool,
    pub address: Address,
}