Commit 2c40966f authored by jiangpeng's avatar jiangpeng Committed by vipwzw

adapting parachain privacy

parent bd063a54
......@@ -22,6 +22,7 @@ import (
func init() {
crypto.Register(privacytypes.SignNameRing, &RingSignED25519{})
crypto.RegisterType(privacytypes.SignNameRing, privacytypes.RingBaseonED25519)
}
// RingSignature 环签名中对于crypto.Signature接口实现
......@@ -156,7 +157,7 @@ func (pubkey *RingSignPublicKey) VerifyBytes(msg []byte, sign crypto.Signature)
return false
}
tx := new(types.Transaction)
if err := types.Decode(msg, tx); err != nil || !bytes.Equal([]byte(privacytypes.PrivacyX), tx.Execer) {
if err := types.Decode(msg, tx); err != nil || !bytes.Equal([]byte(privacytypes.PrivacyX), types.GetRealExecName(tx.Execer)) {
// 目前只有隐私交易用到了环签名
return false
}
......
......@@ -102,7 +102,7 @@ func (p *privacy) Exec_Privacy2Public(payload *ty.Privacy2Public, tx *types.Tran
}
txhashstr := hex.EncodeToString(tx.Hash())
coinsAccount := p.GetCoinsAccount()
receipt, err := coinsAccount.ExecDeposit(tx.To, address.ExecAddress(string(tx.Execer)), payload.Amount)
receipt, err := coinsAccount.ExecDeposit(payload.To, address.ExecAddress(string(tx.Execer)), payload.Amount)
if err != nil {
privacylog.Error("PrivacyTrading Exec", "ActionPrivacy2Public txhash", txhashstr, "ExecDeposit error ", err)
return nil, err
......
......@@ -273,16 +273,20 @@ func (p *privacy) CheckTx(tx *types.Transaction, index int) error {
totalOutput += output.Amount
}
var feeAmount int64
if action.Ty == pty.ActionPrivacy2Privacy {
feeAmount = totalInput - totalOutput
} else {
feeAmount = totalInput - totalOutput - amount
}
//平行链下的隐私交易,utxo不需要燃烧,fee只收取主链的bty,和utxo无关联
if !types.IsPara() {
var feeAmount int64
if action.Ty == pty.ActionPrivacy2Privacy {
feeAmount = totalInput - totalOutput
} else {
feeAmount = totalInput - totalOutput - amount
}
if feeAmount < pty.PrivacyTxFee {
privacylog.Error("PrivacyTrading CheckTx", "txhash", txhashstr, "fee available:", feeAmount, "required:", pty.PrivacyTxFee)
return pty.ErrPrivacyTxFeeNotEnough
if feeAmount < pty.PrivacyTxFee {
privacylog.Error("PrivacyTrading CheckTx", "txhash", txhashstr, "fee available:", feeAmount, "required:", pty.PrivacyTxFee)
return pty.ErrPrivacyTxFeeNotEnough
}
}
return nil
}
......
......@@ -38,6 +38,7 @@ message Privacy2Public {
string tokenname = 1;
int64 amount = 2;
string note = 3;
string to = 6;
PrivacyInput input = 4;
PrivacyOutput output = 5;
}
......@@ -193,49 +194,6 @@ message PrivacyAction4Print {
int32 ty = 4;
}
message ReqPub2Pri {
//接收人可见公钥A
// string viewPublic = 1;
//接收人花费公钥B
// string spendPublic = 2;
string tokenname = 3;
int64 amount = 4;
string note = 5;
//发送人地址
string sender = 6;
int32 mixin = 7;
string pubkeypair = 8;
int64 expire = 9;
}
message ReqPri2Pri {
//接收人可见公钥A
// string viewPublic = 1; //A
//接收人花费公钥B
// string spendPublic = 2; //B
string tokenname = 3;
int64 amount = 4;
string note = 5;
//隐私交易发起人地址
string sender = 6;
int32 mixin = 7;
// string txhash = 7;
string pubkeypair = 8;
int64 expire = 9;
}
message ReqPri2Pub {
string receiver = 1;
string tokenname = 2;
int64 amount = 3;
string note = 4;
//隐私交易发起人地址
string sender = 5;
int32 mixin = 6;
// string txhash = 6;
int64 expire = 7;
}
message ReqCreateUTXOs {
// string viewPublic = 1; //接收人可见公钥A
// string spendPublic = 2; //接收人花费公钥B
......@@ -456,12 +414,6 @@ service privacy {
rpc ShowPrivacyKey(ReqString) returns (ReplyPrivacyPkPair) {}
// 创建一系列UTXO
rpc CreateUTXOs(ReqCreateUTXOs) returns (Reply) {}
// 将资金从公开到隐私转移
rpc MakeTxPublic2Privacy(ReqPub2Pri) returns (Reply) {}
// 将资产从隐私到隐私进行转移
rpc MakeTxPrivacy2Privacy(ReqPri2Pri) returns (Reply) {}
// 将资产从隐私到公开进行转移
rpc MakeTxPrivacy2Public(ReqPri2Pub) returns (Reply) {}
// 扫描UTXO以及获取扫描UTXO后的状态
rpc RescanUtxos(ReqRescanUtxos) returns (RepRescanUtxos) {}
// 使能隐私账户
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -29,36 +29,6 @@ func (policy *privacyPolicy) On_ShowPrivacyKey(req *types.ReqString) (types.Mess
return reply, err
}
func (policy *privacyPolicy) On_Public2Privacy(req *privacytypes.ReqPub2Pri) (types.Message, error) {
policy.getWalletOperate().GetMutex().Lock()
defer policy.getWalletOperate().GetMutex().Unlock()
reply, err := policy.sendPublic2PrivacyTransaction(req)
if err != nil {
bizlog.Error("sendPublic2PrivacyTransaction", "err", err.Error())
}
return reply, err
}
func (policy *privacyPolicy) On_Privacy2Privacy(req *privacytypes.ReqPri2Pri) (types.Message, error) {
policy.getWalletOperate().GetMutex().Lock()
defer policy.getWalletOperate().GetMutex().Unlock()
reply, err := policy.sendPrivacy2PrivacyTransaction(req)
if err != nil {
bizlog.Error("sendPrivacy2PrivacyTransaction", "err", err.Error())
}
return reply, err
}
func (policy *privacyPolicy) On_Privacy2Public(req *privacytypes.ReqPri2Pub) (types.Message, error) {
policy.getWalletOperate().GetMutex().Lock()
defer policy.getWalletOperate().GetMutex().Unlock()
reply, err := policy.sendPrivacy2PublicTransaction(req)
if err != nil {
bizlog.Error("sendPrivacy2PublicTransaction", "err", err.Error())
}
return reply, err
}
func (policy *privacyPolicy) On_CreateUTXOs(req *privacytypes.ReqCreateUTXOs) (types.Message, error) {
policy.getWalletOperate().GetMutex().Lock()
defer policy.getWalletOperate().GetMutex().Unlock()
......
......@@ -171,7 +171,7 @@ func (policy *privacyPolicy) createUTXOsByPub2Priv(priv crypto.PrivKey, reqCreat
}
tx := &types.Transaction{
Execer: []byte("privacy"),
Execer: []byte(types.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Nonce: operater.Nonce(),
To: address.ExecAddress(privacytypes.PrivacyX),
......@@ -601,10 +601,10 @@ func (policy *privacyPolicy) createPublic2PrivacyTx(req *types.ReqCreateTransact
Value: &privacytypes.PrivacyAction_Public2Privacy{Public2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Execer: []byte(types.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(privacytypes.PrivacyX),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
......@@ -619,10 +619,16 @@ func (policy *privacyPolicy) createPublic2PrivacyTx(req *types.ReqCreateTransact
}
func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransaction) (*types.Transaction, error) {
//需要燃烧的utxo
utxoBurnedAmount := privacytypes.PrivacyTxFee
if types.IsPara() {
utxoBurnedAmount = 0
}
buildInfo := &buildInputInfo{
tokenname: req.GetTokenname(),
sender: req.GetFrom(),
amount: req.GetAmount() + privacytypes.PrivacyTxFee,
amount: req.GetAmount() + utxoBurnedAmount,
mixcount: req.GetMixcount(),
}
privacyInfo, err := policy.getPrivacykeyPair(req.GetFrom())
......@@ -653,7 +659,7 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
selectedAmounTotal += input.Amount
}
//构造输出UTXO
privacyOutput, err := generateOuts(viewPublic, spendPublic, viewPub4chgPtr, spendPub4chgPtr, req.GetAmount(), selectedAmounTotal, privacytypes.PrivacyTxFee)
privacyOutput, err := generateOuts(viewPublic, spendPublic, viewPub4chgPtr, spendPub4chgPtr, req.GetAmount(), selectedAmounTotal, utxoBurnedAmount)
if err != nil {
return nil, err
}
......@@ -671,11 +677,11 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Execer: []byte(types.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee,
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(privacytypes.PrivacyX),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
// 创建交易成功,将已经使用掉的UTXO冻结
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
......@@ -690,10 +696,16 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
}
func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransaction) (*types.Transaction, error) {
//需要燃烧的utxo
utxoBurnedAmount := privacytypes.PrivacyTxFee
if types.IsPara() {
utxoBurnedAmount = 0
}
buildInfo := &buildInputInfo{
tokenname: req.GetTokenname(),
sender: req.GetFrom(),
amount: req.GetAmount() + privacytypes.PrivacyTxFee,
amount: req.GetAmount() + utxoBurnedAmount,
mixcount: req.GetMixcount(),
}
privacyInfo, err := policy.getPrivacykeyPair(req.GetFrom())
......@@ -722,7 +734,7 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
changeAmount := selectedAmounTotal - req.GetAmount()
//step 2,generateOuts
//构造输出UTXO,只生成找零的UTXO
privacyOutput, err := generateOuts(nil, nil, viewPub4chgPtr, spendPub4chgPtr, 0, changeAmount, privacytypes.PrivacyTxFee)
privacyOutput, err := generateOuts(nil, nil, viewPub4chgPtr, spendPub4chgPtr, 0, changeAmount, utxoBurnedAmount)
if err != nil {
return nil, err
}
......@@ -733,6 +745,7 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
Note: req.GetNote(),
Input: privacyInput,
Output: privacyOutput,
To: req.GetTo(),
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPrivacy2Public,
......@@ -740,11 +753,11 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Execer: []byte(types.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee,
Nonce: policy.getWalletOperate().Nonce(),
To: req.GetTo(),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
// 创建交易成功,将已经使用掉的UTXO冻结
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
......@@ -959,188 +972,6 @@ func (policy *privacyPolicy) showPrivacyAccountsSpend(req *privacytypes.ReqPrivB
return utxoHaveTxHashs, nil
}
func (policy *privacyPolicy) sendPublic2PrivacyTransaction(public2private *privacytypes.ReqPub2Pri) (*types.Reply, error) {
if ok, err := policy.getWalletOperate().CheckWalletStatus(); !ok {
bizlog.Error("sendPublic2PrivacyTransaction", "CheckWalletStatus error", err)
return nil, err
}
if ok, err := policy.isRescanUtxosFlagScaning(); ok {
bizlog.Error("sendPublic2PrivacyTransaction", "isRescanUtxosFlagScaning error", err)
return nil, err
}
if public2private == nil {
bizlog.Error("sendPublic2PrivacyTransaction public2private is nil")
return nil, types.ErrInvalidParam
}
if len(public2private.GetTokenname()) <= 0 {
bizlog.Error("sendPublic2PrivacyTransaction tokenname is nil")
return nil, types.ErrInvalidParam
}
if !checkAmountValid(public2private.GetAmount()) {
bizlog.Error("sendPublic2PrivacyTransaction", "invalid amount", public2private.GetAmount())
return nil, types.ErrAmount
}
priv, err := policy.getPrivKeyByAddr(public2private.GetSender())
if err != nil {
bizlog.Error("sendPublic2PrivacyTransaction", "getPrivKeyByAddr error", err)
return nil, err
}
return policy.transPub2PriV2(priv, public2private)
}
//公开向隐私账户转账
func (policy *privacyPolicy) transPub2PriV2(priv crypto.PrivKey, reqPub2Pri *privacytypes.ReqPub2Pri) (*types.Reply, error) {
viewPubSlice, spendPubSlice, err := parseViewSpendPubKeyPair(reqPub2Pri.Pubkeypair)
if err != nil {
bizlog.Error("transPub2Pri", "parseViewSpendPubKeyPair error", err)
return nil, err
}
viewPublic := (*[32]byte)(unsafe.Pointer(&viewPubSlice[0]))
spendPublic := (*[32]byte)(unsafe.Pointer(&spendPubSlice[0]))
//因为此时是pub2priv的交易,此时不需要构造找零的输出,同时设置fee为0,也是为了简化计算
privacyOutput, err := generateOuts(viewPublic, spendPublic, nil, nil, reqPub2Pri.Amount, reqPub2Pri.Amount, 0)
if err != nil {
bizlog.Error("transPub2Pri", "generateOuts error", err)
return nil, err
}
operater := policy.getWalletOperate()
value := &privacytypes.Public2Privacy{
Tokenname: reqPub2Pri.Tokenname,
Amount: reqPub2Pri.Amount,
Note: reqPub2Pri.Note,
Output: privacyOutput,
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPublic2Privacy,
Value: &privacytypes.PrivacyAction_Public2Privacy{Public2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte("privacy"),
Payload: types.Encode(action),
Nonce: operater.Nonce(),
// TODO: 采用隐私合约地址来设定目标合约接收的目标地址,让验证通过
To: address.ExecAddress(privacytypes.PrivacyX),
}
tx.SetExpire(time.Duration(reqPub2Pri.GetExpire()))
txSize := types.Size(tx) + types.SignatureSize
realFee := int64((txSize+1023)>>types.Size1Kshiftlen) * types.GInt("MinFee")
tx.Fee = realFee
tx.Sign(int32(operater.GetSignType()), priv)
reply, err := operater.GetAPI().SendTx(tx)
if err != nil {
bizlog.Error("transPub2PriV2", "Send err", err)
return nil, err
}
return reply, err
}
func (policy *privacyPolicy) sendPrivacy2PrivacyTransaction(privacy2privacy *privacytypes.ReqPri2Pri) (*types.Reply, error) {
if ok, err := policy.getWalletOperate().CheckWalletStatus(); !ok {
bizlog.Error("sendPrivacy2PrivacyTransaction", "CheckWalletStatus error", err)
return nil, err
}
if ok, err := policy.isRescanUtxosFlagScaning(); ok {
bizlog.Error("sendPrivacy2PrivacyTransaction", "isRescanUtxosFlagScaning error", err)
return nil, err
}
if privacy2privacy == nil {
bizlog.Error("sendPrivacy2PrivacyTransaction input para is nil")
return nil, types.ErrInvalidParam
}
if !checkAmountValid(privacy2privacy.GetAmount()) {
bizlog.Error("sendPrivacy2PrivacyTransaction", "invalid amount ", privacy2privacy.GetAmount())
return nil, types.ErrAmount
}
privacyInfo, err := policy.getPrivacykeyPair(privacy2privacy.GetSender())
if err != nil {
bizlog.Error("sendPrivacy2PrivacyTransaction", "getPrivacykeyPair error ", err)
return nil, err
}
return policy.transPri2PriV2(privacyInfo, privacy2privacy)
}
func (policy *privacyPolicy) transPri2PriV2(privacykeyParirs *privacy.Privacy, reqPri2Pri *privacytypes.ReqPri2Pri) (*types.Reply, error) {
buildInfo := &buildInputInfo{
tokenname: reqPri2Pri.Tokenname,
sender: reqPri2Pri.Sender,
amount: reqPri2Pri.Amount + privacytypes.PrivacyTxFee,
mixcount: reqPri2Pri.Mixin,
}
//step 1,buildInput
privacyInput, utxosInKeyInput, realkeyInputSlice, selectedUtxo, err := policy.buildInput(privacykeyParirs, buildInfo)
if err != nil {
bizlog.Error("transPri2PriV2", "buildInput error", err)
return nil, err
}
//step 2,generateOuts
viewPublicSlice, spendPublicSlice, err := parseViewSpendPubKeyPair(reqPri2Pri.Pubkeypair)
if err != nil {
bizlog.Error("transPub2Pri", "parseViewSpendPubKeyPair ", err)
return nil, err
}
viewPub4change, spendPub4change := privacykeyParirs.ViewPubkey.Bytes(), privacykeyParirs.SpendPubkey.Bytes()
viewPublic := (*[32]byte)(unsafe.Pointer(&viewPublicSlice[0]))
spendPublic := (*[32]byte)(unsafe.Pointer(&spendPublicSlice[0]))
viewPub4chgPtr := (*[32]byte)(unsafe.Pointer(&viewPub4change[0]))
spendPub4chgPtr := (*[32]byte)(unsafe.Pointer(&spendPub4change[0]))
selectedAmounTotal := int64(0)
for _, input := range privacyInput.Keyinput {
selectedAmounTotal += input.Amount
}
//构造输出UTXO
privacyOutput, err := generateOuts(viewPublic, spendPublic, viewPub4chgPtr, spendPub4chgPtr, reqPri2Pri.Amount, selectedAmounTotal, privacytypes.PrivacyTxFee)
if err != nil {
bizlog.Error("transPub2Pri", "generateOuts ", err)
return nil, err
}
operater := policy.getWalletOperate()
value := &privacytypes.Privacy2Privacy{
Tokenname: reqPri2Pri.Tokenname,
Amount: reqPri2Pri.Amount,
Note: reqPri2Pri.Note,
Input: privacyInput,
Output: privacyOutput,
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPrivacy2Privacy,
Value: &privacytypes.PrivacyAction_Privacy2Privacy{Privacy2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee,
Nonce: operater.Nonce(),
// TODO: 采用隐私合约地址来设定目标合约接收的目标地址,让验证通过
To: address.ExecAddress(privacytypes.PrivacyX),
}
tx.SetExpire(time.Duration(reqPri2Pri.GetExpire()))
//完成了input和output的添加之后,即已经完成了交易基本内容的添加,
//这时候就需要进行交易的签名了
err = policy.signatureTx(tx, privacyInput, utxosInKeyInput, realkeyInputSlice)
if err != nil {
return nil, err
}
reply, err := operater.GetAPI().SendTx(tx)
if err != nil {
bizlog.Error("transPub2Pri", "SendTx ", err)
return nil, err
}
policy.saveFTXOInfo(tx, reqPri2Pri.Tokenname, reqPri2Pri.Sender, hex.EncodeToString(tx.Hash()), selectedUtxo)
return reply, nil
}
func (policy *privacyPolicy) signatureTx(tx *types.Transaction, privacyInput *privacytypes.PrivacyInput, utxosInKeyInput []*privacytypes.UTXOBasics, realkeyInputSlice []*privacytypes.RealKeyInput) (err error) {
tx.Signature = nil
data := types.Encode(tx)
......@@ -1165,110 +996,11 @@ func (policy *privacyPolicy) signatureTx(tx *types.Transaction, privacyInput *pr
Ty: privacytypes.RingBaseonED25519,
Signature: ringSignData,
// 这里填的是隐私合约的公钥,让框架保持一致
Pubkey: address.ExecPubKey(privacytypes.PrivacyX),
Pubkey: address.ExecPubKey(types.ExecName(privacytypes.PrivacyX)),
}
return nil
}
func (policy *privacyPolicy) sendPrivacy2PublicTransaction(privacy2Pub *privacytypes.ReqPri2Pub) (*types.Reply, error) {
if ok, err := policy.getWalletOperate().CheckWalletStatus(); !ok {
bizlog.Error("sendPrivacy2PublicTransaction", "CheckWalletStatus error", err)
return nil, err
}
if ok, err := policy.isRescanUtxosFlagScaning(); ok {
bizlog.Error("sendPrivacy2PublicTransaction", "isRescanUtxosFlagScaning error", err)
return nil, err
}
if privacy2Pub == nil {
bizlog.Error("privacy2privacy input para is nil")
return nil, types.ErrInvalidParam
}
if !checkAmountValid(privacy2Pub.GetAmount()) {
return nil, types.ErrAmount
}
//get 'a'
privacyInfo, err := policy.getPrivacykeyPair(privacy2Pub.GetSender())
if err != nil {
bizlog.Error("sendPrivacy2PublicTransaction", "getPrivacykeyPair error", err)
return nil, err
}
return policy.transPri2PubV2(privacyInfo, privacy2Pub)
}
func (policy *privacyPolicy) transPri2PubV2(privacykeyParirs *privacy.Privacy, reqPri2Pub *privacytypes.ReqPri2Pub) (*types.Reply, error) {
buildInfo := &buildInputInfo{
tokenname: reqPri2Pub.Tokenname,
sender: reqPri2Pub.Sender,
amount: reqPri2Pub.Amount + privacytypes.PrivacyTxFee,
mixcount: reqPri2Pub.Mixin,
}
//step 1,buildInput
privacyInput, utxosInKeyInput, realkeyInputSlice, selectedUtxo, err := policy.buildInput(privacykeyParirs, buildInfo)
if err != nil {
bizlog.Error("transPri2PubV2", "buildInput error", err)
return nil, err
}
viewPub4change, spendPub4change := privacykeyParirs.ViewPubkey.Bytes(), privacykeyParirs.SpendPubkey.Bytes()
viewPub4chgPtr := (*[32]byte)(unsafe.Pointer(&viewPub4change[0]))
spendPub4chgPtr := (*[32]byte)(unsafe.Pointer(&spendPub4change[0]))
selectedAmounTotal := int64(0)
for _, input := range privacyInput.Keyinput {
if input.Amount <= 0 {
return nil, types.ErrAmount
}
selectedAmounTotal += input.Amount
}
changeAmount := selectedAmounTotal - reqPri2Pub.Amount
//step 2,generateOuts
//构造输出UTXO,只生成找零的UTXO
privacyOutput, err := generateOuts(nil, nil, viewPub4chgPtr, spendPub4chgPtr, 0, changeAmount, privacytypes.PrivacyTxFee)
if err != nil {
bizlog.Error("transPri2PubV2", "generateOuts error", err)
return nil, err
}
operater := policy.getWalletOperate()
value := &privacytypes.Privacy2Public{
Tokenname: reqPri2Pub.Tokenname,
Amount: reqPri2Pub.Amount,
Note: reqPri2Pub.Note,
Input: privacyInput,
Output: privacyOutput,
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPrivacy2Public,
Value: &privacytypes.PrivacyAction_Privacy2Public{Privacy2Public: value},
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee,
Nonce: operater.Nonce(),
To: reqPri2Pub.Receiver,
}
tx.SetExpire(time.Duration(reqPri2Pub.GetExpire()))
//step 3,generate ring signature
err = policy.signatureTx(tx, privacyInput, utxosInKeyInput, realkeyInputSlice)
if err != nil {
bizlog.Error("transPri2PubV2", "signatureTx error", err)
return nil, err
}
reply, err := operater.GetAPI().SendTx(tx)
if err != nil {
bizlog.Error("transPri2PubV2", "SendTx error", err)
return nil, err
}
txhashstr := hex.EncodeToString(tx.Hash())
policy.saveFTXOInfo(tx, reqPri2Pub.Tokenname, reqPri2Pub.Sender, txhashstr, selectedUtxo)
bizlog.Info("transPri2PubV2", "txhash", txhashstr)
return reply, nil
}
func (policy *privacyPolicy) buildAndStoreWalletTxDetail(param *buildStoreWalletTxDetailParam) {
blockheight := param.block.Block.Height*maxTxNumPerBlock + int64(param.index)
heightstr := fmt.Sprintf("%018d", blockheight)
......
......@@ -360,117 +360,6 @@ func Test_CreateUTXOs(t *testing.T) {
}
}
func Test_SendPublic2PrivacyTransaction(t *testing.T) {
mock := &testDataMock{mockMempool: true}
mock.init()
mock.enablePrivacy()
testCases := []struct {
req *ty.ReqPub2Pri
needReply *types.Reply
needError error
}{
{
needError: types.ErrInvalidParam,
},
{
req: &ty.ReqPub2Pri{
Tokenname: types.BTY,
Amount: 10 * types.Coin,
Sender: testAddrs[0],
Pubkeypair: testPubkeyPairs[0],
},
needReply: &types.Reply{IsOk: true},
needError: types.ErrAddrNotExist,
},
}
for index, testCase := range testCases {
_, getErr := mock.wallet.GetAPI().ExecWalletFunc(ty.PrivacyX, "Public2Privacy", testCase.req)
require.Equalf(t, getErr, testCase.needError, "Publick2Privacy test case index %d", index)
}
}
func Test_SendPrivacy2PrivacyTransaction(t *testing.T) {
mock := &testDataMock{
mockMempool: true,
mockBlockChain: true,
}
mock.init()
mock.enablePrivacy()
// 创建辅助对象
privacyMock := privacy.PrivacyMock{}
privacyMock.Init(mock.wallet, mock.password)
// 创建几条可用UTXO
privacyMock.CreateUTXOs(testAddrs[0], testPubkeyPairs[0], 17*types.Coin, 10000, 5)
mock.setBlockChainHeight(10020)
testCases := []struct {
req *ty.ReqPri2Pri
needReply *types.Reply
needError error
}{
{
needError: types.ErrInvalidParam,
},
{
req: &ty.ReqPri2Pri{
Tokenname: types.BTY,
Amount: 10 * types.Coin,
Sender: testAddrs[0],
Pubkeypair: testPubkeyPairs[1],
},
needReply: &types.Reply{IsOk: true},
needError: types.ErrAddrNotExist,
},
}
for index, testCase := range testCases {
_, getErr := mock.wallet.GetAPI().ExecWalletFunc(ty.PrivacyX, "Privacy2Privacy", testCase.req)
require.Equalf(t, getErr, testCase.needError, "Privacy2Privacy test case index %d", index)
}
}
func Test_SendPrivacy2PublicTransaction(t *testing.T) {
mock := &testDataMock{
mockMempool: true,
mockBlockChain: true,
}
mock.init()
mock.enablePrivacy()
// 创建辅助对象
privacyMock := privacy.PrivacyMock{}
privacyMock.Init(mock.wallet, mock.password)
// 创建几条可用UTXO
privacyMock.CreateUTXOs(testAddrs[0], testPubkeyPairs[0], 17*types.Coin, 10000, 5)
mock.setBlockChainHeight(10020)
testCases := []struct {
req *ty.ReqPri2Pub
needReply *types.Reply
needError error
}{
{
needError: types.ErrInvalidParam,
},
{
req: &ty.ReqPri2Pub{
Tokenname: types.BTY,
Amount: 10 * types.Coin,
Sender: testAddrs[0],
Receiver: testAddrs[0],
},
needReply: &types.Reply{IsOk: true},
needError: types.ErrAddrNotExist,
},
}
for index, testCase := range testCases {
_, getErr := mock.wallet.GetAPI().ExecWalletFunc(ty.PrivacyX, "Privacy2Public", testCase.req)
require.Equalf(t, getErr, testCase.needError, "Privacy2Public test case index %d", index)
}
}
func Test_CreateTransaction(t *testing.T) {
mock := &testDataMock{
mockMempool: true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment