Last active
July 30, 2024 23:52
-
-
Save kalepail/b87236499736e1d52a2763ba4dbde66b to your computer and use it in GitHub Desktop.
Beards are overrated anyways...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
use soroban_sdk::{ | |
auth::{Context, CustomAccountInterface}, | |
contract, contracterror, contractimpl, contracttype, | |
crypto::Hash, | |
panic_with_error, token, | |
xdr::ToXdr, | |
Address, BytesN, Env, Vec, | |
}; | |
#[contract] | |
pub struct Contract; | |
#[contracttype] | |
#[derive(Clone, Debug)] | |
pub struct Signature { | |
pub address: Address, | |
pub signature: BytesN<64>, | |
} | |
#[contracttype] | |
#[derive(Clone, Debug)] | |
pub enum DataKey { | |
In, | |
Out, | |
} | |
#[contracterror] | |
#[derive(Copy, Clone, Eq, PartialEq, Debug)] | |
#[repr(u32)] | |
pub enum Error { | |
TooBadSoSad = 1, | |
} | |
#[contractimpl] | |
impl Contract { | |
pub fn setup(env: Env, sac_in_address: Address, sac_out_address: Address) { | |
env.storage().temporary().set(&DataKey::In, &sac_in_address); | |
if !env.storage().instance().has(&DataKey::Out) { | |
env.storage() | |
.instance() | |
.set(&DataKey::Out, &sac_out_address); | |
} | |
} | |
} | |
#[contractimpl] | |
impl CustomAccountInterface for Contract { | |
type Error = Error; | |
type Signature = Signature; | |
#[allow(non_snake_case)] | |
fn __check_auth( | |
env: Env, | |
signature_payload: Hash<32>, | |
signature: Signature, | |
_auth_contexts: Vec<Context>, | |
) -> Result<(), Error> { | |
let address_bytes = signature.address.clone().to_xdr(&env); | |
let address_bytes = address_bytes.slice(address_bytes.len() - 32..); | |
let mut slice = [0u8; 32]; | |
address_bytes.copy_into_slice(&mut slice); | |
let public_key = BytesN::from_array(&env, &slice); | |
env.crypto() | |
.ed25519_verify(&public_key, &signature_payload.into(), &signature.signature); | |
let sac_in_client = token::TokenClient::new( | |
&env, | |
&env.storage() | |
.temporary() | |
.get::<DataKey, Address>(&DataKey::In) | |
.unwrap_or_else(|| panic_with_error!(&env, Error::TooBadSoSad)), | |
); | |
let sac_out_client = token::TokenClient::new( | |
&env, | |
&env.storage() | |
.instance() | |
.get::<DataKey, Address>(&DataKey::Out) | |
.unwrap_or_else(|| panic_with_error!(&env, Error::TooBadSoSad)), | |
); | |
sac_in_client.transfer( | |
&signature.address, | |
&env.current_contract_address(), | |
&10_000_000, | |
); | |
sac_out_client.transfer( | |
&env.current_contract_address(), | |
&signature.address, | |
&10_000_000, | |
); | |
Ok(()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Asset Contract | |
PUZZLE:GCUY5ECTBWZ5JGLXPPKSK4Y34RIMN2AEO4UNGQRRBF6AVVDSXSWLJLEN | |
CDGOXJBEKI3MQDB3J477NN3HAQBDCNK5YYB2ZKAG24US53RXW44QIF6Z | |
# Puzzle Contract | |
CAQCCHB3DXHNCBM63KZKDDB433X633HEKOUZCIEH32E74KPZQFVKMUHH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment