From 2a0dc72bc5e34d1dc4f4564a1b96edee02ecec77 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Tue, 2 Dec 2025 05:07:21 +0100 Subject: [PATCH] add test vectors for keys types --- .gitmodules | 4 ++++ src/descriptor.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 2 +- test_vectors/keys_types.json | 27 +++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 test_vectors/keys_types.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2a70dad --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "secp256k1"] + path = c/secp256k1 + url = https://github.com/bitcoin-core/secp256k1.git + diff --git a/src/descriptor.rs b/src/descriptor.rs index 43d4395..f1aca4b 100644 --- a/src/descriptor.rs +++ b/src/descriptor.rs @@ -288,3 +288,31 @@ pub mod tests { assert_eq!(res, (pks, deriv)); } } + +#[cfg(all(test, feature = "rand"))] +mod keys_types { + use super::*; + use alloc::{string::String, vec::Vec}; + + const TEST_VECTORS_JSON: &str = include_str!("../test_vectors/keys_types.json"); + + #[derive(serde::Deserialize, serde::Serialize)] + + struct TestVector { + description: String, + key: String, + expected: String, + } + + #[test] + fn test_vector_keys_types() { + let vectors: Vec = serde_json::from_str(TEST_VECTORS_JSON).unwrap(); + + for v in vectors { + let dpk = DescriptorPublicKey::from_str(&v.key).expect(&v.description); + let pk = dpk_to_pk(&dpk); + let res = hex::encode(pk.serialize()); + assert_eq!(v.expected, res, "{}", v.description); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 4406d92..35e6305 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![no_std] +// #![no_std] use crate::alloc::string::ToString; extern crate alloc; use alloc::{boxed::Box, string::String, vec, vec::Vec}; diff --git a/test_vectors/keys_types.json b/test_vectors/keys_types.json new file mode 100644 index 0000000..75d98ef --- /dev/null +++ b/test_vectors/keys_types.json @@ -0,0 +1,27 @@ +[ + { + "description": "Xpub with origin and multipath", + "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*", + "expected": "03ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Xpub with origin and w/o multipath", + "key": "[d4ab66f1/48'/1'/1'/2']tpubDFTxBKyUCgkwp5enwZh3t2FJ5AMJqmCWoh1NRT13qNYQb1iKTUrAG6u5gpsDYhG8cZGXouYWuQtzcuSVjPStTc4dwU6JqPMFtgaLGvSQXhi", + "expected": "028e886919a6b72579a28bd292505d2afd41c1b5012414c5e24d7b59f4abdfc0ce" + }, + { + "description": "Compressed public key", + "key": "02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708", + "expected": "02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "X only public key", + "key": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708", + "expected": "02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Uncompressed public key", + "key": "04ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b517089e956909c4c07e8529f45f3ff8904d28df5a181619e21bdf748a896322530039", + "expected": "03ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + } +]