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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! Mutate the blockchain with your private key(s).
//!
//! This module contains ways to log in to an account via secret key(s) and an Access API client.

use std::collections::HashMap;
use std::iter::empty;
use std::marker::PhantomData;
use std::slice;

use crate::access::{
    AccountResponse, BlockHeaderResponse, GetAccountAtLatestBlockRequest,
    GetLatestBlockHeaderRequest, SendTransactionRequest, SendTransactionResponse,
};
use crate::algorithms::{
    DefaultHasher, DefaultSecretKey, DefaultSigner, FlowHasher, FlowSigner, HashAlgorithm,
    Signature, SignatureAlgorithm,
};
use crate::client::{FlowClient, GrpcClient};
use crate::entities::AccountKey;
use crate::error::BoxError;
use crate::multi::{Party, PartyTransaction};
use crate::protobuf::Seal;
use crate::sign::{KeyIdIter, MkSigIter, Multi, One, SignIter, SignMethod};
use crate::transaction::rlp::rlp_encode_transaction_envelope;
use crate::transaction::{ProposalKeyE, SignatureE, TransactionE, TransactionHeader};

const PADDED_LEN: usize = 32;

/// The transaction domain tag, padded to 32 bytes.
pub const PADDED_TRANSACTION_DOMAIN_TAG: [u8; PADDED_LEN] =
    padded::<PADDED_LEN>(b"FLOW-V0.0-transaction");

pub use crate::error::AccountError as Error;

/// An account that uses the default signing and hashing algorithms.
pub type DefaultAccount<Client> = Account<Client>;

#[derive(Clone)]
/// An account.
///
/// This is your gateway to making transactions, as this holds the secret keys necessary for signing them, as well
/// as the client, for sending any requests over the network.
pub struct Account<
    Client,
    SecretKey = DefaultSecretKey,
    Signer = DefaultSigner,
    Hasher = DefaultHasher,
> {
    // The address of this account.
    address: Box<[u8]>,
    sign_method: SignMethod<SecretKey>,
    signer: Signer,
    client: FlowClient<Client>,
    _pd: PhantomData<Hasher>,
}

impl<Cl, Sk, Sn, Hs> Account<Cl, Sk, Sn, Hs> {
    /// Returns the address of this account.
    #[inline]
    pub fn address(&self) -> &[u8] {
        &self.address
    }

    /// Returns the signer.
    #[inline]
    pub fn signer(&self) -> &Sn {
        &self.signer
    }

    /// Returns the client.
    #[inline]
    pub fn client(&mut self) -> &mut FlowClient<Cl> {
        &mut self.client
    }

    /// Clones the client from this account.
    #[inline]
    pub fn client_cloned(&self) -> FlowClient<Cl>
    where
        Cl: Clone,
    {
        self.client.clone()
    }

    /// Returns the primary public key of this account.
    pub fn primary_public_key(&self) -> Sn::PublicKey
    where
        Sn: FlowSigner<SecretKey = Sk>,
    {
        self.signer
            .to_public_key(self.sign_method.primary_secret_key())
    }

    /// Returns the primary key id number of this account.
    #[inline]
    pub fn primary_key_id(&self) -> u32 {
        self.sign_method.primary_key_id()
    }
}

impl<Client, SecretKey, Signer, Hasher> Account<Client, SecretKey, Signer, Hasher>
where
    Signer: FlowSigner<SecretKey = SecretKey>,
    Hasher: FlowHasher,
{
    ///////////////////////
    // CONSTRUCTION

    /// Logs in to the account with one key, verifying that the key and the address matches.
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    ///
    ///  - the client returns any errors while making requests
    ///  - the secret key does not have the full weight to be able to act on its own (weight < 1000)
    ///  - could not find any public key of the account that matches the secret key supplied.
    ///  - the algorithms for the signer and the hasher do not match with the public information of the key.
    pub async fn new<Addr>(
        client: Client,
        address: Addr,
        secret_key: SecretKey,
    ) -> Result<Self, Error>
    where
        Client: GrpcClient<GetAccountAtLatestBlockRequest<Addr>, AccountResponse>,
    {
        let mut client = FlowClient::new(client);
        let acc = client
            .account_at_latest_block(address)
            .await
            .map_err(Into::into)?;

        let crate::entities::Account { address, keys, .. } = acc;

        let mut account_key = None;

        let signer = Signer::new();
        let public_key = signer.to_public_key(&secret_key);
        let serialized = signer.serialize_public_key(&public_key);

        for key in keys {
            if *key.public_key == serialized {
                account_key = Some(key);
            }
        }

        let AccountKey {
            index: key_id,
            sign_algo,
            hash_algo,
            weight,
            revoked,
            ..
        } = account_key.ok_or(Error::NoMatchingKeyFound)?;

        if revoked {
            return Err(Error::KeyRevoked);
        }

        if weight < 1000 {
            return Err(Error::NotEnoughWeight);
        }

        if Signer::Algorithm::CODE != sign_algo || Hasher::Algorithm::CODE != hash_algo {
            return Err(Error::AlgoMismatch);
        }

        Ok(Self {
            address,
            sign_method: SignMethod::One(One {
                key_id,
                key: secret_key,
            }),
            signer,
            client,
            _pd: PhantomData,
        })
    }

    /// Logs in to the account with multiple keys, verifying that the keys and the address matches.
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    ///
    ///  - the client returns any errors while making requests
    ///  - the secret keys does not add up to the full weight to be able to sign (weight < 1000)
    ///  - could not find any public key of the account that matches one of the the secret key supplied.
    ///  - there were duplicate secret keys supplied
    ///  - the algorithms for the signer and the hasher do not match with the public information of a key.
    pub async fn new_multisign<Addr>(
        client: Client,
        address: Addr,
        primary_index: usize,
        secret_keys: &[SecretKey],
    ) -> Result<Self, Error>
    where
        Client: GrpcClient<GetAccountAtLatestBlockRequest<Addr>, AccountResponse>,
        SecretKey: Clone,
    {
        assert!(
            secret_keys.len() > 1,
            "cannot have less than 2 secret keys specified for multisign"
        );

        let mut client = FlowClient::new(client);
        let acc = client
            .account_at_latest_block(address)
            .await
            .map_err(Into::into)?;

        let crate::entities::Account { address, keys, .. } = acc;

        assert!(
            primary_index < secret_keys.len(),
            "primary key must be valid"
        );

        let signer = Signer::new();
        let mut primary_key_idx = usize::MAX;
        let mut total_weight = 0;
        let mut found_keys = Vec::new();

        let mut add_key = |key_index: usize, key_id, weight| {
            if key_index == primary_index {
                primary_key_idx = found_keys.len();
            }

            found_keys.push(One {
                key_id,
                key: secret_keys[key_index].clone(),
            });

            total_weight += weight;
        };

        if secret_keys.len() > 10 {
            // Hash the large set of secret keys.
            let mut public_keys_to_find: HashMap<_, _> = secret_keys
                .iter()
                .enumerate()
                .map(|(idx, secret_key)| {
                    (
                        signer.serialize_public_key(&signer.to_public_key(secret_key)),
                        idx,
                    )
                })
                .collect();

            for key in keys {
                if let Some(key_index) = public_keys_to_find.remove(&*key.public_key) {
                    add_key(key_index, key.index, key.weight);
                }
            }

            if !public_keys_to_find.is_empty() {
                return Err(Error::NoMatchingKeyFound);
            }
        } else {
            // Hashing can be expensive for small sets.
            let mut public_keys_to_find: Vec<_> = secret_keys
                .iter()
                .map(|sk| signer.to_public_key(sk))
                .map(|pk| signer.serialize_public_key(&pk))
                .collect();

            for key in keys {
                if let Some((index, _)) = public_keys_to_find
                    .iter()
                    .enumerate()
                    .find(|(_, pubkey)| *pubkey == &*key.public_key)
                {
                    // Do not allow duplicate secret keys
                    public_keys_to_find.swap_remove(index);
                    add_key(index, key.index, key.weight);
                }
            }

            if !public_keys_to_find.is_empty() {
                return Err(Error::NoMatchingKeyFound);
            }
        }

        if total_weight < 1000 {
            return Err(Error::NotEnoughWeight);
        }

        Ok(Self {
            address,
            sign_method: SignMethod::Multi(Multi {
                primary_key_idx,
                keys: found_keys.into_boxed_slice(),
            }),
            signer,
            client,
            _pd: PhantomData,
        })
    }

    /// Creates a new account without checking that the address has the specified key.
    ///
    /// # Safety
    ///
    /// This is unsafe because it can end up signing with a wrong/revoked key, which means
    /// transactions sent to the network could fail.
    ///
    /// Do not do this unless you are sure that the key(s) contained in the sign method matches
    /// the address.
    #[inline]
    pub unsafe fn new_unchecked(
        client: Client,
        address: Box<[u8]>,
        sign_method: SignMethod<SecretKey>,
    ) -> Self {
        Self {
            address,
            sign_method,
            signer: Signer::new(),
            client: FlowClient::new(client),
            _pd: PhantomData,
        }
    }

    ////////////////////
    // INFORMATION

    /// Queries the sequence number for the primary key from the network.
    pub async fn primary_key_sequence_number<'a>(&'a mut self) -> Result<u32, BoxError>
    where
        Client: GrpcClient<GetAccountAtLatestBlockRequest<&'a [u8]>, AccountResponse>,
    {
        let address = &*self.address;
        let public_key = self.signer.serialize_public_key(&self.primary_public_key());

        let acc = self
            .client
            .account_at_latest_block(address)
            .await
            .map_err(Into::into)?;
        for key in acc.keys {
            if *key.public_key == public_key {
                return Ok(key.sequence_number);
            }
        }
        unreachable!();
    }

    //////////////////
    // SIGNING

    /// Creates signature(s) using this account's public key(s), consuming a populated hasher.
    pub fn sign(&self, hasher: Hasher) -> SignIter<Signer> {
        Self::sign_(hasher, self.signer(), &self.sign_method)
    }

    /// Creates signature(s) using this account's public key(s), signing provided data.
    pub fn sign_data(&self, data: impl AsRef<[u8]>) -> SignIter<Signer> {
        let mut hasher = Hasher::new();
        hasher.update(&data);
        self.sign(hasher)
    }

    /// Signs a party, assuming that you have confirmed all the details of the party.
    pub fn sign_party<P: Party<Hasher>>(&self, party: &mut P)
    where
        Signer::Signature: Signature<Serialized = [u8; 64]>,
    {
        let signatures = self.sign(party.payload());
        let key_ids = self.sign_method.key_ids();
        for (sig, key_id) in signatures.zip(key_ids) {
            party.add_payload_signature(self.address.clone(), key_id, sig.serialize())
        }
    }

    /// Signs the party as the payer, thereby converting the party into a transaction, ready to be sent.
    pub fn sign_party_as_payer<P: Party<Hasher>>(
        &self,
        party: P,
    ) -> PartyTransaction<Box<[u8]>, [u8; 64]>
    where
        Signer::Signature: Signature<Serialized = [u8; 64]>,
    {
        assert_eq!(&*self.address, party.payer());
        let signatures = self.sign(party.envelope());
        let key_ids = self.sign_method.key_ids();

        party.into_transaction_with_envelope_signatures(signatures.zip(key_ids).map(
            |(sig, key_id)| SignatureE {
                address: self.address.clone(),
                key_id,
                signature: sig.serialize(),
            },
        ))
    }

    /// Sign a transaction with this account being the proposer, the payer and the only authorizer.
    ///
    /// Returns an envelope signature.
    pub fn sign_transaction(
        &self,
        script: impl AsRef<[u8]>,
        arguments: impl IntoIterator<IntoIter = impl ExactSizeIterator<Item = impl AsRef<[u8]>>>,
        reference_block_id: impl AsRef<[u8]>,
        sequence_number: u64,
        gas_limit: u64,
    ) -> SignIter<Signer> {
        Self::sign_transaction_(
            self.primary_key_id(),
            &self.address,
            &self.signer,
            &self.sign_method,
            script,
            arguments,
            reference_block_id,
            sequence_number,
            gas_limit,
        )
    }

    /// Sign a transaction header with a block id and gas limit.
    pub fn sign_transaction_header<'a, Arguments>(
        &self,
        header: &'a TransactionHeader<Arguments>,
        reference_block_id: impl AsRef<[u8]>,
        sequence_number: u64,
        gas_limit: u64,
    ) -> SignIter<Signer>
    where
        &'a Arguments: IntoIterator,
        <&'a Arguments as IntoIterator>::IntoIter: ExactSizeIterator,
        <<&'a Arguments as IntoIterator>::IntoIter as Iterator>::Item: AsRef<[u8]>,
    {
        Self::sign_transaction_header_(
            self.primary_key_id(),
            &self.address,
            &self.signer,
            &self.sign_method,
            header,
            reference_block_id,
            sequence_number,
            gas_limit,
        )
    }

    /// Send a transaction to the network. Signs the transaction header with a gas limit of 1000
    /// and using the latest sealed block as a reference.
    ///
    /// Note that this does not increment the sequence number.
    ///
    /// # Errors
    ///
    /// This function returns an error if the client returns any errors when making requests.
    pub async fn send_transaction_header<'a, Arguments, Argument>(
        &'a mut self,
        transaction: &'a TransactionHeader<Arguments>,
    ) -> Result<SendTransactionResponse, BoxError>
    where
        Client: for<'b> GrpcClient<GetAccountAtLatestBlockRequest<&'b [u8]>, AccountResponse>,
        Client: GrpcClient<GetLatestBlockHeaderRequest, BlockHeaderResponse>,
        for<'b> Client: GrpcClient<
            SendTransactionRequest<
                &'b [u8],
                &'b SliceHelper<Argument>,
                &'b [u8],
                &'b [u8],
                &'b [u8],
                [&'b [u8]; 1],
                [SignatureE<&'b [u8], &'b [u8]>; 0],
                EmitRefAndDropOnNext<
                    SignatureE<&'b [u8], <Signer::Signature as Signature>::Serialized>,
                    MkSigIter<'b, KeyIdIter<'b, Signer::SecretKey>, SignIter<'b, Signer>>,
                >,
            >,
            SendTransactionResponse,
        >,
        Arguments: AsRef<[Argument]>,
        Argument: AsRef<[u8]>,
        &'a Arguments: IntoIterator,
        <&'a Arguments as IntoIterator>::IntoIter: ExactSizeIterator,
        <<&'a Arguments as IntoIterator>::IntoIter as Iterator>::Item: AsRef<[u8]>,
    {
        let address = &*self.address;
        let acc = self
            .client
            .account_at_latest_block(address)
            .await
            .map_err(Into::into)?;
        let pub_key = self.primary_public_key();
        let pub_key = self.signer.serialize_public_key(&pub_key);
        let key = acc
            .keys
            .into_iter()
            .find(|key| *key.public_key == pub_key)
            .unwrap();
        let sequence_number = key.sequence_number as u64;

        let latest_block = self
            .client
            .latest_block_header(Seal::Sealed)
            .await
            .map_err(Into::into)?;

        let reference_block_id = &*latest_block.id;
        let gas_limit = 1000;
        let sig = Self::sign_transaction_header_(
            self.primary_key_id(),
            &self.address,
            &self.signer,
            &self.sign_method,
            transaction,
            reference_block_id,
            sequence_number,
            gas_limit,
        );

        let envelope_signatures = EmitRefAndDropOnNext(
            MkSigIter::new(&self.address, self.sign_method.key_ids(), sig),
            PhantomData,
        );
        let transaction = TransactionE {
            script: transaction.script.as_ref().as_ref(),
            arguments: SliceHelper::new_ref(transaction.arguments.as_ref()),
            reference_block_id,
            gas_limit,
            proposal_key: ProposalKeyE {
                address: &*self.address,
                key_id: self.primary_key_id(),
                sequence_number,
            },
            payer: &*self.address,
            authorizers: [&*self.address],
            payload_signatures: [],
            envelope_signatures,
        };

        Ok(self
            .client
            .send_transaction(transaction)
            .await
            .map_err(Into::into)?)
    }

    ///////////////
    /// PRIVATE

    fn sign_<'a>(
        hasher: Hasher,
        signer: &'a Signer,
        method: &'a SignMethod<SecretKey>,
    ) -> SignIter<'a, Signer> {
        SignIter::new(hasher.finalize(), signer, method)
    }

    #[allow(clippy::too_many_arguments)]
    fn sign_transaction_<'a>(
        key_id: u32,
        address: &[u8],
        signer: &'a Signer,
        method: &'a SignMethod<SecretKey>,
        script: impl AsRef<[u8]>,
        arguments: impl IntoIterator<IntoIter = impl ExactSizeIterator<Item = impl AsRef<[u8]>>>,
        reference_block_id: impl AsRef<[u8]>,
        sequence_number: u64,
        gas_limit: u64,
    ) -> SignIter<'a, Signer> {
        let mut s = rlp::RlpStream::new();
        rlp_encode_transaction_envelope(
            &mut s,
            script,
            arguments,
            reference_block_id,
            gas_limit,
            address,
            key_id as u64,
            sequence_number,
            address,
            [address],
            empty::<(u32, u32, &[u8])>(),
        );

        let mut hasher = Hasher::new();
        hasher.update(&PADDED_TRANSACTION_DOMAIN_TAG);
        hasher.update(&s.out());

        Self::sign_(hasher, signer, method)
    }

    #[allow(clippy::too_many_arguments)]
    fn sign_transaction_header_<'a, 'b, Arguments>(
        key_id: u32,
        address: &[u8],
        signer: &'a Signer,
        method: &'a SignMethod<SecretKey>,
        header: &'b TransactionHeader<Arguments>,
        reference_block_id: impl AsRef<[u8]>,
        sequence_number: u64,
        gas_limit: u64,
    ) -> SignIter<'a, Signer>
    where
        &'b Arguments: IntoIterator,
        <&'b Arguments as IntoIterator>::IntoIter: ExactSizeIterator,
        <<&'b Arguments as IntoIterator>::IntoIter as Iterator>::Item: AsRef<[u8]>,
    {
        Self::sign_transaction_(
            key_id,
            address,
            signer,
            method,
            &header.script.as_ref(),
            &header.arguments,
            reference_block_id,
            sequence_number,
            gas_limit,
        )
    }
}

#[repr(transparent)]
#[doc(hidden)] // implementation details
pub struct SliceHelper<Item>([Item]);

impl<Item> SliceHelper<Item> {
    #[doc(hidden)]
    pub fn new_ref(t: &[Item]) -> &Self {
        unsafe { &*(t as *const [Item] as *const Self) }
    }
}

impl<'a, 'b, Item: 'a> IntoIterator for &'a &'b SliceHelper<Item> {
    type Item = &'a Item;

    type IntoIter = slice::Iter<'a, Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.as_ref().iter()
    }
}

impl<Item> otopr::HasItem for &'_ SliceHelper<Item> {
    type Item = Item;
}

#[doc(hidden)] // implementation details
#[derive(Clone)]
pub struct EmitRefAndDropOnNextIter<'a, T, I>(Option<T>, I, PhantomData<&'a T>);

#[doc(hidden)]
#[derive(Clone)]
pub struct EmitRefAndDropOnNext<T, I>(I, PhantomData<T>);

impl<'a, T, I: Iterator<Item = T> + Clone> IntoIterator for &'a EmitRefAndDropOnNext<T, I> {
    type Item = &'a T;

    type IntoIter = EmitRefAndDropOnNextIter<'a, T, I>;

    fn into_iter(self) -> Self::IntoIter {
        EmitRefAndDropOnNextIter(None, self.0.clone(), PhantomData)
    }
}

impl<'a, T, I: Iterator<Item = T>> Iterator for EmitRefAndDropOnNextIter<'a, T, I> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(it) = self.1.next() {
            let r = self.0.insert(it);

            // SAFETY: Must ensure that each item is dropped before calling `next()`.
            //
            // FIXME: Can we avoid this and work with the trait system instead?
            Some(unsafe { &*(r as *const T) })
        } else {
            None
        }
    }
}

impl<T, I> otopr::HasItem for EmitRefAndDropOnNext<T, I> {
    type Item = T;
}

#[cfg(test)]
pub(crate) fn test_pad(src: &[u8]) -> [u8; 32] {
    padded(src)
}

const fn padded<const N: usize>(src: &[u8]) -> [u8; N] {
    let mut new_buf = [0; N];

    let mut i = 0;

    while i < src.len() {
        new_buf[i] = src[i];
        i += 1;
    }

    new_buf
}