git.gammaspectra.live/P2Pool/p2pool-observer@v0.0.0-20240403172525-d7dfbf7231c8/p2pool/crypto/crypto_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"encoding/hex"
     5  	"git.gammaspectra.live/P2Pool/edwards25519"
     6  	"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
     7  	"git.gammaspectra.live/P2Pool/p2pool-observer/types"
     8  	"os"
     9  	"path"
    10  	"runtime"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestDeterministicTransactionPrivateKey(t *testing.T) {
    16  	expectedPrivateKey := "c93cbd34c66ba4d5b3ddcccd3f550a0169e02225c8d045bc6418dbca4819260b"
    17  
    18  	previousId, _ := types.HashFromString("b64ec18bf2dfa4658693d7f35836d212e66dee47af6f7263ab2bf00e422bcd68")
    19  	publicSpendKeyBytes, _ := hex.DecodeString("f2be6705a034f8f485ee9bc3c21b6309cd0d9dd2111441cc32753ba2bac41b6d")
    20  	p, _ := (&edwards25519.Point{}).SetBytes(publicSpendKeyBytes)
    21  	spendPublicKey := crypto.PublicKeyFromPoint(p)
    22  
    23  	calculatedPrivateKey := GetDeterministicTransactionPrivateKey(types.Hash(spendPublicKey.AsBytes()), previousId)
    24  	if calculatedPrivateKey.String() != expectedPrivateKey {
    25  		t.Fatalf("got %s, expected %s", calculatedPrivateKey.String(), expectedPrivateKey)
    26  	}
    27  }
    28  
    29  func init() {
    30  	_, filename, _, _ := runtime.Caller(0)
    31  	// The ".." may change depending on you folder structure
    32  	dir := path.Join(path.Dir(filename), "../..")
    33  	err := os.Chdir(dir)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  }
    39  
    40  func GetTestEntries(name string, n int) chan []string {
    41  	buf, err := os.ReadFile("testdata/crypto_tests.txt")
    42  	if err != nil {
    43  		return nil
    44  	}
    45  	result := make(chan []string)
    46  	go func() {
    47  		defer close(result)
    48  		for _, line := range strings.Split(string(buf), "\n") {
    49  			entries := strings.Split(strings.TrimSpace(line), " ")
    50  			if entries[0] == name && len(entries) >= (n+1) {
    51  				result <- entries[1:]
    52  			}
    53  		}
    54  	}()
    55  	return result
    56  }
    57  
    58  func TestGetTxKeys(t *testing.T) {
    59  	results := GetTestEntries("get_tx_keys", 4)
    60  	if results == nil {
    61  		t.Fatal()
    62  	}
    63  	for e := range results {
    64  		walletSpendKey := types.MustHashFromString(e[0])
    65  		moneroBlockId := types.MustHashFromString(e[1])
    66  		expectedPublicKey := types.MustHashFromString(e[2])
    67  		expectedSecretKey := types.MustHashFromString(e[3])
    68  
    69  		privateKey := GetDeterministicTransactionPrivateKey(walletSpendKey, moneroBlockId)
    70  		publicKey := privateKey.PublicKey()
    71  
    72  		if expectedSecretKey.String() != privateKey.String() {
    73  			t.Fatalf("expected %s, got %s", expectedSecretKey.String(), privateKey.String())
    74  		}
    75  		if expectedPublicKey.String() != publicKey.String() {
    76  			t.Fatalf("expected %s, got %s", expectedPublicKey.String(), publicKey.String())
    77  		}
    78  	}
    79  }