Skip to content

Commit 1ebe611

Browse files
committed
keystore: move keystore_root_fingerprint to Rust
1 parent 1f109de commit 1ebe611

File tree

7 files changed

+25
-42
lines changed

7 files changed

+25
-42
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ add_custom_target(rust-bindgen
317317
--allowlist-function keystore_secp256k1_sign
318318
--allowlist-function keystore_secp256k1_schnorr_bip86_sign
319319
--allowlist-function keystore_bip39_mnemonic_to_seed
320-
--allowlist-function keystore_get_root_fingerprint
321320
--allowlist-function keystore_mock_unlocked
322321
--allowlist-var EC_PUBLIC_KEY_UNCOMPRESSED_LEN
323322
--allowlist-var EC_PUBLIC_KEY_LEN

src/keystore.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,6 @@ static bool _get_xprv(const uint32_t* keypath, const size_t keypath_len, struct
466466
return true;
467467
}
468468

469-
bool keystore_get_root_fingerprint(uint8_t* fingerprint)
470-
{
471-
struct ext_key derived_xpub __attribute__((__cleanup__(keystore_zero_xkey))) = {0};
472-
if (!keystore_get_xpub(NULL, 0, &derived_xpub)) {
473-
return false;
474-
}
475-
if (bip32_key_get_fingerprint(&derived_xpub, fingerprint, 4) != WALLY_OK) {
476-
return false;
477-
}
478-
return true;
479-
}
480-
481469
static bool _ext_key_equal(struct ext_key* one, struct ext_key* two)
482470
{
483471
if (!MEMEQ(one->chain_code, two->chain_code, sizeof(one->chain_code))) {

src/keystore.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,6 @@ USE_RESULT bool keystore_bip39_mnemonic_to_seed(
142142
uint8_t* seed_out,
143143
size_t* seed_len_out);
144144

145-
/**
146-
* Fills a uint8_t buffer with a fingerprint of the root public key at m/, which are the first four
147-
* bytes of its hash160 according to:
148-
* https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
149-
* @param[out] fingerprint, buffer of the fingerprint that is supposed to be filled, has to be size
150-
* 4
151-
*/
152-
USE_RESULT bool keystore_get_root_fingerprint(uint8_t* fingerprint);
153-
154145
/**
155146
* Can be used only if the keystore is unlocked. Returns the derived xpub,
156147
* using bip32 derivation. Derivation is done from the xprv master, so hardened

src/rust/bitbox02-rust/src/hww/api/rootfingerprint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ use super::Error;
1717

1818
use pb::response::Response;
1919

20-
use bitbox02::keystore;
20+
use crate::keystore;
2121

2222
/// Returns the keystore's root fingerprint, which is the first 32
2323
/// bits of the hash160 of the pubkey at the keypath m/.
2424
/// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#key-identifiers
2525
pub fn process() -> Result<Response, Error> {
2626
let fingerprint = keystore::root_fingerprint()?;
2727
Ok(Response::Fingerprint(pb::RootFingerprintResponse {
28-
fingerprint: fingerprint.to_vec(),
28+
fingerprint,
2929
}))
3030
}
3131

src/rust/bitbox02-rust/src/keystore.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub fn secp256k1_pubkey_hash160(keypath: &[u32]) -> Result<Vec<u8>, ()> {
3838
Ok(bitbox02::hash160(&xpub.public_key).to_vec())
3939
}
4040

41+
/// Returns fingerprint of the root public key at m/, which are the first four bytes of its hash160
42+
/// according to:
43+
/// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
44+
pub fn root_fingerprint() -> Result<Vec<u8>, ()> {
45+
Ok(secp256k1_pubkey_hash160(&[])?.get(..4).ok_or(())?.to_vec())
46+
}
47+
4148
#[cfg(test)]
4249
mod tests {
4350
use super::*;
@@ -115,4 +122,20 @@ mod tests {
115122
*b"\xe5\xf8\x9a\xb6\x54\x37\x44\xf7\x8f\x15\x86\x7c\x43\x06\xee\x86\x6b\xb1\x1d\xf9"
116123
);
117124
}
125+
126+
#[test]
127+
fn test_root_fingerprint() {
128+
keystore::lock();
129+
assert_eq!(root_fingerprint(), Err(()));
130+
131+
mock_unlocked_using_mnemonic(
132+
"purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay"
133+
);
134+
assert_eq!(root_fingerprint(), Ok(vec![0x02, 0x40, 0xe9, 0x2a]));
135+
136+
mock_unlocked_using_mnemonic(
137+
"small agent wife animal marine cloth exit thank stool idea steel frame",
138+
);
139+
assert_eq!(root_fingerprint(), Ok(vec![0xf4, 0x0b, 0x46, 0x9a]));
140+
}
118141
}

src/rust/bitbox02/src/keystore.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,6 @@ pub fn bip39_mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u
284284
}
285285
}
286286

287-
pub fn root_fingerprint() -> Result<[u8; 4], ()> {
288-
let mut fingerprint = [0u8; 4];
289-
match unsafe { bitbox02_sys::keystore_get_root_fingerprint(fingerprint.as_mut_ptr()) } {
290-
true => Ok(fingerprint),
291-
false => Err(()),
292-
}
293-
}
294-
295287
pub fn encrypt_and_store_seed(seed: &[u8], password: &SafeInputString) -> Result<(), Error> {
296288
match unsafe {
297289
bitbox02_sys::keystore_encrypt_and_store_seed(

test/unit-test/test_keystore.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,6 @@ static void _test_keystore_get_xpub(void** state)
215215
wally_free_string(xpub_string);
216216
}
217217

218-
static void _test_keystore_get_root_fingerprint(void** state)
219-
{
220-
keystore_mock_unlocked(_mock_seed, sizeof(_mock_seed), _mock_bip39_seed);
221-
uint8_t fingerprint[4];
222-
assert_true(keystore_get_root_fingerprint(fingerprint));
223-
uint8_t expected_fingerprint[4] = {0x9e, 0x1b, 0x2d, 0x1e};
224-
assert_memory_equal(fingerprint, expected_fingerprint, 4);
225-
}
226-
227218
static void _test_keystore_secp256k1_nonce_commit(void** state)
228219
{
229220
uint8_t msg[32] = {0};
@@ -783,7 +774,6 @@ int main(void)
783774

784775
const struct CMUnitTest tests[] = {
785776
cmocka_unit_test(_test_keystore_get_xpub),
786-
cmocka_unit_test(_test_keystore_get_root_fingerprint),
787777
cmocka_unit_test(_test_keystore_secp256k1_nonce_commit),
788778
cmocka_unit_test(_test_keystore_secp256k1_sign),
789779
cmocka_unit_test(_test_keystore_encrypt_and_store_seed),

0 commit comments

Comments
 (0)