Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rust fmt #56

Merged
merged 8 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions pallets/account-linker/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use crate::Pallet as AccountLinker;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;

use sp_std::prelude::*;

const SEED: u32 = 0;

benchmarks!{
link_eth {
let caller = account("caller", 0, 0);
let account_id: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
let addr_expected: EthAddress = [16, 146, 71, 235, 177, 95, 237, 92, 255, 45, 73, 190, 133, 132, 185, 41, 14, 77, 9, 207];
let expiring_block_number: u32 = 10000;
let sig: Signature = [133, 13, 66, 20, 141, 102, 233, 186, 153, 38, 81, 149, 29, 16, 191, 87, 206, 103, 230, 184, 32, 165, 174, 40, 221, 54, 212, 61, 132, 38, 254, 39, 19, 118, 77, 20, 241, 238, 52, 206, 124, 232, 254, 37, 109, 69, 191, 253, 242, 19, 48, 32, 92, 134, 123, 2, 6, 223, 233, 225, 129, 41, 235, 116, 28];
}: link_eth(RawOrigin::Signed(caller), account_id.clone(), index, addr_expected, expiring_block_number.into(), sig)

link_btc {
let caller = account("caller", 0, 0);
let account_id: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
let addr_expected = vec![49, 51, 121, 55, 106, 72, 52, 85, 57, 113, 68, 112, 69, 77, 77, 119, 87, 90, 117, 52, 99, 122, 52, 107, 55, 67, 81, 107, 90, 72, 100, 101, 113, 71];
let expiring_block_number: u32 = 10000;
let sig: Signature = [250, 57, 156, 18, 181, 153, 186, 77, 81, 242, 31, 146, 82, 115, 85, 163, 136, 220, 104, 194, 98, 88, 28, 109, 163, 113, 12, 47, 193, 183, 189, 106, 41, 163, 172, 76, 129, 83, 66, 195, 126, 213, 207, 91, 186, 70, 255, 125, 111, 38, 123, 240, 178, 101, 22, 192, 133, 22, 245, 109, 50, 175, 225, 208, 0];
}: link_btc(RawOrigin::Signed(caller), account_id.clone(), index, addr_expected, expiring_block_number.into(), sig)

link_polkadot {
let caller = account("caller", 0, 0);
let linked_account: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
}: _(RawOrigin::Signed(caller), linked_account, index)

accept_polkadot {
let caller: T::AccountId = account("caller", 0, 0);
let linked_account: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
crate::Pallet::<T>::link_polkadot(RawOrigin::Signed(caller.clone()).into(), linked_account.clone(), index)?;
}: _(RawOrigin::Signed(linked_account), caller)
benchmarks! {
link_eth {
let caller = account("caller", 0, 0);
let account_id: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
let addr_expected: EthAddress = [16, 146, 71, 235, 177, 95, 237, 92, 255, 45, 73, 190, 133, 132, 185, 41, 14, 77, 9, 207];
let expiring_block_number: u32 = 10000;
let sig: Signature = [133, 13, 66, 20, 141, 102, 233, 186, 153, 38, 81, 149, 29, 16, 191, 87, 206, 103, 230, 184, 32, 165, 174, 40, 221, 54, 212, 61, 132, 38, 254, 39, 19, 118, 77, 20, 241, 238, 52, 206, 124, 232, 254, 37, 109, 69, 191, 253, 242, 19, 48, 32, 92, 134, 123, 2, 6, 223, 233, 225, 129, 41, 235, 116, 28];
}: link_eth(RawOrigin::Signed(caller), account_id.clone(), index, addr_expected, expiring_block_number.into(), sig)

link_btc {
let caller = account("caller", 0, 0);
let account_id: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
let addr_expected = vec![49, 51, 121, 55, 106, 72, 52, 85, 57, 113, 68, 112, 69, 77, 77, 119, 87, 90, 117, 52, 99, 122, 52, 107, 55, 67, 81, 107, 90, 72, 100, 101, 113, 71];
let expiring_block_number: u32 = 10000;
let sig: Signature = [250, 57, 156, 18, 181, 153, 186, 77, 81, 242, 31, 146, 82, 115, 85, 163, 136, 220, 104, 194, 98, 88, 28, 109, 163, 113, 12, 47, 193, 183, 189, 106, 41, 163, 172, 76, 129, 83, 66, 195, 126, 213, 207, 91, 186, 70, 255, 125, 111, 38, 123, 240, 178, 101, 22, 192, 133, 22, 245, 109, 50, 175, 225, 208, 0];
}: link_btc(RawOrigin::Signed(caller), account_id.clone(), index, addr_expected, expiring_block_number.into(), sig)

link_polkadot {
let caller = account("caller", 0, 0);
let linked_account: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
}: _(RawOrigin::Signed(caller), linked_account, index)

accept_polkadot {
let caller: T::AccountId = account("caller", 0, 0);
let linked_account: T::AccountId = account("Alice", 0, SEED);
let index: u32 = 0;
crate::Pallet::<T>::link_polkadot(RawOrigin::Signed(caller.clone()).into(), linked_account.clone(), index)?;
}: _(RawOrigin::Signed(linked_account), caller)
}

impl_benchmark_test_suite!(
AccountLinker,
crate::mock::new_test_ext(),
crate::mock::Test,
);
impl_benchmark_test_suite!(AccountLinker, crate::mock::new_test_ext(), crate::mock::Test,);
9 changes: 6 additions & 3 deletions pallets/account-linker/src/btc/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ToBase58 for [u8] {
carry /= 58;

// in original trezor implementation it was underflowing
if j > 0 {
if j > 0 {
j -= 1;
}
}
Expand Down Expand Up @@ -57,8 +57,8 @@ impl ToBase58 for [u8] {
#[cfg(test)]
mod tests {
use super::ToBase58;
use std::str::from_utf8;
use hex::decode;
use std::str::from_utf8;

#[test]
fn test_to_base58_basic() {
Expand All @@ -72,7 +72,10 @@ mod tests {
assert_eq!(from_utf8(&[49, 49].to_base58()).unwrap(), "4k8");
assert_eq!(from_utf8(&b"abc".to_base58()).unwrap(), "ZiCa");
assert_eq!(from_utf8(&b"1234598760".to_base58()).unwrap(), "3mJr7AoUXx2Wqd");
assert_eq!(from_utf8(&b"abcdefghijklmnopqrstuvwxyz".to_base58()).unwrap(), "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f");
assert_eq!(
from_utf8(&b"abcdefghijklmnopqrstuvwxyz".to_base58()).unwrap(),
"3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f"
);
}

#[test]
Expand Down
83 changes: 41 additions & 42 deletions pallets/account-linker/src/btc/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use sha2::{Digest, Sha256};
use ripemd160::Ripemd160;
use sha2::{Digest, Sha256};

pub fn btc_addr_from_pk(pk: &[u8]) -> [u8; 25] {
let mut result = [0u8; 25];

// Now only support P2PKH (Mainnet) prefix = 0
result[0] = 0;
result[1..21].copy_from_slice(&hash160(pk));
let cs = checksum(&result[0..21]);
result[21..25].copy_from_slice(&cs);
result
let mut result = [0u8; 25];

// Now only support P2PKH (Mainnet) prefix = 0
result[0] = 0;
result[1..21].copy_from_slice(&hash160(pk));
let cs = checksum(&result[0..21]);
result[21..25].copy_from_slice(&cs);
result
}

pub fn hash160(bytes: &[u8]) -> [u8; 20] {
let mut hasher_sha256 = Sha256::new();
hasher_sha256.update(bytes);
let digest = hasher_sha256.finalize();
let mut hasher_sha256 = Sha256::new();
hasher_sha256.update(bytes);
let digest = hasher_sha256.finalize();

let mut hasher_ripemd = Ripemd160::new();
hasher_ripemd.update(digest);
let mut hasher_ripemd = Ripemd160::new();
hasher_ripemd.update(digest);

let mut ret = [0; 20];
ret.copy_from_slice(&hasher_ripemd.finalize()[..]);
ret
let mut ret = [0; 20];
ret.copy_from_slice(&hasher_ripemd.finalize()[..]);
ret
}

fn checksum(input: &[u8]) -> [u8; 4] {
Expand All @@ -39,16 +39,16 @@ fn checksum(input: &[u8]) -> [u8; 4] {
/// # Returns
/// * The double SHA256 hash encoded as LE bytes from data
fn dsha256(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(bytes);
let digest = hasher.finalize();
let mut hasher = Sha256::new();
hasher.update(bytes);
let digest = hasher.finalize();

let mut second_hasher = Sha256::new();
second_hasher.update(digest);
let mut second_hasher = Sha256::new();
second_hasher.update(digest);

let mut ret = [0; 32];
ret.copy_from_slice(&second_hasher.finalize()[..]);
ret
let mut ret = [0; 32];
ret.copy_from_slice(&second_hasher.finalize()[..]);
ret
}

// test data can be obtained from here http://gobittest.appspot.com/Address
Expand All @@ -59,30 +59,29 @@ mod tests {

#[test]
fn correct_dhash160() {

let pk = decode("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6").unwrap();

let hash = hash160(&pk);
let hash = hash160(&pk);

let result = decode("010966776006953D5567439E5E39F86A0D273BEE").unwrap();
let result = decode("010966776006953D5567439E5E39F86A0D273BEE").unwrap();
let mut hash_expected = [0u8; 20];
hash_expected[0..20].copy_from_slice(&result[0..20]);

assert_eq!(hash, hash_expected);
}
}

#[test]
fn correct_btc_addr_from_pk() {
let pk = decode("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6").unwrap();
let mut pk_input = [0u8; 65];
pk_input[0..65].copy_from_slice(&pk[0..65]);

let addr = btc_addr_from_pk(&pk_input);
#[test]
fn correct_btc_addr_from_pk() {
let pk = decode("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6").unwrap();
let mut pk_input = [0u8; 65];
pk_input[0..65].copy_from_slice(&pk[0..65]);

let addr_expected_hex = decode("00010966776006953D5567439E5E39F86A0D273BEED61967F6").unwrap();
let mut addr_expected = [0u8; 25];
addr_expected[0..25].copy_from_slice(&addr_expected_hex[0..25]);
assert_eq!(addr, addr_expected);
}
let addr = btc_addr_from_pk(&pk_input);

}
let addr_expected_hex =
decode("00010966776006953D5567439E5E39F86A0D273BEED61967F6").unwrap();
let mut addr_expected = [0u8; 25];
addr_expected[0..25].copy_from_slice(&addr_expected_hex[0..25]);
assert_eq!(addr, addr_expected);
}
}
2 changes: 1 addition & 1 deletion pallets/account-linker/src/btc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod base58;
pub mod legacy;
pub mod witness;
pub mod witness;
Loading