Skip to content

Commit

Permalink
Merge pull request #35 from interlay/rustfmt
Browse files Browse the repository at this point in the history
chore: add rustfmt config
  • Loading branch information
gregdhill authored Mar 18, 2021
2 parents cd63260 + dc6192e commit 2c5e6bb
Show file tree
Hide file tree
Showing 101 changed files with 1,681 additions and 3,731 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
### Editors ###
.idea
/target/*

*/launch.json
.vscode

## Rococo
genesis-state*
Expand Down
11 changes: 11 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://rust-lang.github.io/rustfmt/

hard_tabs = false
max_width = 120
edition = "2018"
wrap_comments = true
comment_width = 120
imports_granularity = "Crate"

# TODO: https://github.com/rust-lang/rustfmt/issues/3382#issuecomment-536034758
blank_lines_lower_bound = 0
18 changes: 0 additions & 18 deletions .vscode/launch.json

This file was deleted.

6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

57 changes: 20 additions & 37 deletions crates/bitcoin/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
use crate::types::*;
use crate::Error;
use crate::Script;
use bitcoin_hashes::hash160::Hash as Hash160;
use bitcoin_hashes::Hash;
use crate::{types::*, Error, Script};
use bitcoin_hashes::{hash160::Hash as Hash160, Hash};
use codec::{Decode, Encode};
use sha2::{Digest, Sha256};
use sp_core::H160;

use secp256k1::{
constants::PUBLIC_KEY_SIZE, Error as Secp256k1Error, PublicKey as Secp256k1PublicKey,
};
use secp256k1::{constants::PUBLIC_KEY_SIZE, Error as Secp256k1Error, PublicKey as Secp256k1PublicKey};

/// A Bitcoin address is a serialized identifier that represents the destination for a payment.
/// Address prefixes are used to indicate the network as well as the format. Since the Parachain
/// follows SPV assumptions we do not need to know which network a payment is included in.
#[derive(Encode, Decode, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Copy)]
#[cfg_attr(
feature = "std",
derive(serde::Serialize, serde::Deserialize, std::hash::Hash)
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, std::hash::Hash))]
pub enum Address {
P2PKH(H160),
P2SH(H160),
Expand All @@ -40,7 +32,8 @@ impl std::fmt::Display for Address {
impl Address {
pub fn from_script(script: &Script) -> Result<Self, Error> {
if script.is_p2pkh() {
// 0x76 (OP_DUP) - 0xa9 (OP_HASH160) - 0x14 (20 bytes len) - <20 bytes pubkey hash> - 0x88 (OP_EQUALVERIFY) - 0xac (OP_CHECKSIG)
// 0x76 (OP_DUP) - 0xa9 (OP_HASH160) - 0x14 (20 bytes len) - <20 bytes pubkey hash> - 0x88 (OP_EQUALVERIFY)
// - 0xac (OP_CHECKSIG)
Ok(Self::P2PKH(H160::from_slice(&script.as_bytes()[3..23])))
} else if script.is_p2sh() {
// 0xa9 (OP_HASH160) - 0x14 (20 bytes hash) - <20 bytes script hash> - 0x87 (OP_EQUAL)
Expand Down Expand Up @@ -153,8 +146,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {

pub mod global {
use secp256k1::{ffi::types::AlignedType, AllPreallocated, Secp256k1};
use sp_std::ops::Deref;
use sp_std::{vec, vec::Vec};
use sp_std::{ops::Deref, vec, vec::Vec};
// this is what lazy_static uses internally
use spin::Once;

Expand Down Expand Up @@ -224,10 +216,7 @@ impl PublicKey {
self.new_deposit_public_key_with_secret(&self.new_secret_key(secure_id))
}

fn new_deposit_public_key_with_secret(
&self,
secret_key: &[u8; 32],
) -> Result<Self, Secp256k1Error> {
fn new_deposit_public_key_with_secret(&self, secret_key: &[u8; 32]) -> Result<Self, Secp256k1Error> {
let mut public_key = Secp256k1PublicKey::from_slice(&self.0)?;
// D = V * c
public_key.mul_assign(global::SECP256K1, secret_key)?;
Expand All @@ -245,17 +234,16 @@ impl PublicKey {
mod tests {
use super::*;
use frame_support::assert_err;
use secp256k1::rand::rngs::OsRng;
use secp256k1::{Secp256k1, SecretKey as Secp256k1SecretKey};
use secp256k1::{rand::rngs::OsRng, Secp256k1, SecretKey as Secp256k1SecretKey};

#[test]
fn test_public_key_to_hash() {
// "04ff01b82f2f166c719937d5bd856bd919d9d6d495826cde3733cdb0d1084c8d12b311ced5cc235271c4a16a41fb943ab58e96ca6c4e2f85c6368999c8a3ec26b2"
// "02ff01b82f2f166c719937d5bd856bd919d9d6d495826cde3733cdb0d1084c8d12"

let public_key = PublicKey([
2, 255, 1, 184, 47, 47, 22, 108, 113, 153, 55, 213, 189, 133, 107, 217, 25, 217, 214,
212, 149, 130, 108, 222, 55, 51, 205, 176, 209, 8, 76, 141, 18,
2, 255, 1, 184, 47, 47, 22, 108, 113, 153, 55, 213, 189, 133, 107, 217, 25, 217, 214, 212, 149, 130, 108,
222, 55, 51, 205, 176, 209, 8, 76, 141, 18,
]);

assert_eq!(
Expand All @@ -274,8 +262,7 @@ mod tests {
// https://en.bitcoin.it/wiki/Private_key
assert_err!(
Secp256k1SecretKey::from_slice(
&hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")
.unwrap()
&hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141").unwrap()
),
Secp256k1Error::InvalidSecretKey
);
Expand All @@ -296,9 +283,7 @@ mod tests {
let vault_public_key = PublicKey(vault_public_key.serialize());

// D = V * c
let deposit_public_key = vault_public_key
.new_deposit_public_key(secure_id.clone())
.unwrap();
let deposit_public_key = vault_public_key.new_deposit_public_key(secure_id.clone()).unwrap();

// d = v * c
vault_secret_key
Expand All @@ -315,25 +300,23 @@ mod tests {
fn test_new_deposit_public_key_static() {
// bcrt1qzrkyemjkaxq48zwlnhxvear8fh6lvkwszxy7dm
let old_public_key = PublicKey([
2, 123, 236, 243, 192, 100, 34, 40, 51, 111, 129, 130, 160, 64, 129, 135, 11, 184, 68,
84, 83, 198, 234, 196, 150, 13, 208, 86, 34, 150, 10, 59, 247,
2, 123, 236, 243, 192, 100, 34, 40, 51, 111, 129, 130, 160, 64, 129, 135, 11, 184, 68, 84, 83, 198, 234,
196, 150, 13, 208, 86, 34, 150, 10, 59, 247,
]);

let secret_key = &[
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211,
254, 82, 216, 4, 105, 171, 102, 252, 54, 190, 114, 91, 11, 69,
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211, 254, 82, 216, 4,
105, 171, 102, 252, 54, 190, 114, 91, 11, 69,
];

// bcrt1qn9mgwncjtnavx23utveqqcrxh3zjtll58pc744
let new_public_key = old_public_key
.new_deposit_public_key_with_secret(secret_key)
.unwrap();
let new_public_key = old_public_key.new_deposit_public_key_with_secret(secret_key).unwrap();

assert_eq!(
new_public_key,
PublicKey([
2, 151, 202, 113, 10, 9, 43, 125, 187, 101, 157, 152, 191, 94, 12, 236, 133, 229,
16, 233, 221, 52, 150, 183, 243, 61, 110, 8, 152, 132, 99, 49, 189,
2, 151, 202, 113, 10, 9, 43, 125, 187, 101, 157, 152, 191, 94, 12, 236, 133, 229, 16, 233, 221, 52,
150, 183, 243, 61, 110, 8, 152, 132, 99, 49, 189,
])
);
}
Expand Down
36 changes: 10 additions & 26 deletions crates/bitcoin/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use primitive_types::U256;
use sp_std::vec::Vec;
use sp_std::{prelude::*, vec};
use sp_std::{prelude::*, vec, vec::Vec};

use crate::merkle::MerkleProof;
use crate::script::*;
use crate::types::*;
use crate::Error;
use crate::{merkle::MerkleProof, script::*, types::*, Error};

const WITNESS_FLAG: u8 = 0x01;
const WITNESS_MARKER: u8 = 0x00;
Expand Down Expand Up @@ -290,8 +286,7 @@ impl TryFormattable for MerkleProof {
.ok_or(Error::ArithmeticUnderflow)?;
let mut bytes: Vec<u8> = vec![0; len];
for p in 0..self.flag_bits.len() {
bytes[p.checked_div(8).ok_or(Error::ArithmeticUnderflow)?] |=
(self.flag_bits[p] as u8) << (p % 8) as u8;
bytes[p.checked_div(8).ok_or(Error::ArithmeticUnderflow)?] |= (self.flag_bits[p] as u8) << (p % 8) as u8;
}
formatter.format(bytes.len() as u8);
formatter.output(&bytes);
Expand Down Expand Up @@ -343,8 +338,7 @@ impl Formatter {
#[cfg(test)]
mod tests {
use super::*;
use crate::parser;
use crate::utils::sha256d_le;
use crate::{parser, utils::sha256d_le};

#[test]
fn test_format_int_types() {
Expand Down Expand Up @@ -403,10 +397,8 @@ mod tests {

#[test]
fn test_format_extended_transaction() {
let expected_hash =
H256Le::from_hex_be("b759d39a8596b70b3a46700b83e1edb247e17ba58df305421864fe7a9ac142ea");
let expected_txid =
H256Le::from_hex_be("c586389e5e4b3acb9d6c8be1c19ae8ab2795397633176f5a6442a261bbdefc3a");
let expected_hash = H256Le::from_hex_be("b759d39a8596b70b3a46700b83e1edb247e17ba58df305421864fe7a9ac142ea");
let expected_txid = H256Le::from_hex_be("c586389e5e4b3acb9d6c8be1c19ae8ab2795397633176f5a6442a261bbdefc3a");
let raw_tx = parser::tests::sample_extended_transaction();
let tx_bytes = hex::decode(&raw_tx).unwrap();
let transaction = parser::parse_transaction(&tx_bytes).unwrap();
Expand Down Expand Up @@ -436,13 +428,8 @@ mod tests {
assert_eq!(
parsed_header,
BlockHeader {
merkle_root: H256Le::from_hex_be(
"0c58e162a2d0a6cb5a9ed132f0fdad56eee3a3a03b3ef84aa2ec4a6825a7a029"
),
target: U256::from_dec_str(
"1260618571951953247774709397757627131971305851995253681160192"
)
.unwrap(),
merkle_root: H256Le::from_hex_be("0c58e162a2d0a6cb5a9ed132f0fdad56eee3a3a03b3ef84aa2ec4a6825a7a029"),
target: U256::from_dec_str("1260618571951953247774709397757627131971305851995253681160192").unwrap(),
timestamp: 1603359907,
version: 536870912,
hash_prev_block: H256Le::from_hex_be(
Expand All @@ -458,8 +445,7 @@ mod tests {
// taken from https://bitcoin.org/en/developer-reference#block-headers
#[test]
fn test_format_u256() {
let value = U256::from_dec_str("680733321990486529407107157001552378184394215934016880640")
.unwrap();
let value = U256::from_dec_str("680733321990486529407107157001552378184394215934016880640").unwrap();
let result = value.try_format().unwrap();
let expected = hex::decode("30c31b18").unwrap();
assert_eq!(result, expected);
Expand All @@ -468,9 +454,7 @@ mod tests {
#[test]
fn test_format_u256_testnet() {
// 0xb8e4a3e93640d7a4623e92589e40960b4b20420478d7ed60662176c323cf4caa
let value =
U256::from_dec_str("1260618571951953247774709397757627131971305851995253681160192")
.unwrap();
let value = U256::from_dec_str("1260618571951953247774709397757627131971305851995253681160192").unwrap();
let result = value.try_format().unwrap();
let expected = hex::decode("d4c8001a").unwrap();
assert_eq!(result, expected);
Expand Down
Loading

0 comments on commit 2c5e6bb

Please sign in to comment.