bsn_test.go
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package blockchain
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"github.com/stretchr/testify/assert"
"log"
"os"
"testing"
)
var priK = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2H6x0D1mg5QbXfU7
MZKltypRj+eZktPKIApyEqRsyLqe3sRSd1Eh+VqKlQ9QFI8dae3t0USWlVmyfIDM
0ly85QIDAQABAkAPnKNJ9wOLfYSzs9l+66pTmROkovjqI6exw88SFRVbLCgM8maa
GOWEP/nhZDlQYBKHUqG0/KsLkeyLGkE8N7JBAiEA8lM3npA3q+Kmhy+lmQbfHFPQ
31OSkA+RaW/LPn0lP50CIQDktlF3iDk5kxnzgT/3lvvKhHInUh+pH5F19C6MymMD
6QIgLxDct655MahnAdDOUCeWhBD/e7DmwZZUfu8Ywb1a070CIArsUjO9Q85mIiUp
FR8EDP59GN6b43s2UMIraVW8DMKRAiEAnnMPbDsD2HsQbgmNNEqETUxYGVyO+p7w
OZZReuOyvCM=
-----END RSA PRIVATE KEY-----`)
var pubPem = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLT
yiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ==
-----END PUBLIC KEY-----`
var pubKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLTyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ=="
var host = "https://shangchain.fjmaimaimai.com/prod" //"http://101.34.29.149:9092/test"
func TestSignature(t *testing.T) {
options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc")
bsn := &BSNBlockChain{
PrivatePem: priK,
PublicPem: []byte(pubPem),
}
bInfo := &UpToChainRequest{}
bInfo.Complete(options)
var mapBlockInfo = make(map[string]interface{})
json.UnmarshalFromString(json.MarshalToString(bInfo), &mapBlockInfo)
secret, err := bsn.Signature(mapBlockInfo, "upToChain")
assert.Nil(t, err)
t.Log(secret)
decryptSecret, err := RsaDecrypt(priK, []byte(secret))
if err != nil {
t.Log(err.Error())
}
t.Log(decryptSecret)
}
func TestGenerateRSA(t *testing.T) {
// generate key
privatekey, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
fmt.Printf("Cannot generate RSA key\n")
os.Exit(1)
}
publickey := &privatekey.PublicKey
// dump private key to file
var privateKeyBytes []byte = x509.MarshalPKCS1PrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem := bytes.NewBuffer(nil)
if err != nil {
fmt.Printf("error when create private.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
fmt.Printf("error when encode private pem: %s \n", err)
os.Exit(1)
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
fmt.Printf("error when dumping publickey: %s \n", err)
os.Exit(1)
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem := bytes.NewBuffer(nil)
if err != nil {
fmt.Printf("error when create public.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
fmt.Printf("error when encode public pem: %s \n", err)
os.Exit(1)
}
log.Println(privatePem.String())
log.Println(publicPem.String())
}
func TestBSNBlockChain_UpToChain(t *testing.T) {
bc := &BSNBlockChain{
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
PrivatePem: priK,
}
options := NewUpToChainOptions("table", "2", "149848948000").WithDesc("desc")
rsp, err := bc.UpToChain(options)
if err != nil {
t.Fatal(err)
}
t.Log(string(*rsp))
}
func TestBSNBlockChain_GetToken(t *testing.T) {
bc := &BSNBlockChain{
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
PrivatePem: priK,
EnableDebugLog: true,
}
options := &GetTokenRequest{
Type: 1,
TsTxId: "b2e4b906b5ac7952d6b1adcc05681dc6aef5386b07c05e9cdc9ab9d565c17dd4",
ShowValue: true,
}
token, err := bc.GetToken(options)
if err != nil {
t.Fatal(err)
}
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))
}
}