Skip to content

Commit

Permalink
Add unpackInitData. Extend result of packIntoCell
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Mar 13, 2023
1 parent 4894d1c commit 3777704
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nekoton-wasm"
version = "1.0.18"
version = "1.1.0"
edition = "2021"
authors = ["Ivan Kalinin <[email protected]>"]
repository = "https://github.com/broxus/nekoton-wasm"
Expand Down
25 changes: 0 additions & 25 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,3 @@
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
58 changes: 56 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,56 @@ pub fn get_expected_address(
.unchecked_into())
}

#[wasm_bindgen(js_name = "unpackInitData")]
pub fn unpack_init_data(contract_abi: &str, data: &str) -> Result<InitData, JsValue> {
type UnpackedData = (Option<ed25519_dalek::PublicKey>, Vec<ton_abi::Token>);

fn unpack_init_data_impl(
contract_abi: ton_abi::Contract,
data: ton_types::Cell,
) -> anyhow::Result<UnpackedData> {
let data: ton_types::SliceData = data.into();
let map = ton_types::HashmapE::with_hashmap(
ton_abi::Contract::DATA_MAP_KEYLEN,
data.reference_opt(0),
);

let pubkey = match map.get(0u64.serialize()?.into())? {
Some(mut pubkey) => {
let pubkey = pubkey.get_next_hash()?;
Some(ed25519_dalek::PublicKey::from_bytes(pubkey.as_slice())?)
}
None => None,
};

let mut tokens = Vec::with_capacity(contract_abi.data.len());
for item in contract_abi.data.into_values() {
if let Some(value) = map.get(item.key.serialize()?.into())? {
tokens.append(&mut ton_abi::TokenValue::decode_params(
&[item.value],
value,
&contract_abi.abi_version,
false,
)?);
}
}

Ok((pubkey, tokens))
}

let contract_abi = parse_contract_abi(contract_abi)?;
let data = parse_cell(data)?;

let (pubkey, data) = unpack_init_data_impl(contract_abi, data).handle_error()?;
let data = make_tokens_object(data)?;

Ok(ObjectBuilder::new()
.set("publicKey", pubkey.map(hex::encode))
.set("data", data)
.build()
.unchecked_into())
}

#[wasm_bindgen(js_name = "getBocHash")]
pub fn get_boc_hash(boc: &str) -> Result<String, JsValue> {
Ok(parse_cell(boc)?.repr_hash().to_hex_string())
Expand All @@ -214,13 +264,17 @@ pub fn pack_into_cell(
params: ParamsList,
tokens: TokensObject,
abi_version: Option<String>,
) -> Result<String, JsValue> {
) -> Result<PackedBoc, JsValue> {
let params = parse_params_list(params).handle_error()?;
let tokens = parse_tokens_object(&params, tokens).handle_error()?;

let abi_version = parse_optional_abi_version(abi_version)?;
let cell = nt::abi::pack_into_cell(&tokens, abi_version).handle_error()?;
encode_cell_to_base64_boc(&cell)
Ok(ObjectBuilder::new()
.set("hash", cell.repr_hash().to_hex_string())
.set("boc", encode_cell_to_base64_boc(&cell)?)
.build()
.unchecked_into())
}

#[wasm_bindgen(js_name = "unpackFromCell")]
Expand Down
6 changes: 6 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ extern "C" {
#[wasm_bindgen(typescript_type = "ExpectedAddress")]
pub type ExpectedAddress;

#[wasm_bindgen(typescript_type = "{ hash: string, boc: string, }")]
pub type PackedBoc;

#[wasm_bindgen(typescript_type = "{ publicKey?: string, data: TokensObject }")]
pub type InitData;

#[wasm_bindgen(typescript_type = "DecodedInput")]
pub type DecodedInput;

Expand Down

0 comments on commit 3777704

Please sign in to comment.