use tari_template_abi::{EngineOp, call_engine, rust::prelude::*};
use tari_template_lib_types::ComponentAddress;
use crate::{
args::{AddressAllocationInvokeArg, CallerContextAction, CallerContextInvokeArg, InvokeResult},
error_variants::{ERR_ENGINE_DECODE_FAIL, ERR_NOT_IN_COMPONENT_CONTEXT},
models::{ComponentAddressAllocation, Proof, ResourceAddressAllocation},
types::crypto::RistrettoPublicKeyBytes,
};
pub struct CallerContext;
impl CallerContext {
pub fn transaction_signer_public_key() -> RistrettoPublicKeyBytes {
let resp: InvokeResult = call_engine(EngineOp::CallerContextInvoke, &CallerContextInvokeArg {
action: CallerContextAction::GetCallerPublicKey,
args: invoke_args![],
});
resp.decode().expect(ERR_ENGINE_DECODE_FAIL)
}
pub fn get_main_signer_proof() -> Proof {
Self::get_signer_proof_inner(None)
}
pub fn get_signer_proof_for_public_key(public_key: RistrettoPublicKeyBytes) -> Proof {
Self::get_signer_proof_inner(Some(public_key))
}
fn get_signer_proof_inner(pk: Option<RistrettoPublicKeyBytes>) -> Proof {
let resp: InvokeResult = call_engine(EngineOp::CallerContextInvoke, &CallerContextInvokeArg {
action: CallerContextAction::GetSignerProof,
args: pk.map(|pk| invoke_args![pk]).unwrap_or_default(),
});
resp.decode().expect(ERR_ENGINE_DECODE_FAIL)
}
pub fn current_component_address() -> ComponentAddress {
let resp: InvokeResult = call_engine(EngineOp::CallerContextInvoke, &CallerContextInvokeArg {
action: CallerContextAction::GetComponentAddress,
args: invoke_args![],
});
resp.decode::<Option<ComponentAddress>>()
.expect(ERR_ENGINE_DECODE_FAIL)
.expect(ERR_NOT_IN_COMPONENT_CONTEXT)
}
pub fn allocate_component_address(
public_key_address: Option<RistrettoPublicKeyBytes>,
) -> ComponentAddressAllocation {
let resp: InvokeResult = call_engine(
EngineOp::AddressAllocationInvoke,
&AddressAllocationInvokeArg::CreateComponentAllocation {
public_key: public_key_address,
},
);
resp.decode().expect(ERR_ENGINE_DECODE_FAIL)
}
pub fn allocate_resource_address() -> ResourceAddressAllocation {
let resp: InvokeResult = call_engine(
EngineOp::AddressAllocationInvoke,
&AddressAllocationInvokeArg::CreateResourceAllocation,
);
resp.decode().expect(ERR_ENGINE_DECODE_FAIL)
}
}