Skip to content

Commit

Permalink
fixup! eth: add signing function parameter to specify receive address…
Browse files Browse the repository at this point in the history
… case
  • Loading branch information
Tomasvrba committed Oct 13, 2024
1 parent 45f1bb6 commit 33e2cc5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-npm.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## 0.7.0
- btc: handle error when an input's previous transaction is required but missing
- eth: add method to help clients identify address case (upper/lower/mixed)

## 0.6.0

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 0.6.0
- btc: handle error when an input's previous transaction is required but missing
- cardano: added support for vote delegation
- eth: add method to help clients identify address case (upper/lower/mixed)

## 0.5.0

Expand Down
35 changes: 32 additions & 3 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,29 @@ pub struct EIP1559Transaction {
}

/// Identifies the case of the recipient address given as hexadecimal string.
/// This function exists as a convenience to potentially help clients to determine the case of the
/// This function exists as a convenience to help clients to determine the case of the
/// recipient address.
pub fn eth_identify_case(recipient_address: &str) -> pb::EthAddressCase {
if recipient_address.to_uppercase() == recipient_address {
if recipient_address
.chars()
.all(|c| !c.is_ascii_alphabetic() || c.is_ascii_uppercase())
{
pb::EthAddressCase::Upper
} else if recipient_address.to_lowercase() == recipient_address {
} else if recipient_address
.chars()
.all(|c| !c.is_ascii_alphabetic() || c.is_ascii_lowercase())
{
pb::EthAddressCase::Lower
} else {
pb::EthAddressCase::Mixed
}
// if recipient_address.to_uppercase() == recipient_address {
// pb::EthAddressCase::Upper
// } else if recipient_address.to_lowercase() == recipient_address {
// pb::EthAddressCase::Lower
// } else {
// pb::EthAddressCase::Mixed
// }
}

#[cfg(feature = "rlp")]
Expand Down Expand Up @@ -1267,4 +1280,20 @@ mod tests {
)
.is_err());
}

#[test]
fn test_eth_identify_case() {
assert_eq!(
eth_identify_case("0XF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266"),
pb::EthAddressCase::Upper
);
assert_eq!(
eth_identify_case("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
pb::EthAddressCase::Lower
);
assert_eq!(
eth_identify_case("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),
pb::EthAddressCase::Mixed
);
}
}
13 changes: 11 additions & 2 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ impl PairedBitBox {
) -> Result<types::TsEthSignature, JavascriptError> {
let signature = self
.device
.eth_sign_transaction(chain_id, &keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
.eth_sign_transaction(
chain_id,
&keypath.try_into()?,
&tx.try_into()?,
address_case.map(TryInto::try_into).transpose()?,
)
.await?;

let v: u64 = compute_v(chain_id, signature[64])
Expand All @@ -443,7 +448,11 @@ impl PairedBitBox {
) -> Result<types::TsEthSignature, JavascriptError> {
let signature = self
.device
.eth_sign_1559_transaction(&keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
.eth_sign_1559_transaction(
&keypath.try_into()?,
&tx.try_into()?,
address_case.map(TryInto::try_into).transpose()?,
)
.await?;

Ok(serde_wasm_bindgen::to_value(&types::EthSignature {
Expand Down
18 changes: 9 additions & 9 deletions src/wasm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type EthSignature = {
s: Uint8Array;
v: Uint8Array;
};
type EthAddressCase = 'Upper' | 'Lower' | 'Mixed';
type EthAddressCase = 'upper' | 'lower' | 'mixed';
type CardanoXpub = Uint8Array;
type CardanoXpubs = CardanoXpub[];
type CardanoNetwork = 'mainnet' | 'testnet';
Expand Down Expand Up @@ -287,17 +287,17 @@ impl TryFrom<TsEth1559Transaction> for crate::eth::EIP1559Transaction {
}

impl TryFrom<TsEthAddressCase> for crate::pb::EthAddressCase {
type Error = JavascriptError;
fn try_from(value: TsEthAddressCase) -> Result<Self, Self::Error> {
serde_wasm_bindgen::from_value(value.into())
.map_err(|_| JavascriptError::InvalidType("wrong type for EthAddressCase"))
}
type Error = JavascriptError;
fn try_from(value: TsEthAddressCase) -> Result<Self, Self::Error> {
serde_wasm_bindgen::from_value(value.into())
.map_err(|_| JavascriptError::InvalidType("wrong type for EthAddressCase"))
}
}

impl From<crate::pb::EthAddressCase> for TsEthAddressCase {
fn from(case: crate::pb::EthAddressCase) -> Self {
serde_wasm_bindgen::to_value(&case).unwrap().into()
}
fn from(case: crate::pb::EthAddressCase) -> Self {
serde_wasm_bindgen::to_value(&case).unwrap().into()
}
}

impl TryFrom<TsCardanoNetwork> for crate::pb::CardanoNetwork {
Expand Down

0 comments on commit 33e2cc5

Please sign in to comment.