forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 25
/
bip39.go
82 lines (68 loc) · 2.23 KB
/
bip39.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package libkaspawallet
import (
"fmt"
"github.com/karlsen-network/karlsend/cmd/karlsenwallet/libkaspawallet/bip32"
"github.com/karlsen-network/karlsend/domain/dagconfig"
"github.com/pkg/errors"
"github.com/tyler-smith/go-bip39"
)
// CreateMnemonic creates a new bip-39 compatible mnemonic
func CreateMnemonic() (string, error) {
const bip39BitSize = 256
entropy, _ := bip39.NewEntropy(bip39BitSize)
return bip39.NewMnemonic(entropy)
}
// Purpose and CoinType constants
const (
SingleSignerPurpose = 44
// Note: this is not entirely compatible to BIP 45 since
// BIP 45 doesn't have a coin type in its derivation path.
MultiSigPurpose = 45
// TODO: Register the coin type in https://github.com/satoshilabs/slips/blob/master/slip-0044.md
CoinType = 111111
)
func defaultPath(isMultisig bool) string {
purpose := SingleSignerPurpose
if isMultisig {
purpose = MultiSigPurpose
}
return fmt.Sprintf("m/%d'/%d'/0'", purpose, CoinType)
}
// MasterPublicKeyFromMnemonic returns the master public key with the correct derivation for the given mnemonic.
func MasterPublicKeyFromMnemonic(params *dagconfig.Params, mnemonic string, isMultisig bool) (string, error) {
path := defaultPath(isMultisig)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, params)
if err != nil {
return "", err
}
extendedPublicKey, err := extendedKey.Public()
if err != nil {
return "", err
}
return extendedPublicKey.String(), nil
}
func extendedKeyFromMnemonicAndPath(mnemonic string, path string, params *dagconfig.Params) (*bip32.ExtendedKey, error) {
seed := bip39.NewSeed(mnemonic, "")
version, err := versionFromParams(params)
if err != nil {
return nil, err
}
master, err := bip32.NewMasterWithPath(seed, version, path)
if err != nil {
return nil, err
}
return master, nil
}
func versionFromParams(params *dagconfig.Params) ([4]byte, error) {
switch params.Name {
case dagconfig.MainnetParams.Name:
return bip32.KaspaMainnetPrivate, nil
case dagconfig.TestnetParams.Name:
return bip32.KaspaTestnetPrivate, nil
case dagconfig.DevnetParams.Name:
return bip32.KaspaDevnetPrivate, nil
case dagconfig.SimnetParams.Name:
return bip32.KaspaSimnetPrivate, nil
}
return [4]byte{}, errors.Errorf("unknown network %s", params.Name)
}