├── LICENSE ├── bip44.go ├── bip44_test.go └── bitLength_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Factom Foundation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /bip44.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Factom Foundation 2 | // Use of this source code is governed by the MIT 3 | // license that can be found in the LICENSE file. 4 | 5 | package bip44 6 | 7 | import ( 8 | "github.com/FactomProject/go-bip32" 9 | "github.com/FactomProject/go-bip39" 10 | ) 11 | 12 | const Purpose uint32 = 0x8000002C 13 | 14 | //https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki 15 | //https://github.com/satoshilabs/slips/blob/master/slip-0044.md 16 | //https://github.com/FactomProject/FactomDocs/blob/master/wallet_info/wallet_test_vectors.md 17 | 18 | const ( 19 | TypeBitcoin uint32 = 0x80000000 20 | TypeTestnet uint32 = 0x80000001 21 | TypeLitecoin uint32 = 0x80000002 22 | TypeDogecoin uint32 = 0x80000003 23 | TypeReddcoin uint32 = 0x80000004 24 | TypeDash uint32 = 0x80000005 25 | TypePeercoin uint32 = 0x80000006 26 | TypeNamecoin uint32 = 0x80000007 27 | TypeFeathercoin uint32 = 0x80000008 28 | TypeCounterparty uint32 = 0x80000009 29 | TypeBlackcoin uint32 = 0x8000000a 30 | TypeNuShares uint32 = 0x8000000b 31 | TypeNuBits uint32 = 0x8000000c 32 | TypeMazacoin uint32 = 0x8000000d 33 | TypeViacoin uint32 = 0x8000000e 34 | TypeClearingHouse uint32 = 0x8000000f 35 | TypeRubycoin uint32 = 0x80000010 36 | TypeGroestlcoin uint32 = 0x80000011 37 | TypeDigitalcoin uint32 = 0x80000012 38 | TypeCannacoin uint32 = 0x80000013 39 | TypeDigiByte uint32 = 0x80000014 40 | TypeOpenAssets uint32 = 0x80000015 41 | TypeMonacoin uint32 = 0x80000016 42 | TypeClams uint32 = 0x80000017 43 | TypePrimecoin uint32 = 0x80000018 44 | TypeNeoscoin uint32 = 0x80000019 45 | TypeJumbucks uint32 = 0x8000001a 46 | TypeziftrCOIN uint32 = 0x8000001b 47 | TypeVertcoin uint32 = 0x8000001c 48 | TypeNXT uint32 = 0x8000001d 49 | TypeBurst uint32 = 0x8000001e 50 | TypeMonetaryUnit uint32 = 0x8000001f 51 | TypeZoom uint32 = 0x80000020 52 | TypeVpncoin uint32 = 0x80000021 53 | TypeCanadaeCoin uint32 = 0x80000022 54 | TypeShadowCash uint32 = 0x80000023 55 | TypeParkByte uint32 = 0x80000024 56 | TypePandacoin uint32 = 0x80000025 57 | TypeStartCOIN uint32 = 0x80000026 58 | TypeMOIN uint32 = 0x80000027 59 | TypeArgentum uint32 = 0x8000002D 60 | TypeGlobalCurrencyReserve uint32 = 0x80000031 61 | TypeNovacoin uint32 = 0x80000032 62 | TypeAsiacoin uint32 = 0x80000033 63 | TypeBitcoindark uint32 = 0x80000034 64 | TypeDopecoin uint32 = 0x80000035 65 | TypeTemplecoin uint32 = 0x80000036 66 | TypeAIB uint32 = 0x80000037 67 | TypeEDRCoin uint32 = 0x80000038 68 | TypeSyscoin uint32 = 0x80000039 69 | TypeSolarcoin uint32 = 0x8000003a 70 | TypeSmileycoin uint32 = 0x8000003b 71 | TypeEther uint32 = 0x8000003c 72 | TypeEtherClassic uint32 = 0x8000003d 73 | TypeOpenChain uint32 = 0x80000040 74 | TypeOKCash uint32 = 0x80000045 75 | TypeDogecoinDark uint32 = 0x8000004d 76 | TypeElectronicGulden uint32 = 0x8000004e 77 | TypeClubCoin uint32 = 0x8000004f 78 | TypeRichCoin uint32 = 0x80000050 79 | TypePotcoin uint32 = 0x80000051 80 | TypeQuarkcoin uint32 = 0x80000052 81 | TypeTerracoin uint32 = 0x80000053 82 | TypeGridcoin uint32 = 0x80000054 83 | TypeAuroracoin uint32 = 0x80000055 84 | TypeIXCoin uint32 = 0x80000056 85 | TypeGulden uint32 = 0x80000057 86 | TypeBitBean uint32 = 0x80000058 87 | TypeBata uint32 = 0x80000059 88 | TypeMyriadcoin uint32 = 0x8000005a 89 | TypeBitSend uint32 = 0x8000005b 90 | TypeUnobtanium uint32 = 0x8000005c 91 | TypeMasterTrader uint32 = 0x8000005d 92 | TypeGoldBlocks uint32 = 0x8000005e 93 | TypeSaham uint32 = 0x8000005f 94 | TypeChronos uint32 = 0x80000060 95 | TypeUbiquoin uint32 = 0x80000061 96 | TypeEvotion uint32 = 0x80000062 97 | TypeSaveTheOcean uint32 = 0x80000063 98 | TypeBigUp uint32 = 0x80000064 99 | TypeGameCredits uint32 = 0x80000065 100 | TypeDollarcoins uint32 = 0x80000066 101 | TypeZayedcoin uint32 = 0x80000067 102 | TypeDubaicoin uint32 = 0x80000068 103 | TypeStratis uint32 = 0x80000069 104 | TypeShilling uint32 = 0x8000006a 105 | TypePiggyCoin uint32 = 0x80000076 106 | TypeMonero uint32 = 0x80000080 107 | TypeNavCoin uint32 = 0x80000082 108 | TypeFactomFactoids uint32 = 0x80000083 109 | TypeFactomEntryCredits uint32 = 0x80000084 110 | TypeZcash uint32 = 0x80000085 111 | TypeLisk uint32 = 0x80000086 112 | TypeFactomIdentity uint32 = 0x80000119 113 | ) 114 | 115 | func NewKeyFromMnemonic(mnemonic string, coin, account, chain, address uint32) (*bip32.Key, error) { 116 | seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") 117 | if err != nil { 118 | return nil, err 119 | } 120 | 121 | masterKey, err := bip32.NewMasterKey(seed) 122 | if err != nil { 123 | return nil, err 124 | } 125 | 126 | return NewKeyFromMasterKey(masterKey, coin, account, chain, address) 127 | } 128 | 129 | func NewKeyFromMasterKey(masterKey *bip32.Key, coin, account, chain, address uint32) (*bip32.Key, error) { 130 | child, err := masterKey.NewChildKey(Purpose) 131 | if err != nil { 132 | return nil, err 133 | } 134 | 135 | child, err = child.NewChildKey(coin) 136 | if err != nil { 137 | return nil, err 138 | } 139 | 140 | child, err = child.NewChildKey(account) 141 | if err != nil { 142 | return nil, err 143 | } 144 | 145 | child, err = child.NewChildKey(chain) 146 | if err != nil { 147 | return nil, err 148 | } 149 | 150 | child, err = child.NewChildKey(address) 151 | if err != nil { 152 | return nil, err 153 | } 154 | 155 | return child, nil 156 | } 157 | -------------------------------------------------------------------------------- /bip44_test.go: -------------------------------------------------------------------------------- 1 | package bip44_test 2 | 3 | import ( 4 | "testing" 5 | 6 | //"github.com/FactomProject/factom" 7 | "github.com/FactomProject/go-bip32" 8 | "github.com/FactomProject/go-bip39" 9 | . "github.com/FactomProject/go-bip44" 10 | ) 11 | 12 | func TestNewKeyFromMnemonic(t *testing.T) { 13 | mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow" 14 | fKey, err := NewKeyFromMnemonic(mnemonic, TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0) 15 | if err != nil { 16 | panic(err) 17 | } 18 | if fKey.String() != "xprvA2vH8KdcBBKhMxhENJpJdbwiU5cUXSkaHR7QVTpBmusgYMR8NsZ4BFTNyRLUiaPHg7UYP8u92FJkSEAmmgu3PDQCoY7gBsdvpB7msWGCpXG" { 19 | t.Errorf("Invalid Factoid key - %v", fKey.String()) 20 | } 21 | 22 | ecKey, err := NewKeyFromMnemonic(mnemonic, TypeFactomEntryCredits, bip32.FirstHardenedChild, 0, 0) 23 | if err != nil { 24 | panic(err) 25 | } 26 | if ecKey.String() != "xprvA2ziNegvZRfAAUtDsjeS9LvCP1TFXfR3hUzMcJw7oYAhdPqZyiJTMf1ByyLRxvQmGvgbPcG6Q569m26ixWsmgTR3d3PwicrG7hGD7C7seJA" { 27 | t.Errorf("Invalid EC key - %v", ecKey.String()) 28 | } 29 | } 30 | 31 | func TestNewKeyFromMasterKey(t *testing.T) { 32 | mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow" 33 | 34 | seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") 35 | if err != nil { 36 | panic(err) 37 | } 38 | 39 | masterKey, err := bip32.NewMasterKey(seed) 40 | if err != nil { 41 | panic(err) 42 | } 43 | 44 | fKey, err := NewKeyFromMasterKey(masterKey, TypeFactomFactoids, bip32.FirstHardenedChild, 0, 0) 45 | if err != nil { 46 | panic(err) 47 | } 48 | if fKey.String() != "xprvA2vH8KdcBBKhMxhENJpJdbwiU5cUXSkaHR7QVTpBmusgYMR8NsZ4BFTNyRLUiaPHg7UYP8u92FJkSEAmmgu3PDQCoY7gBsdvpB7msWGCpXG" { 49 | t.Errorf("Invalid Factoid key - %v", fKey.String()) 50 | } 51 | 52 | ecKey, err := NewKeyFromMasterKey(masterKey, TypeFactomEntryCredits, bip32.FirstHardenedChild, 0, 0) 53 | if err != nil { 54 | panic(err) 55 | } 56 | if ecKey.String() != "xprvA2ziNegvZRfAAUtDsjeS9LvCP1TFXfR3hUzMcJw7oYAhdPqZyiJTMf1ByyLRxvQmGvgbPcG6Q569m26ixWsmgTR3d3PwicrG7hGD7C7seJA" { 57 | t.Errorf("Invalid EC key - %v", ecKey.String()) 58 | } 59 | } 60 | 61 | /* 62 | func TestTest(t *testing.T) { 63 | //var factoidHex uint32 = 0x80000083 64 | //var ecHex uint32 = 0x80000084 65 | 66 | mnemonic := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow" 67 | 68 | seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") 69 | if err != nil { 70 | panic(err) 71 | } 72 | 73 | masterKey, err := bip32.NewMasterKey(seed) 74 | if err != nil { 75 | panic(err) 76 | } 77 | 78 | child, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 44) 79 | if err != nil { 80 | panic(err) 81 | } 82 | t.Logf("%v", child.String()) 83 | 84 | child, err = child.NewChildKey(bip32.FirstHardenedChild + 132) 85 | if err != nil { 86 | panic(err) 87 | } 88 | t.Logf("%v", child.String()) 89 | 90 | child, err = child.NewChildKey(bip32.FirstHardenedChild) 91 | if err != nil { 92 | panic(err) 93 | } 94 | t.Logf("%v", child.String()) 95 | 96 | child, err = child.NewChildKey(0) 97 | if err != nil { 98 | panic(err) 99 | } 100 | t.Logf("%v", child.String()) 101 | 102 | child, err = child.NewChildKey(0) 103 | if err != nil { 104 | panic(err) 105 | } 106 | t.Logf("%v", child.String()) 107 | 108 | /* 109 | if child.String()!="xprvA22bpQKA9av7gEKdskwxbBNaMso6XpmW7sXi5LGgKnGCMe82BYW68tcNXtn4ZiLHDYJ2HpRvknV7zdDSgBXtPo4dRwG8XCcU55akAcarx3G" { 110 | 111 | } 112 | */ /* 113 | 114 | key, err := NewKeyFromMnemonic(mnemonic, bip32.FirstHardenedChild, 0, 0, 0) 115 | if err != nil { 116 | panic(err) 117 | } 118 | t.Logf("%v", key.String()) 119 | 120 | add, err := factom.MakeFactoidAddress(key.Key) 121 | if err != nil { 122 | panic(err) 123 | } 124 | t.Logf("%v", add.String()) 125 | 126 | t.FailNow() 127 | } 128 | */ 129 | -------------------------------------------------------------------------------- /bitLength_test.go: -------------------------------------------------------------------------------- 1 | package bip44_test 2 | 3 | import ( 4 | "testing" 5 | 6 | . "github.com/FactomProject/go-bip44" 7 | ) 8 | 9 | func TestBitLength(t *testing.T) { 10 | child, err := NewKeyFromMnemonic( 11 | "element fence situate special wrap snack method volcano busy ribbon neck sphere", 12 | TypeFactomFactoids, 13 | 2147483648, 14 | 0, 15 | 19, 16 | ) 17 | 18 | if err != nil { 19 | t.Errorf("%v", err) 20 | } 21 | if len(child.Key) != 32 { 22 | t.Errorf("len: %d, child.Key:%x\n", len(child.Key), child.Key) 23 | t.Errorf("%v", child.String()) 24 | } 25 | 26 | child, err = NewKeyFromMnemonic( 27 | "element fence situate special wrap snack method volcano busy ribbon neck sphere", 28 | TypeFactomFactoids, 29 | 2147483648, 30 | 1, 31 | 19, 32 | ) 33 | 34 | if err != nil { 35 | t.Errorf("%v", err) 36 | } 37 | if len(child.Key) != 32 { 38 | t.Errorf("len: %d, child.Key:%x\n", len(child.Key), child.Key) 39 | t.Errorf("%v", child.String()) 40 | } 41 | } 42 | --------------------------------------------------------------------------------