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/core.rs b/tests/src/core.rs index dc50bbf..5f2a9d4 100644 --- a/tests/src/core.rs +++ b/tests/src/core.rs @@ -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) @@ -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() + } } diff --git a/tests/src/test_btc.rs b/tests/src/test_btc.rs index e70dfdc..6df3b4c 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"); @@ -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 @@ -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(); +} diff --git a/tests/src/test_eth.rs b/tests/src/test_eth.rs index b944687..f7cf7dc 100644 --- a/tests/src/test_eth.rs +++ b/tests/src/test_eth.rs @@ -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 @@ -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(); +}