-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlib.rs
76 lines (59 loc) · 2.4 KB
/
lib.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
#![no_std]
use soroban_sdk::{
contract, contracterror, contractimpl, symbol_short, Address, Bytes, BytesN, Env, Symbol,
};
mod wallet {
use soroban_sdk::auth::Context;
soroban_sdk::contractimport!(file = "../target/wasm32-unknown-unknown/release/webauthn_wallet.wasm");
}
#[contract]
pub struct Contract;
#[contracterror]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Error {
NotInitialized = 1,
AlreadyInitialized = 2,
}
const WEEK_OF_LEDGERS: u32 = 60 * 60 * 24 / 5 * 7;
const STORAGE_KEY_WASM_HASH: Symbol = symbol_short!("hash");
/* NOTE
- We don't have an upgrade function here because if we want to make a new wallet printer we should just deploy an entirely new one
This ensures some safety so a factory can't sneaky update the wallets it's printing
One downside is if a factory turns out to be printing bugged wallets there's no way to shut the printer down
*/
/* TODO
- For the first NOTE reason above we should consider a self destruct method where a contract can break itself such that it cannot deploy any more wallets
This is important in the case a bug is found in the underlying smart wallet contract code
Could be a simple instance variable or maybe an upgrade to a wasm that's entirely empty and thus always fails
@Later
*/
#[contractimpl]
impl Contract {
pub fn init(env: Env, wasm_hash: BytesN<32>) -> Result<(), Error> {
if env.storage().instance().has(&STORAGE_KEY_WASM_HASH) {
return Err(Error::AlreadyInitialized);
}
let max_ttl = env.storage().max_ttl();
env.storage()
.instance()
.set(&STORAGE_KEY_WASM_HASH, &wasm_hash);
env.storage()
.instance()
.extend_ttl(max_ttl - WEEK_OF_LEDGERS, max_ttl);
Ok(())
}
pub fn deploy(env: Env, salt: BytesN<32>, id: Bytes, pk: BytesN<65>) -> Result<Address, Error> {
let wasm_hash = env
.storage()
.instance()
.get::<Symbol, BytesN<32>>(&STORAGE_KEY_WASM_HASH)
.ok_or(Error::NotInitialized)?;
let address = env.deployer().with_current_contract(salt).deploy(wasm_hash);
wallet::Client::new(&env, &address).add(&id, &pk, &true);
let max_ttl = env.storage().max_ttl();
env.storage()
.instance()
.extend_ttl(max_ttl - WEEK_OF_LEDGERS, max_ttl);
Ok(address)
}
}