github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/compatibility/siag_1.0_test.go (about)

     1  package compatibility
     2  
     3  // siag.go checks that any changes made to the code retain compatibility with
     4  // old versions of siag.
     5  
     6  import (
     7  	"errors"
     8  	"path/filepath"
     9  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/NebulousLabs/Sia/crypto"
    13  	"github.com/NebulousLabs/Sia/encoding"
    14  	"github.com/NebulousLabs/Sia/types"
    15  )
    16  
    17  // KeyPairSiag_1_0 matches the KeyPair struct of the siag 1.0 code.
    18  type KeyPairSiag_1_0 struct {
    19  	Header           string
    20  	Version          string
    21  	Index            int
    22  	SecretKey        crypto.SecretKey
    23  	UnlockConditions types.UnlockConditions
    24  }
    25  
    26  // verifyKeysSiag_1_0 is a copy-pasted version of the verifyKeys method
    27  // from siag 1.0.
    28  func verifyKeysSiag_1_0(uc types.UnlockConditions, folder string, keyname string) error {
    29  	keysRequired := uc.SignaturesRequired
    30  	totalKeys := uint64(len(uc.PublicKeys))
    31  	loadedKeys := make([]KeyPairSiag_1_0, totalKeys)
    32  	for i := 0; i < len(loadedKeys); i++ {
    33  		err := encoding.ReadFile(filepath.Join(folder, keyname+"_Key"+strconv.Itoa(i)+".siakey"), &loadedKeys[i])
    34  		if err != nil {
    35  			return err
    36  		}
    37  	}
    38  	for _, loadedKey := range loadedKeys {
    39  		if loadedKey.UnlockConditions.UnlockHash() != uc.UnlockHash() {
    40  			return errors.New("ErrCorruptedKey")
    41  		}
    42  	}
    43  	txn := types.Transaction{
    44  		SiafundInputs: []types.SiafundInput{
    45  			{
    46  				UnlockConditions: loadedKeys[0].UnlockConditions,
    47  			},
    48  		},
    49  	}
    50  	var i uint64
    51  	for i != totalKeys {
    52  		if i+keysRequired > totalKeys {
    53  			i = totalKeys - keysRequired
    54  		}
    55  		var j uint64
    56  		for j < keysRequired {
    57  			txn.TransactionSignatures = append(txn.TransactionSignatures, types.TransactionSignature{
    58  				PublicKeyIndex: i,
    59  				CoveredFields:  types.CoveredFields{WholeTransaction: true},
    60  			})
    61  			sigHash := txn.SigHash(int(j))
    62  			sig, err := crypto.SignHash(sigHash, loadedKeys[i].SecretKey)
    63  			if err != nil {
    64  				return err
    65  			}
    66  			txn.TransactionSignatures[j].Signature = sig[:]
    67  			i++
    68  			j++
    69  		}
    70  		err := txn.StandaloneValid(0)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		txn.TransactionSignatures = nil
    75  	}
    76  	return nil
    77  }
    78  
    79  // TestVerifyKeysSiag_1_0 loads some keys generated by siag1.0.
    80  // Verification must still work.
    81  func TestVerifyKeysSiag_1_0(t *testing.T) {
    82  	if testing.Short() {
    83  		t.SkipNow()
    84  	}
    85  	var kp KeyPairSiag_1_0
    86  
    87  	// 1 of 1
    88  	err := encoding.ReadFile("siag_1.0_1of1_Key0.siakey", &kp)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	err = verifyKeysSiag_1_0(kp.UnlockConditions, "", "siag_1.0_1of1")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	// 1 of 2
    98  	err = encoding.ReadFile("siag_1.0_1of2_Key0.siakey", &kp)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	err = verifyKeysSiag_1_0(kp.UnlockConditions, "", "siag_1.0_1of2")
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	// 2 of 3
   108  	err = encoding.ReadFile("siag_1.0_2of3_Key0.siakey", &kp)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	err = verifyKeysSiag_1_0(kp.UnlockConditions, "", "siag_1.0_2of3")
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	// 3 of 3
   118  	err = encoding.ReadFile("siag_1.0_3of3_Key0.siakey", &kp)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	err = verifyKeysSiag_1_0(kp.UnlockConditions, "", "siag_1.0_3of3")
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	// 4 of 9
   128  	err = encoding.ReadFile("siag_1.0_4of9_Key0.siakey", &kp)
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	err = verifyKeysSiag_1_0(kp.UnlockConditions, "", "siag_1.0_4of9")
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  }