From 3a684c46df0e892a4855170233af5c095e7bec2e Mon Sep 17 00:00:00 2001 From: mohanson Date: Thu, 25 Jul 2024 14:42:25 +0800 Subject: [PATCH] Add btc/eth e2e tests --- tests/Cargo.lock | 9 ++++++++- tests/Cargo.toml | 1 + tests/src/test_btc.rs | 27 +++++++++++++++++++++++++++ tests/src/test_eth.rs | 23 +++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/Cargo.lock b/tests/Cargo.lock index 74d081e..5aface2 100644 --- a/tests/Cargo.lock +++ b/tests/Cargo.lock @@ -56,6 +56,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64ct" version = "1.6.0" @@ -151,6 +157,7 @@ checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" name = "ccc-lock-tests" version = "0.1.0" dependencies = [ + "base64 0.22.1", "blake2b-ref", "ckb-chain-spec", "ckb-crypto", @@ -1521,7 +1528,7 @@ version = "9.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7a2b3c2bc9693bcb40870c4e9b5bf0d79f9cb46273321bf855ec513e919082" dependencies = [ - "base64", + "base64 0.21.7", "digest", "hex", "miette", diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 8f3855a..3a40d6e 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" edition = "2021" [dependencies] +base64 = "0.22" blake2b-ref = "0.3.1" ckb-chain-spec = "0.116.0" ckb-crypto = "0.116.0" diff --git a/tests/src/test_btc.rs b/tests/src/test_btc.rs index e70dfdc..a0bbd7f 100644 --- a/tests/src/test_btc.rs +++ b/tests/src/test_btc.rs @@ -1,5 +1,6 @@ use crate::common::{assert_script_error, generate_sighash_all, println_hex, ripemd160_sha256, sha256_sha256}; use crate::core::{Pickaxer, Resource, Verifier}; +use base64::Engine; use ckb_types::prelude::{Builder, Entity, Pack}; static BINARY_CCC_LOCK_BTC: &[u8] = include_bytes!("../../build/release/ccc-btc-lock"); @@ -225,3 +226,29 @@ fn test_failure_can_not_recover() { let verifier = Verifier::default(); assert_script_error(verifier.verify(&tx_resolved, &dl).unwrap_err(), 36); } + +#[test] +fn test_e2e() { + let mut dl = Resource::default(); + let mut px = Pickaxer::default(); + let tx = default_tx(&mut dl, &mut px); + + // 1. Install Unisat + // 2. Import account with private key 0x000...0001 + // 3. Open F12 + // 4. Run await unisat.signMessage('Signing a CKB transaction: 0x82ddc4b050a85087fc7cf2c371b12fd6c58645a7f45d8631464f10e8bb0459f4\n\nIMPORTANT: Please verify the integrity and authenticity of connected BTC wallet before signing this message\n') + let wa = ckb_types::packed::WitnessArgs::new_unchecked(tx.witnesses().get_unchecked(0).raw_data()); + let mut wa_lock = wa.lock().to_opt().unwrap().raw_data().to_vec(); + wa_lock.copy_from_slice( + &base64::prelude::BASE64_STANDARD + .decode("ICS5RSHHicZMoZdLwp2Muw7iGoDpbjwbhgSDYSyi04mKG5UdzsKUl6wBcIt513eLNUf+WZ6dyXTAkqti/ycK7uw=") + .unwrap(), + ); + let wa = wa.as_builder().lock(Some(ckb_types::bytes::Bytes::from(wa_lock)).pack()).build(); + let tx = tx.as_advanced_builder().set_witnesses(vec![wa.as_bytes().pack()]).build(); + + let tx_resolved = + ckb_types::core::cell::resolve_transaction(tx, &mut std::collections::HashSet::new(), &dl, &dl).unwrap(); + let verifier = Verifier::default(); + verifier.verify(&tx_resolved, &dl).unwrap(); +} diff --git a/tests/src/test_eth.rs b/tests/src/test_eth.rs index b944687..482bd02 100644 --- a/tests/src/test_eth.rs +++ b/tests/src/test_eth.rs @@ -209,3 +209,26 @@ fn test_failure_sig_use_high_s() { let verifier = Verifier::default(); assert_script_error(verifier.verify(&tx_resolved, &dl).unwrap_err(), 37); } + +#[test] +fn test_e2e() { + let mut dl = Resource::default(); + let mut px = Pickaxer::default(); + let tx = default_tx(&mut dl, &mut px); + + // 1. Install Metamask + // 2. Import account with private key 0x000...0001 + // 3. Open F12 + // 4. Run await ethereum.enable() + // 5. Run await ethereum.send('personal_sign', ['0x5369676e696e67206120434b42207472616e73616374696f6e3a203078363439343565376335383133663961383261633534316630386435363137643637363963373961393264623264663763346438376633633033316638623262380a0a494d504f5254414e543a20506c65617365207665726966792074686520696e7465677269747920616e642061757468656e746963697479206f6620636f6e6e656374656420457468657265756d2077616c6c6574206265666f7265207369676e696e672074686973206d6573736167650a', '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf']) + let wa = ckb_types::packed::WitnessArgs::new_unchecked(tx.witnesses().get_unchecked(0).raw_data()); + let mut wa_lock = wa.lock().to_opt().unwrap().raw_data().to_vec(); + wa_lock.copy_from_slice(&hex::decode("88e0ccbc92a65671ead73682fee2f59938fe91dfd9d0739e43ff9ac9d20eba9d2940a0c665702e6b8c0a7da4d5494b2a00d00fee66d0969b0f32fed4c3b847b31b").unwrap()); + let wa = wa.as_builder().lock(Some(ckb_types::bytes::Bytes::from(wa_lock)).pack()).build(); + let tx = tx.as_advanced_builder().set_witnesses(vec![wa.as_bytes().pack()]).build(); + + let tx_resolved = + ckb_types::core::cell::resolve_transaction(tx, &mut std::collections::HashSet::new(), &dl, &dl).unwrap(); + let verifier = Verifier::default(); + verifier.verify(&tx_resolved, &dl).unwrap(); +}