github.com/amazechain/amc@v0.1.3/common/types/address_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 types
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/json"
    22  	"github.com/holiman/uint256"
    23  	"reflect"
    24  
    25  	// "github.com/amazechain/amc/utils" cycle import
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/libp2p/go-libp2p/core/crypto"
    30  )
    31  
    32  func TestAddress_ed25519(t *testing.T) {
    33  	_, pub, err := crypto.GenerateEd25519Key(rand.Reader)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	addr := PublicToAddress(pub)
    39  	t.Log(addr.String())
    40  }
    41  
    42  func TestFromECDSAPub(t *testing.T) {
    43  	_, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	addr := PublicToAddress(pub)
    49  	t.Log(addr.String())
    50  }
    51  
    52  // func TestAddress_DecodeString(t *testing.T) {
    53  // 	_, pub, err := crypto.GenerateEd25519Key(rand.Reader)
    54  // 	if err != nil {
    55  // 		t.Fatal(err)
    56  // 	}
    57  
    58  // 	addr := PublicToAddress(pub)
    59  // 	t.Log(addr.String())
    60  // 	t.Log(addr.Bytes())
    61  
    62  // 	h := addr.HexBytes()
    63  // 	var c Address
    64  // 	if c.DecodeHexBytes(h) {
    65  // 		t.Log(h)
    66  // 		t.Logf("c: %s", c.String())
    67  // 	}
    68  
    69  // 	var a, b Address
    70  // 	if a.DecodeString(addr.String()) {
    71  // 		t.Logf("a: %s", a.String())
    72  // 	}
    73  // 	if b.DecodeBytes(addr.Bytes()) {
    74  // 		t.Logf("b: %s", b.String())
    75  // 	}
    76  
    77  // 	t.Log("done!")
    78  // }
    79  
    80  func TestPrivateToAddress(t *testing.T) {
    81  	priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	addr := PublicToAddress(pub)
    87  	t.Log(addr.String())
    88  	addr2 := PrivateToAddress(priv)
    89  	t.Log(addr2.String())
    90  }
    91  
    92  func TestAddress_null(t *testing.T) {
    93  	a := Address{}
    94  	if a.IsNull() {
    95  		t.Log("null address")
    96  	} else {
    97  		t.Log(a)
    98  	}
    99  
   100  	_, pub, err := crypto.GenerateEd25519Key(rand.Reader)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	addr := PublicToAddress(pub)
   106  
   107  	if addr.IsNull() {
   108  		t.Log("null address")
   109  	} else {
   110  		t.Log(strings.ToUpper(addr.String()))
   111  	}
   112  }
   113  
   114  func TestAddress(t *testing.T) {
   115  	for i := 0; i < 100; i++ {
   116  		_, pub, err := crypto.GenerateEd25519Key(rand.Reader)
   117  		if err != nil {
   118  			t.Fatal(err)
   119  		}
   120  
   121  		addr := PublicToAddress(pub)
   122  
   123  		if addr.IsNull() {
   124  			t.Log("null address")
   125  		} else {
   126  			t.Log(strings.ToUpper(addr.String()))
   127  		}
   128  	}
   129  }
   130  
   131  func TestSign(t *testing.T) {
   132  	data := []byte("hello")
   133  	//priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
   134  	priv, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  
   139  	s, err := priv.Sign(data)
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	t.Logf("sign len (%d)", len(s))
   145  
   146  	ok, err := pub.Verify(data, s)
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	t.Logf("sign len %d", len(s))
   152  	if !ok {
   153  		t.Log("verify sign failed")
   154  	} else {
   155  		t.Log("verify success")
   156  	}
   157  }
   158  
   159  // func TestAddressToInt(t *testing.T) {
   160  // 	priv, _, err := crypto.GenerateECDSAKeyPair(rand.Reader)
   161  // 	if err != nil {
   162  // 		t.Fatal(err)
   163  // 	}
   164  
   165  // 	sp, err := utils.PrivateToString(priv)
   166  
   167  // 	addr := PrivateToAddress(priv)
   168  // 	t.Logf("address: %s", addr)
   169  // 	t.Logf("private: %s", sp)
   170  // }
   171  
   172  func TestAddressMarshal(t *testing.T) {
   173  	aa := make(map[Address]*uint256.Int)
   174  	var a1, a2 Address
   175  	a1.DecodeString("AMC4541Fc1CCB4e042a3BaDFE46904F9D22d127B682")
   176  	a2.DecodeString("AMC9BA336835422BAeFc537d75642959d2a866500a3")
   177  	aa[a1] = uint256.NewInt(1)
   178  	aa[a2] = uint256.NewInt(2)
   179  
   180  	b, err := json.Marshal(aa)
   181  	if nil != err {
   182  		t.Fatal(err)
   183  	}
   184  
   185  	bb := make(map[Address]*uint256.Int)
   186  
   187  	if err := json.Unmarshal(b, &bb); nil != err {
   188  		t.Fatal(err)
   189  	}
   190  
   191  	if !reflect.DeepEqual(aa, bb) {
   192  		t.Error("failed")
   193  	}
   194  }