github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/test/bipwallet_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 test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/turingchain2020/turingchain/types"
    13  
    14  	"github.com/turingchain2020/turingchain/wallet/bipwallet"
    15  )
    16  
    17  func TestBipwallet(t *testing.T) {
    18  	/*目前暂时支持这些币种创建地址
    19  
    20  	    TypeEther:        "ETH",
    21  		TypeEtherClassic: "ETC",
    22  		TypeBitcoin:      "BTC",
    23  		TypeLitecoin:     "LTC",
    24  		TypeZayedcoin:    "ZEC",
    25  		TypeTrc:          "TRC",
    26  		TypeYcc:          "YCC",
    27  	*/
    28  	//bitsize=128 返回12个单词或者汉子,bitsize+32=160  返回15个单词或者汉子,bitszie=256 返回24个单词或者汉子
    29  	mnem, err := bipwallet.NewMnemonicString(0, 160)
    30  	if err != nil {
    31  		fmt.Println(err)
    32  		return
    33  	}
    34  	fmt.Println("助记词:", mnem)
    35  	//选择币种,填入种子创建wallet对象
    36  	wallet, err := bipwallet.NewWalletFromMnemonic(bipwallet.TypeEther, types.SECP256K1,
    37  		"wish address cram damp very indicate regret sound figure scheme review scout")
    38  	if err != nil {
    39  		fmt.Println("err:", err.Error())
    40  		return
    41  	}
    42  	var index uint32
    43  	//通过索引生成Key pair
    44  	priv, pub, err := wallet.NewKeyPair(index)
    45  	fmt.Println("privkey:", hex.EncodeToString(priv))
    46  	fmt.Println("pubkey:", hex.EncodeToString(pub))
    47  	//通过索引生成对应的地址
    48  	address, err := wallet.NewAddress(index)
    49  	if err != nil {
    50  		fmt.Println("err:", err.Error())
    51  		return
    52  	}
    53  
    54  	fmt.Println("address:", address)
    55  	address, err = bipwallet.PubToAddress(pub)
    56  	if err != nil {
    57  		fmt.Println("err:", err.Error())
    58  		return
    59  	}
    60  
    61  	fmt.Println("PubToAddress:", address)
    62  
    63  	pub, err = bipwallet.PrivkeyToPub(bipwallet.TypeEther, types.SECP256K1, priv)
    64  	if err != nil {
    65  		fmt.Println("err:", err.Error())
    66  		return
    67  	}
    68  
    69  	fmt.Println("PrivToPub:", hex.EncodeToString(pub))
    70  
    71  }