Skip to content

Commit

Permalink
chore: fmt and clean up project. (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Oct 16, 2024
1 parent 2f624ff commit eabed44
Show file tree
Hide file tree
Showing 112 changed files with 4,390 additions and 2,147 deletions.
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.29"
msrv = "1.80"
16 changes: 5 additions & 11 deletions dash/examples/bip32.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
extern crate dashcore;

use std::{env, process};
use std::str::FromStr;
use std::{env, process};

use dashcore::secp256k1::Secp256k1;
use dashcore::PublicKey;
use dashcore::bip32::ExtendedPrivKey;
use dashcore::bip32::ExtendedPubKey;
use dashcore::bip32::DerivationPath;
use dashcore::bip32::ChildNumber;
use dashcore::address::Address;
use dashcore::secp256k1::ffi::types::AlignedType;
use dashcore::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey};
use dashcore::hashes::hex::FromHex;
use dashcore::secp256k1::Secp256k1;
use dashcore::secp256k1::ffi::types::AlignedType;

fn main() {
// This example derives root xprv from a 32-byte seed,
Expand Down Expand Up @@ -55,10 +52,7 @@ fn main() {
// generate first receiving address at m/0/0
// manually creating indexes this time
let zero = ChildNumber::from_normal_idx(0).unwrap();
let public_key = xpub.derive_pub(&secp, &vec![zero, zero])
.unwrap()
.public_key;
let public_key = xpub.derive_pub(&secp, &vec![zero, zero]).unwrap().public_key;
let address = Address::p2wpkh(&PublicKey::new(public_key), network).unwrap();
println!("First receiving address: {}", address);

}
3 changes: 1 addition & 2 deletions dash/examples/ecdsa-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ use dashcore::consensus::encode;
use dashcore::psbt::{self, Input, Psbt, PsbtSighashType};
use dashcore::secp256k1::{Secp256k1, Signing, Verification};
use dashcore::{
Address, Amount, Network, OutPoint, PublicKey, ScriptBuf, Transaction, TxIn, TxOut,
Witness,
Address, Amount, Network, OutPoint, PublicKey, ScriptBuf, Transaction, TxIn, TxOut, Witness,
};

type Result<T> = std::result::Result<T, Error>;
Expand Down
9 changes: 3 additions & 6 deletions dash/examples/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
extern crate dashcore;

use std::io::{BufReader, Write};
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpStream};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, process};
use std::io::{Write, BufReader};

use dashcore::consensus::{encode, Decodable};
use dashcore::consensus::{Decodable, encode};
use dashcore::network::{address, constants, message, message_network};
use dashcore::secp256k1;
use dashcore::secp256k1::rand::Rng;
Expand Down Expand Up @@ -80,10 +80,7 @@ fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
let services = constants::ServiceFlags::NONE;

// "standard UNIX timestamp in seconds"
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time error")
.as_secs();
let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time error").as_secs();

// "The network address of the node receiving this message"
let addr_recv = address::Address::new(&address, constants::ServiceFlags::NONE);
Expand Down
11 changes: 5 additions & 6 deletions dash/examples/taproot-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ use dashcore::secp256k1::Secp256k1;
use dashcore::sighash::{self, SighashCache, TapSighash, TapSighashType};
use dashcore::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
use dashcore::{
absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut,
Witness,
Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness, absolute,
script,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -491,10 +491,9 @@ impl BenefactorWallet {
self.beneficiary_xpub.derive_pub(&self.secp, &new_derivation_path)?.to_x_only_pub();

// Build up the leaf script and combine with internal key into a taproot commitment
let lock_time = absolute::LockTime::from_height(
psbt.unsigned_tx.lock_time + lock_time_delta,
)
.unwrap();
let lock_time =
absolute::LockTime::from_height(psbt.unsigned_tx.lock_time + lock_time_delta)
.unwrap();
let script = Self::time_lock_script(lock_time, beneficiary_key);
let leaf_hash = script.tapscript_leaf_hash();

Expand Down
88 changes: 48 additions & 40 deletions dash/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use core::marker::PhantomData;
use core::str::FromStr;

use bech32;
use hashes::{sha256, Hash, HashEngine};
use hashes::{Hash, HashEngine, sha256};
use internals::write_err;
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};

Expand Down Expand Up @@ -434,9 +434,9 @@ pub struct WitnessProgram {
impl WitnessProgram {
/// Creates a new witness program.
pub fn new<P>(version: WitnessVersion, program: P) -> Result<Self, Error>
where
P: TryInto<PushBytesBuf>,
<P as TryInto<PushBytesBuf>>::Error: PushBytesErrorReport,
where
P: TryInto<PushBytesBuf>,
<P as TryInto<PushBytesBuf>>::Error: PushBytesErrorReport,
{
let program = program
.try_into()
Expand Down Expand Up @@ -766,8 +766,8 @@ struct AddressInner {
///
#[repr(transparent)]
pub struct Address<V = NetworkChecked>(AddressInner, PhantomData<V>)
where
V: NetworkValidation;
where
V: NetworkValidation;

#[cfg(feature = "serde")]
struct DisplayUnchecked<'a>(&'a Address<NetworkUnchecked>);
Expand All @@ -786,8 +786,8 @@ crate::serde_utils::serde_string_deserialize_impl!(Address<NetworkUnchecked>, "a
#[cfg(feature = "serde")]
impl serde::Serialize for Address<NetworkUnchecked> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
where
S: serde::Serializer,
{
serializer.collect_str(&DisplayUnchecked(self))
}
Expand Down Expand Up @@ -1113,9 +1113,7 @@ impl Address<NetworkUnchecked> {
}

/// Returns the payload as a vector.
pub fn payload_to_vec(&self) -> Vec<u8> {
self.0.payload.inner_prog_as_bytes().to_vec()
}
pub fn payload_to_vec(&self) -> Vec<u8> { self.0.payload.inner_prog_as_bytes().to_vec() }
}

// For NetworkUnchecked , it compare Addresses and if network and payload matches then return true.
Expand Down Expand Up @@ -1276,7 +1274,7 @@ mod tests {
use core::str::FromStr;

use hex_lit::hex;
use secp256k1::{XOnlyPublicKey};
use secp256k1::XOnlyPublicKey;

use super::*;
use crate::crypto::key::PublicKey;
Expand Down Expand Up @@ -1480,14 +1478,32 @@ mod tests {
fn test_bip173_350_vectors() {
// Test vectors valid under both BIP-173 and BIP-350
let valid_vectors = [
("BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", "0014751e76e8199196d454941c45d1b3a323f1433bd6"),
("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", "00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"),
("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", "5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"),
(
"BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4",
"0014751e76e8199196d454941c45d1b3a323f1433bd6",
),
(
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7",
"00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
),
(
"bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y",
"5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6",
),
("BC1SW50QGDZ25J", "6002751e"),
("bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", "5210751e76e8199196d454941c45d1b3a323"),
("tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", "0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
("tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c", "5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
("bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0", "512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
(
"tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy",
"0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433",
),
(
"tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c",
"5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433",
),
(
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0",
"512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
),
];
for vector in &valid_vectors {
let addr: Address = vector.0.parse::<Address<_>>().unwrap().assume_checked();
Expand Down Expand Up @@ -1556,7 +1572,7 @@ mod tests {

#[test]
#[cfg(feature = "serde")]
#[ignore="TODO: adjust tests for dash addr"]
#[ignore = "TODO: adjust tests for dash addr"]
fn test_json_serialize() {
use serde_json;

Expand Down Expand Up @@ -1617,7 +1633,7 @@ mod tests {
ScriptBuf::from_hex(
"00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"
)
.unwrap()
.unwrap()
);

let addr = Address::from_str("bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl")
Expand All @@ -1639,10 +1655,9 @@ mod tests {
#[test]
fn test_qr_string() {
for el in
["Xxb77sLZJZMUeYdf3TpWNoRFgEvjQkoi2q", "XanpivH7JqvtWsCkjB5vXqoGBhv4XGBRJn"].iter()
["Xxb77sLZJZMUeYdf3TpWNoRFgEvjQkoi2q", "XanpivH7JqvtWsCkjB5vXqoGBhv4XGBRJn"].iter()
{
let addr =
Address::from_str(el).unwrap().require_network(Dash).expect("mainnet");
let addr = Address::from_str(el).unwrap().require_network(Dash).expect("mainnet");
assert_eq!(addr.to_qr_uri(), format!("dash:{}", el));
}

Expand All @@ -1665,22 +1680,15 @@ mod tests {
WitnessVersion::try_from(version).unwrap(),
vec![0xab; 32], // Choose 32 to make test case valid for all witness versions(including v0)
)
.unwrap(),
.unwrap(),
)
})
.collect::<Vec<_>>();

const LEGACY_EQUIVALENCE_CLASSES: &[&[Network]] =
&[
&[Network::Dash],
&[Network::Testnet, Network::Regtest, Network::Devnet],
];
&[&[Network::Dash], &[Network::Testnet, Network::Regtest, Network::Devnet]];
const SEGWIT_EQUIVALENCE_CLASSES: &[&[Network]] =
&[
&[Network::Dash],
&[Network::Regtest],
&[Network::Testnet, Network::Devnet],
];
&[&[Network::Dash], &[Network::Regtest], &[Network::Testnet, Network::Devnet]];

fn test_addr_type(payloads: &[Payload], equivalence_classes: &[&[Network]]) {
for pl in payloads {
Expand Down Expand Up @@ -1716,7 +1724,7 @@ mod tests {
let internal_key = XOnlyPublicKey::from_str(
"cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115",
)
.unwrap();
.unwrap();
let secp = Secp256k1::verification_only();
let address = Address::p2tr(&secp, internal_key, None, Network::Dash);
assert_eq!(
Expand Down Expand Up @@ -1745,7 +1753,7 @@ mod tests {
let unused_pubkey = PublicKey::from_str(
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
)
.expect("pubkey");
.expect("pubkey");
assert!(!address.is_related_to_pubkey(&unused_pubkey))
}

Expand All @@ -1766,7 +1774,7 @@ mod tests {
let unused_pubkey = PublicKey::from_str(
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
)
.expect("pubkey");
.expect("pubkey");
assert!(!address.is_related_to_pubkey(&unused_pubkey))
}

Expand All @@ -1787,7 +1795,7 @@ mod tests {
let unused_pubkey = PublicKey::from_str(
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
)
.expect("pubkey");
.expect("pubkey");
assert!(!address.is_related_to_pubkey(&unused_pubkey))
}

Expand All @@ -1808,7 +1816,7 @@ mod tests {
let unused_pubkey = PublicKey::from_str(
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
)
.expect("pubkey");
.expect("pubkey");
assert!(!address.is_related_to_pubkey(&unused_pubkey))
}

Expand All @@ -1835,7 +1843,7 @@ mod tests {
let unused_pubkey = PublicKey::from_str(
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
)
.expect("pubkey");
.expect("pubkey");
assert!(!address.is_related_to_pubkey(&unused_pubkey));
}

Expand Down Expand Up @@ -1866,7 +1874,7 @@ mod tests {
let bad_p2wsh = ScriptBuf::from_hex(
"00202d4fa2eb233d008cc83206fa2f4f2e60199000f5b857a835e3172323385623",
)
.unwrap();
.unwrap();
let invalid_segwitv0_script =
ScriptBuf::from_hex("001161458e330389cd0437ee9fe3641d70cc18").unwrap();
let expected = Err(Error::UnrecognizedScript);
Expand Down
Loading

0 comments on commit eabed44

Please sign in to comment.