Skip to content

Commit 86eaa00

Browse files
committed
fixup! eth: add signing function parameter to specify receive address case
1 parent 3dab9db commit 86eaa00

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

CHANGELOG-npm.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- btc: handle error when an input's previous transaction is required but missing
77
- btc: add support for regtest
88
- btc: add support for Taproot wallet policies
9+
- eth: add method to help clients identify address case (upper/lower/mixed)
910

1011
## 0.6.0
1112

CHANGELOG-rust.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- btc: add support for regtest
88
- btc: add support for Taproot wallet policies
99
- cardano: added support for vote delegation
10+
- eth: add method to help clients identify address case (upper/lower/mixed)
1011

1112
## 0.5.0
1213

examples/eth.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async fn eth_demo<R: bitbox_api::runtime::Runtime>() {
8181
1,
8282
&"m/44'/60'/0'/0/0".try_into().unwrap(),
8383
&raw_tx.as_slice().try_into().unwrap(),
84+
None,
8485
)
8586
.await
8687
.unwrap();
@@ -92,6 +93,7 @@ async fn eth_demo<R: bitbox_api::runtime::Runtime>() {
9293
.eth_sign_1559_transaction(
9394
&"m/44'/60'/0'/0/0".try_into().unwrap(),
9495
&raw_tx.as_slice().try_into().unwrap(),
96+
None,
9597
)
9698
.await
9799
.unwrap();

src/eth.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,18 @@ pub struct EIP1559Transaction {
123123
}
124124

125125
/// Identifies the case of the recipient address given as hexadecimal string.
126-
/// This function exists as a convenience to potentially help clients to determine the case of the
126+
/// This function exists as a convenience to help clients to determine the case of the
127127
/// recipient address.
128128
pub fn eth_identify_case(recipient_address: &str) -> pb::EthAddressCase {
129-
if recipient_address.to_uppercase() == recipient_address {
129+
if recipient_address
130+
.chars()
131+
.all(|c| !c.is_ascii_alphabetic() || c.is_ascii_uppercase())
132+
{
130133
pb::EthAddressCase::Upper
131-
} else if recipient_address.to_lowercase() == recipient_address {
134+
} else if recipient_address
135+
.chars()
136+
.all(|c| !c.is_ascii_alphabetic() || c.is_ascii_lowercase())
137+
{
132138
pb::EthAddressCase::Lower
133139
} else {
134140
pb::EthAddressCase::Mixed
@@ -1267,4 +1273,20 @@ mod tests {
12671273
)
12681274
.is_err());
12691275
}
1276+
1277+
#[test]
1278+
fn test_eth_identify_case() {
1279+
assert_eq!(
1280+
eth_identify_case("0XF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266"),
1281+
pb::EthAddressCase::Upper
1282+
);
1283+
assert_eq!(
1284+
eth_identify_case("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
1285+
pb::EthAddressCase::Lower
1286+
);
1287+
assert_eq!(
1288+
eth_identify_case("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),
1289+
pb::EthAddressCase::Mixed
1290+
);
1291+
}
12701292
}

src/wasm/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,12 @@ impl PairedBitBox {
419419
) -> Result<types::TsEthSignature, JavascriptError> {
420420
let signature = self
421421
.device
422-
.eth_sign_transaction(chain_id, &keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
422+
.eth_sign_transaction(
423+
chain_id,
424+
&keypath.try_into()?,
425+
&tx.try_into()?,
426+
address_case.map(TryInto::try_into).transpose()?,
427+
)
423428
.await?;
424429

425430
let v: u64 = compute_v(chain_id, signature[64])
@@ -443,7 +448,11 @@ impl PairedBitBox {
443448
) -> Result<types::TsEthSignature, JavascriptError> {
444449
let signature = self
445450
.device
446-
.eth_sign_1559_transaction(&keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
451+
.eth_sign_1559_transaction(
452+
&keypath.try_into()?,
453+
&tx.try_into()?,
454+
address_case.map(TryInto::try_into).transpose()?,
455+
)
447456
.await?;
448457

449458
Ok(serde_wasm_bindgen::to_value(&types::EthSignature {

src/wasm/types.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type EthSignature = {
7575
s: Uint8Array;
7676
v: Uint8Array;
7777
};
78-
type EthAddressCase = 'Upper' | 'Lower' | 'Mixed';
78+
type EthAddressCase = 'upper' | 'lower' | 'mixed';
7979
type CardanoXpub = Uint8Array;
8080
type CardanoXpubs = CardanoXpub[];
8181
type CardanoNetwork = 'mainnet' | 'testnet';
@@ -287,17 +287,17 @@ impl TryFrom<TsEth1559Transaction> for crate::eth::EIP1559Transaction {
287287
}
288288

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

297297
impl From<crate::pb::EthAddressCase> for TsEthAddressCase {
298-
fn from(case: crate::pb::EthAddressCase) -> Self {
299-
serde_wasm_bindgen::to_value(&case).unwrap().into()
300-
}
298+
fn from(case: crate::pb::EthAddressCase) -> Self {
299+
serde_wasm_bindgen::to_value(&case).unwrap().into()
300+
}
301301
}
302302

303303
impl TryFrom<TsCardanoNetwork> for crate::pb::CardanoNetwork {

0 commit comments

Comments
 (0)