diff --git a/Cargo.toml b/Cargo.toml index dae340d..1b227e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ bech32 = { version = "0.9", default-features = false } bip39 = { version = "2", default-features = false } bitcoin = { version = "0.30", default-features = false } bitcoin_hashes = { version = "0.12", default-features = false } +bs58 = "0.5" crc = "3" criterion = { version = "0.4" } foundation-arena = { path = "arena" } diff --git a/LICENSES/BSD-2-Clause.txt b/LICENSES/BSD-2-Clause.txt new file mode 100644 index 0000000..5f662b3 --- /dev/null +++ b/LICENSES/BSD-2-Clause.txt @@ -0,0 +1,9 @@ +Copyright (c) + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/test-vectors/Cargo.toml b/test-vectors/Cargo.toml index 0370010..4c78a51 100644 --- a/test-vectors/Cargo.toml +++ b/test-vectors/Cargo.toml @@ -13,6 +13,7 @@ edition = "2021" [features] default = ["std"] std = ["hex/std"] +bip32 = ["bs58", "hex/serde"] nostr = ["hex/serde"] seedqr = ["bip39/serde", "hex/serde"] blockchain-commons = ["bitcoin/serde", "hex/serde"] @@ -20,6 +21,7 @@ blockchain-commons = ["bitcoin/serde", "hex/serde"] [dependencies] bip39 = { workspace = true, optional = true } bitcoin = { workspace = true, optional = true, features = ["std"] } +bs58 = { workspace = true, optional = true } hex = { workspace = true, optional = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/test-vectors/build.rs b/test-vectors/build.rs index c5b508e..413ecbf 100644 --- a/test-vectors/build.rs +++ b/test-vectors/build.rs @@ -7,6 +7,7 @@ fn main() { println!("cargo:rerun-if-changed=data/bcr-2020-007.json"); println!("cargo:rerun-if-changed=data/bcr-2020-008.json"); println!("cargo:rerun-if-changed=data/bcr-2020-009.json"); + println!("cargo:rerun-if-changed=data/bip-0032.json"); println!("cargo:rerun-if-changed=data/nip-19.json"); println!("cargo:rerun-if-changed=data/seedqr.json"); } diff --git a/test-vectors/data/bip-0032.json b/test-vectors/data/bip-0032.json new file mode 100644 index 0000000..1db3ba7 --- /dev/null +++ b/test-vectors/data/bip-0032.json @@ -0,0 +1,136 @@ +{ + "valid": [ + { + "name": "Test Vector 1", + "seed-hex": "000102030405060708090a0b0c0d0e0f", + "chains": [ + { + "chain": "m", + "extended-public-key": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8", + "extended-private-key": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi" + }, + { + "chain": "m/0'", + "extended-public-key": "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw", + "extended-private-key": "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7" + }, + { + "chain": "m/0'/1", + "extended-public-key": "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ", + "extended-private-key": "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs" + }, + { + "chain": "m/0'/1/2'", + "extended-public-key": "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5", + "extended-private-key": "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM" + }, + { + "chain": "m/0'/1/2'/2", + "extended-public-key": "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV", + "extended-private-key": "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334" + }, + { + "chain": "m/0'/1/2'/2/1000000000", + "extended-public-key": "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy", + "extended-private-key": "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76" + } + ] + }, + { + "name": "Test Vector 2", + "seed-hex": "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", + "chains": [ + { + "chain": "m", + "extended-public-key": "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB", + "extended-private-key": "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U" + }, + { + "chain": "m/0", + "extended-public-key": "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH", + "extended-private-key": "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt" + }, + { + "chain": "m/0/2147483647'", + "extended-public-key": "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a", + "extended-private-key": "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9" + }, + { + "chain": "m/0/2147483647'/1", + "extended-public-key": "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon", + "extended-private-key": "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef" + }, + { + "chain": "m/0/2147483647'/1/2147483646'", + "extended-public-key": "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL", + "extended-private-key": "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc" + }, + { + "chain": "m/0/2147483647'/1/2147483646'/2", + "extended-public-key": "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt", + "extended-private-key": "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j" + } + ] + }, + { + "name": "Test Vector 3", + "seed-hex": "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", + "chains": [ + { + "chain": "m", + "extended-public-key": "xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13", + "extended-private-key": "xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6" + }, + { + "chain": "m/0'", + "extended-public-key": "xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y", + "extended-private-key": "xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L" + } + ] + }, + { + "name": "Test Vector 4", + "seed-hex": "3ddd5602285899a946114506157c7997e5444528f3003f6134712147db19b678", + "chains": [ + { + "chain": "m", + "extended-public-key": "xpub661MyMwAqRbcGczjuMoRm6dXaLDEhW1u34gKenbeYqAix21mdUKJyuyu5F1rzYGVxyL6tmgBUAEPrEz92mBXjByMRiJdba9wpnN37RLLAXa", + "extended-private-key": "xprv9s21ZrQH143K48vGoLGRPxgo2JNkJ3J3fqkirQC2zVdk5Dgd5w14S7fRDyHH4dWNHUgkvsvNDCkvAwcSHNAQwhwgNMgZhLtQC63zxwhQmRv" + }, + { + "chain": "m/0'", + "extended-public-key": "xpub69AUMk3qDBi3uW1sXgjCmVjJ2G6WQoYSnNHyzkmdCHEhSZ4tBok37xfFEqHd2AddP56Tqp4o56AePAgCjYdvpW2PU2jbUPFKsav5ut6Ch1m", + "extended-private-key": "xprv9vB7xEWwNp9kh1wQRfCCQMnZUEG21LpbR9NPCNN1dwhiZkjjeGRnaALmPXCX7SgjFTiCTT6bXes17boXtjq3xLpcDjzEuGLQBM5ohqkao9G" + }, + { + "chain": "m/0'/1'", + "extended-public-key": "xpub6BJA1jSqiukeaesWfxe6sNK9CCGaujFFSJLomWHprUL9DePQ4JDkM5d88n49sMGJxrhpjazuXYWdMf17C9T5XnxkopaeS7jGk1GyyVziaMt", + "extended-private-key": "xprv9xJocDuwtYCMNAo3Zw76WENQeAS6WGXQ55RCy7tDJ8oALr4FWkuVoHJeHVAcAqiZLE7Je3vZJHxspZdFHfnBEjHqU5hG1Jaj32dVoS6XLT1" + } + ] + } + ], + "invalid": [ + { + "name": "Test Vector 5", + "extended-keys": [ + "xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6LBpB85b3D2yc8sfvZU521AAwdZafEz7mnzBBsz4wKY5fTtTQBm", + "xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFGTQQD3dC4H2D5GBj7vWvSQaaBv5cxi9gafk7NF3pnBju6dwKvH", + "xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6Txnt3siSujt9RCVYsx4qHZGc62TG4McvMGcAUjeuwZdduYEvFn", + "xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFGpWnsj83BHtEy5Zt8CcDr1UiRXuWCmTQLxEK9vbz5gPstX92JQ", + "xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6N8ZMMXctdiCjxTNq964yKkwrkBJJwpzZS4HS2fxvyYUA4q2Xe4", + "xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFAzHGBP2UuGCqWLTAPLcMtD9y5gkZ6Eq3Rjuahrv17fEQ3Qen6J", + "xprv9s2SPatNQ9Vc6GTbVMFPFo7jsaZySyzk7L8n2uqKXJen3KUmvQNTuLh3fhZMBoG3G4ZW1N2kZuHEPY53qmbZzCHshoQnNf4GvELZfqTUrcv", + "xpub661no6RGEX3uJkY4bNnPcw4URcQTrSibUZ4NqJEw5eBkv7ovTwgiT91XX27VbEXGENhYRCf7hyEbWrR3FewATdCEebj6znwMfQkhRYHRLpJ", + "xprv9s21ZrQH4r4TsiLvyLXqM9P7k1K3EYhA1kkD6xuquB5i39AU8KF42acDyL3qsDbU9NmZn6MsGSUYZEsuoePmjzsB3eFKSUEh3Gu1N3cqVUN", + "xpub661MyMwAuDcm6CRQ5N4qiHKrJ39Xe1R1NyfouMKTTWcguwVcfrZJaNvhpebzGerh7gucBvzEQWRugZDuDXjNDRmXzSZe4c7mnTK97pTvGS8", + "DMwo58pR1QLEFihHiXPVykYB6fJmsTeHvyTp7hRThAtCX8CvYzgPcn8XnmdfHGMQzT7ayAmfo4z3gY5KfbrZWZ6St24UVf2Qgo6oujFktLHdHY4", + "DMwo58pR1QLEFihHiXPVykYB6fJmsTeHvyTp7hRThAtCX8CvYzgPcn8XnmdfHPmHJiEDXkTiJTVV9rHEBUem2mwVbbNfvT2MTcAqj3nesx8uBf9", + "xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzF93Y5wvzdUayhgkkFoicQZcP3y52uPPxFnfoLZB21Teqt1VvEHx", + "xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFAzHGBP2UuGCqWLTAPLcMtD5SDKr24z3aiUvKr9bJpdrcLg1y3G", + "xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6Q5JXayek4PRsn35jii4veMimro1xefsM58PgBMrvdYre8QyULY", + "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHL" + ] + } + ] +} diff --git a/test-vectors/data/bip-0032.json.license b/test-vectors/data/bip-0032.json.license new file mode 100644 index 0000000..3afc612 --- /dev/null +++ b/test-vectors/data/bip-0032.json.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: © 2012 Pieter Wuille +SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. +SPDX-License-Identifier: BSD-2-Clause diff --git a/test-vectors/src/bip32.rs b/test-vectors/src/bip32.rs new file mode 100644 index 0000000..89418f4 --- /dev/null +++ b/test-vectors/src/bip32.rs @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. +// SPDX-License-Identifier: GPL-3.0-or-later + +#[derive(Debug, serde::Deserialize)] +pub struct TestVectors { + pub valid: Vec, + pub invalid: Vec, +} + +impl TestVectors { + pub fn new() -> Self { + serde_json::from_slice(include_bytes!("../data/bip-0032.json")) + .expect("file should be valid JSON") + } +} + +#[derive(Debug, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct TestVector { + pub name: String, + #[serde(rename = "seed-hex", with = "hex")] + pub seed: Vec, + pub chains: Vec, +} + +#[derive(Debug, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct Chain { + pub chain: String, + pub extended_public_key: ExtendedKey, + pub extended_private_key: ExtendedKey, +} + +#[derive(Debug, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct InvalidTestVector { + pub name: String, + pub extended_keys: Vec, +} + +#[derive(Debug, serde::Deserialize)] +pub struct ExtendedKey(#[serde(with = "base58")] pub Vec); + +mod base58 { + use bs58::decode::DecodeTarget; + use std::fmt::Formatter; + use std::marker::PhantomData; + + use serde::{ + de::{Error, Visitor}, + Deserializer, + }; + + pub fn deserialize<'de, D, T>(deserializer: D) -> Result + where + D: Deserializer<'de>, + T: DecodeTarget + Default, + { + struct Base58Visitor(PhantomData); + impl<'de, T> Visitor<'de> for Base58Visitor + where + T: DecodeTarget + Default, + { + type Value = T; + + fn expecting(&self, f: &mut Formatter) -> std::fmt::Result { + f.write_str("Base58 encoded string") + } + + fn visit_str(self, v: &str) -> Result + where + E: Error, + { + let mut target = T::default(); + bs58::decode(v).onto(&mut target).map_err(E::custom)?; + Ok(target) + } + + fn visit_string(self, v: String) -> Result + where + E: Error, + { + self.visit_str(&v) + } + } + + deserializer.deserialize_str(Base58Visitor(PhantomData)) + } +} diff --git a/test-vectors/src/lib.rs b/test-vectors/src/lib.rs index 6f3a40b..ed7084e 100644 --- a/test-vectors/src/lib.rs +++ b/test-vectors/src/lib.rs @@ -38,6 +38,9 @@ impl SeedQRVector { } } +#[cfg(feature = "bip32")] +pub mod bip32; + #[cfg(feature = "blockchain-commons")] mod blockchain_commons { use serde::Deserialize;