github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/go-bip44/bip44_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bip44_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/turingchain2020/turingchain/wallet/bipwallet"
    11  	bip32 "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip32"
    12  	bip39 "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip39"
    13  	. "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip44"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestNewKeyFromMnemonic(t *testing.T) {
    18  	mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow"
    19  	fKey, err := NewKeyFromMnemonic(mnemonic, bipwallet.TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0)
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  	if fKey.String() != "xprvA2vH8KdcBBKhMxhENJpJdbwiU5cUXSkaHR7QVTpBmusgYMR8NsZ4BFTNyRLUiaPHg7UYP8u92FJkSEAmmgu3PDQCoY7gBsdvpB7msWGCpXG" {
    24  		t.Errorf("Invalid Factoid key - %v", fKey.String())
    25  	}
    26  
    27  	ecKey, err := NewKeyFromMnemonic(mnemonic, bipwallet.TypeFactomEntryCredits, bip32.FirstHardenedChild, 0, 0)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	if ecKey.String() != "xprvA2ziNegvZRfAAUtDsjeS9LvCP1TFXfR3hUzMcJw7oYAhdPqZyiJTMf1ByyLRxvQmGvgbPcG6Q569m26ixWsmgTR3d3PwicrG7hGD7C7seJA" {
    32  		t.Errorf("Invalid EC key - %v", ecKey.String())
    33  	}
    34  
    35  	mnemonic = "aaaaaa"
    36  	_, err = NewKeyFromMnemonic(mnemonic, bipwallet.TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0)
    37  	assert.NotNil(t, err)
    38  }
    39  
    40  func TestNewKeyFromMasterKey(t *testing.T) {
    41  	mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow"
    42  
    43  	seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	masterKey, err := bip32.NewMasterKey(seed)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	fKey, err := NewKeyFromMasterKey(masterKey, bipwallet.TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	if fKey.String() != "xprvA2vH8KdcBBKhMxhENJpJdbwiU5cUXSkaHR7QVTpBmusgYMR8NsZ4BFTNyRLUiaPHg7UYP8u92FJkSEAmmgu3PDQCoY7gBsdvpB7msWGCpXG" {
    58  		t.Errorf("Invalid Factoid key - %v", fKey.String())
    59  	}
    60  
    61  	ecKey, err := NewKeyFromMasterKey(masterKey, bipwallet.TypeFactomEntryCredits, bip32.FirstHardenedChild, 0, 0)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	if ecKey.String() != "xprvA2ziNegvZRfAAUtDsjeS9LvCP1TFXfR3hUzMcJw7oYAhdPqZyiJTMf1ByyLRxvQmGvgbPcG6Q569m26ixWsmgTR3d3PwicrG7hGD7C7seJA" {
    66  		t.Errorf("Invalid EC key - %v", ecKey.String())
    67  	}
    68  
    69  	_, err = NewKeyFromMasterKey(&bip32.Key{}, bipwallet.TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0)
    70  	assert.NotNil(t, err)
    71  }
    72  
    73  /*
    74  func TestTest(t *testing.T) {
    75  	//var factoidHex uint32 = 0x80000083
    76  	//var ecHex uint32 = 0x80000084
    77  
    78  	mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow"
    79  
    80  	seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  	masterKey, err := bip32.NewMasterKey(seed)
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  
    90  	child, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 44)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	t.Logf("%v", child.String())
    95  
    96  	child, err = child.NewChildKey(bip32.FirstHardenedChild + 132)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	t.Logf("%v", child.String())
   101  
   102  	child, err = child.NewChildKey(bip32.FirstHardenedChild)
   103  	if err != nil {
   104  		panic(err)
   105  	}
   106  	t.Logf("%v", child.String())
   107  
   108  	child, err = child.NewChildKey(0)
   109  	if err != nil {
   110  		panic(err)
   111  	}
   112  	t.Logf("%v", child.String())
   113  
   114  	child, err = child.NewChildKey(0)
   115  	if err != nil {
   116  		panic(err)
   117  	}
   118  	t.Logf("%v", child.String())
   119  
   120  	/*
   121  		if child.String()!="xprvA22bpQKA9av7gEKdskwxbBNaMso6XpmW7sXi5LGgKnGCMe82BYW68tcNXtn4ZiLHDYJ2HpRvknV7zdDSgBXtPo4dRwG8XCcU55akAcarx3G" {
   122  
   123  		}
   124  */ /*
   125  
   126  	key, err := NewKeyFromMnemonic(mnemonic, bip32.FirstHardenedChild, 0, 0, 0)
   127  	if err != nil {
   128  		panic(err)
   129  	}
   130  	t.Logf("%v", key.String())
   131  
   132  	add, err := factom.MakeFactoidAddress(key.Key)
   133  	if err != nil {
   134  		panic(err)
   135  	}
   136  	t.Logf("%v", add.String())
   137  
   138  	t.FailNow()
   139  }
   140  */