ab_example_contract_wallet/
lib.rs

1#![no_std]
2
3use ab_contracts_common::ContractError;
4use ab_contracts_common::env::Env;
5use ab_contracts_io_type::trivial_type::TrivialType;
6use ab_contracts_macros::contract;
7use ab_contracts_standards::tx_handler::{
8    TxHandler, TxHandlerPayload, TxHandlerSeal, TxHandlerSlots,
9};
10use ab_system_contract_simple_wallet_base::utils::{
11    authorize, change_public_key, execute, initialize_state,
12};
13use ab_transaction::TransactionHeader;
14
15#[derive(Debug, Copy, Clone, TrivialType)]
16#[repr(C)]
17pub struct ExampleWallet;
18
19#[contract]
20impl TxHandler for ExampleWallet {
21    #[view]
22    fn authorize(
23        #[env] env: &Env<'_>,
24        #[input] header: &TransactionHeader,
25        #[input] read_slots: &TxHandlerSlots,
26        #[input] write_slots: &TxHandlerSlots,
27        #[input] payload: &TxHandlerPayload,
28        #[input] seal: &TxHandlerSeal,
29    ) -> Result<(), ContractError> {
30        authorize(env, header, read_slots, write_slots, payload, seal)
31    }
32
33    #[update]
34    fn execute(
35        #[env] env: &mut Env<'_>,
36        #[input] header: &TransactionHeader,
37        #[input] read_slots: &TxHandlerSlots,
38        #[input] write_slots: &TxHandlerSlots,
39        #[input] payload: &TxHandlerPayload,
40        #[input] seal: &TxHandlerSeal,
41    ) -> Result<(), ContractError> {
42        execute(env, header, read_slots, write_slots, payload, seal)
43    }
44}
45
46/// TODO: Support upgrading wallet to a different implementation
47#[contract]
48impl ExampleWallet {
49    /// Initialize a wallet with specified public key
50    #[update]
51    pub fn initialize(
52        #[env] env: &mut Env<'_>,
53        #[input] public_key: &[u8; 32],
54    ) -> Result<(), ContractError> {
55        initialize_state(env, public_key)
56    }
57
58    /// Change public key to a different one
59    #[update]
60    pub fn change_public_key(
61        #[env] env: &mut Env<'_>,
62        #[input] public_key: &[u8; 32],
63    ) -> Result<(), ContractError> {
64        change_public_key(env, public_key)
65    }
66}