Skip to content

Commit b3e4592

Browse files
committed
use primitive conversions
1 parent 2b3de8e commit b3e4592

File tree

2 files changed

+73
-106
lines changed

2 files changed

+73
-106
lines changed

wallet/src/signer/ledger_signer/ledger_messages.rs

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,27 @@
1616
use std::{collections::BTreeMap, mem::size_of_val, time::Duration};
1717

1818
use crate::signer::{ledger_signer::LedgerError, SignerError, SignerResult};
19-
use common::{
20-
chain::{self},
21-
primitives,
22-
primitives_converters::TryConvertInto as _,
23-
};
2419
use crypto::key::{
2520
extended::ExtendedPublicKey,
2621
hdkd::{chain_code::ChainCode, derivation_path::DerivationPath},
2722
secp256k1::{extended_keys::Secp256k1ExtendedPublicKey, Secp256k1PublicKey},
2823
};
29-
use serialization::Encode;
3024
use utils::ensure;
3125

3226
use ledger_lib::{Device, Exchange};
3327
use ledger_proto::StatusCode;
3428
use mintlayer_ledger_messages::{
35-
decode_all as ledger_decode_all, encode as ledger_encode, AccountCommand as LAccountCommand,
36-
AccountNonce as LAccountNonce, AccountOutPoint as LAccountOutPoint, AdditionalOrderInfo,
37-
AddrType, Amount as LAmount, Apdu, Bip32Path as LedgerBip32Path, CoinType,
38-
GetPublicKeyRespones, Id as LId, Ins, MsgSignature,
39-
OrderAccountCommand as LOrderAccountCommand, OutputValue as LOutputValue, PubKeyP1,
40-
PublicKeyReq, SighashInputCommitment as LSighashInputCommitment, SignMessageReq, SignP1,
41-
SignTxReq, Signature as LedgerSignature, TxInputReq, TxMetadataReq, TxOutput as LTxOutput,
42-
TxOutputReq, UtxoOutPoint as LUtxoOutPoint, APDU_CLASS, H256 as LH256, P2_DONE, P2_MORE,
29+
decode_all as ledger_decode_all, encode as ledger_encode, AddrType, Apdu,
30+
Bip32Path as LedgerBip32Path, CoinType, GetPublicKeyRespones, Ins, MsgSignature, PubKeyP1,
31+
PublicKeyReq, SighashInputCommitment, SignMessageReq, SignP1, SignTxReq, Signature, TxInputReq,
32+
TxMetadataReq, TxOutputReq, APDU_CLASS, P2_DONE, P2_MORE,
4333
};
44-
use wallet_types::partially_signed_transaction::OrderAdditionalInfo;
4534

4635
const TIMEOUT_DUR: Duration = Duration::from_secs(100);
4736
const TX_VERSION: u8 = 1;
4837

4938
struct SignatureResult {
50-
sig: LedgerSignature,
39+
sig: Signature,
5140
input_idx: usize,
5241
has_more_signatures: bool,
5342
}
@@ -217,9 +206,9 @@ pub async fn sign_tx<L: Exchange>(
217206
ledger: &mut L,
218207
chain_type: CoinType,
219208
inputs: Vec<TxInputReq>,
220-
input_commitments: Vec<LSighashInputCommitment>,
209+
input_commitments: Vec<SighashInputCommitment>,
221210
outputs: Vec<TxOutputReq>,
222-
) -> SignerResult<BTreeMap<usize, Vec<LedgerSignature>>> {
211+
) -> SignerResult<BTreeMap<usize, Vec<Signature>>> {
223212
let metadata = ledger_encode(TxMetadataReq {
224213
coin: chain_type,
225214
version: TX_VERSION,
@@ -301,7 +290,7 @@ fn decode_signature_response(resp: &[u8]) -> Result<SignatureResult, LedgerError
301290
let input_idx = *resp.first().ok_or(LedgerError::InvalidResponse)? as usize;
302291
let has_more_signatures = *resp.last().ok_or(LedgerError::InvalidResponse)? == P2_MORE;
303292

304-
let sig: LedgerSignature =
293+
let sig =
305294
ledger_decode_all(&resp[..resp.len() - 1][1..]).ok_or(LedgerError::InvalidResponse)?;
306295

307296
Ok(SignatureResult {
@@ -310,57 +299,3 @@ fn decode_signature_response(resp: &[u8]) -> Result<SignatureResult, LedgerError
310299
has_more_signatures,
311300
})
312301
}
313-
314-
pub fn to_ledger_tx_output(value: &chain::TxOutput) -> LTxOutput {
315-
ledger_decode_all(value.encode().as_slice()).expect("ok")
316-
}
317-
318-
pub fn to_ledger_amount(value: &primitives::Amount) -> LAmount {
319-
LAmount::from_atoms(value.into_atoms())
320-
}
321-
322-
pub fn to_ledger_outpoint(value: &chain::UtxoOutPoint) -> LUtxoOutPoint {
323-
ledger_decode_all(value.encode().as_slice()).expect("ok")
324-
}
325-
326-
pub fn to_ledger_account_outpoint(value: &chain::AccountOutPoint) -> LAccountOutPoint {
327-
ledger_decode_all(value.encode().as_slice()).expect("ok")
328-
}
329-
330-
pub fn to_ledger_account_nonce(value: &chain::AccountNonce) -> LAccountNonce {
331-
ledger_decode_all(value.encode().as_slice()).expect("ok")
332-
}
333-
334-
pub fn to_ledger_account_command(value: &chain::AccountCommand) -> LAccountCommand {
335-
ledger_decode_all(value.encode().as_slice()).expect("ok")
336-
}
337-
338-
pub fn to_ledger_order_account_command(value: &chain::OrderAccountCommand) -> LOrderAccountCommand {
339-
ledger_decode_all(value.encode().as_slice()).expect("ok")
340-
}
341-
342-
pub fn to_ledger_additional_order_info(
343-
info: &OrderAdditionalInfo,
344-
) -> SignerResult<AdditionalOrderInfo> {
345-
Ok(AdditionalOrderInfo {
346-
initially_asked: to_ledger_output_value(&info.initially_asked)?,
347-
initially_given: to_ledger_output_value(&info.initially_given)?,
348-
ask_balance: info.ask_balance.try_convert_into()?,
349-
give_balance: to_ledger_amount(&info.give_balance),
350-
})
351-
}
352-
353-
pub fn to_ledger_output_value(
354-
value: &chain::output_value::OutputValue,
355-
) -> SignerResult<LOutputValue> {
356-
match value {
357-
chain::output_value::OutputValue::Coin(amount) => {
358-
Ok(LOutputValue::Coin(to_ledger_amount(amount)))
359-
}
360-
chain::output_value::OutputValue::TokenV0(_) => Err(SignerError::UnsupportedTokensV0),
361-
chain::output_value::OutputValue::TokenV1(token_id, amount) => Ok(LOutputValue::TokenV1(
362-
LId::new(LH256(token_id.to_hash().into())),
363-
to_ledger_amount(amount),
364-
)),
365-
}
366-
}

wallet/src/signer/ledger_signer/mod.rs

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use crate::{
2222
signer::{
2323
ledger_signer::ledger_messages::{
2424
check_current_app, get_extended_public_key, get_extended_public_key_raw,
25-
sign_challenge, sign_tx, to_ledger_account_command, to_ledger_account_nonce,
26-
to_ledger_account_outpoint, to_ledger_additional_order_info, to_ledger_amount,
27-
to_ledger_order_account_command, to_ledger_outpoint, to_ledger_output_value,
28-
to_ledger_tx_output,
25+
sign_challenge, sign_tx,
2926
},
3027
utils::{is_htlc_utxo, produce_uniparty_signature_for_input},
3128
Signer, SignerError, SignerProvider, SignerResult,
@@ -58,6 +55,7 @@ use common::{
5855
Transaction, TxInput, TxOutput,
5956
},
6057
primitives::{BlockHeight, Idable, H256},
58+
primitives_converters::TryConvertInto as _,
6159
};
6260
use crypto::key::{
6361
extended::ExtendedPublicKey,
@@ -84,7 +82,7 @@ use async_trait::async_trait;
8482
use itertools::{izip, Itertools};
8583
use ledger_lib::{info::Model, Exchange, Filters, LedgerHandle, LedgerProvider, Transport};
8684
use mintlayer_ledger_messages::{
87-
AdditionalUtxoInfo, AddrType, Bip32Path as LedgerBip32Path, CoinType,
85+
AdditionalOrderInfo, AdditionalUtxoInfo, AddrType, Bip32Path as LedgerBip32Path, CoinType,
8886
InputAddressPath as LedgerInputAddressPath, SighashInputCommitment as LSighashInputCommitment,
8987
Signature as LedgerSignature, TxInputReq, TxInputWithAdditionalInfo, TxOutputReq,
9088
};
@@ -378,12 +376,17 @@ where
378376
}
379377
}
380378

381-
fn to_ledger_output_msgs(&self, ptx: &PartiallySignedTransaction) -> Vec<TxOutputReq> {
379+
fn to_ledger_output_msgs(
380+
&self,
381+
ptx: &PartiallySignedTransaction,
382+
) -> SignerResult<Vec<TxOutputReq>> {
382383
ptx.tx()
383384
.outputs()
384385
.iter()
385-
.map(|out| TxOutputReq {
386-
out: to_ledger_tx_output(out),
386+
.map(|out| {
387+
Ok(TxOutputReq {
388+
out: out.clone().try_convert_into()?,
389+
})
387390
})
388391
.collect()
389392
}
@@ -504,7 +507,7 @@ where
504507
)> {
505508
let (inputs, standalone_inputs) = to_ledger_input_msgs(&ptx, key_chain, &db_tx)?;
506509
let input_commitments = to_ledger_input_commitments_reqs(&ptx)?;
507-
let outputs = self.to_ledger_output_msgs(&ptx);
510+
let outputs = self.to_ledger_output_msgs(&ptx)?;
508511
let coin_type = to_ledger_chain_type(&self.chain_config);
509512

510513
let input_commitment_version = self
@@ -898,29 +901,34 @@ fn to_ledger_tx_input_with_additional_info(
898901
.get_pool_info(pool_id)
899902
.ok_or(SignerError::MissingTxExtraInfo)?;
900903
AdditionalUtxoInfo::PoolData {
901-
utxo: to_ledger_tx_output(utxo),
902-
staker_balance: to_ledger_amount(&pool_info.staker_balance),
904+
utxo: utxo.clone().try_convert_into()?,
905+
staker_balance: pool_info.staker_balance.try_convert_into()?,
903906
}
904907
}
905-
_ => AdditionalUtxoInfo::Utxo(to_ledger_tx_output(utxo)),
908+
_ => AdditionalUtxoInfo::Utxo(utxo.clone().try_convert_into()?),
906909
};
907-
TxInputWithAdditionalInfo::Utxo(to_ledger_outpoint(outpoint), info)
910+
TxInputWithAdditionalInfo::Utxo(outpoint.clone().try_convert_into()?, info)
908911
}
909912
TxInput::Account(acc) => {
910-
TxInputWithAdditionalInfo::Account(to_ledger_account_outpoint(acc))
913+
TxInputWithAdditionalInfo::Account(acc.clone().try_convert_into()?)
911914
}
912915
TxInput::AccountCommand(nonce, cmd) => TxInputWithAdditionalInfo::AccountCommand(
913-
to_ledger_account_nonce(nonce),
914-
to_ledger_account_command(cmd),
916+
(*nonce).try_convert_into()?,
917+
cmd.clone().try_convert_into()?,
915918
),
916919
TxInput::OrderAccountCommand(cmd) => {
917920
let info = additional_info
918921
.get_order_info(cmd.order_id())
919922
.ok_or(SignerError::MissingTxExtraInfo)?;
920923

921924
TxInputWithAdditionalInfo::OrderAccountCommand(
922-
to_ledger_order_account_command(cmd),
923-
to_ledger_additional_order_info(info)?,
925+
cmd.clone().try_convert_into()?,
926+
AdditionalOrderInfo {
927+
initially_asked: info.initially_asked.clone().try_convert_into()?,
928+
initially_given: info.initially_given.clone().try_convert_into()?,
929+
ask_balance: info.ask_balance.try_convert_into()?,
930+
give_balance: info.give_balance.try_convert_into()?,
931+
},
924932
)
925933
}
926934
};
@@ -1011,11 +1019,11 @@ fn to_ledger_input_commitments_reqs(
10111019
.get_pool_info(pool_id)
10121020
.ok_or(SignerError::MissingTxExtraInfo)?;
10131021
LSighashInputCommitment::ProduceBlockFromStakeUtxo {
1014-
utxo: to_ledger_tx_output(utxo),
1015-
staker_balance: to_ledger_amount(&pool_info.staker_balance),
1022+
utxo: utxo.clone().try_convert_into()?,
1023+
staker_balance: pool_info.staker_balance.try_convert_into()?,
10161024
}
10171025
}
1018-
_ => LSighashInputCommitment::Utxo(to_ledger_tx_output(utxo)),
1026+
_ => LSighashInputCommitment::Utxo(utxo.clone().try_convert_into()?),
10191027
}
10201028
}
10211029
TxInput::Account(_) => LSighashInputCommitment::None,
@@ -1033,8 +1041,14 @@ fn to_ledger_input_commitments_reqs(
10331041
.get_order_info(order_id)
10341042
.ok_or(SignerError::MissingTxExtraInfo)?;
10351043
LSighashInputCommitment::FillOrderAccountCommand {
1036-
initially_asked: to_ledger_output_value(&order_info.initially_asked)?,
1037-
initially_given: to_ledger_output_value(&order_info.initially_given)?,
1044+
initially_asked: order_info
1045+
.initially_asked
1046+
.clone()
1047+
.try_convert_into()?,
1048+
initially_given: order_info
1049+
.initially_given
1050+
.clone()
1051+
.try_convert_into()?,
10381052
}
10391053
}
10401054
AccountCommand::ConcludeOrder(order_id) => {
@@ -1043,10 +1057,16 @@ fn to_ledger_input_commitments_reqs(
10431057
.get_order_info(order_id)
10441058
.ok_or(SignerError::MissingTxExtraInfo)?;
10451059
LSighashInputCommitment::ConcludeOrderAccountCommand {
1046-
initially_asked: to_ledger_output_value(&order_info.initially_asked)?,
1047-
initially_given: to_ledger_output_value(&order_info.initially_given)?,
1048-
ask_balance: to_ledger_amount(&order_info.ask_balance),
1049-
give_balance: to_ledger_amount(&order_info.give_balance),
1060+
initially_asked: order_info
1061+
.initially_asked
1062+
.clone()
1063+
.try_convert_into()?,
1064+
initially_given: order_info
1065+
.initially_given
1066+
.clone()
1067+
.try_convert_into()?,
1068+
ask_balance: order_info.ask_balance.try_convert_into()?,
1069+
give_balance: order_info.give_balance.try_convert_into()?,
10501070
}
10511071
}
10521072
},
@@ -1057,8 +1077,14 @@ fn to_ledger_input_commitments_reqs(
10571077
.get_order_info(order_id)
10581078
.ok_or(SignerError::MissingTxExtraInfo)?;
10591079
LSighashInputCommitment::FillOrderAccountCommand {
1060-
initially_asked: to_ledger_output_value(&order_info.initially_asked)?,
1061-
initially_given: to_ledger_output_value(&order_info.initially_given)?,
1080+
initially_asked: order_info
1081+
.initially_asked
1082+
.clone()
1083+
.try_convert_into()?,
1084+
initially_given: order_info
1085+
.initially_given
1086+
.clone()
1087+
.try_convert_into()?,
10621088
}
10631089
}
10641090
OrderAccountCommand::ConcludeOrder(order_id) => {
@@ -1067,10 +1093,16 @@ fn to_ledger_input_commitments_reqs(
10671093
.get_order_info(order_id)
10681094
.ok_or(SignerError::MissingTxExtraInfo)?;
10691095
LSighashInputCommitment::ConcludeOrderAccountCommand {
1070-
initially_asked: to_ledger_output_value(&order_info.initially_asked)?,
1071-
initially_given: to_ledger_output_value(&order_info.initially_given)?,
1072-
ask_balance: to_ledger_amount(&order_info.ask_balance),
1073-
give_balance: to_ledger_amount(&order_info.give_balance),
1096+
initially_asked: order_info
1097+
.initially_asked
1098+
.clone()
1099+
.try_convert_into()?,
1100+
initially_given: order_info
1101+
.initially_given
1102+
.clone()
1103+
.try_convert_into()?,
1104+
ask_balance: order_info.ask_balance.try_convert_into()?,
1105+
give_balance: order_info.give_balance.try_convert_into()?,
10741106
}
10751107
}
10761108
OrderAccountCommand::FreezeOrder(_) => LSighashInputCommitment::None,

0 commit comments

Comments
 (0)