作者 yangfu

消息订阅

... ... @@ -20,6 +20,8 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.7.0
github.com/tal-tech/go-queue v1.0.5
github.com/tal-tech/go-zero v1.0.27
github.com/valyala/fasthttp v1.23.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
... ...
... ... @@ -21,6 +21,7 @@ github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+Dx
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
github.com/beanstalkd/go-beanstalk v0.1.0/go.mod h1:/G8YTyChOtpOArwLTQPY1CHB+i212+av35bkPXXj56Y=
github.com/beego/beego/v2 v2.0.1 h1:07a7Z0Ok5vbqyqh+q53sDPl9LdhKh0ZDy3gbyGrhFnE=
github.com/beego/beego/v2 v2.0.1/go.mod h1:8zyHi1FnWO1mZLwTn62aKRIZF/aIKvkCBB2JYs+eqQI=
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
... ... @@ -179,6 +180,7 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/iancoleman/strcase v0.1.2/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/imkira/go-interpol v1.1.0 h1:KIiKr0VSG2CUW1hl1jpiyuzuJeKUUpC8iM1AIE7N1Vk=
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
... ... @@ -204,6 +206,7 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.9.8/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
... ... @@ -312,6 +315,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/segmentio/kafka-go v0.4.2 h1:QXZ6q9Bu1JkAJQ/CQBb2Av8pFRG8LQ0kWCrLXgQyL8c=
github.com/segmentio/kafka-go v0.4.2/go.mod h1:Inh7PqOsxmfgasV8InZYKVXWsdjcCq2d9tFV75GLbuM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
... ... @@ -347,6 +352,9 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v0.0.0-20160425020131-cfa635847112/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/tal-tech/go-queue v1.0.5 h1:cd2o0lPjAFJKIXuEbQvsGypUhzz6FLib4FVVAyxsMtY=
github.com/tal-tech/go-queue v1.0.5/go.mod h1:gQK4Eg8pqel8Z9r1hjlSXbJFavLeJQVyTSwBKeAnpm8=
github.com/tal-tech/go-zero v1.0.21/go.mod h1:llP5PQjnATfnzZo/lo5unjR41njzoL3lkGO/KXbnisw=
github.com/tal-tech/go-zero v1.0.27 h1:QMIbaTxibMc/OsO5RTAuKZ8ndbl2dGN6pITQEtp2x/A=
github.com/tal-tech/go-zero v1.0.27/go.mod h1:JtNXlsh/CgeIHyQnt5C5M2IcSevW7V0NAnqO93TQgm8=
github.com/tiptok/egglib-go v0.0.0-20210608073225-c852ce95ae34 h1:9iDNyYbfpv5KLWDLpDywD/aIODg+PNnwn+v9on7KGlE=
... ... @@ -357,6 +365,7 @@ github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYm
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
... ... @@ -373,7 +382,9 @@ github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgq
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.0 h1:d9X0esnoa3dFsV0FG35rAT0RIhYFlPq7MiP+DW89La0=
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
... ... @@ -423,6 +434,7 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
... ...
... ... @@ -11,6 +11,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/goqueue"
)
func main() {
... ... @@ -28,6 +29,8 @@ func main() {
})
log.Logger.AddHook(bw)
goqueue.SetUp()
log.Logger.Info("server start!")
web.Run()
}
... ...
... ... @@ -4,9 +4,11 @@ import "os"
var (
// kafka 地址
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092"
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092" //"106.75.231.90:9092"
// kafka topic log stash
TOPIC_LOG_STASH = "go_stash_dev"
TOPIC_LOG_STASH = "go_stash_dev" //"pushMessage"
// kafka topic up_block_chain
TOPIC_UP_BLOCK_CHAIN = "pushMessage"
// 是否启用日志收集 (本地不启用)
ENABLE_KAFKA_LOG = false
)
... ...
package blockchain
import (
"bytes"
"encoding/base64"
rawjson "encoding/json"
"fmt"
"github.com/beego/beego/v2/client/httplib"
"github.com/linmadan/egglib-go/utils/json"
"net/http"
"net/url"
"sort"
"time"
)
type (
BSNBlockChain struct {
PublicPem []byte
privatePem []byte
PublicKey string
Host string
}
UpToChainRequest struct {
// 上链数据的数据库、数据表等的标识值 (非必填)
InnerDBTable string `json:"innerDBTable,omitempty"`
// 上链数据的唯一标识主键值 (非必填)
InnerPrimaryKey string `json:"innerPrimaryKey,omitempty"`
// 上链记录的一个标记值(IssueId), 数据溯源出所有相关事件内容,例如快递单号,过滤出该快递的所有相关事件内容并用于展示 (非必填)
InnerPrimaryIssueId string `json:"innerPrimaryIssueId,omitempty"`
// 作用与key1相同 (非必填)
InnerSecondIssueId string `json:"innerSecondIssueId,omitempty"`
// 数据原文 (必填)
Value string `json:"value,omitempty"`
// 数据描述: 对value的描述,无论needHash为何值,本字段均会原文存储到链上
Desc string `json:"desc,omitempty"`
// 是否哈希: true: 需要哈希,会将value进行hash上链,false:不需要哈希,明文上链,链上所有用户都可看到明文,默认false
NeedHash bool `json:"needHash"`
}
UpToChainResponse string
GetTokenRequest struct {
// 操作类型:
//1-交易哈希溯源
//2-溯源ID溯源
//3-验真
Type int `json:"type"`
// type为1或者3时必填
TsTxId string `json:"tsTxId,omitempty"`
// type为2时必填
InnerPrimaryKey string `json:"innerPrimaryKey,omitempty"`
// type为3时必填
Value string `json:"value,omitempty"`
}
GetTokenResponse struct {
Token string `json:"token"`
}
Response struct {
Data rawjson.RawMessage `json:"data"`
Code int `json:"code"`
Message string `json:"message"`
}
)
// 上链
func (c *BSNBlockChain) UpToChain(options *UpToChainOptions) (*UpToChainResponse, error) {
req, err := c.MakeRequest(options, "/chainApi/upToChain", "upToChain", http.MethodPost)
if err != nil {
return nil, err
}
var upToChainResponse UpToChainResponse
_, err = c.HandlerResponse(req, &upToChainResponse)
return &upToChainResponse, err
}
// 浏览器溯源验真申请
func (c *BSNBlockChain) GetToken(options *GetTokenRequest) (*GetTokenResponse, error) {
req, err := c.MakeRequest(options, "/getToken", "getToken", http.MethodPost)
if err != nil {
return nil, err
}
var getTokenResponse = GetTokenResponse{}
_, err = c.HandlerResponse(req, &getTokenResponse)
return &getTokenResponse, err
}
// 签名
func (c *BSNBlockChain) Signature(body map[string]interface{}, method string) (string, error) {
var keys []string
for key, _ := range body {
keys = append(keys, key)
}
sort.Strings(keys)
encryptString := bytes.NewBuffer(nil)
for i := range keys {
key := keys[i]
if v, ok := body[key]; ok {
encryptString.WriteString(fmt.Sprintf("%s=%v&", key, v))
}
}
encryptString.WriteString(fmt.Sprintf("method=%v", method))
encryptData, err := RsaEncrypt(c.PublicPem, encryptString.Bytes())
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encryptData), nil
}
func (c *BSNBlockChain) MakeRequest(obj interface{}, action string, signAction, httpMethod string) (*httplib.BeegoHTTPRequest, error) {
var mapBlockInfo = make(map[string]interface{})
json.UnmarshalFromString(json.MarshalToString(obj), &mapBlockInfo)
secret, err := c.Signature(mapBlockInfo, signAction)
if err != nil {
return nil, err
}
req := httplib.NewBeegoRequest(c.Host+action, httpMethod)
req.Header("pubKey", url.QueryEscape(string(c.PublicKey)))
req.Header("signature", url.QueryEscape(secret))
req.SetTimeout(time.Second*5, time.Second*5)
if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
req.JSONBody(obj)
}
return req, nil
}
func (c *BSNBlockChain) HandlerResponse(req *httplib.BeegoHTTPRequest, value interface{}) (*Response, error) {
response := &Response{}
data, err := req.Bytes()
if err != nil {
return nil, err
}
rsp, err := req.Response()
if err != nil {
return nil, err
}
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response code:%v status:%v", rsp.StatusCode, rsp.Status)
}
err = json.Unmarshal(data, response)
if err != nil {
return nil, err
}
json.Unmarshal(response.Data, value)
return response, nil
}
func (b *UpToChainRequest) Complete(options *UpToChainOptions) {
b.InnerDBTable = options.InnerDBTable
b.InnerPrimaryKey = options.InnerPrimaryKey
b.InnerPrimaryIssueId = options.InnerPrimaryIssueId
b.InnerSecondIssueId = options.InnerSecondIssueId
b.Value = options.Value
b.Desc = options.Desc
b.NeedHash = options.NeedHash
}
... ...
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/nmZLT\nyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ=="
var host = "http://allied-creation-gateway-dev.fjmaimaimai.com"
func TestSignature(t *testing.T) {
options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc")
//options := NewUpToChainOptions("table", "", "").WithDesc("")
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,
}
options := NewUpToChainOptions("table", "1", "149848948").WithDesc("desc")
_, err := bc.UpToChain(options)
if err != nil {
t.Fatal(err)
}
}
func TestBSNBlockChain_GetToken(t *testing.T) {
bc := &BSNBlockChain{
PublicPem: []byte(pubPem),
Host: host,
PublicKey: pubKey,
}
options := &GetTokenRequest{
Type: 1,
TsTxId: "",
}
_, err := bc.GetToken(options)
if err != nil {
t.Fatal(err)
}
}
... ...
package blockchain
type UpToChainOptions struct {
// 上链数据的数据库、数据表等的标识值 (非必填)
InnerDBTable string `json:"innerDBTable"`
// 上链数据的唯一标识主键值 (非必填)
InnerPrimaryKey string `json:"innerPrimaryKey"`
// 上链记录的一个标记值(IssueId), 数据溯源出所有相关事件内容,例如快递单号,过滤出该快递的所有相关事件内容并用于展示 (非必填)
InnerPrimaryIssueId string `json:"innerPrimaryIssueId"`
// 作用与key1相同 (非必填)
InnerSecondIssueId string `json:"innerSecondIssueId"`
// 数据原文 (必填)
Value string `json:"value"`
// 数据描述: 对value的描述,无论needHash为何值,本字段均会原文存储到链上
Desc string `json:"desc"`
// 是否哈希: true: 需要哈希,会将value进行hash上链,false:不需要哈希,明文上链,链上所有用户都可看到明文,默认false
NeedHash bool `json:"needHash"`
}
func NewUpToChainOptions(table, primaryKey, value string) *UpToChainOptions {
return &UpToChainOptions{InnerDBTable: table, InnerPrimaryKey: primaryKey, Value: value, NeedHash: false}
}
func (o *UpToChainOptions) WithInnerDBTable(innerDBTable string) *UpToChainOptions {
o.InnerDBTable = innerDBTable
return o
}
func (o *UpToChainOptions) WithInnerPrimaryKey(innerPrimaryKey string) *UpToChainOptions {
o.InnerPrimaryKey = innerPrimaryKey
return o
}
func (o *UpToChainOptions) WithInnerPrimaryIssueId(innerPrimaryIssueId string) *UpToChainOptions {
o.InnerPrimaryIssueId = innerPrimaryIssueId
return o
}
func (o *UpToChainOptions) WithInnerSecondIssueId(innerSecondIssueId string) *UpToChainOptions {
o.InnerSecondIssueId = innerSecondIssueId
return o
}
func (o *UpToChainOptions) WithValue(Value string) *UpToChainOptions {
o.Value = Value
return o
}
func (o *UpToChainOptions) WithDesc(Desc string) *UpToChainOptions {
o.Desc = Desc
return o
}
func (o *UpToChainOptions) WithNeedHash() *UpToChainOptions {
o.NeedHash = true
return o
}
... ...
package blockchain
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
)
// 加密
func RsaEncrypt(publicKey []byte, origData []byte) ([]byte, error) {
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// md5
hash := md5.New()
hash.Write([]byte(origData))
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)
}
// 解密
func RsaDecrypt(privateKey []byte, ciphertext []byte) ([]byte, error) {
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error!")
}
encryptData, _ := base64.StdEncoding.DecodeString(string(ciphertext))
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
// pkcs1 是标准但裸奔,pkcs8升级支持密码
pri2, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
priv = pri2.(*rsa.PrivateKey)
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, encryptData)
}
... ...
package goqueue
import (
"fmt"
"github.com/tal-tech/go-queue/kq"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"strings"
)
func SetUp() {
go func() {
q := kq.MustNewQueue(NewConfig(constant.TOPIC_UP_BLOCK_CHAIN, constant.TOPIC_UP_BLOCK_CHAIN, 2), kq.WithHandle(UpToChainHandler))
defer func() {
q.Stop()
log.Logger.Info(fmt.Sprintf("goqueue:%v stop!", constant.TOPIC_UP_BLOCK_CHAIN))
}()
q.Start()
}()
log.Logger.Info("goqueue start!")
}
func NewConfig(topic, group string, consumers int) kq.KqConf {
brokers := strings.Split(constant.KAFKA_HOST, ",")
return kq.KqConf{
ServiceConf: service.ServiceConf{
Name: topic,
Log: logx.LogConf{
Mode: "console",
},
Mode: "pro",
},
Brokers: brokers,
Group: group,
Topic: topic,
Offset: "first",
Conns: 1,
Consumers: consumers,
Processors: 4,
MinBytes: 10200,
MaxBytes: 10485760,
}
}
... ...
package goqueue
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
)
func UpToChainHandler(k, v string) error {
log.Logger.Debug(fmt.Sprintf("%s", v), map[string]interface{}{"handler": "UptoChain"})
return nil
}
... ...