Skip to content

Commit

Permalink
chore!: remove blind_sign_transaction public fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 27, 2024
1 parent a77f2da commit 38ff28d
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 139 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "near-ledger"
version = "0.4.0"
version = "0.5.0"
edition = "2018"
authors = ["Bohdan Khorolets <[email protected]>"]
description = "Transport library to integrate with NEAR Ledger app"
Expand Down Expand Up @@ -87,9 +87,6 @@ path = "examples/sign_nep_413_message.rs"
name = "sign_nep_366_delegate_action"
path = "examples/sign_nep_366_delegate_action.rs"

[[example]]
name = "blind_sign_transaction"

[dependencies]
ed25519-dalek = { version = "1" }
ledger-transport = "0.10.0"
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,3 @@ RUST_LOG=info cargo run --example sign_nep_413_message
```bash
RUST_LOG=info cargo run --example sign_nep_366_delegate_action
```

### Blind sign a transaction

```bash
RUST_LOG=info cargo run --example blind_sign_transaction
```
53 changes: 0 additions & 53 deletions examples/blind_sign_transaction.rs

This file was deleted.

6 changes: 3 additions & 3 deletions examples/sign_nep_366_delegate_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ fn main() -> Result<(), NEARLedgerError> {
let ledger_pub_key = near_ledger::get_public_key_with_display_flag(hd_path.clone(), false)?;
display_pub_key(ledger_pub_key);


let sender_id = AccountId::from_str("bob.near").unwrap();

let actions = common::batch_of_all_types_of_actions(ledger_pub_key)
.into_iter()
.map(|action| action.try_into().unwrap())
.collect::<Vec<_>>();

let ledger_pub_key =
near_crypto::PublicKey::ED25519(near_crypto::ED25519PublicKey::from(ledger_pub_key.to_bytes()));
let ledger_pub_key = near_crypto::PublicKey::ED25519(near_crypto::ED25519PublicKey::from(
ledger_pub_key.to_bytes(),
));

let delegate_action = DelegateAction {
sender_id,
Expand Down
74 changes: 1 addition & 73 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use ledger_transport_hid::{
LedgerHIDError, TransportNativeHID,
};
use near_primitives::action::delegate::DelegateAction;
use near_primitives_core::{
borsh::{self, BorshSerialize},
hash::CryptoHash,
};
use near_primitives_core::borsh::{self, BorshSerialize};

const CLA: u8 = 0x80; // Instruction class
const INS_GET_PUBLIC_KEY: u8 = 4; // Instruction code to get public key
Expand All @@ -33,15 +30,6 @@ const P1_GET_PUB_SILENT: u8 = 1;

const P1_SIGN_NORMAL: u8 = 0;
const P1_SIGN_NORMAL_LAST_CHUNK: u8 = 0x80;
const P1_SIGN_BLIND: u8 = 1;

// this is value from LedgerHQ/app-near repo
const SW_INCORRECT_P1_P2: u16 = 0x6A86;
// this is value from LedgerHQ/app-near repo
const SW_BUFFER_OVERFLOW: u16 = 0x6990;

// this is value from LedgerHQ/app-near repo
const SW_SETTING_BLIND_DISABLED: u16 = 0x6192;

/// Alias of `Vec<u8>`. The goal is naming to help understand what the bytes to deal with
pub type NEARLedgerAppVersion = Vec<u8>;
Expand All @@ -56,14 +44,8 @@ pub enum NEARLedgerError {
LedgerHidError(LedgerHIDError),
/// Error occurred while exchanging with Ledger device
APDUExchangeError(String),
/// Blind signature not supported
BlindSignatureNotSupported,
/// Blind signature disabled in ledger's app settings
BlindSignatureDisabled,
/// Error with transport
LedgerHIDError(LedgerHIDError),
/// Transaction is too large to be signed
BufferOverflow { transaction_hash: CryptoHash },
}

/// Converts BIP32Path into bytes (`Vec<u8>`)
Expand Down Expand Up @@ -360,12 +342,6 @@ pub fn sign_transaction(
} else {
let retcode = response.retcode();

if retcode == SW_BUFFER_OVERFLOW {
return Err(NEARLedgerError::BufferOverflow {
transaction_hash: CryptoHash::hash_bytes(&unsigned_tx),
});
}

let error_string = format!("Ledger APDU retcode: 0x{:X}", retcode);
return Err(NEARLedgerError::APDUExchangeError(error_string));
}
Expand Down Expand Up @@ -502,51 +478,3 @@ pub fn sign_message_nep366_delegate_action(
"Unable to process request".to_owned(),
))
}
pub fn blind_sign_transaction(
payload: CryptoHash,
seed_phrase_hd_path: slip10::BIP32Path,
) -> Result<SignatureBytes, NEARLedgerError> {
let transport = get_transport()?;
// seed_phrase_hd_path must be converted into bytes to be sent as `data` to the Ledger
let hd_path_bytes = hd_path_to_bytes(&seed_phrase_hd_path);

let mut data: Vec<u8> = vec![];
data.extend(hd_path_bytes);
data.extend(payload.0);

let command = APDUCommand {
cla: CLA,
ins: INS_SIGN_TRANSACTION,
p1: P1_SIGN_BLIND, // Instruction parameter 1 (offset)
p2: NETWORK_ID,
data,
};
log::info!("APDU in: {}", hex::encode(&command.serialize()));
match transport.exchange(&command) {
Ok(response) => {
log::info!(
"APDU out: {}\nAPDU ret code: {:x}",
hex::encode(response.apdu_data()),
response.retcode(),
);
// Ok means we successfully exchanged with the Ledger
// but doesn't mean our request succeeded
// we need to check it based on `response.retcode`
if response.retcode() == RETURN_CODE_OK {
Ok(response.data().to_vec())
} else {
let retcode = response.retcode();
if retcode == SW_INCORRECT_P1_P2 {
return Err(NEARLedgerError::BlindSignatureNotSupported);
}
if retcode == SW_SETTING_BLIND_DISABLED {
return Err(NEARLedgerError::BlindSignatureDisabled);
}

let error_string = format!("Ledger APDU retcode: 0x{:X}", retcode);
Err(NEARLedgerError::APDUExchangeError(error_string))
}
}
Err(err) => Err(NEARLedgerError::LedgerHIDError(err)),
}
}

0 comments on commit 38ff28d

Please sign in to comment.