github.com/amazechain/amc@v0.1.3/utils/util_test.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package utils
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/hex"
    22  	"github.com/amazechain/amc/common/types"
    23  	"github.com/libp2p/go-libp2p/core/crypto"
    24  	"testing"
    25  )
    26  
    27  func TestHash256toS(t *testing.T) {
    28  	data := []byte("123aaaaa")
    29  	s := Hash256toS(data)
    30  	t.Log(s)
    31  	s1 := Hash256toS(data)
    32  	t.Log(s1)
    33  	s2 := Hash256toS(data)
    34  	t.Log(s2)
    35  	s3 := Hash256toS(data)
    36  	t.Log(s3)
    37  
    38  }
    39  
    40  func TestKeccak256(t *testing.T) {
    41  	data := []byte("123aaaaa")
    42  	s := Keccak256(data)
    43  	t.Log(hex.EncodeToString(s))
    44  }
    45  
    46  func TestPrivate(t *testing.T) {
    47  	privstr := "CAMSeTB3AgEBBCCpK8butBo2fq28tb1zdfeIZBZRcXcbcxmAJcd+h+nrjKAKBggqhkjOPQMBB6FEA0IABFYoS6MEcv5opOrOmC6dNsu9tW/isw6e5Qymx7p5mO3CvgR1WiKzbuT3FJ5N9Kn6kUNY3mzmeo04Rj/wihxVEh8="
    48  	priv, err := StringToPrivate(privstr)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	public := priv.GetPublic()
    54  
    55  	s, err := PublicToString(public)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	t.Logf("public: %s", s)
    61  
    62  	a := types.PrivateToAddress(priv)
    63  	t.Logf("address: %s", a.String())
    64  }
    65  
    66  func TestStringToPublic(t *testing.T) {
    67  	priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
    68  	//priv, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	s, err := PublicToString(pub)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	t.Logf("public: %s", s)
    79  
    80  	pub2, err := StringToPublic(s)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	if pub2.Equals(pub) {
    86  		t.Log("success")
    87  	} else {
    88  		t.Logf("failed")
    89  	}
    90  
    91  	ps, err := PrivateToString(priv)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	t.Logf("private :%s", ps)
    97  	t.Log(len(ps))
    98  }