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
use std::convert::TryFrom;
use ergo_lib::ergotree_ir::chain::base16_bytes::Base16DecodedBytes;
use ergo_lib::ergotree_ir::serialization::SigmaSerializable;
use wasm_bindgen::prelude::*;
extern crate derive_more;
use derive_more::{From, Into};
use crate::{ast::Constant, error_conversion::to_js};
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct ErgoTree(ergo_lib::ergotree_ir::ergo_tree::ErgoTree);
#[wasm_bindgen]
impl ErgoTree {
pub fn from_base16_bytes(s: &str) -> Result<ErgoTree, JsValue> {
let bytes = Base16DecodedBytes::try_from(s.to_string()).map_err(to_js)?;
ErgoTree::from_bytes(bytes.0)
}
pub fn from_bytes(data: Vec<u8>) -> Result<ErgoTree, JsValue> {
ergo_lib::ergotree_ir::ergo_tree::ErgoTree::sigma_parse_bytes(&data)
.map(ErgoTree)
.map_err(to_js)
}
pub fn sigma_serialize_bytes(&self) -> Result<Vec<u8>, JsValue> {
self.0.sigma_serialize_bytes().map_err(to_js)
}
pub fn to_base16_bytes(&self) -> Result<String, JsValue> {
self.0.to_base16_bytes().map_err(to_js)
}
pub fn constants_len(&self) -> Result<usize, JsValue> {
self.0.constants_len().map_err(to_js)
}
pub fn get_constant(&self, index: usize) -> Result<Option<Constant>, JsValue> {
self.0
.get_constant(index)
.map(|opt| opt.map(|c| c.into()))
.map_err(to_js)
}
pub fn with_constant(self, index: usize, constant: &Constant) -> Result<ErgoTree, JsValue> {
self.0
.with_constant(index, constant.clone().into())
.map(ErgoTree)
.map_err(to_js)
}
pub fn template_bytes(&self) -> Result<Vec<u8>, JsValue> {
self.0.template_bytes().map_err(to_js)
}
}