github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/go-bip44/bip44.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  // Copyright 2016 Factom Foundation
     6  // Use of this source code is governed by the MIT
     7  // license that can be found in the LICENSE file.
     8  
     9  // Package bip44 基于 BIP32 的系统,赋予树状结构中的各层特殊的意义。让同一个 seed 可以支援多币种、多帐户等。
    10  package bip44
    11  
    12  import (
    13  	bip32 "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip32"
    14  	bip39 "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip39"
    15  )
    16  
    17  // Purpose Purpose
    18  const Purpose uint32 = 0x8000002C
    19  
    20  //https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
    21  //https://github.com/satoshilabs/slips/blob/master/slip-0044.md
    22  //https://github.com/FactomProject/FactomDocs/blob/master/wallet_info/wallet_test_vectors.md
    23  
    24  /*
    25  const (
    26  	// TypeBitcoin 比特币类型
    27  	TypeBitcoin uint32 = 0x80000000
    28  	TypeTestnet uint32 = 0x80000001
    29  	// TypeLitecoin 莱特币类型
    30  	TypeLitecoin              uint32 = 0x80000002
    31  	TypeDogecoin              uint32 = 0x80000003
    32  	TypeReddcoin              uint32 = 0x80000004
    33  	TypeDash                  uint32 = 0x80000005
    34  	TypePeercoin              uint32 = 0x80000006
    35  	TypeNamecoin              uint32 = 0x80000007
    36  	TypeFeathercoin           uint32 = 0x80000008
    37  	TypeCounterparty          uint32 = 0x80000009
    38  	TypeBlackcoin             uint32 = 0x8000000a
    39  	TypeNuShares              uint32 = 0x8000000b
    40  	TypeNuBits                uint32 = 0x8000000c
    41  	TypeMazacoin              uint32 = 0x8000000d
    42  	TypeViacoin               uint32 = 0x8000000e
    43  	TypeClearingHouse         uint32 = 0x8000000f
    44  	TypeRubycoin              uint32 = 0x80000010
    45  	TypeGroestlcoin           uint32 = 0x80000011
    46  	TypeDigitalcoin           uint32 = 0x80000012
    47  	TypeCannacoin             uint32 = 0x80000013
    48  	TypeDigiByte              uint32 = 0x80000014
    49  	TypeOpenAssets            uint32 = 0x80000015
    50  	TypeMonacoin              uint32 = 0x80000016
    51  	TypeClams                 uint32 = 0x80000017
    52  	TypePrimecoin             uint32 = 0x80000018
    53  	TypeNeoscoin              uint32 = 0x80000019
    54  	TypeJumbucks              uint32 = 0x8000001a
    55  	TypeziftrCOIN             uint32 = 0x8000001b
    56  	TypeVertcoin              uint32 = 0x8000001c
    57  	TypeNXT                   uint32 = 0x8000001d
    58  	TypeBurst                 uint32 = 0x8000001e
    59  	TypeMonetaryUnit          uint32 = 0x8000001f
    60  	TypeZoom                  uint32 = 0x80000020
    61  	TypeVpncoin               uint32 = 0x80000021
    62  	TypeCanadaeCoin           uint32 = 0x80000022
    63  	TypeShadowCash            uint32 = 0x80000023
    64  	TypeParkByte              uint32 = 0x80000024
    65  	TypePandacoin             uint32 = 0x80000025
    66  	TypeStartCOIN             uint32 = 0x80000026
    67  	TypeMOIN                  uint32 = 0x80000027
    68  	TypeArgentum              uint32 = 0x8000002D
    69  	TypeGlobalCurrencyReserve uint32 = 0x80000031
    70  	TypeNovacoin              uint32 = 0x80000032
    71  	TypeAsiacoin              uint32 = 0x80000033
    72  	TypeBitcoindark           uint32 = 0x80000034
    73  	TypeDopecoin              uint32 = 0x80000035
    74  	TypeTemplecoin            uint32 = 0x80000036
    75  	TypeAIB                   uint32 = 0x80000037
    76  	TypeEDRCoin               uint32 = 0x80000038
    77  	TypeSyscoin               uint32 = 0x80000039
    78  	TypeSolarcoin             uint32 = 0x8000003a
    79  	TypeSmileycoin            uint32 = 0x8000003b
    80  	// TypeEther 以太坊类型
    81  	TypeEther uint32 = 0x8000003c
    82  	// TypeEtherClassic 经典以太坊
    83  	TypeEtherClassic     uint32 = 0x8000003d
    84  	TypeOpenChain        uint32 = 0x80000040
    85  	TypeOKCash           uint32 = 0x80000045
    86  	TypeDogecoinDark     uint32 = 0x8000004d
    87  	TypeElectronicGulden uint32 = 0x8000004e
    88  	TypeClubCoin         uint32 = 0x8000004f
    89  	TypeRichCoin         uint32 = 0x80000050
    90  	TypePotcoin          uint32 = 0x80000051
    91  	TypeQuarkcoin        uint32 = 0x80000052
    92  	TypeTerracoin        uint32 = 0x80000053
    93  	TypeGridcoin         uint32 = 0x80000054
    94  	TypeAuroracoin       uint32 = 0x80000055
    95  	TypeIXCoin           uint32 = 0x80000056
    96  	TypeGulden           uint32 = 0x80000057
    97  	TypeBitBean          uint32 = 0x80000058
    98  	TypeBata             uint32 = 0x80000059
    99  	TypeMyriadcoin       uint32 = 0x8000005a
   100  	TypeBitSend          uint32 = 0x8000005b
   101  	TypeUnobtanium       uint32 = 0x8000005c
   102  	TypeMasterTrader     uint32 = 0x8000005d
   103  	TypeGoldBlocks       uint32 = 0x8000005e
   104  	TypeSaham            uint32 = 0x8000005f
   105  	TypeChronos          uint32 = 0x80000060
   106  	TypeUbiquoin         uint32 = 0x80000061
   107  	TypeEvotion          uint32 = 0x80000062
   108  	TypeSaveTheOcean     uint32 = 0x80000063
   109  	TypeBigUp            uint32 = 0x80000064
   110  	TypeGameCredits      uint32 = 0x80000065
   111  	TypeDollarcoins      uint32 = 0x80000066
   112  	// TypeZayedcoin Zayed币
   113  	TypeZayedcoin uint32 = 0x80000067
   114  	TypeDubaicoin uint32 = 0x80000068
   115  	TypeStratis   uint32 = 0x80000069
   116  	TypeShilling  uint32 = 0x8000006a
   117  	TypePiggyCoin uint32 = 0x80000076
   118  	TypeMonero    uint32 = 0x80000080
   119  	TypeNavCoin   uint32 = 0x80000082
   120  	// TypeFactomFactoids FactomFactoids
   121  	TypeFactomFactoids uint32 = 0x80000083
   122  	// TypeFactomEntryCredits FactomEntryCredits
   123  	TypeFactomEntryCredits uint32 = 0x80000084
   124  	TypeZcash              uint32 = 0x80000085
   125  	TypeLisk               uint32 = 0x80000086
   126  )
   127  */
   128  
   129  // NewKeyFromMnemonic 新建Key
   130  func NewKeyFromMnemonic(mnemonic string, coin, account, chain, address uint32) (*bip32.Key, error) {
   131  	seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	masterKey, err := bip32.NewMasterKey(seed)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return NewKeyFromMasterKey(masterKey, coin, account, chain, address)
   142  }
   143  
   144  // NewKeyFromMasterKey 新建Key
   145  func NewKeyFromMasterKey(masterKey *bip32.Key, coin, account, chain, address uint32) (*bip32.Key, error) {
   146  	child, err := masterKey.NewChildKey(Purpose)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	child, err = child.NewChildKey(coin)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	child, err = child.NewChildKey(account)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	child, err = child.NewChildKey(chain)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	child, err = child.NewChildKey(address)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	return child, nil
   172  }