-
Notifications
You must be signed in to change notification settings - Fork 46
/
ergo_box.rs
192 lines (171 loc) · 6.6 KB
/
ergo_box.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! Box (aka coin, or an unspent output) is a basic concept of a UTXO-based cryptocurrency.
//! In Bitcoin, such an object is associated with some monetary value (arbitrary,
//! but with predefined precision, so we use integer arithmetic to work with the value),
//! and also a guarding script (aka proposition) to protect the box from unauthorized opening.
//!
//! In other way, a box is a state element locked by some proposition (ErgoTree).
//!
//! In Ergo, box is just a collection of registers, some with mandatory types and semantics,
//! others could be used by applications in any way.
//! We add additional fields in addition to amount and proposition~(which stored in the registers R0 and R1).
//! Namely, register R2 contains additional tokens (a sequence of pairs (token identifier, value)).
//! Register R3 contains height when block got included into the blockchain and also transaction
//! identifier and box index in the transaction outputs.
//! Registers R4-R9 are free for arbitrary usage.
//!
//! A transaction is unsealing a box. As a box can not be open twice, any further valid transaction
//! can not be linked to the same box.
use std::convert::TryFrom;
use chain::ergo_box::register::NonMandatoryRegisters;
use ergo_lib::chain;
use wasm_bindgen::prelude::*;
use crate::ast::Constant;
use crate::{contract::Contract, transaction::TxId};
pub mod box_builder;
/// ErgoBox candidate not yet included in any transaction on the chain
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ErgoBoxCandidate(chain::ergo_box::ErgoBoxCandidate);
#[wasm_bindgen]
impl ErgoBoxCandidate {
/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn register_value(&self, register_id: NonMandatoryRegisterId) -> Option<Constant> {
self.0
.additional_registers
.get(register_id.into())
.cloned()
.map(Constant::from)
}
}
impl Into<chain::ergo_box::ErgoBoxCandidate> for ErgoBoxCandidate {
fn into(self) -> chain::ergo_box::ErgoBoxCandidate {
self.0
}
}
/// Ergo box, that is taking part in some transaction on the chain
/// Differs with [`ErgoBoxCandidate`] by added transaction id and an index in the input of that transaction
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ErgoBox(chain::ergo_box::ErgoBox);
#[wasm_bindgen]
impl ErgoBox {
/// make a new box with:
/// `value` - amount of money associated with the box
/// `contract` - guarding contract([`Contract`]), which should be evaluated to true in order
/// to open(spend) this box
/// `creation_height` - height when a transaction containing the box is created.
/// `tx_id` - transaction id in which this box was "created" (participated in outputs)
/// `index` - index (in outputs) in the transaction
#[wasm_bindgen(constructor)]
pub fn new(
value: &BoxValue,
creation_height: u32,
contract: &Contract,
tx_id: &TxId,
index: u16,
) -> ErgoBox {
let chain_contract: chain::contract::Contract = contract.clone().into();
let b = chain::ergo_box::ErgoBox::new(
value.0,
chain_contract.ergo_tree(),
vec![],
NonMandatoryRegisters::empty(),
creation_height,
tx_id.clone().into(),
index,
);
ErgoBox(b)
}
/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn register_value(&self, register_id: NonMandatoryRegisterId) -> Option<Constant> {
self.0
.additional_registers
.get(register_id.into())
.cloned()
.map(Constant::from)
}
// JSON representation
// pub fn to_json(&self) -> Result<JsValue, JsValue> {
// JsValue::from_serde(&self.0).map_err(|e| JsValue::from_str(&format!("{}", e)))
// }
}
impl Into<chain::ergo_box::ErgoBox> for ErgoBox {
fn into(self) -> chain::ergo_box::ErgoBox {
self.0
}
}
/// Box value with with bound checks
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct BoxValue(chain::ergo_box::box_value::BoxValue);
#[wasm_bindgen]
impl BoxValue {
/// Recommended (safe) minimal box value to use in case box size estimation is unavailable.
/// Allows box size upto 2777 bytes with current min box value per byte of 360 nanoERGs
#[allow(non_snake_case)]
pub fn SAFE_USER_MIN() -> BoxValue {
BoxValue(chain::ergo_box::box_value::BoxValue::SAFE_USER_MIN)
}
/// Create from u32 with bounds check
pub fn from_u32(v: u32) -> Result<BoxValue, JsValue> {
Ok(BoxValue(
chain::ergo_box::box_value::BoxValue::try_from(v as u64)
.map_err(|e| JsValue::from_str(&format!("{}", e)))?,
))
}
}
impl From<BoxValue> for chain::ergo_box::box_value::BoxValue {
fn from(v: BoxValue) -> Self {
v.0
}
}
impl From<chain::ergo_box::box_value::BoxValue> for BoxValue {
fn from(v: chain::ergo_box::box_value::BoxValue) -> Self {
BoxValue(v)
}
}
/// newtype for box registers R4 - R9
#[wasm_bindgen]
#[repr(u8)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum NonMandatoryRegisterId {
/// id for R4 register
R4 = 4,
/// id for R5 register
R5 = 5,
/// id for R6 register
R6 = 6,
/// id for R7 register
R7 = 7,
/// id for R8 register
R8 = 8,
/// id for R9 register
R9 = 9,
}
impl NonMandatoryRegisterId {}
impl From<NonMandatoryRegisterId> for chain::ergo_box::register::NonMandatoryRegisterId {
fn from(v: NonMandatoryRegisterId) -> Self {
use chain::ergo_box::register::NonMandatoryRegisterId::*;
match v {
NonMandatoryRegisterId::R4 => R4,
NonMandatoryRegisterId::R5 => R5,
NonMandatoryRegisterId::R6 => R6,
NonMandatoryRegisterId::R7 => R7,
NonMandatoryRegisterId::R8 => R8,
NonMandatoryRegisterId::R9 => R9,
}
}
}
impl From<chain::ergo_box::register::NonMandatoryRegisterId> for NonMandatoryRegisterId {
fn from(v: chain::ergo_box::register::NonMandatoryRegisterId) -> Self {
use NonMandatoryRegisterId::*;
match v {
chain::ergo_box::register::NonMandatoryRegisterId::R4 => R4,
chain::ergo_box::register::NonMandatoryRegisterId::R5 => R5,
chain::ergo_box::register::NonMandatoryRegisterId::R6 => R6,
chain::ergo_box::register::NonMandatoryRegisterId::R7 => R7,
chain::ergo_box::register::NonMandatoryRegisterId::R8 => R8,
chain::ergo_box::register::NonMandatoryRegisterId::R9 => R9,
}
}
}