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
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::str::FromStr;
#[cfg(feature = "async")]
use darkfi_serial::async_trait;
use darkfi_serial::{SerialDecodable, SerialEncodable};
use pasta_curves::pallas;
use super::{pasta_prelude::*, poseidon_hash, ContractId};
use crate::{fp_from_bs58, fp_to_bs58, ty_from_fp, ContractError};
pub type FunctionCode = u8;
#[derive(Copy, Clone, Debug, Eq, PartialEq, SerialEncodable, SerialDecodable)]
pub struct FuncRef {
pub contract_id: ContractId,
pub func_code: FunctionCode,
}
impl FuncRef {
pub fn to_func_id(&self) -> FuncId {
let func_id =
poseidon_hash([self.contract_id.inner(), pallas::Base::from(self.func_code as u64)]);
FuncId(func_id)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, SerialEncodable, SerialDecodable)]
pub struct FuncId(pallas::Base);
impl FuncId {
pub fn none() -> Self {
Self(pallas::Base::ZERO)
}
pub fn inner(&self) -> pallas::Base {
self.0
}
/// Create a `FuncId` object from given bytes, erroring if the
/// input bytes are noncanonical.
pub fn from_bytes(x: [u8; 32]) -> Result<Self, ContractError> {
match pallas::Base::from_repr(x).into() {
Some(v) => Ok(Self(v)),
None => {
Err(ContractError::IoError("Failed to instantiate FuncId from bytes".to_string()))
}
}
}
/// Convert the `FuncId` type into 32 raw bytes
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_repr()
}
}
fp_from_bs58!(FuncId);
fp_to_bs58!(FuncId);
ty_from_fp!(FuncId);