Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;

use crate::identifier::Identifier;
use crate::util::hash::hash;
use crate::util::vec::vec_to_array;
use crate::{
errors::NonConsensusError, identifier::Identifier, util::hash::hash, util::vec::vec_to_array,
};

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChainAssetLockProof {
#[serde(rename = "type")]
asset_lock_type: u8,
Expand Down Expand Up @@ -40,10 +41,8 @@ impl ChainAssetLockProof {
}

/// Create identifier
pub fn create_identifier(&self) -> Identifier {
return Identifier::new(
vec_to_array(hash(self.out_point()).as_ref())
.expect("Expected hash function to give a 32 byte output"),
);
pub fn create_identifier(&self) -> Result<Identifier, NonConsensusError> {
let array = vec_to_array(hash(self.out_point).as_ref())?;
Ok(Identifier::new(array))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ impl AssetLockProof {
pub fn create_identifier(&self) -> Result<Identifier, NonConsensusError> {
match self {
AssetLockProof::Instant(instant_proof) => instant_proof.create_identifier(),
AssetLockProof::Chain(chain_proof) => {
// TODO: fix return type
Ok(chain_proof.create_identifier())
}
AssetLockProof::Chain(chain_proof) => chain_proof.create_identifier(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl IdentityCreateTransition {
.iter()
.map(|val| serde_json::from_value(val.clone()))
.collect::<Result<Vec<IdentityPublicKey>, serde_json::Error>>()?;
state_transition = state_transition.set_public_keys(keys);
state_transition.set_public_keys(keys);
}

if let Some(proof) = transition_map.get(property_names::ASSET_LOCK_PROOF) {
Expand Down Expand Up @@ -142,14 +142,14 @@ impl IdentityCreateTransition {
}

/// Replaces existing set of public keys with a new one
pub fn set_public_keys(mut self, public_keys: Vec<IdentityPublicKey>) -> Self {
pub fn set_public_keys(&mut self, public_keys: Vec<IdentityPublicKey>) -> &mut Self {
self.public_keys = public_keys;

self
}

/// Adds public keys to the existing public keys array
pub fn add_public_keys(mut self, public_keys: &mut Vec<IdentityPublicKey>) -> Self {
pub fn add_public_keys(&mut self, public_keys: &mut Vec<IdentityPublicKey>) -> &mut Self {
self.public_keys.append(public_keys);

self
Expand Down
12 changes: 4 additions & 8 deletions packages/wasm-dpp/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ use crate::utils::to_vec_of_serde_values;
use crate::IdentityPublicKeyWasm;
use crate::MetadataWasm;

use state_transition::*;

pub mod state_transition;

#[wasm_bindgen(js_name=Identity)]
#[derive(Clone)]
pub struct IdentityWasm(Identity);

#[wasm_bindgen(js_name=AssetLockProof)]
pub struct AssetLockProofWasm(AssetLockProof);
impl From<AssetLockProof> for AssetLockProofWasm {
fn from(v: AssetLockProof) -> Self {
AssetLockProofWasm(v)
}
}

#[wasm_bindgen(js_class=Identity)]
impl IdentityWasm {
#[wasm_bindgen(constructor)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use wasm_bindgen::prelude::*;

use crate::{
buffer::Buffer,
errors::{from_dpp_err, RustConversionError},
identifier::IdentifierWrapper,
with_js_error,
};
use dpp::identity::state_transition::asset_lock_proof::chain::ChainAssetLockProof;
use dpp::util::string_encoding;
use dpp::util::string_encoding::Encoding;

#[wasm_bindgen(js_name=ChainAssetLockProof)]
pub struct ChainAssetLockProofWasm(ChainAssetLockProof);

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ChainAssetLockProofParams {
core_chain_locked_height: u32,
out_point: Vec<u8>,
}

impl From<ChainAssetLockProof> for ChainAssetLockProofWasm {
fn from(v: ChainAssetLockProof) -> Self {
ChainAssetLockProofWasm(v)
}
}

impl From<ChainAssetLockProofWasm> for ChainAssetLockProof {
fn from(v: ChainAssetLockProofWasm) -> Self {
v.0
}
}

#[wasm_bindgen(js_class = ChainAssetLockProof)]
impl ChainAssetLockProofWasm {
#[wasm_bindgen(constructor)]
pub fn new(raw_parameters: JsValue) -> Result<ChainAssetLockProofWasm, JsValue> {
let parameters: ChainAssetLockProofParams =
with_js_error!(serde_wasm_bindgen::from_value(raw_parameters))?;

let out_point: [u8; 36] = parameters.out_point.try_into().map_err(|_| {
RustConversionError::Error(String::from("outPoint must be a 36 byte array"))
.to_js_value()
})?;

let chain_asset_lock_proof =
ChainAssetLockProof::new(parameters.core_chain_locked_height, out_point);

Ok(ChainAssetLockProofWasm(chain_asset_lock_proof))
}

#[wasm_bindgen(js_name=getType)]
pub fn get_type(&self) -> u8 {
ChainAssetLockProof::asset_lock_type()
}

#[wasm_bindgen(js_name=getCoreChainLockedHeight)]
pub fn get_core_chain_locked_height(&self) -> u32 {
self.0.core_chain_locked_height()
}

#[wasm_bindgen(js_name=getOutPoint)]
pub fn get_out_point(&self) -> Buffer {
Buffer::from_bytes(self.0.out_point().as_slice())
}

#[wasm_bindgen(js_name=toJSON)]
pub fn to_json(&self) -> Result<JsValue, JsValue> {
let js_object = self.to_object()?;

let out_point_base64 = string_encoding::encode(self.0.out_point(), Encoding::Base64);

js_sys::Reflect::set(
&js_object,
&"outPoint".to_owned().into(),
&JsValue::from(out_point_base64),
)?;

Ok(js_object)
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self) -> Result<JsValue, JsValue> {
let asset_lock_json =
serde_json::to_value(self.0.clone()).map_err(|e| from_dpp_err(e.into()))?;

let asset_lock_json_string =
serde_json::to_string(&asset_lock_json).map_err(|e| from_dpp_err(e.into()))?;
let js_object = js_sys::JSON::parse(&asset_lock_json_string)?;

let out_point = self.get_out_point();

js_sys::Reflect::set(
&js_object,
&"outPoint".to_owned().into(),
&JsValue::from(out_point),
)?;

Ok(js_object)
}

#[wasm_bindgen(js_name=createIdentifier)]
pub fn create_identifier(&self) -> Result<IdentifierWrapper, JsValue> {
let identifier = self
.0
.create_identifier()
.map_err(|e| from_dpp_err(e.into()))?;
Ok(identifier.into())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use chain_asset_lock_proof::*;

pub mod chain_asset_lock_proof;
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
use dpp::{
dashcore::{
blockdata::{script::Script, transaction::txout::TxOut},
consensus::encode::serialize,
},
util::string_encoding,
util::string_encoding::Encoding,
};

use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use wasm_bindgen::prelude::*;

use crate::{
buffer::Buffer,
errors::{from_dpp_err, RustConversionError},
identifier::IdentifierWrapper,
with_js_error,
};
use dpp::identity::state_transition::asset_lock_proof::instant::{
InstantAssetLockProof, RawInstantLock,
};

#[derive(Serialize, Deserialize)]
#[serde(remote = "TxOut")]
struct TxOutJS {
#[serde(rename = "satoshis")]
value: u64,
#[serde(rename = "script")]
script_pubkey: Script,
}

#[derive(Serialize)]
struct TxOutSerdeHelper<'a>(#[serde(with = "TxOutJS")] &'a TxOut);

#[derive(Debug, Deserialize, Serialize)]
#[wasm_bindgen(js_name=InstantAssetLockProof)]
pub struct InstantAssetLockProofWasm(InstantAssetLockProof);

impl From<InstantAssetLockProof> for InstantAssetLockProofWasm {
fn from(v: InstantAssetLockProof) -> Self {
InstantAssetLockProofWasm(v)
}
}

impl From<InstantAssetLockProofWasm> for InstantAssetLockProof {
fn from(v: InstantAssetLockProofWasm) -> Self {
v.0
}
}

#[wasm_bindgen(js_class = InstantAssetLockProof)]
impl InstantAssetLockProofWasm {
#[wasm_bindgen(constructor)]
pub fn new(raw_parameters: JsValue) -> Result<InstantAssetLockProofWasm, JsValue> {
let raw_instant_lock: RawInstantLock =
with_js_error!(serde_wasm_bindgen::from_value(raw_parameters))?;

let instant_asset_lock_proof: InstantAssetLockProof =
raw_instant_lock.try_into().map_err(|_| {
RustConversionError::Error(String::from("object passed is not a raw Instant Lock"))
.to_js_value()
})?;

Ok(instant_asset_lock_proof.into())
}

#[wasm_bindgen(js_name=getType)]
pub fn get_type(&self) -> u8 {
self.0.asset_lock_type()
}

#[wasm_bindgen(js_name=getOutputIndex)]
pub fn get_output_index(&self) -> usize {
self.0.output_index()
}

#[wasm_bindgen(js_name=getOutPoint)]
pub fn get_out_point(&self) -> Option<Buffer> {
self.0
.out_point()
.map(|out_point| Buffer::from_bytes(out_point.as_slice()))
}

#[wasm_bindgen(js_name=getOutput)]
pub fn get_output(&self) -> Result<JsValue, JsValue> {
let output = self.0.output().unwrap();
let output_json_string =
serde_json::to_string(&TxOutSerdeHelper(output)).map_err(|e| from_dpp_err(e.into()))?;

let js_object = js_sys::JSON::parse(&output_json_string)?;
Ok(js_object)
}

#[wasm_bindgen(js_name=createIdentifier)]
pub fn create_identifier(&self) -> Result<IdentifierWrapper, JsValue> {
let identifier = self
.0
.create_identifier()
.map_err(|e| from_dpp_err(e.into()))?;
Ok(identifier.into())
}

#[wasm_bindgen(js_name=getInstantLock)]
pub fn get_instant_lock(&self) -> Buffer {
let instant_lock = self.0.instant_lock();
let serialized_instant_lock = serialize(instant_lock);
Buffer::from_bytes(serialized_instant_lock.as_slice())
}

#[wasm_bindgen(js_name=getTransaction)]
pub fn get_transaction(&self) -> Buffer {
let transaction = self.0.transaction();
let serialized_transaction = serialize(transaction);
Buffer::from_bytes(serialized_transaction.as_slice())
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self) -> Result<JsValue, JsValue> {
let asset_lock_json =
serde_json::to_value(self.0.clone()).map_err(|e| from_dpp_err(e.into()))?;

let asset_lock_json_string =
serde_json::to_string(&asset_lock_json).map_err(|e| from_dpp_err(e.into()))?;
let js_object = js_sys::JSON::parse(&asset_lock_json_string)?;

let transaction = self.get_transaction();
let instant_lock = self.get_instant_lock();

js_sys::Reflect::set(
&js_object,
&"transaction".to_owned().into(),
&JsValue::from(transaction),
)?;

js_sys::Reflect::set(
&js_object,
&"instantLock".to_owned().into(),
&JsValue::from(instant_lock),
)?;

Ok(js_object)
}

#[wasm_bindgen(js_name=toJSON)]
pub fn to_json(&self) -> Result<JsValue, JsValue> {
let js_object = self.to_object()?;

let transaction = self.0.transaction();
let serialized_transaction = serialize(transaction);

let instant_lock = self.0.instant_lock();
let serialized_instant_lock = serialize(instant_lock);

let instant_lock_base64 =
string_encoding::encode(serialized_instant_lock.as_slice(), Encoding::Base64);

let mut transaction_hex = String::new();
for &byte in serialized_transaction.as_slice() {
transaction_hex.push_str(&format!("{:02x}", byte));
}

js_sys::Reflect::set(
&js_object,
&"transaction".to_owned().into(),
&JsValue::from(transaction_hex),
)?;

js_sys::Reflect::set(
&js_object,
&"instantLock".to_owned().into(),
&JsValue::from(instant_lock_base64),
)?;

Ok(js_object)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use instant_asset_lock_proof::*;

pub mod instant_asset_lock_proof;
Loading