Skip to content

Commit

Permalink
add ErgoBoxCandidate::R4-R9 in WASM;
Browse files Browse the repository at this point in the history
remove ErgoBox*::register_value();
  • Loading branch information
greenhat committed Oct 12, 2020
1 parent d96a2e3 commit 8053fa1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
64 changes: 57 additions & 7 deletions bindings/ergo-lib-wasm/src/ergo_box.rs
Expand Up @@ -34,7 +34,51 @@ pub mod box_builder;
pub struct ErgoBoxCandidate(chain::ergo_box::ErgoBoxCandidate);

#[wasm_bindgen]
impl ErgoBoxCandidate {}
impl ErgoBoxCandidate {
/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r4(&self) -> Option<Constant> {
self.0
.additional_registers
.get(NonMandatoryRegisterId::R4)
.cloned()
.map(Constant::from)
}
/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r5(&self) -> Option<Constant> {
self.0
.additional_registers
.get(NonMandatoryRegisterId::R5)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r6(&self) -> Option<Constant> {
self.0
.additional_registers
.get(NonMandatoryRegisterId::R6)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r7(&self) -> Option<Constant> {
self.0
.additional_registers
.get(NonMandatoryRegisterId::R7)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r9(&self) -> Option<Constant> {
self.0
.additional_registers
.get(NonMandatoryRegisterId::R9)
.cloned()
.map(Constant::from)
}
}

impl Into<chain::ergo_box::ErgoBoxCandidate> for ErgoBoxCandidate {
fn into(self) -> chain::ergo_box::ErgoBoxCandidate {
Expand Down Expand Up @@ -81,47 +125,53 @@ impl ErgoBox {
/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r4(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R4)
.additional_registers
.get(NonMandatoryRegisterId::R4)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r5(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R5)
.additional_registers
.get(NonMandatoryRegisterId::R5)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r6(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R6)
.additional_registers
.get(NonMandatoryRegisterId::R6)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r7(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R7)
.additional_registers
.get(NonMandatoryRegisterId::R7)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r8(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R8)
.additional_registers
.get(NonMandatoryRegisterId::R8)
.cloned()
.map(Constant::from)
}

/// Returns value (ErgoTree constant) stored in the register or None if the register is empty
pub fn r9(&self) -> Option<Constant> {
self.0
.register_value(NonMandatoryRegisterId::R9)
.additional_registers
.get(NonMandatoryRegisterId::R9)
.cloned()
.map(Constant::from)
}
Expand Down
9 changes: 1 addition & 8 deletions ergo-lib/src/chain/ergo_box.rs
Expand Up @@ -5,16 +5,14 @@ pub mod box_id;
pub mod box_value;
pub mod register;

use self::register::NonMandatoryRegisterId;

#[cfg(feature = "json")]
use super::json;
use super::{
digest32::blake2b256_hash,
token::{TokenAmount, TokenId},
transaction::TxId,
};
use crate::ast::Constant;

use crate::{
ergo_tree::ErgoTree,
serialization::{
Expand Down Expand Up @@ -141,11 +139,6 @@ impl ErgoBox {
}
}

/// Return value (ErgoTree constant) stored in a register with a given register index (R4..R9)
pub fn register_value(&self, reg_id: NonMandatoryRegisterId) -> Option<&Constant> {
self.additional_registers.get(reg_id)
}

fn calc_box_id(&self) -> BoxId {
let bytes = self.sigma_serialise_bytes();
BoxId(blake2b256_hash(&bytes))
Expand Down

0 comments on commit 8053fa1

Please sign in to comment.