dusk-wallet-core 1.7.0

The core functionality of the Dusk wallet
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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Implementations of basic wallet functionalities to create transactions.

use alloc::vec::Vec;

use dusk_bytes::Serializable;
use dusk_core::abi::ContractId;
use dusk_core::signatures::bls::{
    PublicKey as BlsPublicKey, SecretKey as BlsSecretKey,
};
use dusk_core::stake::{STAKE_CONTRACT, Stake, Withdraw as StakeWithdraw};
use dusk_core::transfer::data::{
    ContractCall, ContractDeploy, TransactionData,
};
use dusk_core::transfer::moonlight::Transaction as MoonlightTransaction;
use dusk_core::transfer::phoenix::{
    Note, NoteOpening, Prove, PublicKey as PhoenixPublicKey,
    SecretKey as PhoenixSecretKey, Transaction as PhoenixTransaction,
};
use dusk_core::transfer::withdraw::{
    Withdraw, WithdrawReceiver, WithdrawReplayToken,
};
use dusk_core::transfer::{TRANSFER_CONTRACT, Transaction};
use dusk_core::{BlsScalar, Error, JubJubScalar};
use ff::Field;
use rand::{CryptoRng, RngCore};
use zeroize::Zeroize;

/// An unproven-transaction is nearly identical to a [`PhoenixTransaction`] with
/// the only difference being that it carries a serialized [`TxCircuitVec`]
/// instead of the proof bytes.
/// This way it is possible to delegate the proof generation of the
/// [`TxCircuitVec`] after the unproven transaction was created while at the
/// same time ensuring non-malleability of the transaction, as the transaction's
/// payload-hash is part of the public inputs of the circuit.
/// Once the proof is generated from the [`TxCircuitVec`] bytes, it can
/// replace the serialized circuit in the transaction by calling
/// [`Transaction::replace_proof`].
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `phoenix_sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
/// - the Memo provided with `data` is too large
#[allow(clippy::too_many_arguments)]
pub fn phoenix<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    sender_sk: &PhoenixSecretKey,
    refund_pk: &PhoenixPublicKey,
    receiver_pk: &PhoenixPublicKey,
    inputs: Vec<(Note, NoteOpening)>,
    root: BlsScalar,
    transfer_value: u64,
    is_transfer: bool,
    deposit: u64,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    data: Option<impl Into<TransactionData>>,
    prover: &P,
) -> Result<Transaction, Error> {
    // always create a transparent transfer-note (the change-note will always be
    // obfuscated) if the transfer-value is 0 (the transfer-contract doesn't
    // append transparent notes with value 0 to the tree)
    let mut obfuscate_transfer_note = is_transfer;
    if transfer_value == 0 {
        obfuscate_transfer_note = false;
    }

    Ok(PhoenixTransaction::new::<R, P>(
        rng,
        sender_sk,
        refund_pk,
        receiver_pk,
        inputs,
        root,
        transfer_value,
        obfuscate_transfer_note,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        data,
        prover,
    )?
    .into())
}

/// Creates a totally generic Moonlight [`Transaction`], all fields being
/// variable.
///
/// # Note
/// The `moonlight_nonce` is NOT incremented and should be incremented
/// by the caller of this function, if its not done so, rusk
/// will throw 500 error
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - the Memo provided with `data` is too large
#[allow(clippy::too_many_arguments)]
pub fn moonlight(
    sender_sk: &BlsSecretKey,
    receiver_pk: Option<BlsPublicKey>,
    transfer_value: u64,
    deposit: u64,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    chain_id: u8,
    data: Option<impl Into<TransactionData>>,
) -> Result<Transaction, Error> {
    Ok(MoonlightTransaction::new(
        sender_sk,
        receiver_pk,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        data,
    )?
    .into())
}

/// Create a [`Transaction`] to stake from phoenix-notes.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `phoenix_sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
#[allow(clippy::too_many_arguments)]
pub fn phoenix_stake<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    inputs: Vec<(Note, NoteOpening)>,
    root: BlsScalar,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    stake_value: u64,
    prover: &P,
) -> Result<Transaction, Error> {
    // in a staking transaction the receiver and refund-address is the sender
    let phoenix_receiver_pk = PhoenixPublicKey::from(phoenix_sender_sk);
    let phoenix_refund_pk = PhoenixPublicKey::from(phoenix_sender_sk);

    let transfer_value = 0;
    let is_transfer = false;
    let deposit = stake_value;

    let stake = Stake::new(stake_sk, stake_owner_sk, stake_value, chain_id);

    let contract_call =
        ContractCall::new(STAKE_CONTRACT, "stake").with_args(&stake)?;

    phoenix::<R, P>(
        rng,
        phoenix_sender_sk,
        &phoenix_refund_pk,
        &phoenix_receiver_pk,
        inputs,
        root,
        transfer_value,
        is_transfer,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        Some(contract_call),
        prover,
    )
}

/// Create a [`Transaction`] to stake from a Moonlight account.
///
/// # Note
/// The `moonlight_nonce` is NOT incremented and should be incremented by the
/// caller of this function, if its not done so, rusk will throw 500 error
///
/// # Errors
/// The creation of this transaction doesn't error, but still returns a result
/// for the sake of API consistency.
#[allow(clippy::too_many_arguments)]
pub fn moonlight_stake(
    moonlight_sender_sk: &BlsSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    stake_value: u64,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    chain_id: u8,
) -> Result<Transaction, Error> {
    let transfer_value = 0;
    let deposit = stake_value;

    let stake = Stake::new(stake_sk, stake_owner_sk, stake_value, chain_id);

    let contract_call =
        ContractCall::new(STAKE_CONTRACT, "stake").with_args(&stake)?;

    moonlight(
        moonlight_sender_sk,
        None,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        Some(contract_call),
    )
}

/// Create an unproven [`Transaction`] to withdraw stake rewards into a
/// phoenix-note.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `phoenix_sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
#[allow(clippy::too_many_arguments)]
pub fn phoenix_stake_reward<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    inputs: Vec<(Note, NoteOpening, BlsScalar)>,
    root: BlsScalar,
    reward_amount: u64,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    prover: &P,
) -> Result<Transaction, Error> {
    // in a staking transaction the receiver and refund-address is the sender
    let phoenix_receiver_pk = PhoenixPublicKey::from(phoenix_sender_sk);
    let phoenix_refund_pk = PhoenixPublicKey::from(phoenix_sender_sk);

    let transfer_value = 0;
    let is_transfer = false;
    let deposit = 0;

    // split the input notes and openings from the nullifiers
    let mut nullifiers = Vec::with_capacity(inputs.len());
    let inputs = inputs
        .into_iter()
        .map(|(note, opening, nullifier)| {
            nullifiers.push(nullifier);
            (note, opening)
        })
        .collect();

    let gas_payment_token = WithdrawReplayToken::Phoenix(nullifiers);

    let contract_call = stake_reward_to_phoenix(
        rng,
        phoenix_sender_sk,
        stake_sk,
        stake_owner_sk,
        gas_payment_token,
        reward_amount,
    )?;

    phoenix::<R, P>(
        rng,
        phoenix_sender_sk,
        &phoenix_refund_pk,
        &phoenix_receiver_pk,
        inputs,
        root,
        transfer_value,
        is_transfer,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        Some(contract_call),
        prover,
    )
}

/// Create a [`Transaction`] to withdraw stake rewards into Moonlight account.
///
/// # Note
/// The `moonlight_nonce` is NOT incremented and should be incremented by the
/// caller of this function, if its not done so, rusk will throw 500 error
///
/// # Errors
/// The creation of this transaction doesn't error, but still returns a result
/// for the sake of API consistency.
#[allow(clippy::too_many_arguments)]
pub fn moonlight_stake_reward<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_sender_sk: &BlsSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    reward_amount: u64,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    chain_id: u8,
) -> Result<Transaction, Error> {
    let transfer_value = 0;
    let deposit = 0;

    let gas_payment_token = WithdrawReplayToken::Moonlight(moonlight_nonce);

    let contract_call = stake_reward_to_moonlight(
        rng,
        moonlight_sender_sk,
        stake_sk,
        stake_owner_sk,
        gas_payment_token,
        reward_amount,
    )?;

    moonlight(
        moonlight_sender_sk,
        None,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        Some(contract_call),
    )
}

/// Create an unproven [`Transaction`] to unstake into a phoenix-note.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
#[allow(clippy::too_many_arguments)]
pub fn phoenix_unstake<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    inputs: Vec<(Note, NoteOpening, BlsScalar)>,
    root: BlsScalar,
    unstake_value: u64,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    prover: &P,
) -> Result<Transaction, Error> {
    // in a staking transaction the receiver and refund-address is the sender
    let phoenix_receiver_pk = PhoenixPublicKey::from(phoenix_sender_sk);
    let phoenix_refund_pk = PhoenixPublicKey::from(phoenix_sender_sk);

    let transfer_value = 0;
    let is_transfer = false;
    let deposit = 0;

    // split the input notes and openings from the nullifiers
    let mut nullifiers = Vec::with_capacity(inputs.len());
    let inputs = inputs
        .into_iter()
        .map(|(note, opening, nullifier)| {
            nullifiers.push(nullifier);
            (note, opening)
        })
        .collect();

    let gas_payment_token = WithdrawReplayToken::Phoenix(nullifiers);

    let contract_call = unstake_to_phoenix(
        rng,
        phoenix_sender_sk,
        stake_sk,
        stake_owner_sk,
        gas_payment_token,
        unstake_value,
    )?;

    phoenix::<R, P>(
        rng,
        phoenix_sender_sk,
        &phoenix_refund_pk,
        &phoenix_receiver_pk,
        inputs,
        root,
        transfer_value,
        is_transfer,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        Some(contract_call),
        prover,
    )
}

/// Create a [`Transaction`] to unstake into a Moonlight account.
///
/// # Note
/// The `moonlight_nonce` is NOT incremented and should be incremented by the
/// caller of this function, if its not done so, rusk will throw 500 error
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - the Memo provided with `data` is too large
#[allow(clippy::too_many_arguments)]
pub fn moonlight_unstake<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_sender_sk: &BlsSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    unstake_value: u64,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    chain_id: u8,
) -> Result<Transaction, Error> {
    let transfer_value = 0;
    let deposit = 0;

    let gas_payment_token = WithdrawReplayToken::Moonlight(moonlight_nonce);

    let contract_call = unstake_to_moonlight(
        rng,
        moonlight_sender_sk,
        stake_sk,
        stake_owner_sk,
        gas_payment_token,
        unstake_value,
    )?;

    moonlight(
        moonlight_sender_sk,
        None,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        Some(contract_call),
    )
}

/// Create an unproven [`Transaction`] to convert Phoenix Dusk into Moonlight
/// Dusk.
///
/// # Note
/// The ownership of both sender and receiver keys is required, and
/// enforced by the protocol.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
#[allow(clippy::too_many_arguments)]
pub fn phoenix_to_moonlight<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    moonlight_receiver_sk: &BlsSecretKey,
    inputs: Vec<(Note, NoteOpening, BlsScalar)>,
    root: BlsScalar,
    convert_value: u64,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    prover: &P,
) -> Result<Transaction, Error> {
    // because a conversion is done through a contract call, the receiver and
    // refund-address of the transaction is the sender
    let phoenix_receiver_pk = PhoenixPublicKey::from(phoenix_sender_sk);
    let phoenix_refund_pk = PhoenixPublicKey::from(phoenix_sender_sk);

    let transfer_value = 0;
    let is_transfer = false;
    // a conversion is a simultaneous deposit to *and* withdrawal from the
    // transfer contract
    let deposit = convert_value;

    // split the input notes and openings from the nullifiers
    let mut nullifiers = Vec::with_capacity(inputs.len());
    let inputs = inputs
        .into_iter()
        .map(|(note, opening, nullifier)| {
            nullifiers.push(nullifier);
            (note, opening)
        })
        .collect();

    let gas_payment_token = WithdrawReplayToken::Phoenix(nullifiers);

    let contract_call = convert_to_moonlight(
        rng,
        moonlight_receiver_sk,
        gas_payment_token,
        convert_value,
    )?;

    phoenix::<R, P>(
        rng,
        phoenix_sender_sk,
        &phoenix_refund_pk,
        &phoenix_receiver_pk,
        inputs,
        root,
        transfer_value,
        is_transfer,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        Some(contract_call),
        prover,
    )
}

/// Create a [`Transaction`] to convert Moonlight Dusk into Phoenix Dusk.
///
/// # Note
/// 1. The ownership of both sender and receiver keys is required, and enforced
///    by the protocol.
/// 2. `moonlight_nonce` is NOT incremented and should be incremented by the
///    caller of this function, if its not done so, rusk will throw 500 error
///
/// # Errors
/// The creation of this transaction doesn't error, but still returns a result
/// for the sake of API consistency.
#[allow(clippy::too_many_arguments)]
pub fn moonlight_to_phoenix<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_sender_sk: &BlsSecretKey,
    phoenix_receiver_sk: &PhoenixSecretKey,
    convert_value: u64,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    chain_id: u8,
) -> Result<Transaction, Error> {
    // a conversion is a simultaneous deposit to *and* withdrawal from the
    // transfer contract
    let deposit = convert_value;
    let transfer_value = 0;
    let gas_payment_token = WithdrawReplayToken::Moonlight(moonlight_nonce);

    let contract_call = convert_to_phoenix(
        rng,
        phoenix_receiver_sk,
        gas_payment_token,
        convert_value,
    )?;

    moonlight(
        moonlight_sender_sk,
        None,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        Some(contract_call),
    )
}

/// Create a new unproven [`Transaction`] to deploy a contract to the network.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - one of the input-notes doesn't belong to the `sender_sk`
/// - the transaction input doesn't cover the transaction costs
/// - the `inputs` vector is either empty or larger than 4 elements
/// - the `inputs` vector contains duplicate `Note`s
/// - the `Prove` trait is implemented incorrectly
#[allow(clippy::too_many_arguments)]
pub fn phoenix_deployment<R: RngCore + CryptoRng, P: Prove>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    inputs: Vec<(Note, NoteOpening, BlsScalar)>,
    root: BlsScalar,
    bytecode: impl Into<Vec<u8>>,
    owner: &BlsPublicKey,
    init_args: Vec<u8>,
    nonce: u64,
    gas_limit: u64,
    gas_price: u64,
    chain_id: u8,
    prover: &P,
) -> Result<Transaction, Error> {
    // in a contract deployment transaction the receiver and refund-address is
    // the sender
    let phoenix_receiver_pk = PhoenixPublicKey::from(phoenix_sender_sk);
    let phoenix_refund_pk = PhoenixPublicKey::from(phoenix_sender_sk);

    let transfer_value = 0;
    let is_transfer = false;
    let deposit = 0;

    // split the input notes and openings from the nullifiers
    let mut nullifiers = Vec::with_capacity(inputs.len());
    let inputs = inputs
        .into_iter()
        .map(|(note, opening, nullifier)| {
            nullifiers.push(nullifier);
            (note, opening)
        })
        .collect();

    let deploy = ContractDeploy::new(
        bytecode,
        owner.to_bytes().to_vec(),
        Some(init_args),
        nonce,
    );

    phoenix(
        rng,
        phoenix_sender_sk,
        &phoenix_refund_pk,
        &phoenix_receiver_pk,
        inputs,
        root,
        transfer_value,
        is_transfer,
        deposit,
        gas_limit,
        gas_price,
        chain_id,
        Some(deploy),
        prover,
    )
}

/// Create a new [`Transaction`] to deploy a contract to the network.
///
/// # Note
/// The `moonlight_nonce` is NOT incremented and should be incremented by the
/// caller of this function, if its not done so, rusk will throw 500 error
///
/// # Errors
/// The creation of this transaction doesn't error, but still returns a result
/// for the sake of API consistency.
#[allow(clippy::too_many_arguments)]
pub fn moonlight_deployment(
    moonlight_sender_sk: &BlsSecretKey,
    bytecode: impl Into<Vec<u8>>,
    owner: &BlsPublicKey,
    init_args: Vec<u8>,
    gas_limit: u64,
    gas_price: u64,
    moonlight_nonce: u64,
    deploy_nonce: u64,
    chain_id: u8,
) -> Result<Transaction, Error> {
    let transfer_value = 0;
    let deposit = 0;

    let deploy = ContractDeploy::new(
        bytecode,
        owner.to_bytes().to_vec(),
        Some(init_args),
        deploy_nonce,
    );

    moonlight(
        moonlight_sender_sk,
        None,
        transfer_value,
        deposit,
        gas_limit,
        gas_price,
        moonlight_nonce,
        chain_id,
        Some(deploy),
    )
}

fn stake_reward_to_phoenix<R: RngCore + CryptoRng>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    gas_payment_token: WithdrawReplayToken,
    reward_amount: u64,
) -> Result<ContractCall, Error> {
    let withdraw = withdraw_to_phoenix(
        rng,
        phoenix_sender_sk,
        STAKE_CONTRACT,
        gas_payment_token,
        reward_amount,
    );

    let reward_withdraw =
        StakeWithdraw::new(stake_sk, stake_owner_sk, withdraw);

    ContractCall::new(STAKE_CONTRACT, "withdraw").with_args(&reward_withdraw)
}

pub(crate) fn stake_reward_to_moonlight<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_receiver_sk: &BlsSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    gas_payment_token: WithdrawReplayToken,
    reward_amount: u64,
) -> Result<ContractCall, Error> {
    let withdraw = withdraw_to_moonlight(
        rng,
        moonlight_receiver_sk,
        STAKE_CONTRACT,
        gas_payment_token,
        reward_amount,
    );

    let reward_withdraw =
        StakeWithdraw::new(stake_sk, stake_owner_sk, withdraw);

    ContractCall::new(STAKE_CONTRACT, "withdraw").with_args(&reward_withdraw)
}

fn unstake_to_phoenix<R: RngCore + CryptoRng>(
    rng: &mut R,
    phoenix_sender_sk: &PhoenixSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    gas_payment_token: WithdrawReplayToken,
    unstake_value: u64,
) -> Result<ContractCall, Error> {
    let withdraw = withdraw_to_phoenix(
        rng,
        phoenix_sender_sk,
        STAKE_CONTRACT,
        gas_payment_token,
        unstake_value,
    );

    let unstake = StakeWithdraw::new(stake_sk, stake_owner_sk, withdraw);

    ContractCall::new(STAKE_CONTRACT, "unstake").with_args(&unstake)
}

pub(crate) fn unstake_to_moonlight<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_receiver_sk: &BlsSecretKey,
    stake_sk: &BlsSecretKey,
    stake_owner_sk: &BlsSecretKey,
    gas_payment_token: WithdrawReplayToken,
    unstake_value: u64,
) -> Result<ContractCall, Error> {
    let withdraw = withdraw_to_moonlight(
        rng,
        moonlight_receiver_sk,
        STAKE_CONTRACT,
        gas_payment_token,
        unstake_value,
    );

    let unstake = StakeWithdraw::new(stake_sk, stake_owner_sk, withdraw);

    ContractCall::new(STAKE_CONTRACT, "unstake").with_args(&unstake)
}

fn convert_to_moonlight<R: RngCore + CryptoRng>(
    rng: &mut R,
    moonlight_receiver_sk: &BlsSecretKey,
    gas_payment_token: WithdrawReplayToken,
    convert_value: u64,
) -> Result<ContractCall, Error> {
    let fn_args = withdraw_to_moonlight(
        rng,
        moonlight_receiver_sk,
        TRANSFER_CONTRACT,
        gas_payment_token,
        convert_value,
    );
    ContractCall::new(TRANSFER_CONTRACT, "convert").with_args(&fn_args)
}

fn convert_to_phoenix<R: RngCore + CryptoRng>(
    rng: &mut R,
    phoenix_receiver_sk: &PhoenixSecretKey,
    gas_payment_token: WithdrawReplayToken,
    convert_value: u64,
) -> Result<ContractCall, Error> {
    let fn_args = withdraw_to_phoenix(
        rng,
        phoenix_receiver_sk,
        TRANSFER_CONTRACT,
        gas_payment_token,
        convert_value,
    );
    ContractCall::new(TRANSFER_CONTRACT, "convert").with_args(&fn_args)
}

/// Create a [`Withdraw`] struct to be used to withdraw funds from a contract
/// into a phoenix-note.
///
/// The gas payment can be done by either phoenix or moonlight by setting the
/// `gas_payment_token` accordingly.
fn withdraw_to_phoenix<R: RngCore + CryptoRng>(
    rng: &mut R,
    receiver_sk: &PhoenixSecretKey,
    contract: impl Into<ContractId>,
    gas_payment_token: WithdrawReplayToken,
    value: u64,
) -> Withdraw {
    let withdraw_address = PhoenixPublicKey::from(receiver_sk)
        .gen_stealth_address(&JubJubScalar::random(&mut *rng));
    let mut withdraw_note_sk = receiver_sk.gen_note_sk(&withdraw_address);

    let withdraw = Withdraw::new(
        rng,
        &withdraw_note_sk,
        contract.into(),
        value,
        WithdrawReceiver::Phoenix(withdraw_address),
        gas_payment_token,
    );

    withdraw_note_sk.zeroize();

    withdraw
}

/// Create a [`Withdraw`] struct to be used to withdraw funds from a contract
/// into a Moonlight account.
///
/// The gas payment can be done by either Phoenix or Moonlight by setting the
/// `gas_payment_token` accordingly.
fn withdraw_to_moonlight<R: RngCore + CryptoRng>(
    rng: &mut R,
    receiver_sk: &BlsSecretKey,
    contract: impl Into<ContractId>,
    gas_payment_token: WithdrawReplayToken,
    value: u64,
) -> Withdraw {
    Withdraw::new(
        rng,
        receiver_sk,
        contract.into(),
        value,
        WithdrawReceiver::Moonlight(BlsPublicKey::from(receiver_sk)),
        gas_payment_token,
    )
}