1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 05:48:23 +00:00
trezor-firmware/rust/trezor-client/examples/sign_message.rs
Roman Zeyde 3d356b25ab fix(rust/trezor-client): update sign_message example
The existing code seems to fail with Model T 2.6.0:

```
2023-11-25T15:52:06.035004Z TRACE trezor_client::client: Sending MessageType_SignMessage msg: SignMessage { address_n: [2147483648, 2147483648, 2147483649], message: Some([114, 101, 103, 101, 108, 32, 104, 101, 116]), coin_name: Some("Testnet"), script_type: Some(SPENDADDRESS), no_script_type: None, chunkify: None, special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } }
2023-11-25T15:52:06.413333Z DEBUG trezor_client::client: Received failure: Failure { code: Some(Failure_DataError), message: Some("Forbidden key path"), special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } }
thread 'main' panicked at examples/sign_message.rs:10:49:
called `Result::unwrap()` on an `Err` value: FailureResponse(Failure { code: Some(Failure_DataError), message: Some("Forbidden key path"), special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } })
```

Also, use `handle_interaction` from `trezor_client::client::common` to
simplify the example.
2023-11-28 09:55:39 +01:00

41 lines
1.2 KiB
Rust

use std::str::FromStr;
use bitcoin::{bip32::DerivationPath, network::Network, Address};
use trezor_client::{client::common::handle_interaction, InputScriptType};
fn main() {
tracing_subscriber::fmt().with_max_level(tracing::Level::TRACE).init();
// init with debugging
let mut trezor = trezor_client::unique(false).unwrap();
trezor.init_device(None).unwrap();
let pubkey = handle_interaction(
trezor
.get_public_key(
&DerivationPath::from_str("m/44h/1h/0h/0/0").unwrap(),
trezor_client::protos::InputScriptType::SPENDADDRESS,
Network::Testnet,
true,
)
.unwrap(),
)
.unwrap();
let addr = Address::p2pkh(&pubkey.to_pub(), Network::Testnet);
println!("address: {}", addr);
let (addr, signature) = handle_interaction(
trezor
.sign_message(
"regel het".to_owned(),
&DerivationPath::from_str("m/44h/1h/0h/0/0").unwrap(),
InputScriptType::SPENDADDRESS,
Network::Testnet,
)
.unwrap(),
)
.unwrap();
println!("Addr from device: {}", addr);
println!("Signature: {:?}", signature);
}