Skip to content

Commit f589573

Browse files
committed
fix review comments
1 parent 4fd2a77 commit f589573

5 files changed

Lines changed: 53 additions & 69 deletions

File tree

common/src/address/dehexify.rs

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ pub fn to_dehexified_json<T: serde::Serialize>(
4444
// TODO: add tests that create blocks, and ensure the replacement in json works properly.
4545
#[cfg(test)]
4646
mod tests {
47+
use rstest::rstest;
48+
use strum::{EnumCount, EnumDiscriminants, EnumIter, IntoEnumIterator};
49+
4750
use crypto::{
4851
key::{KeyKind, PrivateKey},
4952
vrf::{VRFKeyKind, VRFPrivateKey, VRFPublicKey},
5053
};
51-
use rstest::rstest;
52-
use strum::{EnumCount, EnumDiscriminants, EnumIter, IntoEnumIterator};
53-
use test_utils::random::{make_seedable_rng, Rng, Seed};
54+
use test_utils::random::{gen_random_alnum_string, make_seedable_rng, Seed};
5455

5556
use crate::{
5657
address::{hexified::HexifiedAddress, pubkeyhash::PublicKeyHash, Address},
@@ -61,13 +62,6 @@ mod tests {
6162
primitives::H256,
6263
};
6364

64-
fn random_string(length: usize, rng: &mut impl Rng) -> String {
65-
rng.sample_iter(&randomness::distributions::Alphanumeric)
66-
.take(length)
67-
.map(char::from)
68-
.collect()
69-
}
70-
7165
#[derive(PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumDiscriminants)]
7266
#[strum_discriminants(name(HexifiableTag), derive(EnumIter))]
7367
enum Hexifiable {
@@ -90,51 +84,44 @@ mod tests {
9084
let chain_config = create_regtest();
9185

9286
let strings = (0..100)
93-
.map(|_| {
94-
let size = rng.gen::<usize>() % 50;
95-
random_string(size, &mut rng)
96-
})
87+
.map(|_| gen_random_alnum_string(&mut rng, 0, 50))
9788
.collect::<Vec<String>>();
9889

9990
let keys = (0..strings.len())
100-
.map(|_| {
101-
//
102-
103-
match HexifiableTag::iter().choose(&mut rng).unwrap() {
104-
HexifiableTag::Destination => {
105-
let dest = match DestinationTag::iter().choose(&mut rng).unwrap() {
106-
DestinationTag::AnyoneCanSpend => Destination::AnyoneCanSpend,
107-
DestinationTag::PublicKey => {
108-
let (_private_key, public_key) =
109-
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
110-
Destination::PublicKey(public_key)
111-
}
112-
DestinationTag::PublicKeyHash => {
113-
let (_private_key, public_key) =
114-
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
115-
Destination::PublicKeyHash(PublicKeyHash::from(&public_key))
116-
}
117-
DestinationTag::ScriptHash => Destination::ScriptHash(
118-
crate::primitives::Id::new(H256::random_using(&mut rng)),
119-
),
120-
DestinationTag::ClassicMultisig => {
121-
let (_private_key, public_key) =
122-
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
123-
Destination::ClassicMultisig(PublicKeyHash::from(&public_key))
124-
}
125-
};
126-
Hexifiable::Destination(dest)
127-
}
128-
HexifiableTag::PoolId => Hexifiable::PoolId(PoolId::random_using(&mut rng)),
129-
HexifiableTag::DelegationId => {
130-
Hexifiable::DelegationId(DelegationId::random_using(&mut rng))
131-
}
132-
HexifiableTag::TokenId => Hexifiable::TokenId(TokenId::random_using(&mut rng)),
133-
HexifiableTag::OrderId => Hexifiable::OrderId(OrderId::random_using(&mut rng)),
134-
HexifiableTag::VRFPublicKey => Hexifiable::VRFPublicKey(
135-
VRFPrivateKey::new_from_rng(&mut rng, VRFKeyKind::Schnorrkel).1,
136-
),
91+
.map(|_| match HexifiableTag::iter().choose(&mut rng).unwrap() {
92+
HexifiableTag::Destination => {
93+
let dest = match DestinationTag::iter().choose(&mut rng).unwrap() {
94+
DestinationTag::AnyoneCanSpend => Destination::AnyoneCanSpend,
95+
DestinationTag::PublicKey => {
96+
let (_private_key, public_key) =
97+
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
98+
Destination::PublicKey(public_key)
99+
}
100+
DestinationTag::PublicKeyHash => {
101+
let (_private_key, public_key) =
102+
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
103+
Destination::PublicKeyHash(PublicKeyHash::from(&public_key))
104+
}
105+
DestinationTag::ScriptHash => Destination::ScriptHash(
106+
crate::primitives::Id::new(H256::random_using(&mut rng)),
107+
),
108+
DestinationTag::ClassicMultisig => {
109+
let (_private_key, public_key) =
110+
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
111+
Destination::ClassicMultisig(PublicKeyHash::from(&public_key))
112+
}
113+
};
114+
Hexifiable::Destination(dest)
115+
}
116+
HexifiableTag::PoolId => Hexifiable::PoolId(PoolId::random_using(&mut rng)),
117+
HexifiableTag::DelegationId => {
118+
Hexifiable::DelegationId(DelegationId::random_using(&mut rng))
137119
}
120+
HexifiableTag::TokenId => Hexifiable::TokenId(TokenId::random_using(&mut rng)),
121+
HexifiableTag::OrderId => Hexifiable::OrderId(OrderId::random_using(&mut rng)),
122+
HexifiableTag::VRFPublicKey => Hexifiable::VRFPublicKey(
123+
VRFPrivateKey::new_from_rng(&mut rng, VRFKeyKind::Schnorrkel).1,
124+
),
138125
})
139126
.collect::<Vec<_>>();
140127

wasm-wrappers/WASM-API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ ScriptHash and ClassicMultisig destinations are not supported.
211211
Given inputs as bytes, outputs as bytes, and flags settings, this function returns
212212
the transaction that contains them all, as bytes.
213213

214-
### Function: `decode_signed_transaction_to_json_str`
214+
### Function: `decode_signed_transaction_to_js`
215215

216-
Decodes a signed transaction from its binary encoding into a Json string.
216+
Decodes a signed transaction from its binary encoding into a JavaScript object.
217217

218218
### Function: `encode_witness_no_signature`
219219

wasm-wrappers/js-bindings-test/tests/test_transaction_and_witness_encoding.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
estimate_transaction_size,
2222
make_default_account_privkey,
2323
make_receiving_address,
24-
decode_signed_transaction_to_json_str,
24+
decode_signed_transaction_to_js,
2525
Network,
2626
SignatureHashType,
2727
} from "../../pkg/wasm_wrappers.js";
@@ -331,10 +331,7 @@ export function test_transaction_and_witness_encoding() {
331331
`estimated size ${estimated_size} vs real ${expected_signed_tx.length}`,
332332
);
333333

334-
let decoded_tx = decode_signed_transaction_to_json_str(
335-
signed_tx,
336-
Network.Testnet,
337-
);
334+
let decoded_tx = decode_signed_transaction_to_js(signed_tx, Network.Testnet);
338335

339336
const expected_decoded_tx = {
340337
transaction: {
@@ -421,5 +418,8 @@ export function test_transaction_and_witness_encoding() {
421418
],
422419
};
423420

424-
assert_eq_vals(decoded_tx, JSON.stringify(expected_decoded_tx));
421+
assert_eq_vals(
422+
JSON.stringify(decoded_tx),
423+
JSON.stringify(expected_decoded_tx),
424+
);
425425
}

wasm-wrappers/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ pub enum Error {
8181
#[error("Invalid signed transaction intent encoding: {0}")]
8282
InvalidSignedTransactionIntentEncoding(serialization::Error),
8383

84-
#[error("Invalid signed transaction encoding: {0}")]
85-
InvalidSignedTransactionEncoding(serialization::Error),
84+
#[error("JSON parsing error: {0:?}")]
85+
JsonParseError(JsValue),
8686

8787
#[error("Error creating multisig spend: {0}")]
8888
MultisigSpendCreationError(DestinationSigError),

wasm-wrappers/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
3232
use std::{num::NonZeroU8, str::FromStr};
3333

34-
use wasm_bindgen::JsValue;
35-
3634
use bip39::Language;
3735
use itertools::Itertools as _;
38-
use wasm_bindgen::prelude::*;
36+
use wasm_bindgen::{prelude::*, JsValue};
3937

4038
use common::{
4139
address::{dehexify::dehexify_all_addresses, pubkeyhash::PublicKeyHash, Address},
@@ -691,21 +689,20 @@ pub fn encode_transaction(inputs: &[u8], outputs: &[u8], flags: u64) -> Result<V
691689
Ok(tx.encode())
692690
}
693691

694-
/// Decodes a signed transaction from its binary encoding into a Json string.
692+
/// Decodes a signed transaction from its binary encoding into a JavaScript object.
695693
#[wasm_bindgen]
696-
pub fn decode_signed_transaction_to_json_str(
694+
pub fn decode_signed_transaction_to_js(
697695
transaction: &[u8],
698696
network: Network,
699697
) -> Result<JsValue, Error> {
700698
let chain_config = Builder::new(network.into()).build();
701699
let tx = SignedTransaction::decode_all(&mut &transaction[..])
702-
.map_err(Error::InvalidSignedTransactionEncoding)?;
700+
.map_err(Error::InvalidTransactionEncoding)?;
703701

704702
let str = JsonEncoded::new(&tx).to_string();
705703
let str = dehexify_all_addresses(&chain_config, &str);
706704

707-
let value = JsValue::from_str(&str);
708-
Ok(value)
705+
js_sys::JSON::parse(&str).map_err(Error::JsonParseError)
709706
}
710707

711708
/// Encode an input witness of the variant that contains no signature.

0 commit comments

Comments
 (0)