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
use std::borrow::Cow;
use std::collections::HashMap;

use cadence_json::{EntryRef, UFix64, ValueRef};
use serde::Serialize;

use crate::algorithms::*;
use crate::multi::PartyBuilder;

/// A `TransactionHeader` is a template plus arguments.
#[derive(Clone, PartialEq, Eq)]
pub struct TransactionHeader<Arguments> {
    /// The script, provided by the template.
    pub script: Cow<'static, str>,
    /// Encoded arguments.
    ///
    /// The number of arguments is determined by the template.
    pub arguments: Arguments,
}

impl<Arguments> TransactionHeader<Arguments> {
    /// Creates a [`PartyBuilder`] and feeds it the transaction header's
    /// script and arguments.
    pub fn into_party_builder(self) -> PartyBuilder
    where
        Arguments: IntoIterator<Item = Box<[u8]>>,
    {
        PartyBuilder::new()
            .script(self.script.into_owned())
            .arguments_raw(self.arguments)
    }
}

/// A builder for [`TransactionHeader`]s.
#[derive(Default)]
pub struct TransactionHeaderBuilder {
    script: Option<Cow<'static, str>>,
    arguments: Vec<Box<[u8]>>,
}

/// A builder for a transaction header.
///
/// ```rust
/// use cadence_json::ValueRef;
/// # use flow_sdk::transaction::{TransactionHeader, TransactionHeaderBuilder};
/// const SCRIPT: &str = r#"
///     transaction(greeting: String) {
///        let guest: Address
///
///        prepare(authorizer: AuthAccount) {
///            self.guest = authorizer.address
///        }
///
///        execute {
///            log(greeting.concat(",").concat(guest.toString()))
///        }
///     }
/// "#;
///
/// let argument = ValueRef::String("Hello");
///
/// let header = TransactionHeaderBuilder::new().script_static(SCRIPT).argument(&argument);
///
/// assert_eq!(header.build(), TransactionHeader {
///     script: SCRIPT.into(),
///     arguments: vec![serde_json::to_vec(&argument).unwrap().into_boxed_slice()]
/// })
/// ```
impl TransactionHeaderBuilder {
    /// Creates a new builder for [`TransactionHeader`]s.
    #[inline]
    pub const fn new() -> Self {
        Self {
            script: None,
            arguments: Vec::new(),
        }
    }

    /// Sets the script of the transaction.
    #[inline]
    pub fn script_static<B: ?Sized + AsRef<str>>(mut self, script: &'static B) -> Self {
        self.script = Some(Cow::Borrowed(script.as_ref()));
        self
    }

    /// Sets the script of the transaction.
    #[inline]
    pub fn script_owned(mut self, script: String) -> Self {
        self.script = Some(Cow::Owned(script));
        self
    }

    /// Clone the script into the builder. Do not use this if you have owned instances or static reference.
    #[inline]
    pub fn script_shared(mut self, script: impl AsRef<str>) -> Self {
        self.script = Some(Cow::Owned(script.as_ref().to_owned()));
        self
    }

    /// Adds an argument to the transaction header.
    #[inline]
    pub fn argument<'a>(mut self, val: impl AsRef<ValueRef<'a>>) -> Self {
        self.arguments
            .push(serde_json::to_vec(val.as_ref()).unwrap().into_boxed_slice());
        self
    }

    /// Adds arguments to the transaction header.
    #[inline]
    pub fn arguments<I>(mut self, args: I) -> Self
    where
        I: IntoIterator,
        I::Item: Serialize,
    {
        self.arguments.extend(
            args.into_iter()
                .map(|v| serde_json::to_vec(&v))
                .map(Result::unwrap)
                .map(Vec::into_boxed_slice),
        );
        self
    }

    /// Builds a transaction header, assuming the script was set.
    #[inline]
    pub fn build(self) -> TransactionHeader<Vec<Box<[u8]>>> {
        TransactionHeader {
            script: self.script.unwrap(),
            arguments: self.arguments,
        }
    }

    /// Shorthand for `self.build().into_party_builder()`.
    #[inline]
    pub fn into_party_builder(self) -> PartyBuilder {
        self.build().into_party_builder()
    }

    /// Builds a transaction header, returning Err(self) if the script was not set.
    #[inline]
    pub fn build_checked(self) -> Result<TransactionHeader<Vec<Box<[u8]>>>, Self> {
        match self.script {
            Some(script) => Ok(TransactionHeader {
                script,
                arguments: self.arguments,
            }),
            None => Err(self),
        }
    }
}

/// A simple transaction to create an account with full weight keys.
#[derive(Clone, Copy)]
pub struct CreateAccountTransaction<'a, PubKey> {
    /// The public keys of the new account. The keys will have full signing weight.
    pub public_keys: &'a [PubKey],
}

/// A transaction to create an account with weighed keys.
#[derive(Clone, Copy)]
pub struct CreateAccountWeightedTransaction<'a, PubKey> {
    /// The public keys and their weight of the new account.
    pub public_keys: &'a [(PubKey, UFix64)],
}

/// Adds a contract to an account.
pub struct AddContractTransaction<'a, Name: AsRef<str>, Script: AsRef<str>> {
    /// The name of the contract.
    pub name: Name,

    /// The script of the contract.
    pub script: Script,

    /// The extra arguments passed to the contract's initialization.
    pub extra_args: HashMap<String, ValueRef<'a>>,
}

/// Updates a contract of an account by name.
pub struct UpdateContractTransaction<Name: AsRef<str>, Script: AsRef<str>> {
    /// The name of the contract to be updated.
    pub name: Name,

    /// The updated script of the contract.
    pub script: Script,
}

/// Remove a contract of an account by name.
pub struct RemoveContractTransaction<Name: AsRef<str>> {
    /// The name of the contract to be removed.
    pub name: Name,
}

impl<PubKey> CreateAccountTransaction<'_, PubKey> {
    /// Turns this transaction template into a [`TransactionHeader`].
    ///
    /// Requires the signer to serialize the public keys,
    /// and the hasher type argument to obtain the hashing algorithm.
    pub fn to_header<S: FlowSigner<PublicKey = PubKey>, H: FlowHasher>(
        &self,
        signer: &S,
    ) -> TransactionHeader<[Box<[u8]>; 1]> {
        match self.public_keys {
            [pubkey] => {
                let bytes = signer.serialize_public_key(pubkey);
                let bytes = bytes.map(ValueRef::UInt8);
                let script = format!(
                    include_str!("create_account_one_key.cdc.template"),
                    S::Algorithm::NAME,
                    H::Algorithm::NAME
                );
                header_array(script.into(), [ValueRef::Array(&bytes)])
            }
            pubs => {
                let arrays = pubs
                    .iter()
                    .map(|pubkey| signer.serialize_public_key(pubkey))
                    .map(|bytes| bytes.map(ValueRef::UInt8))
                    .collect::<Vec<_>>();

                let args = arrays
                    .iter()
                    .map(AsRef::as_ref)
                    .map(ValueRef::Array)
                    .collect::<Vec<_>>();

                let val = ValueRef::Array(&args);

                header_array(
                    format!(
                        include_str!("create_account.cdc.template"),
                        S::Algorithm::NAME,
                        H::Algorithm::NAME
                    )
                    .into(),
                    [val],
                )
            }
        }
    }
}

impl<PubKey> CreateAccountWeightedTransaction<'_, PubKey> {
    /// Turns this transaction template into a [`TransactionHeader`].
    ///
    /// Requires the signer to serialize the public keys,
    /// and the hasher type argument to obtain the hashing algorithm.
    pub fn to_header<S: FlowSigner<PublicKey = PubKey>, H: FlowHasher>(
        &self,
        signer: &S,
    ) -> TransactionHeader<[Box<[u8]>; 1]> {
        let script = format!(
            include_str!("create_account_weighted.cdc.template"),
            S::Algorithm::NAME,
            H::Algorithm::NAME
        );
        let entries: Vec<_> = self
            .public_keys
            .iter()
            .map(|(key, seqnum)| (hex::encode(signer.serialize_public_key(key)), seqnum))
            .collect();
        let dict_entries: Vec<_> = entries
            .iter()
            .map(|(key, seqnum)| EntryRef {
                key: ValueRef::String(key),
                value: ValueRef::UFix64(**seqnum),
            })
            .collect();
        let dict = ValueRef::Dictionary(&dict_entries);

        header_array(script.into(), [dict])
    }
}

impl<Name: AsRef<str>, Script: AsRef<str>> AddContractTransaction<'_, Name, Script> {
    /// Turns this transaction template into a [`TransactionHeader`].
    pub fn to_header(&self) -> TransactionHeader<Vec<Box<[u8]>>> {
        // Extra args passed to the transaction.
        // name: type, name: type, ...
        let mut extra_args_transaction_args = String::new();
        // Extra args passed to contracts.add().
        // name: name, name: name, ...
        let mut extra_args_add_args = String::new();

        let base_arguments = [
            ValueRef::String(self.name.as_ref()),
            ValueRef::String(self.script.as_ref()),
        ];

        let extra_args = self.extra_args.iter().enumerate().map(|(n, (name, val))| {
            if n != 0 {
                extra_args_transaction_args.push_str(", ");
                extra_args_add_args.push_str(", ")
            }
            extra_args_transaction_args.push_str(name);
            extra_args_transaction_args.push_str(": ");
            extra_args_transaction_args.push_str(val.ty().as_str());

            extra_args_add_args.push_str(name);
            extra_args_add_args.push_str(": ");
            extra_args_add_args.push_str(name);

            val
        });

        let arguments = base_arguments
            .iter()
            .chain(extra_args)
            .map(|v| serde_json::to_vec(v).unwrap().into_boxed_slice())
            .collect();

        TransactionHeader {
            script: format!(
                include_str!("add_contract.cdc.template"),
                extra_args_transaction_args, extra_args_add_args
            )
            .into(),
            arguments,
        }
    }
}

impl<Name: AsRef<str>, Script: AsRef<str>> UpdateContractTransaction<Name, Script> {
    /// Turns this transaction template into a [`TransactionHeader`].
    pub fn to_header(&self) -> TransactionHeader<[Box<[u8]>; 2]> {
        header_array(
            include_str!("update_contract.cdc").into(),
            [
                ValueRef::String(self.name.as_ref()),
                ValueRef::String(self.script.as_ref()),
            ],
        )
    }
}

impl<Name: AsRef<str>> RemoveContractTransaction<Name> {
    /// Turns this transaction template into a [`TransactionHeader`].
    pub fn to_header(&self) -> TransactionHeader<[Box<[u8]>; 1]> {
        header_array(
            include_str!("remove_contract.cdc").into(),
            [ValueRef::String(self.name.as_ref())],
        )
    }
}

fn header_array<const ARGS: usize>(
    script: Cow<'static, str>,
    args: [ValueRef; ARGS],
) -> TransactionHeader<[Box<[u8]>; ARGS]> {
    TransactionHeader {
        script,
        arguments: args.map(|s| serde_json::to_vec(&s).unwrap().into_boxed_slice()),
    }
}