作者 yangfu

1. 结构微调

@@ -15,10 +15,11 @@ import ( @@ -15,10 +15,11 @@ import (
15 15
16 type ( 16 type (
17 BSNBlockChain struct { 17 BSNBlockChain struct {
18 - PublicPem []byte  
19 - PrivatePem []byte  
20 - PublicKey string  
21 - Host string 18 + PublicPem []byte
  19 + PrivatePem []byte
  20 + PublicKey string
  21 + Host string
  22 + EnableDebugLog bool
22 } 23 }
23 UpToChainRequest struct { 24 UpToChainRequest struct {
24 // 上链数据的数据库、数据表等的标识值 (非必填) 25 // 上链数据的数据库、数据表等的标识值 (非必填)
@@ -47,7 +48,7 @@ type ( @@ -47,7 +48,7 @@ type (
47 // type为1或者3时必填 48 // type为1或者3时必填
48 TsTxId string `json:"tsTxId,omitempty"` 49 TsTxId string `json:"tsTxId,omitempty"`
49 // type为2时必填 50 // type为2时必填
50 - InnerPrimaryKey string `json:"innerPrimaryKey,omitempty"` 51 + IssueId string `json:"issueId,omitempty"`
51 // type为3时必填 52 // type为3时必填
52 Value string `json:"value,omitempty"` 53 Value string `json:"value,omitempty"`
53 // 当type=1或者2必填,为false只显示密文,为true溯源才会显示原文 54 // 当type=1或者2必填,为false只显示密文,为true溯源才会显示原文
@@ -70,8 +71,7 @@ func (c *BSNBlockChain) UpToChain(options *UpToChainOptions) (*UpToChainResponse @@ -70,8 +71,7 @@ func (c *BSNBlockChain) UpToChain(options *UpToChainOptions) (*UpToChainResponse
70 if err != nil { 71 if err != nil {
71 return nil, err 72 return nil, err
72 } 73 }
73 - data, _ := httputil.DumpRequest(req.GetRequest(), true)  
74 - fmt.Println(string(data)) 74 +
75 var upToChainResponse UpToChainResponse 75 var upToChainResponse UpToChainResponse
76 _, err = c.HandlerResponse(req, &upToChainResponse) 76 _, err = c.HandlerResponse(req, &upToChainResponse)
77 77
@@ -84,8 +84,6 @@ func (c *BSNBlockChain) GetToken(options *GetTokenRequest) (*GetTokenResponse, e @@ -84,8 +84,6 @@ func (c *BSNBlockChain) GetToken(options *GetTokenRequest) (*GetTokenResponse, e
84 if err != nil { 84 if err != nil {
85 return nil, err 85 return nil, err
86 } 86 }
87 - data, _ := httputil.DumpRequest(req.GetRequest(), true)  
88 - fmt.Println(string(data))  
89 var getTokenResponse = GetTokenResponse{} 87 var getTokenResponse = GetTokenResponse{}
90 _, err = c.HandlerResponse(req, &getTokenResponse) 88 _, err = c.HandlerResponse(req, &getTokenResponse)
91 return &getTokenResponse, err 89 return &getTokenResponse, err
@@ -130,12 +128,15 @@ func (c *BSNBlockChain) MakeRequest(obj interface{}, action string, signAction, @@ -130,12 +128,15 @@ func (c *BSNBlockChain) MakeRequest(obj interface{}, action string, signAction,
130 if httpMethod == http.MethodPost || httpMethod == http.MethodPut { 128 if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
131 req.JSONBody(obj) 129 req.JSONBody(obj)
132 } 130 }
  131 + if c.EnableDebugLog {
  132 + data, _ := httputil.DumpRequest(req.GetRequest(), true)
  133 + fmt.Println(string(data))
  134 + }
133 return req, nil 135 return req, nil
134 } 136 }
135 137
136 func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value interface{}) (*Response, error) { 138 func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value interface{}) (*Response, error) {
137 response := &Response{} 139 response := &Response{}
138 - //req.DumpBody(true)  
139 data, err := req.Bytes() 140 data, err := req.Bytes()
140 if err != nil { 141 if err != nil {
141 return nil, err 142 return nil, err
@@ -151,6 +152,9 @@ func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value int @@ -151,6 +152,9 @@ func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value int
151 if err != nil { 152 if err != nil {
152 return nil, err 153 return nil, err
153 } 154 }
  155 + if c.EnableDebugLog {
  156 + fmt.Println("\nHttp Response-> \n", string(data))
  157 + }
154 if response.Code != 0 { 158 if response.Code != 0 {
155 return nil, fmt.Errorf("upchain code:%v msg:%v", response.Code, response.Message) 159 return nil, fmt.Errorf("upchain code:%v msg:%v", response.Code, response.Message)
156 } 160 }
@@ -52,7 +52,6 @@ var host = "http://101.34.29.149:9092/test" @@ -52,7 +52,6 @@ var host = "http://101.34.29.149:9092/test"
52 52
53 func TestSignature(t *testing.T) { 53 func TestSignature(t *testing.T) {
54 options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc") 54 options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc")
55 - //options := NewUpToChainOptions("table", "", "").WithDesc("")  
56 bsn := &BSNBlockChain{ 55 bsn := &BSNBlockChain{
57 PrivatePem: priK, 56 PrivatePem: priK,
58 PublicPem: []byte(pubPem), 57 PublicPem: []byte(pubPem),
@@ -138,10 +137,11 @@ func TestBSNBlockChain_UpToChain(t *testing.T) { @@ -138,10 +137,11 @@ func TestBSNBlockChain_UpToChain(t *testing.T) {
138 137
139 func TestBSNBlockChain_GetToken(t *testing.T) { 138 func TestBSNBlockChain_GetToken(t *testing.T) {
140 bc := &BSNBlockChain{ 139 bc := &BSNBlockChain{
141 - PublicPem: []byte(pubPem),  
142 - Host: host,  
143 - PublicKey: pubKey,  
144 - PrivatePem: priK, 140 + PublicPem: []byte(pubPem),
  141 + Host: host,
  142 + PublicKey: pubKey,
  143 + PrivatePem: priK,
  144 + EnableDebugLog: true,
145 } 145 }
146 options := &GetTokenRequest{ 146 options := &GetTokenRequest{
147 Type: 1, 147 Type: 1,
@@ -152,5 +152,63 @@ func TestBSNBlockChain_GetToken(t *testing.T) { @@ -152,5 +152,63 @@ func TestBSNBlockChain_GetToken(t *testing.T) {
152 if err != nil { 152 if err != nil {
153 t.Fatal(err) 153 t.Fatal(err)
154 } 154 }
155 - t.Log(token.Token) 155 + fmt.Println(token.Token)
  156 +}
  157 +
  158 +func TestBSNBlockChain_UpToChain_All_Type(t *testing.T) {
  159 + bc := &BSNBlockChain{
  160 + PublicPem: []byte(pubPem),
  161 + Host: host,
  162 + PublicKey: pubKey,
  163 + PrivatePem: priK,
  164 + EnableDebugLog: true,
  165 + }
  166 + inputs := []struct {
  167 + name string
  168 + option *UpToChainOptions
  169 + t int
  170 + }{
  171 + {
  172 + "1.交易哈希溯源",
  173 + NewUpToChainOptions("app.order", "793745u988434", `{"orderId":"793745u988434"}`).WithDesc(""),
  174 + 1,
  175 + },
  176 + {
  177 + "2.交易哈希溯源",
  178 + NewUpToChainOptions("app.order", "793745u988435", `{"orderId":"793745u988435"}`).WithDesc("").WithInnerPrimaryIssueId("893745u988435"),
  179 + 2,
  180 + },
  181 + {
  182 + "3.验真",
  183 + NewUpToChainOptions("app.order", "793745u988436", `{"orderId":"793745u988436"}`).WithDesc("").WithInnerPrimaryIssueId("893745u988436"),
  184 + 3,
  185 + },
  186 + }
  187 + for i := range inputs {
  188 + input := inputs[i]
  189 + fmt.Println(input.name)
  190 + rsp, err := bc.UpToChain(input.option)
  191 + if err != nil {
  192 + t.Fatal(err)
  193 + }
  194 + fmt.Println()
  195 + options := &GetTokenRequest{
  196 + Type: input.t,
  197 + ShowValue: true,
  198 + }
  199 + switch input.t {
  200 + case 1:
  201 + options.TsTxId = string(*rsp)
  202 + case 2:
  203 + options.IssueId = input.option.InnerPrimaryIssueId
  204 + case 3:
  205 + options.TsTxId = string(*rsp)
  206 + options.Value = input.option.Value
  207 + }
  208 + token, err := bc.GetToken(options)
  209 + if err != nil {
  210 + t.Fatal(err)
  211 + }
  212 + fmt.Println(fmt.Sprintf("Token:%v \n", token.Token))
  213 + }
156 } 214 }
@@ -23,18 +23,16 @@ func RsaSign(publicKey []byte, origData []byte) ([]byte, error) { @@ -23,18 +23,16 @@ func RsaSign(publicKey []byte, origData []byte) ([]byte, error) {
23 return nil, err 23 return nil, err
24 } 24 }
25 25
26 - fmt.Println(string(origData))  
27 // md5 26 // md5
28 hash := md5.New() 27 hash := md5.New()
29 hash.Write([]byte(origData)) 28 hash.Write([]byte(origData))
30 pub := pubInterface.(*rsa.PrivateKey) 29 pub := pubInterface.(*rsa.PrivateKey)
31 - fmt.Println(hash.Sum(nil))  
32 return rsa.SignPKCS1v15(rand.Reader, pub, crypto.MD5, hash.Sum(nil)) 30 return rsa.SignPKCS1v15(rand.Reader, pub, crypto.MD5, hash.Sum(nil))
33 //pub := pubInterface.(*rsa.PublicKey) 31 //pub := pubInterface.(*rsa.PublicKey)
34 //return rsa.EncryptPKCS1v15(rand.Reader, pub, origData) 32 //return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
35 } 33 }
36 34
37 -func RsaEncryptBak(publicKey []byte, origData []byte) ([]byte, error) { 35 +func RsaEncrypt(publicKey []byte, origData []byte) ([]byte, error) {
38 block, _ := pem.Decode(publicKey) 36 block, _ := pem.Decode(publicKey)
39 if block == nil { 37 if block == nil {
40 return nil, errors.New("public key error") 38 return nil, errors.New("public key error")
@@ -51,8 +49,6 @@ func RsaEncryptBak(publicKey []byte, origData []byte) ([]byte, error) { @@ -51,8 +49,6 @@ func RsaEncryptBak(publicKey []byte, origData []byte) ([]byte, error) {
51 pub := pubInterface.(*rsa.PublicKey) 49 pub := pubInterface.(*rsa.PublicKey)
52 fmt.Println(hash.Sum(nil)) 50 fmt.Println(hash.Sum(nil))
53 return rsa.EncryptPKCS1v15(rand.Reader, pub, hash.Sum(nil)) 51 return rsa.EncryptPKCS1v15(rand.Reader, pub, hash.Sum(nil))
54 - //pub := pubInterface.(*rsa.PublicKey)  
55 - //return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)  
56 } 52 }
57 53
58 // 解密 54 // 解密