Skip to content

Commit deaa943

Browse files
committed
fix tests
1 parent bb982d7 commit deaa943

17 files changed

Lines changed: 190 additions & 147 deletions

File tree

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
.unwrap();
3636

3737
let output = Command::new("git")
38-
.args(&["rev-parse", "HEAD"])
38+
.args(["rev-parse", "HEAD"])
3939
.output()
4040
.expect("Failed to execute git command");
4141

messages/src/lib.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,19 @@ pub struct GetPublicKeyResponse {
269269
}
270270

271271
#[derive(Encode, Decode)]
272-
pub struct Signature(pub [u8; 64]);
272+
pub struct SignatureResponse(pub [u8; 64]);
273273

274274
#[derive(Encode, Decode)]
275275
pub struct TxInputSignatureResponse {
276-
pub signature: Signature,
276+
pub signature: SignatureResponse,
277277
pub input_idx: u32,
278278
pub multisig_idx: Option<u32>,
279279
pub has_next: bool,
280280
}
281281

282282
#[derive(Encode, Decode)]
283283
pub struct MsgSignatureResponse {
284-
pub signature: Signature,
284+
pub signature: SignatureResponse,
285285
}
286286

287287
#[derive(Encode, Decode)]
@@ -416,54 +416,44 @@ pub enum StatusWord {
416416
// App Specific Errors (0xB...)
417417
#[display("Transaction display failed")]
418418
TxDisplayFail = 0xB000,
419-
#[display("Address display failed")]
420-
AddrDisplayFail = 0xB001,
419+
#[display("Transaction lock time value is invalid")]
420+
TxLockTimeInvalid = 0xB001,
421421
#[display("Transaction wrong length")]
422422
TxWrongLength = 0xB002,
423-
#[display("Transaction parsing failed")]
424-
TxParsingFail = 0xB003,
425423
#[display("Transaction hashing failed")]
426-
TxHashFail = 0xB004,
424+
TxHashFail = 0xB003,
427425
#[display("Transaction address failed")]
428-
TxAddressFail = 0xB005,
429-
#[display("Transaction signing failed")]
430-
TxSignFail = 0xB006,
426+
TxAddressFail = 0xB004,
427+
#[display("Different instruction than expected")]
428+
WrongInstruction = 0xB005,
431429
#[display("Key derivation failed")]
432-
KeyDeriveFail = 0xB007,
433-
#[display("Version parsing failed")]
434-
VersionParsingFail = 0xB008,
430+
KeyDeriveFail = 0xB006,
431+
#[display("Orders V0 not supported")]
432+
OrdersV0NotSupported = 0xB007,
435433
#[display("Wrong context")]
436-
WrongContext = 0xB009,
434+
WrongContext = 0xB008,
437435
#[display("Deserialization failed")]
438-
DeserializeFail = 0xB00A,
436+
DeserializeFail = 0xB009,
439437
#[display("Invalid input UTXO")]
440-
TxInvalidInputUtxo = 0xB00B,
438+
TxInvalidInputUtxo = 0xB00A,
441439
#[display("Numeric operation failed")]
442-
TxNumericOperationFail = 0xB00C,
440+
TxNumericOperationFail = 0xB00B,
443441
#[display("Tx fee underflow")]
444-
TxFeeUnderflow = 0xB00D,
445-
#[display("Unsupported input")]
446-
TxUnsupportedInput = 0xB00E,
447-
#[display("Invalid Token V0")]
448-
TxInvalidTokenV0 = 0xB00F,
442+
TxFeeUnderflow = 0xB00C,
449443
#[display("Invalid input path")]
450-
TxInvalidInputPath = 0xB010,
444+
TxInvalidInputPath = 0xB00D,
451445
#[display("Nothing to sign")]
452-
NothingToSign = 0xB011,
446+
NothingToSign = 0xB00E,
453447
#[display("Transaction already finished")]
454-
TxAlreadyFinished = 0xB012,
448+
TxAlreadyFinished = 0xB00F,
455449
#[display("Invalid path")]
456-
InvalidPath = 0xB013,
450+
InvalidPath = 0xB010,
457451
#[display("Invalid uncompressed public key")]
458-
InvalidUncompressedPublicKey = 0xB014,
452+
InvalidUncompressedPublicKey = 0xB011,
459453
#[display("Max buffer length exceeded")]
460-
MaxBufferLenExceeded = 0xB015,
454+
MaxBufferLenExceeded = 0xB012,
461455
#[display("Different input commitment hash")]
462-
DifferentInputCommitmentHash = 0xB016,
463-
#[display("Orders V0 not supported")]
464-
OrdersV0NotSupported = 0xB017,
465-
#[display("Different instruction than expected")]
466-
WrongInstruction = 0xB018,
456+
DifferentInputCommitmentHash = 0xB013,
467457

468458
// Ecc Errors
469459
#[display("ECC Carry")]

src/app_ui/sign.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn ui_display_message<const T: char>(
213213
let addr = to_address(&dest, coin_type)?;
214214

215215
let message_str = match core::str::from_utf8(message) {
216-
Ok(s) if s.bytes().all(|b| b >= 0x20 && b <= 0x7E) => s.to_string(),
216+
Ok(s) if s.bytes().all(|b| (0x20..=0x7E).contains(&b)) => s.to_string(),
217217
Ok(_) | Err(_) => format!("0x{}", hex::encode(message)),
218218
};
219219

@@ -282,11 +282,11 @@ fn format_value(value: &OutputValue, coin: CoinType) -> Result<String, StatusWor
282282
fn format_timestamp(seconds_u64: u64) -> Result<String, StatusWord> {
283283
let seconds_i64: i64 = seconds_u64
284284
.try_into()
285-
.map_err(|_| StatusWord::TxDisplayFail)?;
285+
.map_err(|_| StatusWord::TxLockTimeInvalid)?;
286286
let datetime = Utc
287287
.timestamp_opt(seconds_i64, 0)
288288
.earliest()
289-
.ok_or(StatusWord::TxDisplayFail)?;
289+
.ok_or(StatusWord::TxLockTimeInvalid)?;
290290

291291
Ok(datetime.format("%Y-%m-%d %H:%M:%S").to_string())
292292
}
@@ -421,9 +421,7 @@ fn format_output(output: &TxOutput, coin: CoinType) -> Result<FormatedOutput, St
421421
}
422422

423423
TxOutput::IssueNft(_nft_id, data, destination) => {
424-
let data = match data {
425-
NftIssuance::V0(data) => data,
426-
};
424+
let NftIssuance::V0(data) = data;
427425
let address_short = format!(
428426
"Name: {}\nCreator: {}\nTicker: {}\nAddress: {}\nIcon URI: {}\nAdditional metadata URI: {}\nMedia URI: {}",
429427
String::from_utf8_lossy(data.name.as_ref()),

src/app_ui/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn compress_public_key<const T: char>(
7070
let mut compressed_key = [0u8; 33];
7171

7272
let y_coordinate = &uncompressed_key[33..65];
73-
let prefix = if y_coordinate[31] % 2 == 0 {
73+
let prefix = if y_coordinate[31].is_multiple_of(2) {
7474
0x02
7575
} else {
7676
0x03

src/handlers/sign_message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
DataContext, StatusWord,
2121
};
2222
use messages::{
23-
mlcp::CoinType, AddrType, Bip32Path, MsgSignatureResponse, SignMessageReq, Signature,
23+
mlcp::CoinType, AddrType, Bip32Path, MsgSignatureResponse, SignMessageReq, SignatureResponse,
2424
};
2525

2626
use alloc::vec::Vec;
@@ -94,7 +94,7 @@ fn compute_signature<const N: usize>(
9494
let sig = schnorr_sign(private_key, message_hash2.as_bytes())?;
9595

9696
let response = MsgSignatureResponse {
97-
signature: Signature(sig),
97+
signature: SignatureResponse(sig),
9898
};
9999

100100
Ok(response)

src/handlers/sign_tx/mod.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
* limitations under the License.
1616
*****************************************************************************/
1717

18-
use alloc::vec::Vec;
18+
use alloc::{
19+
vec::Vec,
20+
boxed::Box,
21+
};
1922

2023
use crate::{
2124
app_ui::sign::{
@@ -28,7 +31,7 @@ use crate::{
2831
use messages::{
2932
encode_as_compact, encode_to,
3033
mlcp::{CoinType as PCoinType, SighashInputCommitment, TxOutput, H256},
31-
CoinType, Encode, InputAddressPath, Response, SignTxReq, Signature, TxInputReq,
34+
CoinType, Encode, InputAddressPath, Response, SignTxReq, SignatureResponse, TxInputReq,
3235
TxInputSignatureResponse, TxMetadataReq, TxMetadataV1Req, TxMetadataVersionReq, TxOutputReq,
3336
};
3437

@@ -120,9 +123,9 @@ pub struct TxParsingInputCommitmentsContext {
120123
}
121124

122125
impl TxParsingInputCommitmentsContext {
123-
fn advance_next_input_additional_info_step<'a>(
126+
fn advance_next_input_additional_info_step(
124127
mut self,
125-
review: &'a NbglStreamingReview,
128+
review: &NbglStreamingReview,
126129
) -> Result<TxParsingContext, StatusWord> {
127130
let finished_with_inputs = self.num_inputs_parsed >= (self.metadata.num_inputs - 1);
128131

@@ -164,17 +167,22 @@ impl TxParsingInputCommitmentsContext {
164167
});
165168
Ok(new_context)
166169
} else {
167-
self.num_inputs_parsed = self.num_inputs_parsed + 1;
170+
self.num_inputs_parsed += 1;
168171
Ok(TxParsingContext::ParsingInputCommitments(self))
169172
}
170173
}
171174
}
172175

173176
impl TxParsingInputsContext {
174177
fn advance_next_input_step(mut self) -> Result<TxParsingContext, StatusWord> {
175-
let finished_with_inputs = self.num_inputs_parsed >= (self.metadata.num_inputs - 1);
178+
self.num_inputs_parsed += 1;
179+
let finished_with_inputs = self.num_inputs_parsed >= self.metadata.num_inputs;
176180

177181
if finished_with_inputs {
182+
if self.inputs.is_empty() {
183+
return Err(StatusWord::NothingToSign);
184+
}
185+
178186
// Update hash for input commitments and proceed with outputs
179187
self.tx_hasher
180188
.update(&self.metadata.num_inputs.to_le_bytes())
@@ -198,7 +206,6 @@ impl TxParsingInputsContext {
198206
},
199207
))
200208
} else {
201-
self.num_inputs_parsed = self.num_inputs_parsed + 1;
202209
Ok(TxParsingContext::ParsingInputs(self))
203210
}
204211
}
@@ -226,13 +233,13 @@ impl TxParsingOutputsContext {
226233
&self.summary
227234
}
228235

229-
fn advance_next_output_state<'a>(
236+
fn advance_next_output_state(
230237
mut self,
231-
review: &'a NbglStreamingReview,
238+
review: &NbglStreamingReview,
232239
output: &TxOutput,
233240
) -> Result<TxParsingContext, StatusWord> {
234241
if self.num_outputs_parsed < (self.metadata.num_outputs - 1) {
235-
self.num_outputs_parsed = self.num_outputs_parsed + 1;
242+
self.num_outputs_parsed += 1;
236243
Ok(TxParsingContext::ParsingOutputs(self))
237244
} else {
238245
// Finalize the tx hash for signing
@@ -284,7 +291,7 @@ impl TxSigningContext {
284291
let private_key = Secp256k1::derive_from_path(&addr);
285292
let sig = schnorr_sign(&private_key, self.tx_hash.as_bytes())?;
286293

287-
let signature = Signature(sig);
294+
let signature = SignatureResponse(sig);
288295
let input_idx = address.input_idx;
289296
let multisig_idx = address.multisig_idx;
290297

@@ -298,7 +305,7 @@ impl TxSigningContext {
298305
};
299306

300307
let new_ctx = if has_next {
301-
self.num_inputs_signed = self.num_inputs_signed + 1;
308+
self.num_inputs_signed += 1;
302309
TxParsingContext::Signing(self)
303310
} else {
304311
TxParsingContext::Finished
@@ -382,10 +389,7 @@ impl TxParsingContext {
382389
}
383390

384391
pub fn finished(&self) -> bool {
385-
match self {
386-
Self::Finished => true,
387-
_ => false,
388-
}
392+
matches!(self, Self::Finished)
389393
}
390394
}
391395

@@ -396,7 +400,7 @@ pub fn setup_sign_tx(req: TxMetadataReq) -> Result<DataContext, StatusWord> {
396400

397401
tx_ctx.show_spinner();
398402

399-
Ok(DataContext::TxContext(tx_ctx, ui_new_streaming_review()))
403+
Ok(DataContext::TxContext(Box::new(tx_ctx), ui_new_streaming_review()))
400404
}
401405

402406
fn handle_input_req(
@@ -419,20 +423,20 @@ fn handle_input_req(
419423
ctx.advance_next_input_step()
420424
}
421425

422-
fn handle_input_commitment_req<'a>(
426+
fn handle_input_commitment_req(
423427
req: SighashInputCommitment,
424428
mut ctx: TxParsingInputCommitmentsContext,
425-
review: &'a NbglStreamingReview,
429+
review: &NbglStreamingReview,
426430
) -> Result<TxParsingContext, StatusWord> {
427431
update_hash(&req, &mut ctx.input_commitments_hasher)?;
428432
update_hash(&req, &mut ctx.tx_hasher)?;
429433
ctx.advance_next_input_additional_info_step(review)
430434
}
431435

432-
fn handle_output_req<'a>(
436+
fn handle_output_req(
433437
req: TxOutputReq,
434438
mut ctx: TxParsingOutputsContext,
435-
review: &'a NbglStreamingReview,
439+
review: &NbglStreamingReview,
436440
) -> Result<TxParsingContext, StatusWord> {
437441
if ui_streaming_review_show_output(review, &req.out, ctx.metadata.coin)? {
438442
ctx.summary.process_output(&req.out)?;
@@ -464,7 +468,7 @@ pub fn handle_sign_tx(
464468
(SignTxReq::NextSignature, TxParsingContext::Finished) => {
465469
return Err(StatusWord::TxAlreadyFinished)
466470
}
467-
_ => return Err(StatusWord::WrongP1P2),
471+
_ => return Err(StatusWord::WrongContext),
468472
};
469473

470474
let new_ctx = match new_ctx {

src/handlers/sign_tx/summary_collector.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl TxSummaryCollector {
185185
| TxOutput::LockThenTransfer(value, _, _)
186186
| TxOutput::Htlc(value, _) => {
187187
let (coin_or_token_id, amount) =
188-
into_coin_or_token_id_and_amount(&value)?;
188+
into_coin_or_token_id_and_amount(value)?;
189189
self.increase_input_totals(coin_or_token_id, amount)?;
190190
}
191191
TxOutput::Burn(_)
@@ -259,9 +259,9 @@ impl TxSummaryCollector {
259259
match cmd {
260260
OrderAccountCommand::FillOrder(_, fill_amount) => {
261261
let (fill_coin_or_token_id, asked_amount) =
262-
into_coin_or_token_id_and_amount(&initially_asked)?;
262+
into_coin_or_token_id_and_amount(initially_asked)?;
263263
let (given_coin_or_token_id, given_amount) =
264-
into_coin_or_token_id_and_amount(&initially_given)?;
264+
into_coin_or_token_id_and_amount(initially_given)?;
265265

266266
self.increase_output_totals(fill_coin_or_token_id, *fill_amount)?;
267267

@@ -278,11 +278,11 @@ impl TxSummaryCollector {
278278
}
279279
OrderAccountCommand::ConcludeOrder(_) => {
280280
let (coin_or_token_id, _) =
281-
into_coin_or_token_id_and_amount(&initially_asked)?;
281+
into_coin_or_token_id_and_amount(initially_asked)?;
282282
self.increase_input_totals(coin_or_token_id, *ask_balance)?;
283283

284284
let (coin_or_token_id, _) =
285-
into_coin_or_token_id_and_amount(&initially_given)?;
285+
into_coin_or_token_id_and_amount(initially_given)?;
286286
self.increase_input_totals(coin_or_token_id, *give_balance)?;
287287

288288
self.tx_type = merge_tx_type(self.tx_type, TxType::ConcludeOrder);

0 commit comments

Comments
 (0)