Skip to content

Commit 3dab9db

Browse files
committed
eth: add signing function parameter to specify receive address case
1 parent 861af3f commit 3dab9db

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Diff for: sandbox/src/Ethereum.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ function EthSignTransaction({ bb02 } : Props) {
143143
value: new Uint8Array(hexToArrayBuffer(parsed.value)),
144144
data: new Uint8Array(hexToArrayBuffer(parsed.data)),
145145
};
146-
setResult(await bb02.ethSignTransaction(BigInt(chainID), keypath, tx));
146+
const addressCase = await bitbox.ethIdentifyCase(parsed.recipient);
147+
setResult(await bb02.ethSignTransaction(BigInt(chainID), keypath, tx, addressCase));
147148
} catch (err) {
148149
setErr(bitbox.ensureError(err));
149150
} finally {
@@ -224,7 +225,8 @@ function EthSignEIP1559Transaction({ bb02 } : Props) {
224225
value: new Uint8Array(hexToArrayBuffer(parsed.value)),
225226
data: new Uint8Array(hexToArrayBuffer(parsed.data)),
226227
};
227-
setResult(await bb02.ethSign1559Transaction(keypath, tx));
228+
const addressCase = bitbox.ethIdentifyCase(parsed.recipient);
229+
setResult(await bb02.ethSign1559Transaction(keypath, tx, addressCase));
228230
} catch (err) {
229231
setErr(bitbox.ensureError(err));
230232
} finally {

Diff for: src/eth.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ pub struct EIP1559Transaction {
122122
pub data: Vec<u8>,
123123
}
124124

125+
/// 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
127+
/// recipient address.
128+
pub fn eth_identify_case(recipient_address: &str) -> pb::EthAddressCase {
129+
if recipient_address.to_uppercase() == recipient_address {
130+
pb::EthAddressCase::Upper
131+
} else if recipient_address.to_lowercase() == recipient_address {
132+
pb::EthAddressCase::Lower
133+
} else {
134+
pb::EthAddressCase::Mixed
135+
}
136+
}
137+
125138
#[cfg(feature = "rlp")]
126139
impl TryFrom<&[u8]> for Transaction {
127140
type Error = ();
@@ -465,6 +478,7 @@ impl<R: Runtime> PairedBitBox<R> {
465478
chain_id: u64,
466479
keypath: &Keypath,
467480
tx: &Transaction,
481+
address_case: Option<pb::EthAddressCase>,
468482
) -> Result<[u8; 65], Error> {
469483
// passing chainID instead of coin only since v9.10.0
470484
self.validate_version(">=9.10.0")?;
@@ -483,7 +497,7 @@ impl<R: Runtime> PairedBitBox<R> {
483497
commitment: crate::antiklepto::host_commit(&host_nonce).to_vec(),
484498
}),
485499
chain_id,
486-
address_case: pb::EthAddressCase::Mixed as _,
500+
address_case: address_case.unwrap_or(pb::EthAddressCase::Mixed).into(),
487501
});
488502
let response = self.query_proto_eth(request).await?;
489503
self.handle_antiklepto(&response, host_nonce).await
@@ -496,6 +510,7 @@ impl<R: Runtime> PairedBitBox<R> {
496510
&self,
497511
keypath: &Keypath,
498512
tx: &EIP1559Transaction,
513+
address_case: Option<pb::EthAddressCase>,
499514
) -> Result<[u8; 65], Error> {
500515
// EIP1559 is suported from v9.16.0
501516
self.validate_version(">=9.16.0")?;
@@ -516,7 +531,7 @@ impl<R: Runtime> PairedBitBox<R> {
516531
host_nonce_commitment: Some(pb::AntiKleptoHostNonceCommitment {
517532
commitment: crate::antiklepto::host_commit(&host_nonce).to_vec(),
518533
}),
519-
address_case: pb::EthAddressCase::Mixed as _,
534+
address_case: address_case.unwrap_or(pb::EthAddressCase::Mixed).into(),
520535
});
521536
let response = self.query_proto_eth(request).await?;
522537
self.handle_antiklepto(&response, host_nonce).await

Diff for: src/wasm/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ pub fn is_user_abort(err: types::TsError) -> bool {
100100
}
101101
}
102102

103+
#[wasm_bindgen(js_name = ethIdentifyCase)]
104+
pub fn eth_identify_case(recipient_address: &str) -> types::TsEthAddressCase {
105+
crate::eth::eth_identify_case(recipient_address).into()
106+
}
107+
103108
#[wasm_bindgen(raw_module = "./webhid")]
104109
extern "C" {
105110
async fn jsSleep(millis: f64);
@@ -410,10 +415,11 @@ impl PairedBitBox {
410415
chain_id: u64,
411416
keypath: types::TsKeypath,
412417
tx: types::TsEthTransaction,
418+
address_case: Option<types::TsEthAddressCase>,
413419
) -> Result<types::TsEthSignature, JavascriptError> {
414420
let signature = self
415421
.device
416-
.eth_sign_transaction(chain_id, &keypath.try_into()?, &tx.try_into()?)
422+
.eth_sign_transaction(chain_id, &keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
417423
.await?;
418424

419425
let v: u64 = compute_v(chain_id, signature[64])
@@ -433,10 +439,11 @@ impl PairedBitBox {
433439
&self,
434440
keypath: types::TsKeypath,
435441
tx: types::TsEth1559Transaction,
442+
address_case: Option<types::TsEthAddressCase>,
436443
) -> Result<types::TsEthSignature, JavascriptError> {
437444
let signature = self
438445
.device
439-
.eth_sign_1559_transaction(&keypath.try_into()?, &tx.try_into()?)
446+
.eth_sign_1559_transaction(&keypath.try_into()?, &tx.try_into()?, address_case.map(TryInto::try_into).transpose()?)
440447
.await?;
441448

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

Diff for: src/wasm/types.rs

+17
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type EthSignature = {
7575
s: Uint8Array;
7676
v: Uint8Array;
7777
};
78+
type EthAddressCase = 'Upper' | 'Lower' | 'Mixed';
7879
type CardanoXpub = Uint8Array;
7980
type CardanoXpubs = CardanoXpub[];
8081
type CardanoNetwork = 'mainnet' | 'testnet';
@@ -186,6 +187,8 @@ extern "C" {
186187
pub type TsEth1559Transaction;
187188
#[wasm_bindgen(typescript_type = "EthSignature")]
188189
pub type TsEthSignature;
190+
#[wasm_bindgen(typescript_type = "EthAddressCase")]
191+
pub type TsEthAddressCase;
189192
#[wasm_bindgen(typescript_type = "CardanoXpub")]
190193
pub type TsCardanoXpub;
191194
#[wasm_bindgen(typescript_type = "CardanoXpubs")]
@@ -283,6 +286,20 @@ impl TryFrom<TsEth1559Transaction> for crate::eth::EIP1559Transaction {
283286
}
284287
}
285288

289+
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+
}
295+
}
296+
297+
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+
}
301+
}
302+
286303
impl TryFrom<TsCardanoNetwork> for crate::pb::CardanoNetwork {
287304
type Error = JavascriptError;
288305
fn try_from(value: TsCardanoNetwork) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)