From 7af82352812cc25859b894389bfc37f5152dcea3 Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Wed, 8 Nov 2023 12:35:02 +0800 Subject: [PATCH] refactor!: user_lock_script -> module_lock_script --- README.md | 2 +- examples/sudt-transfer.rs | 57 +++++++++++++++++++++------------------ src/config.rs | 14 +++++----- tests/sudt-transfer.rs | 6 ++--- tests/transaction.rs | 6 ++--- 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index f5b9da7..acb5d22 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Use `cargo +nightly doc --open` to view the full API docs. ## Configuration -- user_lock_script: module lock script +- module_lock_script - axon_metadata_type_script - channel_contract_type_id_args - channel_id diff --git a/examples/sudt-transfer.rs b/examples/sudt-transfer.rs index 1bc5418..09723b5 100644 --- a/examples/sudt-transfer.rs +++ b/examples/sudt-transfer.rs @@ -111,45 +111,47 @@ async fn main() -> Result<()> { let pubkey = secp256k1::PublicKey::from_secret_key(&secp, &sk); let address = AddressPayload::from_pubkey(&pubkey); - let user_lock_script = packed::Script::from(&address); + let sender_lock_script = packed::Script::from(&address); println!( "port: {}", hex::encode( config .sdk_config - .user_lock_script() + .module_lock_script() .calc_script_hash() .as_slice() ) ); ensure!( - config.sdk_config.user_lock_script().code_hash() + config.sdk_config.module_lock_script().code_hash() == packed::Script::from(config.sudt_transfer_contract_type_script.clone()) .calc_script_hash(), "port id code hash is not sudt transfer contract type script hash", ); match cli.command { - Commands::Recv => receive(config, sk, user_lock_script).await, - Commands::CreateStCell { sudt } => create_st_cell(config, sk, user_lock_script, sudt).await, - Commands::ConsumeAck => consume_ack(config, sk, user_lock_script).await, + Commands::Recv => receive(config, sk, sender_lock_script).await, + Commands::CreateStCell { sudt } => { + create_st_cell(config, sk, sender_lock_script, sudt).await + } + Commands::ConsumeAck => consume_ack(config, sk, sender_lock_script).await, Commands::Send { sudt, receiver, amount, - } => send(config, sk, user_lock_script, sudt, receiver, amount).await, + } => send(config, sk, sender_lock_script, sudt, receiver, amount).await, } } async fn consume_ack( config: Config, sk: secp256k1::SecretKey, - user_lock_script: packed::Script, + sender_lock_script: packed::Script, ) -> Result<()> { let client = CkbRpcClient::new(config.ckb_rpc_url.clone()); - let sender = user_lock_script.calc_script_hash().as_bytes().slice(..20); + let sender = sender_lock_script.calc_script_hash().as_bytes().slice(..20); let mut ack_packets = pin!( PacketCell::subscribe(client.clone(), config.sdk_config.clone()) @@ -178,7 +180,7 @@ async fn consume_ack( .await .context("get sudt dep")?; - let user_input = get_capacity_input(&client, &user_lock_script).await?; + let user_input = get_capacity_input(&client, &sender_lock_script).await?; let packet_contract_cell = get_latest_cell_by_type_script( &client, @@ -205,7 +207,7 @@ async fn consume_ack( .input(simple_input(user_input.out_point.into())) .witness(placeholder_witness.as_bytes().pack()); let tx = add_ibc_envelope(tx, &envelope).build(); - let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?; + let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script, sk)?; send_transaction(&config.ckb_rpc_url, tx)?; Ok(()) @@ -228,14 +230,14 @@ fn get_sudt_type_script(config: &Config, sudt: &str) -> Result { async fn create_st_cell( config: Config, sk: secp256k1::SecretKey, - user_lock_script: packed::Script, + sender_lock_script: packed::Script, sudt: String, ) -> Result<()> { let client = CkbRpcClient::new(config.ckb_rpc_url.clone()); let sudt_type_script = get_sudt_type_script(&config, &sudt)?; - let st_cell_lock_script = config.sdk_config.user_lock_script(); + let st_cell_lock_script = config.sdk_config.module_lock_script(); let a_sudt_cell = get_latest_cell_by_type_script(&client, sudt_type_script.clone().into()) .await @@ -256,7 +258,7 @@ async fn create_st_cell( .cell_dep(sudt_dep) .build(); - let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?; + let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script, sk)?; send_transaction(&config.ckb_rpc_url, tx)?; Ok(()) @@ -265,7 +267,7 @@ async fn create_st_cell( async fn send( config: Config, sk: secp256k1::SecretKey, - user_lock_script: packed::Script, + sender_lock_script: packed::Script, sudt: String, receiver: String, amount: u128, @@ -281,7 +283,7 @@ async fn send( .out_point .into(), ); - let st_cell_lock_script = config.sdk_config.user_lock_script(); + let st_cell_lock_script = config.sdk_config.module_lock_script(); let sudt_search_filter = ckb_indexer::SearchKeyFilter { script: Some(sudt_type_script.clone().into()), script_len_range: { @@ -329,7 +331,7 @@ async fn send( ckb_indexer::SearchKey { filter: Some(sudt_search_filter), group_by_transaction: Some(true), - script: user_lock_script.clone().into(), + script: sender_lock_script.clone().into(), script_search_mode: Some(ckb_indexer::ScriptSearchMode::Exact), script_type: ckb_indexer::ScriptType::Lock, with_data: Some(true), @@ -374,7 +376,7 @@ async fn send( let channel = IbcChannelCell::get_latest(&client, &config.sdk_config).await?; let data = FungibleTokenPacketData { amount: amount.try_into().context("amount overflow")?, - sender: user_lock_script.calc_script_hash().as_bytes()[..20].to_vec(), + sender: sender_lock_script.calc_script_hash().as_bytes()[..20].to_vec(), receiver: hex::decode(receiver.strip_prefix("0x").unwrap_or(&receiver)) .context("receiver")?, denom: hex::encode(sudt_type_script.calc_script_hash().as_slice()), @@ -424,13 +426,13 @@ async fn send( let tx = add_ibc_envelope(tx, &envelope).build(); - let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script.clone(), sk)?; + let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script.clone(), sk)?; send_transaction(&config.ckb_rpc_url, tx)?; println!("consuming ack"); - consume_ack(config, sk, user_lock_script).await?; + consume_ack(config, sk, sender_lock_script).await?; Ok(()) } @@ -438,10 +440,13 @@ async fn send( async fn receive( config: Config, sk: secp256k1::SecretKey, - user_lock_script: packed::Script, + receiver_lock_script: packed::Script, ) -> Result<()> { let client = CkbRpcClient::new(config.ckb_rpc_url.clone()); - let receiver = user_lock_script.calc_script_hash().as_bytes().slice(..20); + let receiver = receiver_lock_script + .calc_script_hash() + .as_bytes() + .slice(..20); let mut recv_packets = pin!( PacketCell::subscribe(client.clone(), config.sdk_config.clone()) @@ -499,7 +504,7 @@ async fn receive( vec![1], )?; - let user_input = get_capacity_input(&client, &user_lock_script).await?; + let user_input = get_capacity_input(&client, &receiver_lock_script).await?; // sighash placeholder witness. let placeholder_witness = packed::WitnessArgs::new_builder() @@ -523,7 +528,7 @@ async fn receive( // sudt output. .output( packed::CellOutput::new_builder() - .lock(user_lock_script.clone()) + .lock(receiver_lock_script.clone()) .type_(Some(sudt_type_script).pack()) .build_exact_capacity(Capacity::bytes(16).unwrap()) .unwrap(), @@ -535,7 +540,7 @@ async fn receive( let tx = add_ibc_envelope(tx, &envelope).build(); - let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?; + let tx = complete_tx(&config.ckb_rpc_url, &tx, receiver_lock_script, sk)?; send_transaction(&config.ckb_rpc_url, tx)?; @@ -584,7 +589,7 @@ async fn get_st_cell_by_sudt_type_hash( .out_point .into(), ); - let st_cell_lock_script = config.sdk_config.user_lock_script(); + let st_cell_lock_script = config.sdk_config.module_lock_script(); let st_cells = client .get_cells( ckb_indexer::SearchKey { diff --git a/src/config.rs b/src/config.rs index 16ef402..c113124 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,7 +15,7 @@ pub use types::*; #[derive(Serialize, Deserialize, Clone)] pub struct Config { /// Its hash is used as port ID. Address (string) or script. - pub user_lock_script: AddressOrScript, + pub module_lock_script: AddressOrScript, /// Axon metadata cell type script. pub axon_metadata_type_script: AddressOrScript, @@ -28,8 +28,8 @@ pub struct Config { } impl Config { - pub fn user_lock_script(&self) -> packed::Script { - self.user_lock_script.script() + pub fn module_lock_script(&self) -> packed::Script { + self.module_lock_script.script() } pub fn axon_metadata_type_script(&self) -> packed::Script { @@ -49,7 +49,7 @@ impl Config { } pub fn port_id(&self) -> [u8; 32] { - self.user_lock_script().calc_script_hash().unpack().0 + self.module_lock_script().calc_script_hash().unpack().0 } pub fn port_id_string(&self) -> String { @@ -65,7 +65,7 @@ impl Config { .0, open: true, channel_id: self.channel_id, - port_id: self.user_lock_script().calc_script_hash().unpack().0, + port_id: self.module_lock_script().calc_script_hash().unpack().0, }; packed::Script::new_builder() .hash_type(ScriptHashType::Type.into()) @@ -81,7 +81,7 @@ impl Config { pub fn packet_cell_lock_script_prefix(&self) -> packed::Script { let packet_args = PacketArgs { channel_id: self.channel_id, - port_id: self.user_lock_script().calc_script_hash().unpack().0, + port_id: self.module_lock_script().calc_script_hash().unpack().0, sequence: 0, }; @@ -96,7 +96,7 @@ impl Config { pub fn packet_cell_lock_script(&self, sequence: u16) -> packed::Script { let packet_args = PacketArgs { channel_id: self.channel_id, - port_id: self.user_lock_script().calc_script_hash().unpack().0, + port_id: self.module_lock_script().calc_script_hash().unpack().0, sequence, }; diff --git a/tests/sudt-transfer.rs b/tests/sudt-transfer.rs index 0523983..eb543be 100644 --- a/tests/sudt-transfer.rs +++ b/tests/sudt-transfer.rs @@ -97,7 +97,7 @@ fn test_send_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: [0u8; 32].into(), - user_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()), + module_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()), }; let current_channel_state = IbcChannel { @@ -225,7 +225,7 @@ fn test_write_ack_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: packet_contract_type_id_args.into(), - user_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()), + module_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()), }; let current_channel_state = IbcChannel { @@ -404,7 +404,7 @@ fn test_consume_ack_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: packet_contract_type_id_args.into(), - user_lock_script: AddressOrScript::Script(sudt_transfer_lock.clone().into()), + module_lock_script: AddressOrScript::Script(sudt_transfer_lock.clone().into()), }; let packet = IbcPacket { diff --git a/tests/transaction.rs b/tests/transaction.rs index 6a6cc16..ff87ae7 100644 --- a/tests/transaction.rs +++ b/tests/transaction.rs @@ -61,7 +61,7 @@ fn test_send_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: [0u8; 32].into(), - user_lock_script: AddressOrScript::Script(always_success_lock.into()), + module_lock_script: AddressOrScript::Script(always_success_lock.into()), }; let current_channel_state = IbcChannel { @@ -167,7 +167,7 @@ fn test_write_ack_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: packet_contract_type_id_args.into(), - user_lock_script: AddressOrScript::Script(always_success_lock.into()), + module_lock_script: AddressOrScript::Script(always_success_lock.into()), }; let current_channel_state = IbcChannel { @@ -286,7 +286,7 @@ fn test_consume_ack_packet() -> Result<()> { channel_id, confirmations: 1, packet_contract_type_id_args: packet_contract_type_id_args.into(), - user_lock_script: AddressOrScript::Script(always_success_lock.into()), + module_lock_script: AddressOrScript::Script(always_success_lock.into()), }; let packet = IbcPacket {