-
Notifications
You must be signed in to change notification settings - Fork 13
/
bip39.go
52 lines (43 loc) · 1.57 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
package ed25519
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/crypto/sakg"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/crypto/slip10"
"github.com/tyler-smith/go-bip39"
)
// FromBip39Mnemonic derives an ed25519 key from BIP39 mnemonic, following SLIP10 specification.
func FromBip39Mnemonic(mnemonic, passphrase, path string) (PrivKey, error) {
if !bip39.IsMnemonicValid(mnemonic) {
return nil, fmt.Errorf("invalid mnemonic")
}
seed := bip39.NewSeed(mnemonic, passphrase)
signer, chainCode, err := slip10.NewMasterKey(seed)
if err != nil {
return nil, fmt.Errorf("cannot derive master key from mnemonic: %w", err)
}
signer, err = deriveFromBip32Path(signer, chainCode, path)
if err != nil {
return nil, fmt.Errorf("derive for path %s: %w", path, err)
}
unsafeSigner, ok := signer.(signature.UnsafeSigner)
if !ok {
return nil, fmt.Errorf("cannot retrieve private key from %T", signer)
}
return unsafeSigner.UnsafeBytes(), nil
}
// GetAccountSigner generates a signer for the given mnemonic, passphrase and
// account according to ADR 0008.
func deriveFromBip32Path(signer signature.Signer, chainCode slip10.ChainCode, bip32path string) (signature.Signer, error) {
path, err := sakg.NewBIP32Path(bip32path)
if err != nil {
return nil, fmt.Errorf("invalid path %s: %w", path, err)
}
for _, i := range path {
signer, chainCode, err = slip10.NewChildKey(signer, chainCode, i)
if err != nil {
return nil, fmt.Errorf("cannot derive child key %d: %w", i, err)
}
}
return signer, nil
}