/
sakg.go
60 lines (48 loc) · 1.59 KB
/
sakg.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
// Package sakg implements ADR 0008: Standard Account Key Generation.
package sakg
import (
"fmt"
bip39 "github.com/tyler-smith/go-bip39"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/crypto/slip10"
)
// MaxAccountKeyNumber is the maximum allowed key number when using ADR 0008.
const MaxAccountKeyNumber = uint32(0x7fffffff)
// BIP32PathPrefix is the Oasis Network's BIP-0032 path prefix as defined by
// ADR 0008.
const BIP32PathPrefix = "m/44'/474'"
// Generate signer for the given mnemonic, passphrase and account according to
// ADR 0008.
func GetAccountSigner(
mnemonic string,
passphrase string,
number uint32,
) (signature.Signer, BIP32Path, error) {
if number > MaxAccountKeyNumber {
return nil, nil, fmt.Errorf(
"sakg: invalid key number: %d (maximum: %d)",
number,
MaxAccountKeyNumber,
)
}
if !bip39.IsMnemonicValid(mnemonic) {
return nil, nil, fmt.Errorf("sakg: invalid mnemonic")
}
seed := bip39.NewSeed(mnemonic, passphrase)
signer, chainCode, err := slip10.NewMasterKey(seed)
if err != nil {
return nil, nil, fmt.Errorf("sakg: error deriving master key: %w", err)
}
pathStr := fmt.Sprintf("%s/%d'", BIP32PathPrefix, number)
path, err := NewBIP32Path(pathStr)
if err != nil {
return nil, nil, fmt.Errorf("sakg: error creating BIP-0032 path %s: %w", pathStr, err)
}
for _, index := range path {
signer, chainCode, err = slip10.NewChildKey(signer, chainCode, index)
if err != nil {
return nil, nil, fmt.Errorf("sakg: error deriving child key: %w", err)
}
}
return signer, path, nil
}