作者 yangfu

1. 结构微调

... ... @@ -15,10 +15,11 @@ import (
type (
BSNBlockChain struct {
PublicPem []byte
PrivatePem []byte
PublicKey string
Host string
PublicPem []byte
PrivatePem []byte
PublicKey string
Host string
EnableDebugLog bool
}
UpToChainRequest struct {
// 上链数据的数据库、数据表等的标识值 (非必填)
... ... @@ -47,7 +48,7 @@ type (
// type为1或者3时必填
TsTxId string `json:"tsTxId,omitempty"`
// type为2时必填
InnerPrimaryKey string `json:"innerPrimaryKey,omitempty"`
IssueId string `json:"issueId,omitempty"`
// type为3时必填
Value string `json:"value,omitempty"`
// 当type=1或者2必填,为false只显示密文,为true溯源才会显示原文
... ... @@ -70,8 +71,7 @@ func (c *BSNBlockChain) UpToChain(options *UpToChainOptions) (*UpToChainResponse
if err != nil {
return nil, err
}
data, _ := httputil.DumpRequest(req.GetRequest(), true)
fmt.Println(string(data))
var upToChainResponse UpToChainResponse
_, err = c.HandlerResponse(req, &upToChainResponse)
... ... @@ -84,8 +84,6 @@ func (c *BSNBlockChain) GetToken(options *GetTokenRequest) (*GetTokenResponse, e
if err != nil {
return nil, err
}
data, _ := httputil.DumpRequest(req.GetRequest(), true)
fmt.Println(string(data))
var getTokenResponse = GetTokenResponse{}
_, err = c.HandlerResponse(req, &getTokenResponse)
return &getTokenResponse, err
... ... @@ -130,12 +128,15 @@ func (c *BSNBlockChain) MakeRequest(obj interface{}, action string, signAction,
if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
req.JSONBody(obj)
}
if c.EnableDebugLog {
data, _ := httputil.DumpRequest(req.GetRequest(), true)
fmt.Println(string(data))
}
return req, nil
}
func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value interface{}) (*Response, error) {
response := &Response{}
//req.DumpBody(true)
data, err := req.Bytes()
if err != nil {
return nil, err
... ... @@ -151,6 +152,9 @@ func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value int
if err != nil {
return nil, err
}
if c.EnableDebugLog {
fmt.Println("\nHttp Response-> \n", string(data))
}
if response.Code != 0 {
return nil, fmt.Errorf("upchain code:%v msg:%v", response.Code, response.Message)
}
... ...
... ... @@ -52,7 +52,6 @@ var host = "http://101.34.29.149:9092/test"
func TestSignature(t *testing.T) {
options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc")
//options := NewUpToChainOptions("table", "", "").WithDesc("")
bsn := &BSNBlockChain{
PrivatePem: priK,
PublicPem: []byte(pubPem),
... ... @@ -138,10 +137,11 @@ func TestBSNBlockChain_UpToChain(t *testing.T) {
func TestBSNBlockChain_GetToken(t *testing.T) {
bc := &BSNBlockChain{
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
PrivatePem: priK,
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
PrivatePem: priK,
EnableDebugLog: true,
}
options := &GetTokenRequest{
Type: 1,
... ... @@ -152,5 +152,63 @@ func TestBSNBlockChain_GetToken(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Log(token.Token)
fmt.Println(token.Token)
}
func TestBSNBlockChain_UpToChain_All_Type(t *testing.T) {
bc := &BSNBlockChain{
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
PrivatePem: priK,
EnableDebugLog: true,
}
inputs := []struct {
name string
option *UpToChainOptions
t int
}{
{
"1.交易哈希溯源",
NewUpToChainOptions("app.order", "793745u988434", `{"orderId":"793745u988434"}`).WithDesc(""),
1,
},
{
"2.交易哈希溯源",
NewUpToChainOptions("app.order", "793745u988435", `{"orderId":"793745u988435"}`).WithDesc("").WithInnerPrimaryIssueId("893745u988435"),
2,
},
{
"3.验真",
NewUpToChainOptions("app.order", "793745u988436", `{"orderId":"793745u988436"}`).WithDesc("").WithInnerPrimaryIssueId("893745u988436"),
3,
},
}
for i := range inputs {
input := inputs[i]
fmt.Println(input.name)
rsp, err := bc.UpToChain(input.option)
if err != nil {
t.Fatal(err)
}
fmt.Println()
options := &GetTokenRequest{
Type: input.t,
ShowValue: true,
}
switch input.t {
case 1:
options.TsTxId = string(*rsp)
case 2:
options.IssueId = input.option.InnerPrimaryIssueId
case 3:
options.TsTxId = string(*rsp)
options.Value = input.option.Value
}
token, err := bc.GetToken(options)
if err != nil {
t.Fatal(err)
}
fmt.Println(fmt.Sprintf("Token:%v \n", token.Token))
}
}
... ...
... ... @@ -23,18 +23,16 @@ func RsaSign(publicKey []byte, origData []byte) ([]byte, error) {
return nil, err
}
fmt.Println(string(origData))
// md5
hash := md5.New()
hash.Write([]byte(origData))
pub := pubInterface.(*rsa.PrivateKey)
fmt.Println(hash.Sum(nil))
return rsa.SignPKCS1v15(rand.Reader, pub, crypto.MD5, hash.Sum(nil))
//pub := pubInterface.(*rsa.PublicKey)
//return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
func RsaEncryptBak(publicKey []byte, origData []byte) ([]byte, error) {
func RsaEncrypt(publicKey []byte, origData []byte) ([]byte, error) {
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
... ... @@ -51,8 +49,6 @@ func RsaEncryptBak(publicKey []byte, origData []byte) ([]byte, error) {
pub := pubInterface.(*rsa.PublicKey)
fmt.Println(hash.Sum(nil))
return rsa.EncryptPKCS1v15(rand.Reader, pub, hash.Sum(nil))
//pub := pubInterface.(*rsa.PublicKey)
//return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
// 解密
... ...