Skip to content

Commit

Permalink
Use Option<> if returning non-empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Oct 29, 2024
1 parent e5cbde5 commit c5f6123
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions starknet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ use ledger_device_sdk::io::Reply;

const SIG_LENGTH: u8 = 0x41;

fn send_data(comm: &mut io::Comm, data: Result<Vec<u8>, Reply>) {
fn send_data(comm: &mut io::Comm, data: Result<Option<Vec<u8>>, Reply>) {
match data {
Ok(data) => {
comm.append(data.as_slice());
match data {
Some(data) => comm.append(data.as_slice()),
None => (),
}
comm.reply_ok();
}
Err(sw) => comm.reply(sw),
Expand All @@ -129,7 +132,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

rdata.extend_from_slice([version_major, version_minor, version_patch].as_slice());

send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
}
Ins::GetPubkey { display } => {
ctx.reset();
Expand Down Expand Up @@ -161,7 +164,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
};
if ret {
rdata.extend_from_slice(key.as_ref());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
} else {
send_data(comm, Err(io::StatusWords::UserCancelled.into()));
}
Expand All @@ -188,7 +191,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

match crypto::set_derivation_path(&mut data, ctx) {
Ok(()) => {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
Err(e) => {
send_data(comm, Err(e.into()));
Expand All @@ -209,7 +212,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, false, ctx);
}
false => {
Expand Down Expand Up @@ -240,7 +243,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

match crypto::set_derivation_path(&mut data, ctx) {
Ok(()) => {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
Err(e) => {
send_data(comm, Err(e.into()));
Expand All @@ -249,20 +252,20 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
1 => {
transaction::set_tx_fields(data, &mut ctx.tx, transaction::TxVersion::V3);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
2 => {
transaction::set_paymaster_data(data, p2, &mut ctx.tx);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
3 => {
transaction::set_account_deployment_data(data, p2, &mut ctx.tx);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
4 => {
let nb_calls: u8 = FieldElement::from(data).into();
transaction::set_calldata_nb(&mut ctx.tx, nb_calls);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
5 => {
if let Some(err) = transaction::set_calldata(data, p2.into(), &mut ctx.tx).err()
Expand All @@ -283,7 +286,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -307,7 +310,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -322,7 +325,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
}
} else {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
}
_ => {
Expand All @@ -349,7 +352,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

match crypto::set_derivation_path(&mut data, ctx) {
Ok(()) => {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
Err(e) => {
send_data(comm, Err(e.into()));
Expand All @@ -358,12 +361,12 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
1 => {
transaction::set_tx_fields(data, &mut ctx.tx, transaction::TxVersion::V1);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
2 => {
let nb_calls: u8 = FieldElement::from(data).into();
transaction::set_calldata_nb(&mut ctx.tx, nb_calls);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
3 => {
if let Some(err) = transaction::set_calldata(data, p2.into(), &mut ctx.tx).err()
Expand All @@ -384,7 +387,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -408,7 +411,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -423,7 +426,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
}
} else {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
}
_ => {
Expand All @@ -450,7 +453,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

match crypto::set_derivation_path(&mut data, ctx) {
Ok(()) => {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
Err(e) => {
send_data(comm, Err(e.into()));
Expand All @@ -459,20 +462,20 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
1 => {
transaction::set_tx_fields(data, &mut ctx.tx, transaction::TxVersion::V3);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
2 => {
transaction::set_tx_fees(data, &mut ctx.tx);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
3 => {
transaction::set_paymaster_data(data, p2, &mut ctx.tx);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
4 => {
let constructor_calldata_length: u8 = FieldElement::from(data).into();
transaction::set_calldata_nb(&mut ctx.tx, constructor_calldata_length);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
5 => {
if let Some(err) = transaction::set_calldata(data, p2.into(), &mut ctx.tx).err()
Expand All @@ -491,7 +494,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -504,7 +507,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
}
} else {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
}
_ => {
Expand All @@ -531,7 +534,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {

match crypto::set_derivation_path(&mut data, ctx) {
Ok(()) => {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
Err(e) => {
send_data(comm, Err(e.into()));
Expand All @@ -540,16 +543,16 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
1 => {
transaction::set_tx_fields(data, &mut ctx.tx, transaction::TxVersion::V1);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
2 => {
transaction::set_tx_fees(data, &mut ctx.tx);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
3 => {
let constructor_calldata_length: u8 = FieldElement::from(data).into();
transaction::set_calldata_nb(&mut ctx.tx, constructor_calldata_length);
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
4 => {
if let Some(err) = transaction::set_calldata(data, p2.into(), &mut ctx.tx).err()
Expand All @@ -568,7 +571,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
send_data(comm, Ok(rdata));
send_data(comm, Ok(Some(rdata)));
display::show_status(true, true, ctx);
}
false => {
Expand All @@ -581,7 +584,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) {
}
}
} else {
send_data(comm, Ok(Vec::new()));
send_data(comm, Ok(None));
}
}
_ => {
Expand Down

0 comments on commit c5f6123

Please sign in to comment.