From 2ac5a35d65b431473d391464cc8fc342a869358e Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Thu, 11 May 2023 16:48:46 +0800 Subject: [PATCH 1/3] feat: Add withdrawal lock hash to transfer smt rpc --- src/ckb/rpc.rs | 6 +++--- src/entries/claim.rs | 7 +++---- src/entries/claim_update.rs | 10 +++------- src/entries/transfer.rs | 5 ++--- src/entries/transfer_update.rs | 6 +----- src/request/transfer.rs | 28 +++++++++++++++++++++++----- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/ckb/rpc.rs b/src/ckb/rpc.rs index 152ab0c..1caca3c 100644 --- a/src/ckb/rpc.rs +++ b/src/ckb/rpc.rs @@ -10,7 +10,7 @@ use cota_smt::common::{ Byte32, Byte32VecBuilder, Bytes, BytesBuilder, MerkleProofBuilder, TransactionProof, TransactionProofBuilder, Uint32, Uint32VecBuilder, }; -use cota_smt::smt::H256; +use cota_smt::smt::{blake2b_256, H256}; use molecule::prelude::{Builder, Byte, Entity}; use serde_json::from_str; use std::env; @@ -27,7 +27,7 @@ pub struct WithdrawRawTx { pub async fn get_withdraw_info( block_number: u64, - withdrawal_lock_script: Vec, + withdrawal_lock_hash: [u8; 32], ) -> Result { let is_mainnet: bool = match env::var("IS_MAINNET") { Ok(mainnet) => from_str::(&mainnet).unwrap(), @@ -54,7 +54,7 @@ pub async fn get_withdraw_info( .filter(|tx| { let position = tx.inner.outputs.clone().into_iter().position(|output| { let lock: Script = output.lock.clone().into(); - let lock_ret: bool = lock.as_slice() == &withdrawal_lock_script; + let lock_ret: bool = blake2b_256(lock.as_slice()) == withdrawal_lock_hash; let type_ret: bool = output.type_.clone().map_or(false, |type_: RPCScript| { type_.code_hash.as_bytes() == &cota_code_hash }); diff --git a/src/entries/claim.rs b/src/entries/claim.rs index 5ecd1db..0aecf46 100644 --- a/src/entries/claim.rs +++ b/src/entries/claim.rs @@ -31,9 +31,9 @@ pub async fn generate_claim_smt( .iter() .map(|claim| (claim.cota_id, claim.token_index)) .collect(); - let withdraw_lock_hash = blake2b_256(&claim_req.withdrawal_lock_script); + let withdrawal_lock_hash = blake2b_256(&claim_req.withdrawal_lock_script); let sender_withdrawals = - get_withdrawal_cota_by_lock_hash(withdraw_lock_hash, &cota_id_index_pairs)?.0; + get_withdrawal_cota_by_lock_hash(withdrawal_lock_hash, &cota_id_index_pairs)?.0; if sender_withdrawals.is_empty() || sender_withdrawals.len() != claims_len { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } @@ -133,8 +133,7 @@ pub async fn generate_claim_smt( .extend(merkel_proof_vec.iter().map(|v| Byte::from(*v))) .build(); - let withdraw_info = - get_withdraw_info(withdrawal_block_number, claim_req.withdrawal_lock_script).await?; + let withdraw_info = get_withdraw_info(withdrawal_block_number, withdrawal_lock_hash).await?; let withdraw_proof = parse_witness_withdraw_proof( withdraw_info.witnesses, &cota_id_index_pairs, diff --git a/src/entries/claim_update.rs b/src/entries/claim_update.rs index 26eece3..a39be50 100644 --- a/src/entries/claim_update.rs +++ b/src/entries/claim_update.rs @@ -31,9 +31,9 @@ pub async fn generate_claim_update_smt( .iter() .map(|nft| (nft.cota_id, nft.token_index)) .collect(); - let withdraw_lock_hash = blake2b_256(&claim_update_req.withdrawal_lock_script); + let withdrawal_lock_hash = blake2b_256(&claim_update_req.withdrawal_lock_script); let sender_withdrawals = - get_withdrawal_cota_by_lock_hash(withdraw_lock_hash, &cota_id_index_pairs)?.0; + get_withdrawal_cota_by_lock_hash(withdrawal_lock_hash, &cota_id_index_pairs)?.0; if sender_withdrawals.is_empty() || sender_withdrawals.len() != nfts_len { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } @@ -147,11 +147,7 @@ pub async fn generate_claim_update_smt( .extend(merkel_proof_vec.iter().map(|v| Byte::from(*v))) .build(); - let withdraw_info = get_withdraw_info( - withdrawal_block_number, - claim_update_req.withdrawal_lock_script, - ) - .await?; + let withdraw_info = get_withdraw_info(withdrawal_block_number, withdrawal_lock_hash).await?; let withdraw_proof = parse_witness_withdraw_proof( withdraw_info.witnesses, &cota_id_index_pairs, diff --git a/src/entries/transfer.rs b/src/entries/transfer.rs index de72f3e..b818511 100644 --- a/src/entries/transfer.rs +++ b/src/entries/transfer.rs @@ -36,7 +36,7 @@ pub async fn generate_transfer_smt( .iter() .map(|transfer| (transfer.cota_id, transfer.token_index)) .collect(); - let withdraw_lock_hash = blake2b_256(&transfer_req.withdrawal_lock_script); + let withdraw_lock_hash = transfer_req.withdrawal_lock_hash; let sender_withdrawals = get_withdrawal_cota_by_lock_hash(withdraw_lock_hash, &cota_id_index_pairs)?.0; if sender_withdrawals.is_empty() || sender_withdrawals.len() != transfers_len { @@ -154,8 +154,7 @@ pub async fn generate_transfer_smt( .extend(transfer_merkel_proof_vec.iter().map(|v| Byte::from(*v))) .build(); - let withdraw_info = - get_withdraw_info(withdrawal_block_number, transfer_req.withdrawal_lock_script).await?; + let withdraw_info = get_withdraw_info(withdrawal_block_number, withdraw_lock_hash).await?; let withdraw_proof = parse_witness_withdraw_proof( withdraw_info.witnesses, &cota_id_index_pairs, diff --git a/src/entries/transfer_update.rs b/src/entries/transfer_update.rs index 6af7f78..7e41cc0 100644 --- a/src/entries/transfer_update.rs +++ b/src/entries/transfer_update.rs @@ -178,11 +178,7 @@ pub async fn generate_transfer_update_smt( ) .build(); - let withdraw_info = get_withdraw_info( - withdrawal_block_number, - transfer_update_req.withdrawal_lock_script, - ) - .await?; + let withdraw_info = get_withdraw_info(withdrawal_block_number, transfer_lock_hash).await?; let withdraw_proof = parse_witness_withdraw_proof( withdraw_info.witnesses, &cota_id_index_pairs, diff --git a/src/request/transfer.rs b/src/request/transfer.rs index 5222791..cdcb374 100644 --- a/src/request/transfer.rs +++ b/src/request/transfer.rs @@ -2,24 +2,42 @@ use super::helper::HexParser; use crate::request::helper::{parse_vec_map, ReqParser}; use crate::request::withdrawal::TransferWithdrawal; use crate::utils::error::Error; +use cota_smt::ckb_types::packed::Script; +use cota_smt::ckb_types::prelude::Entity; +use cota_smt::smt::blake2b_256; use jsonrpc_http_server::jsonrpc_core::serde_json::Map; use jsonrpc_http_server::jsonrpc_core::Value; #[derive(Clone, Eq, PartialEq, Debug)] pub struct TransferReq { pub lock_script: Vec, - pub withdrawal_lock_script: Vec, + pub withdrawal_lock_script: Option>, + pub withdrawal_lock_hash: [u8; 32], pub transfer_out_point: [u8; 24], pub transfers: Vec, } impl TransferReq { pub fn from_map(map: &Map) -> Result { + let withdrawal_lock_hash = match map.get("withdrawal_lock_script") { + Some(_) => { + let lock = map.get_hex_vec_filed("withdrawal_lock_script")?; + if Script::from_slice(&lock).is_err() { + return Err(Error::RequestParamTypeError("Script".to_string())); + } + blake2b_256(&lock) + } + None => map + .get_hex_bytes_filed::<32>("withdrawal_lock_hash") + .map_err(|_| Error::RequestParamTypeError("Withdrawal lock hash".to_string()))?, + }; + Ok(TransferReq { - lock_script: map.get_hex_vec_filed("lock_script")?, - withdrawal_lock_script: map.get_hex_vec_filed("withdrawal_lock_script")?, - transfer_out_point: map.get_hex_bytes_filed::<24>("transfer_out_point")?, - transfers: parse_vec_map::(map, "transfers")?, + lock_script: map.get_hex_vec_filed("lock_script")?, + withdrawal_lock_script: None, + withdrawal_lock_hash, + transfer_out_point: map.get_hex_bytes_filed::<24>("transfer_out_point")?, + transfers: parse_vec_map::(map, "transfers")?, }) } } From 530d299b6b7bde393a8166824e70a7be9721660f Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Thu, 11 May 2023 16:49:10 +0800 Subject: [PATCH 2/3] docs: Update readme api parameters --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 09192f6..2a8ebe2 100644 --- a/README.md +++ b/README.md @@ -222,12 +222,14 @@ Generate smt data(`smt_entry` for `witness_args.input_type` and `smt_root` for c ``` lock_script - The sender's lock script withdrawal_lock_script - The withdrawal's lock script of the NFTs +withdrawal_lock_hash - The withdrawal's lock hash of the NFTs transfer_out_point - The out_point([12..]) of sender's live cell transfers - The information of transfer cota_id - CoTA NFT Class Unique ID token_index - The index of the NFT Class (increment from zero) to_lock_script - The receiver's lock script ``` +> At least one of withdrawal lock script, and withdrawal lock hash must be non-null ```shell echo '{ @@ -237,6 +239,7 @@ echo '{ "params":{ "lock_script":"0x49000000100000003000000031000000124a60cd799e1fbca664196de46b3f7f0ecb7138133dcaea4893c51df5b02be60114000000fa15357eb4ad2989f910268db3b3a585a9b51cbe", "withdrawal_lock_script":"0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000af4baf7e15c13b9f95ee56166b9c840dd46973b1", + "withdrawal_lock_hash":"0xc84947784cce65bdd259948630a5e77ebcfce205fd53a55dc333afe98007bd19", "transfer_out_point":"0x777347181a25dc39c31ad290b9e2d52ded42295000000000", "transfers":[ { From 7c45ab2dcf535b76a8625a330bc19963f3b67838 Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Thu, 11 May 2023 16:49:43 +0800 Subject: [PATCH 3/3] chore: Bump version to v0.10.9 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a9533c5..33360df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,7 +508,7 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "cota-aggregator" -version = "0.10.8" +version = "0.10.9" dependencies = [ "chrono", "ckb-jsonrpc-types", diff --git a/Cargo.toml b/Cargo.toml index e843462..16b1c0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cota-aggregator" -version = "0.10.8" +version = "0.10.9" edition = "2018" [dependencies]