forked from jobbykingz/Verinode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
66 lines (57 loc) · 1.8 KB
/
lib.rs
File metadata and controls
66 lines (57 loc) · 1.8 KB
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
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Address, Env, String, Vec};
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProofVersion {
pub version: u32,
pub hash: String,
pub uri: String,
pub timestamp: u64,
pub author: Address,
pub message: String,
pub branch: String,
}
#[contract]
pub struct VerinodeContract;
#[contractimpl]
impl VerinodeContract {
// Add a new version to a proof
pub fn add_version(
env: Env,
proof_id: String,
hash: String,
uri: String,
author: Address,
message: String,
branch: String
) -> u32 {
author.require_auth();
let mut versions: Vec<ProofVersion> = env.storage().persistent().get(&proof_id).unwrap_or(Vec::new(&env));
let new_version_num = versions.len() + 1;
let version = ProofVersion {
version: new_version_num,
hash,
uri,
timestamp: env.ledger().timestamp(),
author,
message,
branch,
};
versions.push_back(version);
env.storage().persistent().set(&proof_id, &versions);
new_version_num
}
// Get the full history of a proof
pub fn get_history(env: Env, proof_id: String) -> Vec<ProofVersion> {
env.storage().persistent().get(&proof_id).unwrap_or(Vec::new(&env))
}
// Get a specific version
pub fn get_version(env: Env, proof_id: String, version: u32) -> Option<ProofVersion> {
let versions: Vec<ProofVersion> = env.storage().persistent().get(&proof_id).unwrap_or(Vec::new(&env));
if version == 0 || version > versions.len() {
None
} else {
Some(versions.get(version - 1).unwrap())
}
}
}