From 619ac758817c1e3f3926a60bdcf515b2dfd0c6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Mon, 22 Jan 2024 17:43:49 +0200 Subject: [PATCH] chore: add `delete_key_ed25519` example --- Cargo.toml | 4 ++++ README.md | 6 +++++ examples/get_public_key/display.rs | 2 +- examples/get_public_key/silent.rs | 2 +- examples/get_wallet_id.rs | 2 +- .../sign_transaction/delete_key_ed25519.rs | 22 +++++++++++++++++++ .../sign_transaction/multiple_transfers.rs | 1 - examples/sign_transaction/transfer.rs | 1 - src/lib.rs | 3 +-- 9 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 examples/sign_transaction/delete_key_ed25519.rs diff --git a/Cargo.toml b/Cargo.toml index adbe8f2..aa3e45a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,10 @@ path = "examples/sign_transaction/delete_account_short.rs" name = "sign_delete_account_long" path = "examples/sign_transaction/delete_account_long.rs" +[[example]] +name = "sign_delete_key_ed25519" +path = "examples/sign_transaction/delete_key_ed25519.rs" + [[example]] name = "blind_sign_transaction" diff --git a/README.md b/README.md index 43f7c4a..cc91248 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,12 @@ RUST_LOG=sign_delete_account_short,near_ledger=info cargo run --example sign_del RUST_LOG=sign_delete_account_long,near_ledger=info cargo run --example sign_delete_account_long ``` +#### Delete key (ed25519) + +```bash +RUST_LOG=sign_delete_key_ed25519,near_ledger=info cargo run --example sign_delete_key_ed25519 +``` + #### Transfer ```bash diff --git a/examples/get_public_key/display.rs b/examples/get_public_key/display.rs index d4e1934..ae626f9 100644 --- a/examples/get_public_key/display.rs +++ b/examples/get_public_key/display.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use near_ledger::{NEARLedgerError, get_public_key_with_display_flag}; +use near_ledger::{get_public_key_with_display_flag, NEARLedgerError}; use slip10::BIP32Path; #[path = "../common/lib.rs"] diff --git a/examples/get_public_key/silent.rs b/examples/get_public_key/silent.rs index 5126146..afd29b8 100644 --- a/examples/get_public_key/silent.rs +++ b/examples/get_public_key/silent.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use near_ledger::{NEARLedgerError, get_public_key_with_display_flag}; +use near_ledger::{get_public_key_with_display_flag, NEARLedgerError}; use slip10::BIP32Path; #[path = "../common/lib.rs"] diff --git a/examples/get_wallet_id.rs b/examples/get_wallet_id.rs index eb097f7..1fead57 100644 --- a/examples/get_wallet_id.rs +++ b/examples/get_wallet_id.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use near_ledger::{NEARLedgerError, get_wallet_id}; +use near_ledger::{get_wallet_id, NEARLedgerError}; use slip10::BIP32Path; #[path = "./common/lib.rs"] diff --git a/examples/sign_transaction/delete_key_ed25519.rs b/examples/sign_transaction/delete_key_ed25519.rs new file mode 100644 index 0000000..88d2193 --- /dev/null +++ b/examples/sign_transaction/delete_key_ed25519.rs @@ -0,0 +1,22 @@ +use near_crypto::SecretKey; +use near_ledger::NEARLedgerError; + +#[path = "../common/lib.rs"] +mod common; + +fn tx(ledger_pub_key: ed25519_dalek::PublicKey) -> near_primitives::transaction::Transaction { + let mut tx = common::tx_template(ledger_pub_key.clone()); + let sk = SecretKey::from_seed( + near_crypto::KeyType::ED25519, + &format!("{:?}", ledger_pub_key), + ); + let public_key = sk.public_key(); + tx.actions = vec![near_primitives::transaction::Action::DeleteKey( + near_primitives::transaction::DeleteKeyAction { public_key }, + )]; + tx +} + +fn main() -> Result<(), NEARLedgerError> { + common::get_key_sign_and_verify_flow(tx) +} diff --git a/examples/sign_transaction/multiple_transfers.rs b/examples/sign_transaction/multiple_transfers.rs index 6e367b9..6383d43 100644 --- a/examples/sign_transaction/multiple_transfers.rs +++ b/examples/sign_transaction/multiple_transfers.rs @@ -1,6 +1,5 @@ use near_ledger::NEARLedgerError; - #[path = "../common/lib.rs"] mod common; diff --git a/examples/sign_transaction/transfer.rs b/examples/sign_transaction/transfer.rs index 62b4a2b..1c5eada 100644 --- a/examples/sign_transaction/transfer.rs +++ b/examples/sign_transaction/transfer.rs @@ -1,6 +1,5 @@ use near_ledger::NEARLedgerError; - #[path = "../common/lib.rs"] mod common; diff --git a/src/lib.rs b/src/lib.rs index 1fbd6f4..a4ac3fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ use near_primitives_core::hash::CryptoHash; const CLA: u8 = 0x80; // Instruction class const INS_GET_PUBLIC_KEY: u8 = 4; // Instruction code to get public key -const INS_GET_WALLET_ID: u8 = 0x05; // Get Wallet ID +const INS_GET_WALLET_ID: u8 = 0x05; // Get Wallet ID const INS_GET_VERSION: u8 = 6; // Instruction code to get app version from the Ledger const INS_SIGN_TRANSACTION: u8 = 2; // Instruction code to sign a transaction on the Ledger const NETWORK_ID: u8 = 'W' as u8; // Instruction parameter 2 @@ -218,7 +218,6 @@ pub fn get_wallet_id( // hd_path must be converted into bytes to be sent as `data` to the Ledger let hd_path_bytes = hd_path_to_bytes(&hd_path); - let command = APDUCommand { cla: CLA, ins: INS_GET_WALLET_ID,