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
use crate::account::AccountFlags;
use crate::crypto::{MuxedAccount, PublicKey};
use crate::error::{Error, Result};
use crate::operations::Operation;
use crate::signature::Signer;
use crate::xdr;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetOptionsOperation {
    source_account: Option<MuxedAccount>,
    inflation_destination: Option<PublicKey>,
    clear_flags: Option<AccountFlags>,
    set_flags: Option<AccountFlags>,
    master_weight: Option<u32>,
    low_threshold: Option<u32>,
    medium_threshold: Option<u32>,
    high_threshold: Option<u32>,
    home_domain: Option<String>,
    signer: Option<Signer>,
}

#[derive(Debug, Default)]
pub struct SetOptionsOperationBuilder {
    source_account: Option<MuxedAccount>,
    inflation_destination: Option<PublicKey>,
    clear_flags: Option<AccountFlags>,
    set_flags: Option<AccountFlags>,
    master_weight: Option<u32>,
    low_threshold: Option<u32>,
    medium_threshold: Option<u32>,
    high_threshold: Option<u32>,
    home_domain: Option<String>,
    signer: Option<Signer>,
}

impl SetOptionsOperation {
    /// Retrieves the operation source account.
    pub fn source_account(&self) -> &Option<MuxedAccount> {
        &self.source_account
    }

    /// Retrieves a reference to the operation source account.
    pub fn source_account_mut(&mut self) -> &mut Option<MuxedAccount> {
        &mut self.source_account
    }

    /// Retrieves the operation inflation destination.
    pub fn inflation_destination(&self) -> &Option<PublicKey> {
        &self.inflation_destination
    }

    /// Retrieves a mutable reference to the operation inflation destination.
    pub fn inflation_destination_mut(&mut self) -> &mut Option<PublicKey> {
        &mut self.inflation_destination
    }

    /// Retrieves the operation clear flags.
    pub fn clear_flags(&self) -> &Option<AccountFlags> {
        &self.clear_flags
    }

    /// Retrieves a mutable reference to the operation clear flags.
    pub fn clear_flags_mut(&mut self) -> &mut Option<AccountFlags> {
        &mut self.clear_flags
    }

    /// Retrieves the operation set flags.
    pub fn set_flags(&self) -> &Option<AccountFlags> {
        &self.set_flags
    }

    /// Retrieves a mutable reference to the operation set flags.
    pub fn set_flags_mut(&mut self) -> &mut Option<AccountFlags> {
        &mut self.set_flags
    }

    /// Retrieves the operation master weight.
    pub fn master_weight(&self) -> &Option<u32> {
        &self.master_weight
    }

    /// Retrieves a mutable reference to the operation master weight.
    pub fn master_weight_mut(&mut self) -> &mut Option<u32> {
        &mut self.master_weight
    }

    /// Retrieves the operation low threshold.
    pub fn low_threshold(&self) -> &Option<u32> {
        &self.low_threshold
    }

    /// Retrieves a mutable reference to the operation low threshold.
    pub fn low_threshold_mut(&mut self) -> &mut Option<u32> {
        &mut self.low_threshold
    }

    /// Retrieves the operation medium threshold.
    pub fn medium_threshold(&self) -> &Option<u32> {
        &self.medium_threshold
    }

    /// Retrieves a mutable reference to the operation medium threshold.
    pub fn medium_threshold_mut(&mut self) -> &mut Option<u32> {
        &mut self.medium_threshold
    }

    /// Retrieves the operation high threshold.
    pub fn high_threshold(&self) -> &Option<u32> {
        &self.high_threshold
    }

    /// Retrieves a mutable reference to the operation high threshold.
    pub fn high_threshold_mut(&mut self) -> &mut Option<u32> {
        &mut self.high_threshold
    }

    /// Retrieves the operation home domain.
    pub fn home_domain(&self) -> &Option<String> {
        &self.home_domain
    }

    /// Retrieves a mutable reference to the operation home domain.
    pub fn home_domain_mut(&mut self) -> &mut Option<String> {
        &mut self.home_domain
    }

    /// Retrieves the operation signer.
    pub fn signer(&self) -> &Option<Signer> {
        &self.signer
    }

    /// Retrieves a mutable reference the operation signer.
    pub fn signer_mut(&mut self) -> &mut Option<Signer> {
        &mut self.signer
    }

    /// Returns the xdr operation body.
    pub fn to_xdr_operation_body(&self) -> Result<xdr::OperationBody> {
        let inflation_dest = self
            .inflation_destination
            .as_ref()
            .map(|d| d.to_xdr_account_id())
            .transpose()?;
        let clear_flags = self.clear_flags.map(|f| xdr::Uint32::new(f.bits()));
        let set_flags = self.set_flags.map(|f| xdr::Uint32::new(f.bits()));
        let master_weight = self.master_weight.map(xdr::Uint32::new);
        let low_threshold = self.low_threshold.map(xdr::Uint32::new);
        let med_threshold = self.medium_threshold.map(xdr::Uint32::new);
        let high_threshold = self.high_threshold.map(xdr::Uint32::new);
        let signer = self.signer.as_ref().map(|s| s.to_xdr()).transpose()?;

        if let Some(home_domain) = &self.home_domain {
            if home_domain.len() > 32 {
                return Err(Error::HomeDomainTooLong);
            }
        }

        let home_domain = self
            .home_domain
            .as_ref()
            .map(|h| xdr::String32::new(h.to_string()));

        let inner = xdr::SetOptionsOp {
            inflation_dest,
            clear_flags,
            set_flags,
            master_weight,
            low_threshold,
            med_threshold,
            high_threshold,
            home_domain,
            signer,
        };

        Ok(xdr::OperationBody::SetOptions(inner))
    }

    /// Creates from the xdr operation body.
    pub fn from_xdr_operation_body(
        source_account: Option<MuxedAccount>,
        x: &xdr::SetOptionsOp,
    ) -> Result<SetOptionsOperation> {
        let inflation_destination = x
            .inflation_dest
            .as_ref()
            .map(|d| PublicKey::from_xdr_account_id(d))
            .transpose()?;
        let clear_flags = x
            .clear_flags
            .as_ref()
            .map(|f| AccountFlags::from_bits(f.value).ok_or(Error::InvalidAccountFlags))
            .transpose()?;
        let set_flags = x
            .set_flags
            .as_ref()
            .map(|f| AccountFlags::from_bits(f.value).ok_or(Error::InvalidAccountFlags))
            .transpose()?;
        let master_weight = x.master_weight.as_ref().map(|w| w.value);
        let low_threshold = x.low_threshold.as_ref().map(|w| w.value);
        let medium_threshold = x.med_threshold.as_ref().map(|w| w.value);
        let high_threshold = x.high_threshold.as_ref().map(|w| w.value);
        let home_domain = x.home_domain.as_ref().map(|h| h.value.clone());
        let signer = x
            .signer
            .as_ref()
            .map(|s| Signer::from_xdr(&s))
            .transpose()?;
        Ok(SetOptionsOperation {
            source_account,
            inflation_destination,
            clear_flags,
            set_flags,
            master_weight,
            low_threshold,
            medium_threshold,
            high_threshold,
            home_domain,
            signer,
        })
    }
}

impl SetOptionsOperationBuilder {
    pub fn new() -> SetOptionsOperationBuilder {
        Default::default()
    }

    pub fn with_source_account<S>(mut self, source: S) -> SetOptionsOperationBuilder
    where
        S: Into<MuxedAccount>,
    {
        self.source_account = Some(source.into());
        self
    }

    pub fn with_inflation_destination(
        mut self,
        destination: Option<PublicKey>,
    ) -> SetOptionsOperationBuilder {
        self.inflation_destination = destination;
        self
    }

    pub fn with_clear_flags(mut self, flags: Option<AccountFlags>) -> SetOptionsOperationBuilder {
        self.clear_flags = flags;
        self
    }

    pub fn with_set_flags(mut self, flags: Option<AccountFlags>) -> SetOptionsOperationBuilder {
        self.set_flags = flags;
        self
    }

    pub fn with_master_weight(mut self, weight: Option<u32>) -> SetOptionsOperationBuilder {
        self.master_weight = weight;
        self
    }

    pub fn with_low_threshold(mut self, weight: Option<u32>) -> SetOptionsOperationBuilder {
        self.low_threshold = weight;
        self
    }
    pub fn with_medium_threshold(mut self, weight: Option<u32>) -> SetOptionsOperationBuilder {
        self.medium_threshold = weight;
        self
    }

    pub fn with_high_threshold(mut self, weight: Option<u32>) -> SetOptionsOperationBuilder {
        self.high_threshold = weight;
        self
    }

    pub fn with_signer(mut self, signer: Option<Signer>) -> SetOptionsOperationBuilder {
        self.signer = signer;
        self
    }

    pub fn build(self) -> Result<Operation> {
        Ok(Operation::SetOptions(SetOptionsOperation {
            source_account: self.source_account,
            inflation_destination: self.inflation_destination,
            clear_flags: self.clear_flags,
            set_flags: self.set_flags,
            master_weight: self.master_weight,
            low_threshold: self.low_threshold,
            medium_threshold: self.medium_threshold,
            high_threshold: self.high_threshold,
            home_domain: self.home_domain,
            signer: self.signer,
        }))
    }
}

#[cfg(test)]
mod tests {
    use crate::account::AccountFlags;
    use crate::crypto::KeyPair;
    use crate::network::Network;
    use crate::operations::Operation;
    use crate::signature::{Signer, SignerKey};
    use crate::transaction::{Transaction, TransactionEnvelope, MIN_BASE_FEE};
    use crate::xdr::{XDRDeserialize, XDRSerialize};

    fn keypair0() -> KeyPair {
        // GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3
        KeyPair::from_secret_seed("SBPQUZ6G4FZNWFHKUWC5BEYWF6R52E3SEP7R3GWYSM2XTKGF5LNTWW4R")
            .unwrap()
    }

    fn keypair1() -> KeyPair {
        // GAS4V4O2B7DW5T7IQRPEEVCRXMDZESKISR7DVIGKZQYYV3OSQ5SH5LVP
        KeyPair::from_secret_seed("SBMSVD4KKELKGZXHBUQTIROWUAPQASDX7KEJITARP4VMZ6KLUHOGPTYW")
            .unwrap()
    }

    fn keypair2() -> KeyPair {
        // GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H
        KeyPair::from_secret_seed("SBZVMB74Z76QZ3ZOY7UTDFYKMEGKW5XFJEB6PFKBF4UYSSWHG4EDH7PY")
            .unwrap()
    }

    #[test]
    fn test_set_options() {
        let kp = keypair0();
        let kp1 = keypair1();

        let op = Operation::new_set_options()
            .with_inflation_destination(Some(kp1.public_key().clone()))
            .with_set_flags(Some(
                AccountFlags::AUTH_REQUIRED | AccountFlags::AUTH_IMMUTABLE,
            ))
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAFAAAAAQAAAAAlyvHaD8duz+iEXkJUUbsHkklIlH46oMrMMYrt0odkfgAAAAAAAAABAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB6i5yxQAAAEBtYhsjGguNMF06uqEn/cUIdy9eAp/X2jlhTRiVcIGUQJ2U/45eFGXZ8AjgE5P/fWoQYlsUihurccOMwu891EAD";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }

    #[test]
    fn test_set_options_with_source_account() {
        let kp = keypair0();
        let kp2 = keypair2();

        let op = Operation::new_set_options()
            .with_source_account(kp2.public_key().clone())
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAEAAAAAfhHLNNY19eGrAtSgLD3VpaRm2AjNjxIBWQg9zS4VWZgAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHqLnLFAAAAQBf+wJNmYicge0JOI5iRVzprRG7AXpfQWCHRCIjqiXvJ0MRv71eSyPdJgUVlcStKM8prTF2TPuO8uWPk2kIRKAo=";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }

    #[test]
    fn test_set_options_with_set_flags() {
        let kp = keypair0();

        let op = Operation::new_set_options()
            .with_set_flags(Some(
                AccountFlags::AUTH_REQUIRED | AccountFlags::AUTH_IMMUTABLE,
            ))
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAABAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB6i5yxQAAAECKOes75G40HX5JiVxydn+pu/DTZSGRf0A9eKdDXdS3Znog4kDjnw0vgZ7efMGl8NYW165N13sBub8Dnrc1E+MA";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }

    #[test]
    fn test_set_options_with_clear_flags() {
        let kp = keypair0();

        let op = Operation::new_set_options()
            .with_clear_flags(Some(
                AccountFlags::AUTH_REQUIRED | AccountFlags::AUTH_IMMUTABLE,
            ))
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAFAAAAAAAAAAEAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB6i5yxQAAAEAXg+D6BNKJyRzCHu2np0PSWoAcFanuZa2gfS8a1iAB62buUwxezc/RULixb5W2rQwBxbSyaIrFA/3QJBf480UA";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }

    #[test]
    fn test_set_options_with_weights() {
        let kp = keypair0();

        let op = Operation::new_set_options()
            .with_master_weight(Some(1))
            .with_low_threshold(Some(2))
            .with_medium_threshold(Some(3))
            .with_high_threshold(Some(4))
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAQAAAAEAAAABAAAAAgAAAAEAAAADAAAAAQAAAAQAAAAAAAAAAAAAAAAAAAAB6i5yxQAAAECfQ+WZfpgizILpZL84nvzoDM5+JMQOlA0+9FQZjj6Xr+njvLP/84HFz+lgK3/orX/1MdoBQb61sybrfC1kjdcA";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }

    #[test]
    fn test_set_options_with_signer() {
        let kp = keypair0();
        let kp2 = keypair2();

        let signer_key = SignerKey::Ed25519(kp2.public_key().clone());
        let signer = Signer::new(signer_key, 8);

        let op = Operation::new_set_options()
            .with_signer(Some(signer))
            .build()
            .unwrap();
        let mut tx = Transaction::builder(kp.public_key().clone(), 3556091187167235, MIN_BASE_FEE)
            .add_operation(op)
            .into_transaction()
            .unwrap();
        tx.sign(&kp, &Network::new_test()).unwrap();
        let envelope = tx.to_envelope();
        let xdr = envelope.xdr_base64().unwrap();
        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAH4RyzTWNfXhqwLUoCw91aWkZtgIzY8SAVkIPc0uFVmYAAAACAAAAAAAAAAB6i5yxQAAAEDlGdxaTcfjFp4ukgepGrUe2ALXJZvDRBIGWw3ROBsQlxFV9kgx2YvszPy4DWtXQNvc3i0KxrUrR+r2liPGr/QJ";
        assert_eq!(expected, xdr);
        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
        assert_eq!(envelope, back);
    }
}