Repository navigation
Expand file tree
/
Copy pathtest_btc.rs
More file actions
141 lines (128 loc) · 4.24 KB
/
Copy pathtest_btc.rs
File metadata and controls
141 lines (128 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: Apache-2.0
#![cfg(feature = "simulator")]
// Simulators only run on linux/amd64.
#![cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[cfg(not(feature = "tokio"))]
compile_error!("Enable the tokio feature to run simulator tests");
mod util;
use util::test_initialized_simulators;
use bitcoin::hashes::Hash;
use std::str::FromStr;
use bitbox_api::pb;
#[tokio::test]
async fn test_btc_xpub() {
test_initialized_simulators(async |paired_bitbox| {
let xpub = paired_bitbox
.btc_xpub(
pb::BtcCoin::Tbtc,
&"m/49'/1'/0'".try_into().unwrap(),
pb::btc_pub_request::XPubType::Ypub,
false,
)
.await
.unwrap();
assert_eq!(
xpub.as_str(),
"ypub6WqXiL3fbDK5QNPe3hN4uSVkEvuE8wXoNCcecgggSuKVpU3Kc4fTvhuLgUhtnbAdaTb9gpz5PQdvzcsKPTLgW2CPkF5ZNRzQeKFT4NSc1xN",
);
})
.await
}
#[tokio::test]
async fn test_btc_xpubs() {
test_initialized_simulators(async |paired_bitbox| {
let result = paired_bitbox
.btc_xpubs(
pb::BtcCoin::Tbtc,
&[
"m/49'/1'/0'".try_into().unwrap(),
"m/84'/1'/0'".try_into().unwrap(),
"m/86'/1'/0'".try_into().unwrap(),
],
pb::btc_xpubs_request::XPubType::Tpub,
)
.await;
assert_eq!(
result.unwrap(),
vec![
"tpubDCNtvuCS9oj3psPNfXZXuGjcQ5rSBi3MzigjBqqwQohWWetoRdLzT5v2uJq6KBTwxj1FYvuPTr7RoWkN4cmubDy5wW8SU3q9xYnDRpQepiT",
"tpubDCYNsKenq7Cuuf4fHsu2fsWA7Wb5cTD2qRUrw6uHbNNYQoNkEoJk4hgNhxbnGss5gnEe2MpqN2qbRVqWJGmuofAWmwFFi4CZ9Tg1LHKJDhF",
"tpubDDc6eecoyYxL4g3WKYpbbinyUmnfVikQCzHTPd6rJQivaPqGKBFiueQqWoAYonB8hAEXGM1ak7LqrnwczH24EbW7jbG5bNK5rncmRXtv7nG",
],
);
})
.await
}
#[tokio::test]
async fn test_btc_address() {
test_initialized_simulators(async |paired_bitbox| {
let address = paired_bitbox
.btc_address(
pb::BtcCoin::Tbtc,
&"m/84'/1'/0'/1/10".try_into().unwrap(),
&bitbox_api::btc::make_script_config_simple(
pb::btc_script_config::SimpleType::P2wpkh,
),
false,
)
.await
.unwrap();
assert_eq!(
address.as_str(),
"tb1qq064dxjgl9h9wzgsmzy6t6306qew42w9ka02u3"
);
})
.await
}
#[tokio::test]
async fn test_btc_sign_message() {
test_initialized_simulators(async |paired_bitbox| {
let xpub_str = paired_bitbox
.btc_xpub(
pb::BtcCoin::Btc,
&"m/49'/0'/0'".try_into().unwrap(),
pb::btc_pub_request::XPubType::Xpub,
false,
)
.await
.unwrap();
let secp = bitcoin::secp256k1::Secp256k1::new();
let pubkey = bitcoin::bip32::Xpub::from_str(&xpub_str)
.unwrap()
.derive_pub(
&secp,
&"m/0/10".parse::<bitcoin::bip32::DerivationPath>().unwrap(),
)
.unwrap()
.to_pub()
.0;
let sign_result = paired_bitbox
.btc_sign_message(
pb::BtcCoin::Btc,
pb::BtcScriptConfigWithKeypath {
script_config: Some(bitbox_api::btc::make_script_config_simple(
pb::btc_script_config::SimpleType::P2wpkhP2sh,
)),
keypath: bitbox_api::Keypath::try_from("m/49'/0'/0'/0/10")
.unwrap()
.to_vec(),
},
b"message",
)
.await
.unwrap();
pubkey
.verify(
&secp,
&bitcoin::secp256k1::Message::from_digest(
bitcoin::hashes::sha256d::Hash::hash(
b"\x18Bitcoin Signed Message:\n\x07message",
)
.to_byte_array(),
),
&bitcoin::secp256k1::ecdsa::Signature::from_compact(&sign_result.sig).unwrap(),
)
.unwrap();
})
.await
}