Skip to content

Commit 5d79393

Browse files
authored
Merge pull request #148 from owl352/feat/add-structs
Feat re-implement structs on napi pt.1
2 parents eebaf8e + 78c146c commit 5d79393

File tree

47 files changed

+5390
-207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+5390
-207
lines changed

Cargo.lock

Lines changed: 532 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ crate-type = ["cdylib", "lib"]
1010
[dependencies]
1111
dpp = { git = "https://github.com/owl352/platform", rev = "3f13188", default-features = false, features = [
1212
"identity-value-conversion",
13-
"identity-serialization"
13+
"identity-serialization",
14+
"state-transition-signing",
15+
"platform-value",
16+
"document-json-conversion"
1417
] }
1518
drive = { git = "https://github.com/owl352/platform", rev = "3f13188", default-features = false }
16-
napi = {version = "3.4.0", features = ["napi3"], default-features = false}
17-
napi-derive = "3.3.0"
19+
napi = {version = "3.8.1", features = ["napi3"], default-features = false}
20+
napi-derive = "3.5.0"
21+
serde_json = { version = "1.0", features = ["preserve_order"] }
22+
hex = "0.4.3"
23+
sha2 = "0.10.8"
1824

1925
[build-dependencies]
20-
napi-build = "2.2.4"
26+
napi-build = "2.3.1"
2127

2228
[profile.release]
2329
lto = "fat"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"babel": "^6.23.0",
4040
"babel-jest": "^30.2.0",
4141
"core-js": "^3.41.0",
42-
"ferric-cli": "^0.3.9",
42+
"ferric-cli": "0.3.9",
4343
"jest": "^30.2.0",
4444
"mocha": "^11.1.0",
4545
"node-addon-api": "^8.5.0",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use dpp::identity::state_transition::asset_lock_proof::chain::ChainAssetLockProof;
2+
use napi::{Status, bindgen_prelude::Uint8Array};
3+
use napi_derive::napi;
4+
5+
use crate::{asset_lock_proof::outpoint::OutPointNAPI, identifier::IdentifierNAPI};
6+
7+
#[napi(object, js_name = "ChainAssetLockProofParams")]
8+
pub struct ChainAssetLockProofParams {
9+
#[napi(js_name = "coreChainLockedHeight")]
10+
pub core_chain_locked_height: u32,
11+
#[napi(js_name = "outPoint")]
12+
pub out_point: Uint8Array,
13+
}
14+
15+
#[napi(js_name = "ChainAssetLockProofNAPI")]
16+
#[derive(Clone)]
17+
pub struct ChainAssetLockProofNAPI(ChainAssetLockProof);
18+
19+
impl From<ChainAssetLockProofNAPI> for ChainAssetLockProof {
20+
fn from(chain_lock: ChainAssetLockProofNAPI) -> Self {
21+
chain_lock.0
22+
}
23+
}
24+
25+
impl From<ChainAssetLockProof> for ChainAssetLockProofNAPI {
26+
fn from(chain_lock: ChainAssetLockProof) -> Self {
27+
ChainAssetLockProofNAPI(chain_lock)
28+
}
29+
}
30+
31+
#[napi]
32+
impl ChainAssetLockProofNAPI {
33+
#[napi(constructor)]
34+
pub fn new(core_chain_locked_height: u32, out_point: &OutPointNAPI) -> ChainAssetLockProofNAPI {
35+
ChainAssetLockProofNAPI(ChainAssetLockProof {
36+
core_chain_locked_height,
37+
out_point: out_point.clone().into(),
38+
})
39+
}
40+
41+
#[napi(js_name = "fromRawObject")]
42+
pub fn from_raw_value(
43+
raw_asset_lock_proof: ChainAssetLockProofParams,
44+
) -> Result<ChainAssetLockProofNAPI, napi::Error> {
45+
let vec_outpoint = raw_asset_lock_proof.out_point.to_vec();
46+
47+
let out_point: [u8; 36] = vec_outpoint.try_into().map_err(|_| {
48+
napi::Error::new(Status::GenericFailure, "outPoint must be a 36 byte array")
49+
})?;
50+
51+
let rs_proof =
52+
ChainAssetLockProof::new(raw_asset_lock_proof.core_chain_locked_height, out_point);
53+
54+
Ok(ChainAssetLockProofNAPI(rs_proof))
55+
}
56+
57+
#[napi(setter, js_name = "coreChainLockedHeight")]
58+
pub fn set_core_chain_locked_height(&mut self, core_chain_locked_height: u32) {
59+
self.0.core_chain_locked_height = core_chain_locked_height;
60+
}
61+
62+
#[napi(setter, js_name = "outPoint")]
63+
pub fn set_out_point(&mut self, outpoint: &OutPointNAPI) {
64+
self.0.out_point = outpoint.clone().into();
65+
}
66+
67+
#[napi(getter, js_name = "coreChainLockedHeight")]
68+
pub fn get_core_chain_locked_height(&self) -> u32 {
69+
self.0.core_chain_locked_height
70+
}
71+
72+
#[napi(getter, js_name = "outPoint")]
73+
pub fn get_out_point(&self) -> OutPointNAPI {
74+
self.0.out_point.into()
75+
}
76+
77+
#[napi(js_name = "createIdentityId")]
78+
pub fn create_identifier(&self) -> IdentifierNAPI {
79+
let identifier = self.0.create_identifier();
80+
81+
identifier.into()
82+
}
83+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use dpp::dashcore::consensus::{deserialize, serialize};
2+
use dpp::dashcore::{InstantLock, Transaction};
3+
use dpp::identity::state_transition::asset_lock_proof::InstantAssetLockProof;
4+
use napi::bindgen_prelude::Uint8Array;
5+
use napi_derive::napi;
6+
7+
use crate::asset_lock_proof::outpoint::OutPointNAPI;
8+
use crate::asset_lock_proof::tx_out::TxOutNAPI;
9+
use crate::identifier::IdentifierNAPI;
10+
use crate::instant_lock::InstantLockNAPI;
11+
use crate::utils::WithJsError;
12+
13+
#[napi(object, js_name = "InstantAssetLockProofRAW")]
14+
pub struct InstantAssetLockProofRAW {
15+
#[napi(js_name = "instantLock")]
16+
pub instant_lock: Uint8Array,
17+
#[napi(js_name = "transaction")]
18+
pub transaction: Uint8Array,
19+
#[napi(js_name = "outputIndex")]
20+
pub output_index: u32,
21+
}
22+
23+
#[derive(Clone)]
24+
#[napi(js_name = "InstantAssetLockProofNAPI")]
25+
pub struct InstantAssetLockProofNAPI(InstantAssetLockProof);
26+
27+
impl From<InstantAssetLockProofNAPI> for InstantAssetLockProof {
28+
fn from(proof: InstantAssetLockProofNAPI) -> Self {
29+
proof.0
30+
}
31+
}
32+
33+
impl From<InstantAssetLockProof> for InstantAssetLockProofNAPI {
34+
fn from(proof: InstantAssetLockProof) -> Self {
35+
InstantAssetLockProofNAPI(proof)
36+
}
37+
}
38+
39+
#[napi]
40+
impl InstantAssetLockProofNAPI {
41+
#[napi(constructor)]
42+
pub fn new(
43+
instant_lock: Uint8Array,
44+
transaction: Uint8Array,
45+
output_index: u32,
46+
) -> Result<InstantAssetLockProofNAPI, napi::Error> {
47+
let instant_lock_bytes = instant_lock.to_vec();
48+
let transaction_bytes = transaction.to_vec();
49+
50+
let instant_lock: InstantLock = deserialize(instant_lock_bytes.as_slice())
51+
.map_err(|err| napi::Error::new(napi::Status::InvalidArg, err.to_string()))?;
52+
let transaction: Transaction = deserialize(transaction_bytes.as_slice())
53+
.map_err(|err| napi::Error::new(napi::Status::InvalidArg, err.to_string()))?;
54+
55+
Ok(InstantAssetLockProofNAPI(InstantAssetLockProof {
56+
instant_lock,
57+
transaction,
58+
output_index,
59+
}))
60+
}
61+
62+
#[napi(js_name = "fromRawObject ")]
63+
pub fn from_object(
64+
value: InstantAssetLockProofRAW,
65+
) -> Result<InstantAssetLockProofNAPI, napi::Error> {
66+
InstantAssetLockProofNAPI::new(value.instant_lock, value.transaction, value.output_index)
67+
}
68+
69+
#[napi(js_name = "getOutput")]
70+
pub fn get_output(&self) -> Option<TxOutNAPI> {
71+
match self.0.output() {
72+
Some(output) => Some(output.clone().into()),
73+
None => None,
74+
}
75+
}
76+
77+
#[napi(js_name = "getOutPoint")]
78+
pub fn get_out_point(&self) -> Option<OutPointNAPI> {
79+
match self.0.out_point() {
80+
Some(output) => Some(output.clone().into()),
81+
None => None,
82+
}
83+
}
84+
85+
#[napi(getter, js_name = "outputIndex")]
86+
pub fn get_output_index(&self) -> u32 {
87+
self.0.output_index()
88+
}
89+
90+
#[napi(getter, js_name = "instantLock")]
91+
pub fn get_instant_lock(&self) -> InstantLockNAPI {
92+
self.0.instant_lock.clone().into()
93+
}
94+
95+
#[napi(setter, js_name = "outputIndex")]
96+
pub fn set_output_index(&mut self, output_index: u32) {
97+
self.0.output_index = output_index;
98+
}
99+
100+
#[napi(setter, js_name = "instantLock")]
101+
pub fn set_instant_lock(&mut self, instant_lock: &InstantLockNAPI) {
102+
self.0.instant_lock = instant_lock.clone().into();
103+
}
104+
105+
#[napi(js_name=getTransaction)]
106+
pub fn get_transaction(&self) -> Uint8Array {
107+
let transaction = self.0.transaction();
108+
serialize(transaction).into()
109+
}
110+
111+
#[napi(js_name=getInstantLockBytes)]
112+
pub fn get_instant_lock_bytes(&self) -> Uint8Array {
113+
let instant_lock = self.0.instant_lock();
114+
serialize(instant_lock).into()
115+
}
116+
117+
#[napi(js_name = "createIdentityId")]
118+
pub fn create_identifier(&self) -> Result<IdentifierNAPI, napi::Error> {
119+
let identifier = self.0.create_identifier().with_js_error()?;
120+
121+
Ok(identifier.into())
122+
}
123+
}

0 commit comments

Comments
 (0)