stacks-rs 0.2.5

A minimal rust toolkit to interact with the Stacks Blockchain.
Documentation
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
// © 2024 Max Karou. All Rights Reserved.
// Licensed under Apache Version 2.0, or MIT License, at your discretion.
//
// Apache License: http://www.apache.org/licenses/LICENSE-2.0
// MIT License: http://opensource.org/licenses/MIT
//
// Usage of this file is permitted solely under a sanctioned license.

use std::fmt::Debug;
use std::fmt::Display;

use dyn_clone::clone_trait_object;
use dyn_clone::DynClone;

use crate::clarity;
use crate::clarity::macros::impl_clarity_primitive;
use crate::clarity::Cast;
use crate::clarity::Clarity;
use crate::clarity::Codec;
use crate::clarity::LengthPrefixedStr;
use crate::clarity::PrincipalContract;
use crate::clarity::PrincipalStandard;
use crate::clarity::CLARITY_TYPE_NON_STD;

/// The standard STX condition type.
pub(crate) const POST_CONDITION_TYPE_STX: u8 = 0x00;
/// The fungible condition type.
pub(crate) const POST_CONDITION_TYPE_FUNGIBLE: u8 = 0x01;
/// The non-fungible condition type.
pub(crate) const POST_CONDITION_TYPE_NON_FUNGIBLE: u8 = 0x02;
/// The standard principal type.
pub(crate) const POST_CONDITION_PRINCIPAL_STD: u8 = 0x02;
/// The contract principal type.
pub(crate) const POST_CONDITION_PRINCIPAL_CON: u8 = 0x03;

/// Convenience macro for creating post-conditions.
#[macro_export]
macro_rules! post_condition {
    (@box $e:expr) => (Box::new($e) as Box<dyn $crate::transaction::Condition>);
    () => ($crate::transaction::PostConditions::new(Vec::new()));
    ($(($type:ident, $($args:tt)*)),* $(,)?) => {{
        let mut tmp = Vec::new();
        $(tmp.push($crate::post_condition!(@gen $type, $($args)*));)*
        $crate::transaction::PostConditions::new(tmp)
    }};
    ($type:ident, $($args:tt)*) => ($crate::post_condition!(@gen $type, $($args)*));
    (@gen STXCondition, $($args:tt)*) => ($crate::post_condition!(@box $crate::transaction::STXPostCondition::new($($args)*)));
    (@gen FungibleCondition, $($args:tt)*) => ($crate::post_condition!(@box $crate::transaction::FungiblePostCondition::new($($args)*)));
    (@gen NonFungibleCondition, $($args:tt)*) => ($crate::post_condition!(@box $crate::transaction::NonFungiblePostCondition::new($($args)*)));
}

/// Marker trait for post-conditions.
pub trait Condition: Codec + DynClone + Debug {}
clone_trait_object!(Condition);

/// The post-condition code.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConditionCode {
    /// Equal condition.
    EQ = 0x01,
    /// Greater than condition.
    GT = 0x02,
    /// Greater than or equal condition.
    GTE = 0x03,
    /// Less than condition.
    LT = 0x04,
    /// Less than or equal condition.
    LTE = 0x05,
    /// Has not or doesn't own condition. (Non-Fungible)
    HasNot = 0x10,
    /// Has or owns condition. (Non-Fungible)
    Has = 0x11,
}

/// The post-condition mode.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PostConditionMode {
    /// Allow mode, this turns off all post-conditions.
    Allow = 0x01,
    /// Deny mode, this turns on all post-conditions.
    Deny = 0x02,
}

impl_clarity_primitive!(
    PostConditions,
    Vec<Box<dyn Condition>>,
    CLARITY_TYPE_NON_STD
);

impl Codec for PostConditions {
    fn encode(&self) -> Result<Vec<u8>, clarity::Error> {
        let mut buff = vec![];
        buff.extend_from_slice(&u32::try_from(self.__value.len())?.to_be_bytes());

        for value in &self.__value {
            buff.extend_from_slice(&value.encode()?);
        }

        Ok(buff)
    }

    #[allow(unused_variables)]
    fn decode(bytes: &[u8]) -> Result<Self, clarity::Error>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}

impl Default for PostConditions {
    fn default() -> Self {
        PostConditions::new(Vec::default())
    }
}

impl Display for PostConditions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.__value)
    }
}

impl Debug for PostConditions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.__value)
    }
}

impl Clone for PostConditions {
    fn clone(&self) -> Self {
        Self::new(self.__value.clone())
    }
}

impl IntoIterator for PostConditions {
    type IntoIter = std::vec::IntoIter<Self::Item>;
    type Item = Box<dyn Condition>;

    fn into_iter(self) -> Self::IntoIter {
        self.__value.into_iter()
    }
}

/// The post-condition for native STX tokens.
#[derive(Debug, Clone)]
pub struct STXPostCondition {
    /// The principal address of the condition.
    address: Box<dyn Clarity>,
    /// The amount of the condition.
    amount: u64,
    /// The condition code of the condition.
    code: ConditionCode,
}

impl STXPostCondition {
    /// Creates a new `STXPostCondition`.
    pub fn new<T>(address: T, amount: u64, code: ConditionCode) -> Self
    where
        T: Clarity,
    {
        Self {
            address: Box::new(address),
            amount,
            code,
        }
    }
}

impl Codec for STXPostCondition {
    fn encode(&self) -> Result<Vec<u8>, clarity::Error> {
        let mut buff = vec![POST_CONDITION_TYPE_STX];

        if let Ok(addr) = self.address.cast_as::<PrincipalStandard>() {
            buff.push(POST_CONDITION_PRINCIPAL_STD);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else if let Ok(addr) = self.address.cast_as::<PrincipalContract>() {
            buff.push(POST_CONDITION_PRINCIPAL_CON);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else {
            return Err(clarity::Error::BadDowncast);
        }

        buff.push(self.code as u8);
        buff.extend_from_slice(&self.amount.to_be_bytes());
        Ok(buff)
    }

    #[allow(unused_variables)]
    fn decode(bytes: &[u8]) -> Result<Self, clarity::Error>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}

impl Condition for STXPostCondition {}

/// The post-condition for fungible tokens.
#[derive(Debug, Clone)]
pub struct FungiblePostCondition {
    /// The principal address of the condition.
    address: Box<dyn Clarity>,
    /// The amount of the condition.
    amount: u64,
    /// The condition code of the condition.
    code: ConditionCode,
    /// The asset info of the condition.
    info: AssetInfo,
}

impl FungiblePostCondition {
    /// Creates a new `FungiblePostCondition`.
    pub fn new<T>(address: T, amount: u64, code: ConditionCode, info: AssetInfo) -> Self
    where
        T: Clarity,
    {
        Self {
            address: Box::new(address),
            amount,
            code,
            info,
        }
    }
}

impl Codec for FungiblePostCondition {
    fn encode(&self) -> Result<Vec<u8>, clarity::Error> {
        let mut buff = vec![POST_CONDITION_TYPE_FUNGIBLE];

        if let Ok(addr) = self.address.cast_as::<PrincipalStandard>() {
            buff.push(POST_CONDITION_PRINCIPAL_STD);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else if let Ok(addr) = self.address.cast_as::<PrincipalContract>() {
            buff.push(POST_CONDITION_PRINCIPAL_CON);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else {
            return Err(clarity::Error::BadDowncast);
        }

        buff.extend_from_slice(&self.info.encode()?);
        buff.push(self.code as u8);
        buff.extend_from_slice(&self.amount.to_be_bytes());
        Ok(buff)
    }

    #[allow(unused_variables)]
    fn decode(bytes: &[u8]) -> Result<Self, clarity::Error>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}

impl Condition for FungiblePostCondition {}

/// The post-condition for non-fungible tokens.
#[derive(Debug, Clone)]
pub struct NonFungiblePostCondition {
    /// The principal address of the condition.
    pub address: Box<dyn Clarity>,
    /// A clarity value that names the token instance.
    pub name: Box<dyn Clarity>,
    /// The condition code of the condition.
    pub code: ConditionCode,
    /// The asset info of the condition.
    pub info: AssetInfo,
}

impl NonFungiblePostCondition {
    /// Creates a new `NonFungiblePostCondition`.
    pub fn new<T, S>(address: T, name: S, code: ConditionCode, info: AssetInfo) -> Self
    where
        T: Clarity,
        S: Clarity,
    {
        Self {
            address: Box::new(address),
            name: Box::new(name),
            code,
            info,
        }
    }
}

impl Codec for NonFungiblePostCondition {
    fn encode(&self) -> Result<Vec<u8>, clarity::Error> {
        let mut buff = vec![POST_CONDITION_TYPE_NON_FUNGIBLE];

        if let Ok(addr) = self.address.cast_as::<PrincipalStandard>() {
            buff.push(POST_CONDITION_PRINCIPAL_STD);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else if let Ok(addr) = self.address.cast_as::<PrincipalContract>() {
            buff.push(POST_CONDITION_PRINCIPAL_CON);
            buff.extend_from_slice(&addr.encode()?[1..]);
        } else {
            return Err(clarity::Error::BadDowncast);
        }

        buff.extend_from_slice(&self.info.encode()?);
        buff.extend_from_slice(&self.name.encode()?);
        buff.push(self.code as u8);
        Ok(buff)
    }

    #[allow(unused_variables)]
    fn decode(bytes: &[u8]) -> Result<Self, clarity::Error>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}

impl Condition for NonFungiblePostCondition {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssetInfo {
    /// The principal address of the asset.
    address: PrincipalStandard,
    /// The contract name of the asset.
    name: LengthPrefixedStr,
    /// The name of the asset.
    asset: LengthPrefixedStr,
}

impl AssetInfo {
    pub fn new<T>(address: T, name: T, asset: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            address: PrincipalStandard::new(address.into()),
            name: LengthPrefixedStr::new(name.into()),
            asset: LengthPrefixedStr::new(asset.into()),
        }
    }
}

impl Codec for AssetInfo {
    fn encode(&self) -> Result<Vec<u8>, clarity::Error> {
        let mut buff = vec![];
        buff.extend_from_slice(&self.address.encode()?[1..]);
        buff.extend_from_slice(&self.name.encode()?);
        buff.extend_from_slice(&self.asset.encode()?);
        Ok(buff)
    }

    #[allow(unused_variables)]
    fn decode(bytes: &[u8]) -> Result<Self, clarity::Error>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crypto::hex::bytes_to_hex;

    #[test]
    fn test_transaction_conditions_encode() {
        let (addr, name, info) = get_test_data();

        let prefixed = clarity!(LengthPrefixedStr, name);

        let conditions = post_condition!(
            (
                STXCondition,
                clarity!(PrincipalStandard, addr),
                1000000,
                ConditionCode::GTE
            ),
            (
                STXCondition,
                clarity!(PrincipalContract, addr, name),
                1000000,
                ConditionCode::GTE
            ),
            (
                NonFungibleCondition,
                clarity!(PrincipalStandard, addr),
                prefixed,
                ConditionCode::Has,
                info.clone()
            ),
            (
                FungibleCondition,
                clarity!(PrincipalContract, addr, name),
                1000000,
                ConditionCode::GTE,
                info.clone()
            )
        );

        let encoded = conditions.encode().unwrap();
        let hex = bytes_to_hex(&encoded);

        let expected = "00000004000216a5d9d331000f5b79578ce56bd157f29a9056f0d60300000000000f4240000316a5d9d331000f5b79578ce56bd157f29a9056f0d604746573740300000000000f4240020216a5d9d331000f5b79578ce56bd157f29a9056f0d616a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d6173736574047465737411010316a5d9d331000f5b79578ce56bd157f29a9056f0d6047465737416a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d61737365740300000000000f4240";
        assert_eq!(hex, expected);
    }

    #[test]
    fn test_transaction_conditions_stx_encode() {
        let (addr, name, _) = get_test_data();

        let std = clarity!(PrincipalStandard, addr);
        let con = clarity!(PrincipalContract, addr, name);

        let std_pc = STXPostCondition::new(std, 1000000, ConditionCode::GTE);

        let std_pc_encoded = std_pc.encode().unwrap();
        let std_pc_hex = bytes_to_hex(&std_pc_encoded);

        let std_pc_expected = "000216a5d9d331000f5b79578ce56bd157f29a9056f0d60300000000000f4240";
        assert_eq!(std_pc_hex, std_pc_expected);

        let con_pc = STXPostCondition::new(con, 1000000, ConditionCode::GTE);

        let con_pc_encoded = con_pc.encode().unwrap();
        let con_pc_hex = bytes_to_hex(&con_pc_encoded);

        let con_pc_expected =
            "000316a5d9d331000f5b79578ce56bd157f29a9056f0d604746573740300000000000f4240";
        assert_eq!(con_pc_hex, con_pc_expected)
    }

    #[test]
    fn test_transaction_conditions_ft_encode() {
        let (addr, name, info) = get_test_data();

        let std = clarity!(PrincipalStandard, addr);
        let con = clarity!(PrincipalContract, addr, name);

        let std_pc = FungiblePostCondition::new(std, 1000000, ConditionCode::EQ, info.clone());

        let std_pc_encoded = std_pc.encode().unwrap();
        let std_pc_hex = bytes_to_hex(&std_pc_encoded);

        let std_pc_expected = "010216a5d9d331000f5b79578ce56bd157f29a9056f0d616a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d61737365740100000000000f4240";
        assert_eq!(std_pc_hex, std_pc_expected);

        let con_pc = FungiblePostCondition::new(con, 1000000, ConditionCode::EQ, info);

        let con_pc_encoded = con_pc.encode().unwrap();
        let con_pc_hex = bytes_to_hex(&con_pc_encoded);

        let con_pc_expected = "010316a5d9d331000f5b79578ce56bd157f29a9056f0d6047465737416a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d61737365740100000000000f4240";
        assert_eq!(con_pc_hex, con_pc_expected)
    }

    #[test]
    fn test_transaction_conditions_nft_encode() {
        let (addr, name, info) = get_test_data();

        let std = clarity!(PrincipalStandard, addr);
        let con = clarity!(PrincipalContract, addr, name);
        let prefixed = clarity!(LengthPrefixedStr, name);

        let std_pc =
            NonFungiblePostCondition::new(std, prefixed.clone(), ConditionCode::Has, info.clone());

        let std_pc_encoded = std_pc.encode().unwrap();
        let std_pc_hex = bytes_to_hex(&std_pc_encoded);

        let std_pc_expected = "020216a5d9d331000f5b79578ce56bd157f29a9056f0d616a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d6173736574047465737411";
        assert_eq!(std_pc_hex, std_pc_expected);

        let con_pc = NonFungiblePostCondition::new(con, prefixed, ConditionCode::Has, info);

        let con_pc_encoded = con_pc.encode().unwrap();
        let con_pc_hex = bytes_to_hex(&con_pc_encoded);

        let con_pc_expected = "020316a5d9d331000f5b79578ce56bd157f29a9056f0d6047465737416a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d6173736574047465737411";
        assert_eq!(con_pc_hex, con_pc_expected)
    }

    #[test]
    fn test_transaction_conditions_info_encode() {
        let (_, _, info) = get_test_data();
        let encoded = info.encode().unwrap();
        let hex = bytes_to_hex(&encoded);
        let expected =
            "16a5d9d331000f5b79578ce56bd157f29a9056f0d60b6d792d636f6e7472616374086d792d6173736574";
        assert_eq!(hex, expected);
    }

    fn get_test_data() -> (String, String, AssetInfo) {
        let addr = "SP2JXKMSH007NPYAQHKJPQMAQYAD90NQGTVJVQ02B";
        let name = "test";

        let info = AssetInfo::new(
            "SP2JXKMSH007NPYAQHKJPQMAQYAD90NQGTVJVQ02B",
            "my-contract",
            "my-asset",
        );

        (addr.to_string(), name.to_string(), info)
    }
}