Skip to content

隐私交易

QM edited this page Dec 21, 2020 · 1 revision

如何使用隐私交易

本文档通过rpc接口调用, 介绍chain33隐私交易基本用法

简介

隐私交易由privacy合约实现, 其内部是基于utxo的账户模型, 常规地址在隐私合约中生成对应的隐私地址, 因此会存在三种转账模式, 常规到隐私, 隐私到隐私, 隐私到常规, 即公对私, 私对私, 私对公

初始化账户

在节点中预设A, B两个地址账户

变量定义

A="12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
B="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
#private key
privA="0x4257d8692ef7fe13c68b65d6a52f03933db2fa5ce8faf210b5b8b80c721ced01"
privB="0xcc38546e9e659d15e6b4893f0ab32a06d103931a8230b0bde71459d2b27d6944"
fee=1000000
#chain33 rpc
rpc=http://localhost:8801

开启隐私功能

需要指定地址参数, 为空时开启节点钱包内所有地址隐私功能

$ curl -sd '{"method":"privacy.EnablePrivacy", "params":[{"addrs":["'$A'", "'$B'"]}]}' $rpc
{"id":null,"result":{"results":[{"addr":"12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv","IsOK":true,"msg":""},{"addr":"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt","IsOK":true,"msg":""}]},"error":null}

获取隐私地址

#获取A的隐私地址
$ keyPairA=$(curl -sd '{"method":"privacy.ShowPrivacyKey", "params":[{"data":"'$A'"}]}' $rpc |jq .result.pubkeypair)
#获取B的隐私地址
  keyPairB=$(curl -sd '{"method":"privacy.ShowPrivacyKey", "params":[{"data":"'$B'"}]}' $rpc |jq .result.pubkeypair)

转账到隐私合约

隐私交易需要在隐私合约内部进行, A需要在合约内部有足量余额

#获取privacy合约地址
$ privacyExecAddr=$(curl -sd '{"method":"Chain33.ConvertExectoAddr", "params":[{"execname":"privacy"}]}' $rpc | jq .result)
#转账10个bty到合约
$ curl -d '{"method":"Chain33.SendToAddress", "params":[{"from":"'$A'", "to":'$privacyExecAddr', "amount":1000000000}]}' $rpc
{"id":null,"result":{"hash":"0x304db9bf585cbf0f01d654ba40b9f7f840472a2de9e67c820fe05df38e371830"},"error":null}

发起交易

隐私交易同其他交易类似, 需要创建,签名和发送三个步骤

公对私转账

A转5个bty到B的私有账户, type值设为1

#构造
$ txHex=$(curl -sd '{"method":"privacy.CreateRawTransaction", "params":[{"type":1, "tokenname":"BTY", "from":"'$A'", "pubkeypair":'$keyPairB', "amount":500000000}]}' $rpc | jq .result)
#签名
$ signHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"expire":"300s", "privkey":"'$privA'","txHex":'$txHex'}]}' $rpc |jq .result)
#发送
$ curl -sd '{"method":"Chain33.SendTransaction","params":[{"data":'$signHex'}]}' $rpc
{"id":null,"result":"0xf637df0b8c6d1cfcc6cd23cda68a7824fdd40a10712d83c0fe36caa90b329f1f","error":null}

私对私转账

type设为2

#构造
$ txHex=$(curl -sd '{"method":"privacy.CreateRawTransaction", "params":[{"type":2, "tokenname":"BTY", "from":"'$B'", "pubkeypair":'$keyPairA', "amount":100000000}]}' $rpc | jq .result)
#签名
$ signHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"expire":"300s", "privkey":"'$privB'","txHex":'$txHex'}]}' $rpc |jq .result)
#发送
$ curl -sd '{"method":"Chain33.SendTransaction","params":[{"data":'$signHex'}]}' $rpc
{"id":null,"result":"0x38c1895e476a171492521c708c03fbfb2ff535f35bd721d1e0470228c90e78c8","error":null}

私到公转账

type值设为3

#构造
$ txHex=$(curl -sd '{"method":"privacy.CreateRawTransaction", "params":[{"type":3, "tokenname":"BTY", "from":"'$B'", "to":"'$A'", "amount":100000000}]}' $rpc | jq .result)
#签名
$ signHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"expire":"300s", "privkey":"'$privB'","txHex":'$txHex'}]}' $rpc |jq .result)
#发送
$ curl -sd '{"method":"Chain33.SendTransaction","params":[{"data":'$signHex'}]}' $rpc
{"id":null,"result":"0xee4c39099167b7434cf2d52a3258fcb9b1cab7c8a5ce5871fffa05d06ed45f90","error":null}

查询

钱包utxo相关查询

查询utxo余额信息

$ curl -sd '{"method":"privacy.ShowPrivacyAccountInfo","params":[{"addr":"'$A'", "token":"BTY"}]}' $rpc |jq .result

A地址隐私账户utxo详情

{
  "utxos": [
    {
      "amount": "100000000",
      "utxoBasic": {
        "utxoGlobalIndex": {
          "txhash": "0x38c1895e476a171492521c708c03fbfb2ff535f35bd721d1e0470228c90e78c8",
          "outindex": 0
        },
        "onetimePubkey": "0xce782e7010f5e343dd417963330e3d5bad0939947df50f68e3ee9fec284e941e"
      }
    }
  ]
}

查询utxo花费信息

B地址隐私账户utxo花费详情

$ curl -sd '{"method":"privacy.ShowPrivacyAccountSpend","params":[{"addr":"'$B'", "token":"BTY"}]}' $rpc |jq .result

B账户utxo花费情况

{
  "utxoHaveTxHashs": [
    {
      "amount": "500000000",
      "txHash": "38c1895e476a171492521c708c03fbfb2ff535f35bd721d1e0470228c90e78c8",
      "utxoBasic": {
        "utxoGlobalIndex": {
          "txhash": "0xc2bd49d60d6a0a572684664598e0f99e71feaebf77730f2481dcfad2a50a91ff",
          "outindex": 0
        },
        "onetimePubkey": "0xd9a8990f0c93609c1d1e9cea6acba3b4b34c56c06356e6794074ce31a621877b"
      }
    },
    {
      "amount": "200000000",
      "txHash": "ee4c39099167b7434cf2d52a3258fcb9b1cab7c8a5ce5871fffa05d06ed45f90",
      "utxoBasic": {
        "utxoGlobalIndex": {
          "txhash": "0x38c1895e476a171492521c708c03fbfb2ff535f35bd721d1e0470228c90e78c8",
          "outindex": 2
        },
        "onetimePubkey": "0x45e86d975688c3b05c9ec610f7998183359b63b1a550803fd1607f674ac71eea"
      }
    }
  ]
}