Commit e89c3116 authored by 袁邓@五瓣科技's avatar 袁邓@五瓣科技

根据接口信息做相应修改

parent 938d11dd
......@@ -5,6 +5,7 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
"fmt"
"encoding/json"
"strings"
)
const KEY = "COUPONS-"
......@@ -59,18 +60,22 @@ func getCouponsApi(args []string, stub shim.ChaincodeStubInterface) pb.Response
return shim.Success(coupByte)
}
// 入参:utxo地址,证书、交易签名
// 入参:utxo地址,证书、交易签名.商家查看,只能查看已领取的UTXO
func getCouponsByAddressApi(args []string, stub shim.ChaincodeStubInterface) pb.Response {
// 解析请求数据
trans,_,_,err := messageToTrans(CreateCoupon,args,stub)
trans,_,_,err := messageToTrans(UseCoupon,args,stub)
if err != nil{
return shim.Error(err.Error())
}
//查询utxo信息
utxoByte,err :=getStateByte(KEY+trans.From,stub)
utxo,err :=getStateUtxo(KEY+trans.From,stub)
if err != nil{
return shim.Error(err.Error())
}
if strings.Compare(utxo.Status,"2") != 0{
return shim.Error("Query for exceptions, UTXO status exceptions")
}
utxoByte,_ :=json.Marshal(utxo)
return shim.Success(utxoByte)
}
......
......@@ -309,4 +309,17 @@ func TestCouponsFlow(t *testing.T){
responseByPutschema = stub.MockInvoke("invoke1",[][]byte{[]byte("createTx/useCoupon"),[]byte("111"),[]byte(useCouponsDate),[]byte(useCouponsSign)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
//fmt.Println("========================================query getCoupons============================================")
//fmt.Println()
//fmt.Println()
//responseByPutschema = stub.MockInvoke("invoke1",[][]byte{[]byte("getCoupons"),[]byte("111"),[]byte(useCouponsDate),[]byte(useCouponsSign)})
//fmt.Printf("Invoke status %d,message %s and payload %s\n",responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
//
//fmt.Println("========================================query getCouponsByAddress============================================")
//fmt.Println()
//fmt.Println()
//responseByPutschema = stub.MockInvoke("invoke1",[][]byte{[]byte("getCouponsByAddress"),[]byte("111"),[]byte(useCouponsDate),[]byte(useCouponsSign)})
//fmt.Printf("Invoke status %d,message %s and payload %s\n",responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
}
......@@ -7,7 +7,6 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"time"
)
//券结构
type Coupons struct {
CoupId string `json:"coup_id"` //券id
......@@ -19,7 +18,6 @@ type Coupons struct {
CoupQuantity int `json:"coup_quantity"` //发行数量
CoupAmount float64 `json:"coup_amount"` //面额
CoupDiscount float64 `json:"coup_discount"` //折扣
CoupTotalAmount float64 `json:"coup_total_amount"` //券总金额
FloorAmount float64 `json:"floor_amount"` //券使用门槛,最低消费金额
UseMerchats []string `json:"use_merchats"` //当前券支持的商户Id
UseMerchantsType *MerchantsType `json:"use_merchants_type"`
......@@ -38,7 +36,6 @@ type CoupAvailable struct {
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
}
//UTXO结构
type Utxo struct {
Address string `json:"address"` //Utxo记录地址
......@@ -65,8 +62,8 @@ type Order struct {
type UserInfo struct {
Uid string `json:"uid"` //用户id
Name string `json:"name"` //用户名
Number string `json:"number"` //身份证号
Phone string `json:"phone"` //手机号
IdNumber string `json:"id_number"` //身份证号
PhoneNumber string `json:"phone_number"` //手机号
CreateTime time.Time `json:"create_time"` //创建时间
}
......
......@@ -6,7 +6,6 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
"encoding/json"
"strings"
"encoding/hex"
)
func transactionProcess(paths, args []string, stub shim.ChaincodeStubInterface) pb.Response {
......@@ -255,15 +254,21 @@ func useCouponApi(args []string, stub shim.ChaincodeStubInterface) pb.Response {
if err != nil{
fmt.Println(err)
}
privHex, ok := dat["privHex"].(string)
//拿到订单信息
order, ok := dat["order"].(string)
//订单签名信息
orderSign, ok := dat["orderSign"].(string)
if !ok {
return shim.Error("Error privHex parameter type,must be string")
}
_,pubKey,err := ParsePrivateKey(privHex)
pubHex := hex.EncodeToString(pubKey)
if utxo.PublicKey != pubHex {
return shim.Error("coupons verify error")
}
return shim.Error("Error orderSign parameter type,must be string")
}
//TODO 验证订单签名信息
fmt.Println(order)
fmt.Println(orderSign)
//_,pubKey,err := ParsePrivateKey(privHex)
//pubHex := hex.EncodeToString(pubKey)
//if utxo.PublicKey != pubHex {
// return shim.Error("coupons verify error")
//}
utxo.Status = "3"
var mertNewUtxo Utxo
mertByteUtxo, _ := stub.GetState(KEY+trans.To)
......
This diff is collapsed.
......@@ -28,9 +28,7 @@ import (
"errors"
"io"
"math/big"
"github.com/TMChain/go-TMChain_test/crypto/crypto_interface"
"github.com/chaincodecert/sm_crypto/sm3"
"github.com/coupons/sm_crypto/sm3"
)
const (
......@@ -52,13 +50,13 @@ func (priv *PrivateKey) Public() crypto.PublicKey {
return &priv.PublicKey
}
// sign format = 30 + len(z) + 02 + len(r) + r + 02 + len(s) + s, z being what follows its size, ie 02+len(r)+r+02+len(s)+s
/// sign format = 30 + len(z) + 02 + len(r) + r + 02 + len(s) + s, z being what follows its size, ie 02+len(r)+r+02+len(s)+s
func (priv *PrivateKey) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
r, s, err := Sign(priv, msg)
if err != nil {
return nil, err
}
sig := icrypto.FormatSm2SigTo64(r, s)
sig := FormatSm2SigTo64(r, s)
return sig, nil
}
......@@ -67,7 +65,7 @@ func SM2Sign(priv *PrivateKey, hash []byte) ([]byte, error) {
if err != nil {
return nil, err
}
sig := icrypto.FormatSm2SigTo64(r, s)
sig := FormatSm2SigTo64(r, s)
return sig, nil
}
......@@ -445,3 +443,32 @@ func (z *zr) Read(dst []byte) (n int, err error) {
}
var zeroReader = &zr{}
func FormatSm2SigTo64(r, s *big.Int) []byte {
R := r.Bytes()
S := s.Bytes()
var zeroInsertR []byte
for i := 0; i < 32-len(R); i++ {
zeroInsertR = append(zeroInsertR, byte(0))
}
R = insertByteSliceCopy(R, zeroInsertR, 0)
var zeroInsertS []byte
for i := 0; i < 32-len(S); i++ {
zeroInsertS = append(zeroInsertS, byte(0))
}
S = insertByteSliceCopy(S, zeroInsertS, 0)
sig := make([]byte, 64)
copy(sig[:32], R[:])
copy(sig[32:], S[:])
return sig
}
func insertByteSliceCopy(slice, insertion []byte, index int) []byte {
result := make([]byte, len(slice)+len(insertion))
at := copy(result, slice[:index])
at += copy(result[at:], insertion)
copy(result[at:], slice[index:])
return result
}
\ No newline at end of file
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