-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: go/common/crypto/sakg: Add ADR 0008 implementation
TODO: Add tests
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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. | ||
var 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 '%s'", 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) | ||
} | ||
|
||
path := NewBIP32Path(fmt.Sprintf("%s/%d'", BIP32PathPrefix, number)) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package sakg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters