stake_contract_types/
sig.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7//! Signatures messages used in the stake contract.
8
9use alloc::vec::Vec;
10
11use dusk_bls12_381::BlsScalar;
12use dusk_bytes::Serializable;
13use phoenix_core::StealthAddress;
14
15const STAKE_MESSAGE_SIZE: usize = u64::SIZE + u64::SIZE;
16const WITHDRAW_MESSAGE_SIZE: usize =
17    u64::SIZE + StealthAddress::SIZE + BlsScalar::SIZE;
18
19/// Return the digest to be signed in the `stake` function of the stake
20/// contract.
21#[must_use]
22pub fn stake_signature_message(
23    counter: u64,
24    value: u64,
25) -> [u8; STAKE_MESSAGE_SIZE] {
26    let mut bytes = [0u8; STAKE_MESSAGE_SIZE];
27
28    bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
29    bytes[u64::SIZE..].copy_from_slice(&value.to_bytes());
30
31    bytes
32}
33
34/// Signature message used for [`Unstake`].
35pub fn unstake_signature_message<T>(counter: u64, note: T) -> Vec<u8>
36where
37    T: AsRef<[u8]>,
38{
39    let mut vec = Vec::new();
40
41    vec.extend_from_slice(&counter.to_bytes());
42    vec.extend_from_slice(note.as_ref());
43
44    vec
45}
46
47/// Signature message used for [`Withdraw`].
48#[must_use]
49pub fn withdraw_signature_message(
50    counter: u64,
51    address: StealthAddress,
52    nonce: BlsScalar,
53) -> [u8; WITHDRAW_MESSAGE_SIZE] {
54    let mut bytes = [0u8; WITHDRAW_MESSAGE_SIZE];
55
56    bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
57    bytes[u64::SIZE..u64::SIZE + StealthAddress::SIZE]
58        .copy_from_slice(&address.to_bytes());
59    bytes[u64::SIZE + StealthAddress::SIZE..]
60        .copy_from_slice(&nonce.to_bytes());
61
62    bytes
63}