Skip to content

Commit

Permalink
Merge pull request #161 from nervina-labs/transfer-lock-hash
Browse files Browse the repository at this point in the history
Add withdrawal lock hash to transfer smt rpc
  • Loading branch information
duanyytop authored May 11, 2023
2 parents 95430a4 + 7c45ab2 commit f247f03
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cota-aggregator"
version = "0.10.8"
version = "0.10.9"
edition = "2018"

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 '{
Expand All @@ -237,6 +239,7 @@ echo '{
"params":{
"lock_script":"0x49000000100000003000000031000000124a60cd799e1fbca664196de46b3f7f0ecb7138133dcaea4893c51df5b02be60114000000fa15357eb4ad2989f910268db3b3a585a9b51cbe",
"withdrawal_lock_script":"0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000af4baf7e15c13b9f95ee56166b9c840dd46973b1",
"withdrawal_lock_hash":"0xc84947784cce65bdd259948630a5e77ebcfce205fd53a55dc333afe98007bd19",
"transfer_out_point":"0x777347181a25dc39c31ad290b9e2d52ded42295000000000",
"transfers":[
{
Expand Down
6 changes: 3 additions & 3 deletions src/ckb/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ pub struct WithdrawRawTx {

pub async fn get_withdraw_info(
block_number: u64,
withdrawal_lock_script: Vec<u8>,
withdrawal_lock_hash: [u8; 32],
) -> Result<WithdrawRawTx, Error> {
let is_mainnet: bool = match env::var("IS_MAINNET") {
Ok(mainnet) => from_str::<bool>(&mainnet).unwrap(),
Expand All @@ -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
});
Expand Down
7 changes: 3 additions & 4 deletions src/entries/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 3 additions & 7 deletions src/entries/claim_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/entries/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/entries/transfer_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 23 additions & 5 deletions src/request/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
pub withdrawal_lock_script: Vec<u8>,
pub withdrawal_lock_script: Option<Vec<u8>>,
pub withdrawal_lock_hash: [u8; 32],
pub transfer_out_point: [u8; 24],
pub transfers: Vec<TransferWithdrawal>,
}

impl TransferReq {
pub fn from_map(map: &Map<String, Value>) -> Result<Self, Error> {
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::<TransferWithdrawal>(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::<TransferWithdrawal>(map, "transfers")?,
})
}
}
Expand Down

0 comments on commit f247f03

Please sign in to comment.