Skip to content

Commit

Permalink
remove Address::p2pk_from_ergo_tree();
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Nov 17, 2020
1 parent f377726 commit 720708a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 50 deletions.
10 changes: 7 additions & 3 deletions bindings/ergo-lib-wasm/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ pub struct Address(chain::address::Address);

#[wasm_bindgen]
impl Address {
/// Create a P2PK address from an ergo tree if ProveDlog is the root of the tree, otherwise returns an error
pub fn p2pk_from_ergo_tree(ergo_tree: &ErgoTree) -> Result<Address, JsValue> {
chain::address::Address::p2pk_from_ergo_tree(&ergo_tree.clone().into())
/// Re-create the address from ErgoTree that was built from the address
///
/// At some point in the past a user entered an address from which the ErgoTree was built.
/// Re-create the address from this ErgoTree.
/// `tree` - ErgoTree that was created from an Address
pub fn recreate_from_ergo_tree(ergo_tree: &ErgoTree) -> Result<Address, JsValue> {
chain::address::Address::recreate_from_ergo_tree(&ergo_tree.clone().into())
.map(Address)
.map_err(|e| JsValue::from_str(&format!("{}", e)))
}
Expand Down
13 changes: 11 additions & 2 deletions bindings/ergo-lib-wasm/tests/test_address.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import {
ErgoTree
} from '../pkg/ergo_lib_wasm';

it('new_p2pk from base16 ergo tree', async () => {
it('P2PK from base16 ergo tree', async () => {
// ProveDlog in ErgoTree root
let tree_bytes_base16_str = '0008cd0327e65711a59378c59359c3e1d0f7abe906479eccb76094e50fe79d743ccc15e6';
let tree = ErgoTree.from_base16_bytes(tree_bytes_base16_str);
let addr = Address.p2pk_from_ergo_tree(tree);
let addr = Address.recreate_from_ergo_tree(tree);
assert(addr != null);
});

it('P2S from base16 ergo tree', async () => {
// Non ProveDlog in ErgoTree root
let tree_bytes_base16_str = "100204a00b08cd021dde34603426402615658f1d970cfa7c7bd92ac81a8b16eeebff264d59ce4604ea02d192a39a8cc7a70173007301";
let tree = ErgoTree.from_base16_bytes(tree_bytes_base16_str);
let addr = Address.recreate_from_ergo_tree(tree);
assert(addr != null);
});
46 changes: 1 addition & 45 deletions ergo-lib/src/chain/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::digest32;
use crate::types::SType;
use crate::{
ast::{Constant, ConstantVal, Expr},
ast::{Constant, Expr},
ergo_tree::{ErgoTree, ErgoTreeParsingError},
serialization::{SerializationError, SigmaSerializable},
sigma_protocol::{
Expand Down Expand Up @@ -70,29 +70,6 @@ pub enum Address {
}

impl Address {
/// Create a P2PK address from an ergo tree if ProveDlog is the root of the tree, otherwise returns an error
pub fn p2pk_from_ergo_tree(tree: &ErgoTree) -> Result<Address, AddressError> {
let expr = &*tree.proposition()?;
match expr {
Expr::Const(Constant {
tpe: _,
v: ConstantVal::SigmaProp(sp),
}) => match sp.value() {
SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDlog(
prove_dlog,
)) => Ok(Address::P2PK(prove_dlog.clone())),
_ => Err(AddressError::UnexpectedErgoTree(
tree.clone(),
"Expected ErgoTree with ProveDlog as root".to_string(),
)),
},
_ => Err(AddressError::UnexpectedErgoTree(
tree.clone(),
"Expected ErgoTree with ProveDlog as root".to_string(),
)),
}
}

/// Create a P2PK address from serialized PK bytes(EcPoint/GroupElement)
pub fn p2pk_from_pk_bytes(bytes: &[u8]) -> Result<Address, SerializationError> {
EcPoint::sigma_parse_bytes(bytes.to_vec())
Expand Down Expand Up @@ -342,7 +319,6 @@ impl AddressEncoder {
#[cfg(test)]
mod tests {
use crate::chain::Base16DecodedBytes;
use crate::types::SType;

use super::*;
use proptest::prelude::*;
Expand All @@ -368,28 +344,8 @@ mod tests {
}
}

#[test]
fn new_p2pk_non_provedlog_error() {
let tree = ErgoTree::from(Rc::new(Expr::Const(Constant {
tpe: SType::SBoolean,
v: ConstantVal::Boolean(true),
})));
assert!(Address::p2pk_from_ergo_tree(&tree).is_err());
}

proptest! {

#[test]
fn ergo_tree_p2pk_roundtrip(prove_dlog in any::<ProveDlog>()) {
let encoder = AddressEncoder::new(NetworkPrefix::Testnet);
let address = Address::P2PK(prove_dlog);
let ergo_tree = address.script().unwrap();
let address_copy = Address::p2pk_from_ergo_tree(&ergo_tree).unwrap();
let encoded_addr = encoder.address_to_str(&address);
let encoded_addr_copy = encoder.address_to_str(&address_copy);
prop_assert_eq![encoded_addr, encoded_addr_copy];
}

#[test]
fn str_roundtrip(v in any::<Address>()) {
let encoder = AddressEncoder::new(NetworkPrefix::Testnet);
Expand Down

0 comments on commit 720708a

Please sign in to comment.