bsn_test.go 5.1 KB
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))
	}
}