use std::convert::TryInto;
use ergo_lib::wallet::derivation_path::ChildIndexNormal;
use ergo_lib::wallet::ext_pub_key::ChainCode;
use ergo_lib::wallet::ext_pub_key::ExtPubKey as InnerExtPubKey;
use ergo_lib::wallet::ext_pub_key::PubKeyBytes;
use wasm_bindgen::prelude::*;
use super::derivation_path::DerivationPath;
use crate::address::Address;
use crate::error_conversion::to_js;
extern crate derive_more;
use derive_more::{From, Into};
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct ExtPubKey(InnerExtPubKey);
#[wasm_bindgen]
impl ExtPubKey {
pub fn new(
public_key_bytes: &[u8],
chain_code: &[u8],
derivation_path: &DerivationPath,
) -> Result<ExtPubKey, JsValue> {
let public_key_bytes: PubKeyBytes = public_key_bytes.try_into().map_err(to_js)?;
let chain_code: ChainCode = chain_code.try_into().map_err(to_js)?;
Ok(ExtPubKey(
InnerExtPubKey::new(public_key_bytes, chain_code, derivation_path.clone().into())
.map_err(to_js)?,
))
}
pub fn child(&self, index: u32) -> Result<ExtPubKey, JsValue> {
let index = ChildIndexNormal::normal(index).map_err(to_js)?;
Ok(self.0.child(index).into())
}
pub fn derive(&self, path: DerivationPath) -> Result<ExtPubKey, JsValue> {
Ok(self.0.derive(path.into()).map_err(to_js)?.into())
}
pub fn to_address(&self) -> Address {
let address: ergo_lib::ergotree_ir::chain::address::Address = self.0.clone().into();
address.into()
}
pub fn chain_code(&self) -> Vec<u8> {
self.0.chain_code().into()
}
pub fn pub_key_bytes(&self) -> Vec<u8> {
self.0.pub_key_bytes().into()
}
}