bsn_test.go 4.7 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/nmZLT\nyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ=="
//
//var host = "http://allied-creation-gateway-dev.fjmaimaimai.com"

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 = "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),
	}
	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", "149848948").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,
	}
	options := &GetTokenRequest{
		Type:      1,
		TsTxId:    "54df75d3bead65d144a1123d1f18af8bb4db65420c5c449631e9a93b81fcdb93",
		ShowValue: true,
	}
	token, err := bc.GetToken(options)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(token.Token)
}