snarkvm_console_program/owner/mod.rs
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
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod bytes;
mod serialize;
mod string;
use snarkvm_console_account::{Address, PrivateKey, Signature};
use snarkvm_console_network::Network;
use snarkvm_console_types::prelude::*;
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct ProgramOwner<N: Network> {
/// The address of the program owner.
address: Address<N>,
/// The signature of the program owner, over the deployment transaction ID.
signature: Signature<N>,
}
impl<N: Network> ProgramOwner<N> {
/// Initializes a new program owner.
pub fn new<R: Rng + CryptoRng>(private_key: &PrivateKey<N>, deployment_id: Field<N>, rng: &mut R) -> Result<Self> {
// Derive the address.
let address = Address::try_from(private_key)?;
// Sign the transaction ID.
let signature = private_key.sign(&[deployment_id], rng)?;
// Return the program owner.
Ok(Self { signature, address })
}
/// Initializes a new program owner from an address and signature.
pub fn from(address: Address<N>, signature: Signature<N>) -> Self {
Self { address, signature }
}
/// Returns the address of the program owner.
pub const fn address(&self) -> Address<N> {
self.address
}
/// Returns the signature of the program owner.
pub const fn signature(&self) -> &Signature<N> {
&self.signature
}
/// Verify that the signature is valid for the given deployment ID.
pub fn verify(&self, deployment_id: Field<N>) -> bool {
self.signature.verify(&self.address, &[deployment_id])
}
}
#[cfg(test)]
pub(crate) mod test_helpers {
use super::*;
use snarkvm_console_network::MainnetV0;
use once_cell::sync::OnceCell;
type CurrentNetwork = MainnetV0;
pub(crate) fn sample_program_owner() -> ProgramOwner<CurrentNetwork> {
static INSTANCE: OnceCell<ProgramOwner<CurrentNetwork>> = OnceCell::new();
*INSTANCE.get_or_init(|| {
// Initialize the RNG.
let rng = &mut TestRng::default();
// Initialize a private key.
let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();
// Initialize a deployment ID.
let deployment_id: Field<CurrentNetwork> = rng.gen();
// Return the program owner.
ProgramOwner::new(&private_key, deployment_id, rng).unwrap()
})
}
#[test]
fn test_verify_program_owner() {
// Initialize the RNG.
let rng = &mut TestRng::default();
// Initialize a private key.
let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();
// Initialize a deployment ID.
let deployment_id: Field<CurrentNetwork> = rng.gen();
// Construct the program owner.
let owner = ProgramOwner::new(&private_key, deployment_id, rng).unwrap();
// Ensure that the program owner is verified for the given deployment ID.
assert!(owner.verify(deployment_id));
// Ensure that the program owner is not verified for a different deployment ID.
let incorrect_deployment_id: Field<CurrentNetwork> = rng.gen();
assert!(!owner.verify(incorrect_deployment_id));
}
}