Skip to content

Commit

Permalink
Fix all test
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Mar 3, 2020
1 parent 11bbb7f commit a986433
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 42 deletions.
4 changes: 1 addition & 3 deletions tcx-btc-fork/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ mod tests {
use tcx_constants::btc_fork_network::network_from_param;

use tcx_constants::{CoinInfo, CurveType};
use tcx_primitive::{
Bip32DeterministicPrivateKey, Derive, DerivePath, DeterministicPrivateKey, Ss58Codec,
};
use tcx_primitive::{Bip32DeterministicPrivateKey, Derive, DeterministicPrivateKey, Ss58Codec};

#[test]
pub fn test_btc_fork_address() {
Expand Down
4 changes: 2 additions & 2 deletions tcx-btc-fork/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::marker::PhantomData;
use tcx_chain::Address;
use tcx_constants::CoinInfo;
use tcx_primitive::{
Bip32DeterministicPublicKey, Derive, DerivePath, DeterministicPublicKey, FromHex, PrivateKey,
PublicKey, TypedDeterministicPublicKey,
Bip32DeterministicPublicKey, Derive, DeterministicPublicKey, FromHex, PrivateKey, PublicKey,
TypedDeterministicPublicKey,
};

const DUST: u64 = 546;
Expand Down
5 changes: 2 additions & 3 deletions tcx-primitive/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,10 @@ impl Ss58Codec for Bip32DeterministicPrivateKey {
mod tests {
use crate::ToHex;
use crate::{
Bip32DeterministicPrivateKey, Bip32DeterministicPublicKey, Derive, DerivePath,
DeterministicPrivateKey, PrivateKey, Ss58Codec,
Bip32DeterministicPrivateKey, Bip32DeterministicPublicKey, Derive, DeterministicPrivateKey,
PrivateKey, Ss58Codec,
};
use bip39::{Language, Mnemonic, Seed};
use std::str::FromStr;

fn default_seed() -> Seed {
let mn = Mnemonic::from_phrase(
Expand Down
4 changes: 2 additions & 2 deletions tcx-primitive/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ impl Derive for TypedDeterministicPrivateKey {
#[cfg(test)]
mod tests {
use super::{PrivateKey, PublicKey, TypedDeterministicPrivateKey, TypedPrivateKey};
use crate::{Derive, DerivePath, TypedPublicKey};
use crate::{Derive, TypedPublicKey};
use bip39::{Language, Mnemonic, Seed};
use std::str::FromStr;

use tcx_constants::{CurveType, TEST_MNEMONIC};

fn default_seed() -> Seed {
Expand Down
4 changes: 2 additions & 2 deletions tcx-substrate/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sp_core::crypto::Ss58Codec;
use sp_core::sr25519::Public;
use tcx_chain::Address;
use tcx_constants::{CoinInfo, Result};
use tcx_primitive::{PublicKey, Sr25519PrivateKey, Sr25519PublicKey, TypedPublicKey};
use tcx_primitive::{PublicKey, Sr25519PublicKey, TypedPublicKey};

pub struct SubstrateAddress();

Expand All @@ -26,7 +26,7 @@ impl Address for SubstrateAddress {

fn is_valid(address: &str, coin: &CoinInfo) -> bool {
match Public::from_ss58check_with_version(address) {
Ok((addr, version)) => match coin.network.as_str() {
Ok((_addr, version)) => match coin.network.as_str() {
"KUSAMA" => version == Ss58AddressFormat::KusamaAccountDirect,
"POLKADOT" => version == Ss58AddressFormat::PolkadotAccountDirect,
_ => false,
Expand Down
7 changes: 2 additions & 5 deletions tcx-substrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ pub(crate) const PAYLOAD_HASH_THRESHOLD: usize = 256;

#[cfg(test)]
mod tests {
use sp_core::crypto::Ss58Codec;
use sp_keyring::Sr25519Keyring;

use codec::HasCompact;
use codec::{Decode, Encode};
use sp_core::offchain::Capability::Keystore;
use sp_core::{blake2_256, H256};
use sp_keyring::ed25519::Keyring;

use sp_core::H256;

#[test]
fn it_works() {
Expand Down
10 changes: 3 additions & 7 deletions tcx-substrate/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use crate::transaction::{SubstrateRawTxIn, SubstrateTxIn, SubstrateTxOut};
use crate::tx_serializer::{hash_unsigned_payload, ExtrinsicSignature};
use crate::{
ACCOUNT_INDEX_FLAG, PAYLOAD_HASH_THRESHOLD, SIGNATURE_TYPE_ED25519, SIGNATURE_TYPE_SR25519,
SIGNED_EXTRINSIC_V4,
};
use crate::tx_serializer::hash_unsigned_payload;
use crate::{ACCOUNT_INDEX_FLAG, SIGNATURE_TYPE_SR25519, SIGNED_EXTRINSIC_V4};
use base58::FromBase58;
use codec::Compact;
use codec::Encode;
use failure::format_err;
use sp_core::{blake2_256, Pair};

use tcx_chain::{ChainSigner, Keystore, TransactionSigner as TraitTransactionSigner};
use tcx_constants::Result;
use tcx_primitive::{PublicKey, Sr25519PublicKey, Ss58Codec};

impl TraitTransactionSigner<SubstrateTxIn, SubstrateTxOut> for Keystore {
fn sign_transaction(
Expand Down
20 changes: 9 additions & 11 deletions tcx-substrate/src/tx_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use crate::transaction::ExtrinsicEra;
use crate::{
SubstrateAddress, SubstrateTxIn, SubstrateTxOut, ACCOUNT_INDEX_FLAG, PAYLOAD_HASH_THRESHOLD,
};
use base58::FromBase58;
use byteorder::{LittleEndian, WriteBytesExt};
use codec::{Compact, Decode, Encode, HasCompact};

use codec::{Compact, Decode, Encode};
use failure::format_err;
use prost::Message;
use sp_core::crypto::Ss58Codec as SpSs58Codec;
use sp_core::sr25519::Public;
use sp_core::{blake2_256, H256};
use std::mem;

use tcx_constants::Result;
use tcx_primitive::{Sr25519PublicKey, Ss58Codec};
use tcx_primitive::Ss58Codec;

#[derive(Debug, PartialEq, Encode, Decode)]
struct SubstrateInnerTx {
Expand Down Expand Up @@ -120,15 +119,14 @@ pub struct ExtrinsicSignature {
mod tests {
use crate::transaction::ExtrinsicEra;
use crate::SubstrateTxIn;
use base58::ToBase58;
use codec::{Decode, Encode};

use sp_core::blake2_256;
use sp_core::crypto::Ss58Codec;
use sp_keyring::sr25519::Keyring;

use sp_keyring::ed25519::Keyring;

#[test]
fn serialize_tx() {
let expected = "0603ff96074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d419ceb580800fb030000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582";
let expected = "0403ff96074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d419ceb580800fb030000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582";

let tx_in = SubstrateTxIn {
method: "transfer_keep_alive".to_string(),
Expand All @@ -152,6 +150,6 @@ mod tests {
payload
};
let sig = hex::encode(Keyring::Alice.sign(&hash).0.to_vec());
assert_eq!(sig, "019880e3de5c0f02dfa0ef76ad725b79c94d26586c9a3d44ec6e10af6a803b4e378fea7eea77242df5e26f3f2ecbea8f36fb11dc84aea7dea934a190ccf6ac3b84")
assert_eq!(sig, "03079d9c1ad91fc76c0d49fa679dba20c1d94c132843152af05d8b767c43b687df76b3cda51a0d454d8262f46d527cac338574f3915ab479b6c323db0ada920f")
}
}
10 changes: 3 additions & 7 deletions tcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ mod tests {
use tcx_btc_fork::transaction::BtcForkTxInput;
use tcx_btc_fork::transaction::Utxo;

use sp_core::sr25519::Public;
use sp_core::{Pair, Public as TraitPublic};
use sp_runtime::traits::Verify;
use tcx_ckb::{CachedCell, CellInput, CkbTxInput, CkbTxOutput, OutPoint, Script, Witness};
Expand Down Expand Up @@ -260,7 +259,7 @@ mod tests {

fn import_default_wallet() -> WalletResult {
let param = HdStoreImportParam {
mnemonic: OTHER_MNEMONIC.to_string(),
mnemonic: TEST_MNEMONIC.to_string(),
// mnemonic: TEST_MNEMONIC.to_string(),
password: TEST_PASSWORD.to_string(),
source: "MNEMONIC".to_string(),
Expand Down Expand Up @@ -1457,6 +1456,7 @@ mod tests {
};

let wallet = import_and_derive(derivation);

let unsigned_msg = "0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c0703d148e25901007b000000dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025bde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7";
let input = SubstrateRawTxIn {
raw_data: unsigned_msg.to_string(),
Expand All @@ -1477,17 +1477,13 @@ mod tests {
let ret = call_api("sign_tx", tx).unwrap();
let output: SubstrateTxOut = SubstrateTxOut::decode(&ret).unwrap();

let expected_ret_before_sig =
"550284ffce9e36de55716d91b1c50caa36a58cee6d28e532a710df0cf90609363947dd7801";
let expected_ret_after_sig = "dbae140700e40b54020400ff68686f29461fcc99ab3538c391e42556e49efc1ffa7933da42335aa626fae25a0700e40b5402";

assert_eq!(output.signature[0..4].to_string(), "0x01",);

let sig_bytes = hex::decode(output.signature[4..].to_string()).unwrap();
let signature = sp_core::sr25519::Signature::from_slice(&sig_bytes);

let pub_key =
hex::decode("ce9e36de55716d91b1c50caa36a58cee6d28e532a710df0cf90609363947dd78")
hex::decode("90742a577c8515391a46b7881c98c80ec92fe04255bb5b5fec862c7d633ada21")
.unwrap();
let singer = sp_core::sr25519::Public::from_slice(&pub_key);
let msg = hex::decode("0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c0703d148e25901007b000000dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025bde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7").unwrap();
Expand Down

0 comments on commit a986433

Please sign in to comment.