github.com/amazechain/amc@v0.1.3/common/transaction/transaction_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 transaction
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/json"
    22  	"github.com/amazechain/amc/common/types"
    23  	"github.com/holiman/uint256"
    24  	"github.com/libp2p/go-libp2p/core/crypto"
    25  	"testing"
    26  )
    27  
    28  func TestNewLegacyTx(t *testing.T) {
    29  	_, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	addr := types.PublicToAddress(pub)
    35  
    36  	tx := NewTransaction(1, addr, &addr, uint256.NewInt(10000), 21000, uint256.NewInt(10000000), []byte("hello"))
    37  	t.Logf("tx: %v", tx)
    38  
    39  	buf1, err := json.Marshal(tx)
    40  	t.Log(types.BytesHash(buf1).String())
    41  
    42  	switch txt := tx.inner.(type) {
    43  	case *LegacyTx:
    44  		buf, err := json.Marshal(txt)
    45  		if err != nil {
    46  			t.Fatal(err)
    47  		}
    48  
    49  		txHash := types.BytesHash(buf)
    50  		t.Log(txHash.String())
    51  	}
    52  
    53  	hash := tx.Hash()
    54  
    55  	t.Log(hash.String())
    56  
    57  }
    58  
    59  func TestTDin(t *testing.T) {
    60  	_, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	addr := types.PublicToAddress(pub)
    66  
    67  	tx := NewTransaction(1, addr, &addr, uint256.NewInt(10000), 21000, uint256.NewInt(10000000), []byte("hello"))
    68  	t.Logf("tx: %v", tx)
    69  
    70  	b, err := tx.Marshal()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	tx.Unmarshal(b)
    76  
    77  	t.Log(b)
    78  }
    79  
    80  func TestNewDynamicTx(t *testing.T) {
    81  	//_, pub, err := crypto.GenerateECDSAKeyPair(rand.Reader)
    82  	//if err != nil {
    83  	//	t.Fatal(err)
    84  	//}
    85  	//
    86  	//addr := types.PublicToAddress(pub)
    87  
    88  }