SUDT Cheque Deposit Design and Implementation

Problem to solve

To SUDT deposit to the exchange, the user has to prepare a 142 CKBytes cell to contain the token, or the exchange has to prepare numerous cells for every user and every kind of token. Which is a heavy burden for both sides.

There are some discussions on this, and the community finally figure out a solution, which follows these steps.

  1. The exchange generates secp256k1_sighash_all short addresses for every user sperately.
  2. The user deposit their SUDT to a temporary Cheque Cell.
  3. The exchange scans all the live Cheque Cells, and claims them.
  4. Simultaneously, the claim transaction will return the temporary Cheque Cell capacity back to the user.

Implementation

The lockscript of a Cheque cell is:

Lock: 
	code_hash: <cheque_cell_lock_script>
	hash_type: type
	args:  <receiver_lock_hash[:20]> | <sender_lock_hash[:20]> 

There are two methods to unlock the Cheque cell for both sender and receiver:

Input cell match method

  • The witness field of the Cheque cell is empty
  • There is at least one input cell’s lock script matchs the lock_hash declared in the Cheque cell’s lock args
    • The conresponding input cell’s witness is not empty

Signature match method

  • The witness field of the Cheque cell is an valid secp256k1 signature
  • Generate a lockscript by the following rules:
    • Recover the public key from the signature, get the first 20 bytes of its blake2b hash as lock args
    • Use the default secp256k1_signhash_all script as code_hash
    • Set hash_type as type
  • Ensure the hash of the generated lockscript matchs the lock_hash declared in the Cheque cell’s lock args

And additionally, the transaction must follow other business logics defined by the spec, including the withdrawal time limit and so on.

repo: GitHub - duanyytop/ckb-cheque-script: The lock script of cheque cell on Nervos CKB

Creation example
create cheque cell #1
create cheque cell #2
create cheque cell #3

Claim example
claim cheque cell

5 Likes

翻译 SUDT Cheque Deposit Design and Implementation

要解决的问题

为了要存 sUDT 到交易所,用户必须要准备一个有 142 ckb 的 cell 已包含这个 sUDT token 所需的容量,或者说交易所就必须准备许多的 cell 来服务每位用户和每一种代币,这个负担对双方来说都贼重的。

先前我们针对这个问题有过一些 讨论 社区最后终于想出这个方案了,步骤大概如下:

  1. 交易所生成 secp256k1_sighash_all 地址分别给每位用户
  2. 用户存入我们的 SUDT 到暂时的 Cheque Cell.
  3. 交易所扫描了全部的处于 live 状态的 Cheque Cells,并且提取他。
  4. 同时,这个索取的交易会退回这个暂时的 Cheque Cell 中的 capacity 给回到用户

Implementation 实现

Cheque cell 的锁定脚本长这样:

Lock: 
	code_hash: <cheque_cell_lock_script>
	hash_type: type
	args:  <receiver_lock_hash[:20]> | <sender_lock_hash[:20]> 

对传送人或者接收者来说我们都有两种方法来解锁 cheque cell

使得作为输入的 Cell 匹配的方法

  • Cheque cell 的 witness 字段必须为空
  • 至少要有一个作为输入的 cell 的 lock script 要匹配 Cheque cell 的 lock args 声明的 lock_hash
    *相应作为输入的 cell 的 witness 不能为空

签名匹配法

  • Cheque cell 的 witness 字段是一个有效的 secp256k1 签名。
  • 借由以下的规则生成一个解锁脚本
    *从签名中恢复公钥, 获得了他用来当作是lock args 的 blake2b 哈希的 第一个 20 bytes
    *使用预设的 secp256k1_signhash_all 脚本作为 code_hash
    • 设定 hash_type 来当作 type
  • 确保生成的锁脚本的哈希与 Cheque Cell 的 lock 参数中声明的 lock_hash 相匹配

此外,交易必须遵循此规范定义的其他业务逻辑,包括取款时间限制等等。

repo: GitHub - duanyytop/ckb-cheque-script: The lock script of cheque cell on Nervos CKB

Creation example
create cheque cell #1
create cheque cell #2
create cheque cell #3

Claim example
claim cheque cell

1 Like