From f4d826367f5f4ff60334d72cc0095905125b1d5b Mon Sep 17 00:00:00 2001 From: Maxim Orlovsky Date: Sat, 29 Jul 2023 18:03:02 +0200 Subject: [PATCH] convert: add conversion crate --- .gitignore | 2 +- Cargo.lock | 1 - Cargo.toml | 1 - convert/Cargo.lock | 419 +++++++++++++++++++++++++++++++++++++++++++++ convert/Cargo.toml | 25 +++ convert/src/lib.rs | 39 +++++ src/address.rs | 2 +- src/key.rs | 4 +- src/lib.rs | 1 + src/xpub.rs | 4 +- 10 files changed, 490 insertions(+), 8 deletions(-) create mode 100644 convert/Cargo.lock create mode 100644 convert/Cargo.toml create mode 100644 convert/src/lib.rs diff --git a/.gitignore b/.gitignore index 5f89f2f..a9e1b24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Generated by Cargo # will have compiled files and executables -/target +target/ # These are backup files generated by rustfmt **/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock index f8a15e4..c8b0d2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,7 +116,6 @@ dependencies = [ "bech32", "bitcoin_hashes", "bp-primitives", - "secp256k1", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 2ca7c45..876b81c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ name = "bp" [dependencies] amplify = "4.0.0" -secp256k1 = "0.27.0" bitcoin_hashes = "0.12.0" bech32 = "0.9.1" bp-primitives = "0.10.6" diff --git a/convert/Cargo.lock b/convert/Cargo.lock new file mode 100644 index 0000000..b98a9a3 --- /dev/null +++ b/convert/Cargo.lock @@ -0,0 +1,419 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "amplify" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26966af46e0d200e8bf2b7f16230997c1c3f2d141bc27ccc091c012ed527b58" +dependencies = [ + "amplify_apfloat", + "amplify_derive", + "amplify_num", + "amplify_syn", + "ascii", + "wasm-bindgen", +] + +[[package]] +name = "amplify_apfloat" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5b7f29fad2a3def5dfbfed0da85b886eb927a18fe0296c06e39880b7a96db05" +dependencies = [ + "amplify_num", + "bitflags", +] + +[[package]] +name = "amplify_derive" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87df0f28e6eb1f2d355f29ba6793fa9ca643967528609608d5cbd70bd68f9d1" +dependencies = [ + "amplify_syn", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "amplify_num" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddce3bc63e807ea02065e8d8b702695f3d302ae4158baddff8b0ce5c73947251" + +[[package]] +name = "amplify_syn" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7736fb8d473c0d83098b5bac44df6a561e20470375cd8bcae30516dc889fd62a" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + +[[package]] +name = "bech32" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" + +[[package]] +name = "bitcoin" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e99ff7289b20a7385f66a0feda78af2fc119d28fb56aea8886a9cd0a4abdd75" +dependencies = [ + "bech32", + "bitcoin-private", + "bitcoin_hashes", + "hex_lit", + "secp256k1", +] + +[[package]] +name = "bitcoin-private" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" + +[[package]] +name = "bitcoin_hashes" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501" +dependencies = [ + "bitcoin-private", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bp-convert" +version = "0.11.0" +dependencies = [ + "amplify", + "bitcoin", + "bp-wallet", + "secp256k1", +] + +[[package]] +name = "bp-primitives" +version = "0.10.6" +source = "git+https://github.com/BP-WG/bp-core?branch=primitives#76959e5f9198c9d6e5bfe129ba3ab8fe3c84b64e" +dependencies = [ + "amplify", + "commit_verify", + "secp256k1", + "strict_encoding", +] + +[[package]] +name = "bp-wallet" +version = "0.11.0" +dependencies = [ + "amplify", + "bech32", + "bitcoin_hashes", + "bp-primitives", +] + +[[package]] +name = "bumpalo" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "commit_encoding_derive" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00033f14d67c4169d588f085ea2faeb7b610cf03a74d42ea09eeba31abef2047" +dependencies = [ + "amplify", + "amplify_syn", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "commit_verify" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa8114b3ff20947176c8cbfd1e84e56649501eed4e33ba9205c70374b2615ae" +dependencies = [ + "amplify", + "commit_encoding_derive", + "sha2", + "strict_encoding", +] + +[[package]] +name = "cpufeatures" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hex_lit" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "log" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "secp256k1" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" +dependencies = [ + "bitcoin_hashes", + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" +dependencies = [ + "cc", +] + +[[package]] +name = "sha2" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "strict_encoding" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b1c91b79e62afc09025d828fa0b8dbf4105513de38f126a017919c09bca472" +dependencies = [ + "amplify", + "strict_encoding_derive", +] + +[[package]] +name = "strict_encoding_derive" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5adae55367464f5a229bfd539682c94f870b98a220be6e61dc43f85d612e7e" +dependencies = [ + "amplify_syn", + "heck", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "unicode-ident" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasm-bindgen" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.27", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.27", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" diff --git a/convert/Cargo.toml b/convert/Cargo.toml new file mode 100644 index 0000000..bb37c99 --- /dev/null +++ b/convert/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "bp-convert" +version = "0.11.0" +description = "Convertor between bp-wallet and rust-bitcoin data types" +keywords = ["bitcoin", "wallet"] +categories = ["cryptography::cryptocurrencies"] +readme = "README.md" +authors = ["Dr Maxim Orlovsky "] +homepage = "https://lnp-bp.org" +repository = "https://github.com/BP-WG/bp-wallet" +rust-version = "1.60" # Due to rust-amplify +edition = "2021" +license = "Apache-2.0" + +[lib] +name = "bconv" + +[dependencies] +amplify = "4.0.0" +secp256k1 = "0.27.0" +bitcoin = "0.30.1" +bp-wallet = { path = ".." } + +[patch.crates-io] +bp-primitives = { git = "https://github.com/BP-WG/bp-core", branch = "primitives" } diff --git a/convert/src/lib.rs b/convert/src/lib.rs new file mode 100644 index 0000000..454a03c --- /dev/null +++ b/convert/src/lib.rs @@ -0,0 +1,39 @@ +// Convertor between bp-wallet and rust-bitcoin data types. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Written in 2020-2023 by +// Dr Maxim Orlovsky +// +// Copyright (C) 2020-2023 LNP/BP Standards Association. All rights reserved. +// Copyright (C) 2020-2023 Dr Maxim Orlovsky. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use amplify::RawArray; +use bitcoin::hashes::Hash; + +pub trait Convertible { + type Target: Sized; + fn convert(&self) -> Self::Target; +} + +impl Convertible for bp::Txid { + type Target = bitcoin::Txid; + fn convert(&self) -> Self::Target { Self::Target::from_byte_array(self.to_raw_array()) } +} + +impl Convertible for bitcoin::Txid { + type Target = bp::Txid; + fn convert(&self) -> Self::Target { Self::Target::from_raw_array(self.to_byte_array()) } +} diff --git a/src/address.rs b/src/address.rs index f5efe78..ceef61b 100644 --- a/src/address.rs +++ b/src/address.rs @@ -82,7 +82,7 @@ pub enum AddressParseError { UnrecognizableFormat(String), /// wrong BIP340 public key - #[from(secp256k1::Error)] + #[from(bc::secp256k1::Error)] WrongPublicKeyData, /// unrecognized address format string; must be one of `P2PKH`, `P2SH`, diff --git a/src/key.rs b/src/key.rs index b141ca0..91fd853 100644 --- a/src/key.rs +++ b/src/key.rs @@ -20,14 +20,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use secp256k1::{PublicKey, XOnlyPublicKey}; +use bc::secp256k1::{PublicKey, XOnlyPublicKey}; #[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] #[wrap(Deref, LowerHex)] pub struct TaprootPubkey(pub XOnlyPublicKey); impl TaprootPubkey { - pub fn from_slice(data: impl AsRef<[u8]>) -> Result { + pub fn from_slice(data: impl AsRef<[u8]>) -> Result { XOnlyPublicKey::from_slice(data.as_ref()).map(Self) } diff --git a/src/lib.rs b/src/lib.rs index 69cd551..0b265a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ pub use address::{ Address, AddressNetwork, AddressParseError, AddressPayload, AddressType, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash, }; +pub use bc::{secp256k1, *}; pub use derive::{Derive, DeriveCompr, DeriveXOnly}; pub use descriptors::TrKey; pub use index::{ diff --git a/src/xpub.rs b/src/xpub.rs index dad1404..c92553e 100644 --- a/src/xpub.rs +++ b/src/xpub.rs @@ -25,7 +25,7 @@ use std::fmt::{self, Display, Formatter}; use std::str::FromStr; use amplify::{Array, Wrapper}; -use secp256k1::PublicKey; +use bc::secp256k1::PublicKey; use crate::{base58, DerivationIndex, DerivationPath}; @@ -42,7 +42,7 @@ pub enum XpubDecodeError { UnknownKeyType([u8; 4]), /// extended pubkey contains invalid Secp256k1 pubkey data - #[from(secp256k1::Error)] + #[from(bc::secp256k1::Error)] InvalidPublicKey, }