Skip to content

Commit

Permalink
Add btc/eth e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jul 25, 2024
1 parent 6c16d56 commit 2021942
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
9 changes: 8 additions & 1 deletion tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 23 additions & 1 deletion tests/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ pub struct Pickaxer {
impl Pickaxer {
pub fn insert_cell_data(&mut self, dl: &mut Resource, data: &[u8]) -> ckb_types::core::cell::CellMeta {
let cell_out_point = ckb_types::packed::OutPoint::new(self.outpoint_hash.clone(), self.outpoint_i);
let cell_output_type = ckb_types::packed::Script::new_builder()
.args(self.outpoint_i.to_be_bytes().pack())
.code_hash(ckb_chain_spec::consensus::TYPE_ID_CODE_HASH.pack())
.hash_type(ckb_types::core::ScriptHashType::Type.into())
.build();
let cell_output = ckb_types::packed::CellOutput::new_builder()
.capacity(ckb_types::core::Capacity::bytes(0).unwrap().pack())
.type_(Some(cell_output_type).pack())
.build();
let cell_data = ckb_types::bytes::Bytes::copy_from_slice(data);
let cell_meta = ckb_types::core::cell::CellMetaBuilder::from_cell_output(cell_output, cell_data)
Expand Down Expand Up @@ -155,11 +161,27 @@ impl Pickaxer {
.build()
}

pub fn create_script(&self, cell_meta: &ckb_types::core::cell::CellMeta, args: &[u8]) -> ckb_types::packed::Script {
pub fn create_script_by_data(
&self,
cell_meta: &ckb_types::core::cell::CellMeta,
args: &[u8],
) -> ckb_types::packed::Script {
ckb_types::packed::Script::new_builder()
.args(args.pack())
.code_hash(cell_meta.mem_cell_data_hash.clone().unwrap())
.hash_type(ckb_types::core::ScriptHashType::Data1.into())
.build()
}

pub fn create_script_by_type(
&self,
cell_meta: &ckb_types::core::cell::CellMeta,
args: &[u8],
) -> ckb_types::packed::Script {
ckb_types::packed::Script::new_builder()
.args(args.pack())
.code_hash(cell_meta.cell_output.type_().to_opt().unwrap().calc_script_hash())
.hash_type(ckb_types::core::ScriptHashType::Type.into())
.build()
}
}
34 changes: 32 additions & 2 deletions tests/src/test_btc.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -42,14 +43,15 @@ fn default_tx(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Transact
println_hex("pubkey_hash_expect", &pubkey_hash);
// Create cell meta
let cell_meta_ccc_lock_btc = px.insert_cell_data(dl, BINARY_CCC_LOCK_BTC);
let cell_meta_i = px.insert_cell_fund(dl, px.create_script(&cell_meta_ccc_lock_btc, &pubkey_hash), None, &[]);
let cell_meta_i =
px.insert_cell_fund(dl, px.create_script_by_type(&cell_meta_ccc_lock_btc, &pubkey_hash), None, &[]);
// Create cell dep
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_ccc_lock_btc));
// Create input
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
// Create output
let tx_builder =
tx_builder.output(px.create_cell_output(px.create_script(&cell_meta_ccc_lock_btc, &pubkey_hash), None));
tx_builder.output(px.create_cell_output(px.create_script_by_type(&cell_meta_ccc_lock_btc, &pubkey_hash), None));
// Create output data
let tx_builder = tx_builder.output_data(ckb_types::packed::Bytes::default());
// Create witness
Expand Down Expand Up @@ -225,3 +227,31 @@ 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: 0xff934206c421310835b280fd6c9efd98be590f429c2a27a195b
// 9578bde426cd0\n\nIMPORTANT: Please verify the integrity and authenticity of connected BTC wallet before si
// gning 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("IJIw4RokuCqaS6TBTqJSQWvWJuRRX+0opTmhY6vL88nSOWqULiOXaeZbCtQZJ8lHj3eYoz4+5w9sXrCr5/zfxHA=")
.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();
}
32 changes: 30 additions & 2 deletions tests/src/test_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ fn default_tx(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Transact
println_hex("pubkey_hash_expect", &pubkey_hash);
// Create cell meta
let cell_meta_ccc_lock_btc = px.insert_cell_data(dl, BINARY_CCC_LOCK_ETH);
let cell_meta_i = px.insert_cell_fund(dl, px.create_script(&cell_meta_ccc_lock_btc, &pubkey_hash), None, &[]);
let cell_meta_i =
px.insert_cell_fund(dl, px.create_script_by_type(&cell_meta_ccc_lock_btc, &pubkey_hash), None, &[]);
// Create cell dep
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_ccc_lock_btc));
// Create input
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
// Create output
let tx_builder =
tx_builder.output(px.create_cell_output(px.create_script(&cell_meta_ccc_lock_btc, &pubkey_hash), None));
tx_builder.output(px.create_cell_output(px.create_script_by_type(&cell_meta_ccc_lock_btc, &pubkey_hash), None));
// Create output data
let tx_builder = tx_builder.output_data(ckb_types::packed::Bytes::default());
// Create witness
Expand Down Expand Up @@ -209,3 +210,30 @@ 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', ['5369676e696e67206120434b42207472616e73616374696f6e3a203078363665306
// 4383366303062633332336363316665316530383336653038616234363838653036646537353164366534383133633537383738326
// 66565363032370a0a494d504f5254414e543a20506c65617365207665726966792074686520696e7465677269747920616e6420617
// 57468656e746963697479206f6620636f6e6e656374656420457468657265756d2077616c6c6574206265666f7265207369676e696
// e672074686973206d6573736167650a', '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("2291abe57fc51d83a90b3002c3b1994393a56a3cbdfd54a0fd1ece34971607b020eb1c750dbd1f159c631681e7cf1d6e97a0929299b039d6e93a9d7170b6440d1b").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();
}

0 comments on commit 2021942

Please sign in to comment.