#ledger #near-protocol

near-ledger

Transport library to integrate with NEAR Ledger app

11 releases (6 breaking)

0.7.1 Jun 24, 2024
0.7.0 Jun 21, 2024
0.6.1 Jun 19, 2024
0.5.0 Feb 29, 2024
0.1.2 Jun 22, 2021

#932 in Magic Beans

Download history 189/week @ 2024-04-04 213/week @ 2024-04-11 204/week @ 2024-04-18 116/week @ 2024-04-25 171/week @ 2024-05-02 142/week @ 2024-05-09 148/week @ 2024-05-16 201/week @ 2024-05-23 55/week @ 2024-05-30 200/week @ 2024-06-06 245/week @ 2024-06-13 488/week @ 2024-06-20 158/week @ 2024-06-27 259/week @ 2024-07-04 200/week @ 2024-07-11 228/week @ 2024-07-18

925 downloads per month
Used in 3 crates (via near-cli-rs)

GPL-3.0-or-later

37KB
322 lines

near-ledger-rs

Rust

It is NEAR <-> Ledger transport

Provides a set of commands that can be executed to communicate with NEAR App installed on Ledger device:

  • Read PublicKey from Ledger device by HD Path
  • Sign a Transaction

Examples

Get PublicKey from Ledger

use near_ledger::get_public_key;
use slipped10::BIP32Path;
use std::str::FromStr;

let hd_path = BIP32Path::from_str("44'/397'/0'/0'/1'").unwrap();
let public_key = get_public_key(hd_path).unwrap();
println!("{:#?}", public_key);

Trick

To convert the answer into near_crypto::PublicKey do:

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

How to sign a transaction

use near_ledger::{sign_transaction, SignTarget};
use near_primitives::borsh::BorshSerialize;
use slipped10::BIP32Path;
use std::str::FromStr;

let hd_path = BIP32Path::from_str("44'/397'/0'/0'/1'").unwrap();
let borsh_transaction = near_unsigned_transaction.try_to_vec().unwrap();
let signature = sign_transaction(SignTarget::BorshUnsignedTx(borsh_transaction), hd_path).unwrap();
println!("{:#?}", signature);

Trick

To convert the answer into near_crypto::Signature do:

let signature = near_crypto::Signature::from_parts(near_crypto::KeyType::ED25519, &signature)
    .expect("Signature is not expected to fail on deserialization");

Executable examples

Get version

RUST_LOG=info cargo run --example get_version

Get PublicKey from Ledger

Display

RUST_LOG=info cargo run --example get_public_key_display

Silent

RUST_LOG=info cargo run --example get_public_key_silent

Get WalletID from Ledger

RUST_LOG=info cargo run --example get_wallet_id

Sign a transaction

Transfer

RUST_LOG=info cargo run --example sign_transfer

Other

export RUST_LOG=info
cargo run --example sign_create_account
cargo run --example sign_delete_account_short
cargo run --example sign_delete_account_long
cargo run --example sign_delete_key_ed25519
cargo run --example sign_delete_key_secp256k1
cargo run --example sign_stake
cargo run --example sign_add_key_fullaccess
cargo run --example sign_add_key_functioncall
cargo run --example sign_deploy_contract
cargo run --example sign_functioncall_str
cargo run --example sign_functioncall_bin
cargo run --example sign_functioncall_str_parse_err
cargo run --example sign_batch_all_actions

Sign a NEP-413 message

RUST_LOG=info cargo run --example sign_nep_413_message

Sign a NEP-366 delegate action

RUST_LOG=info cargo run --example sign_nep_366_delegate_action

Dependencies

~21–35MB
~461K SLoC