Skip to content

Commit

Permalink
WIP: go/common/crypto/sakg: Add ADR 0008 implementation
Browse files Browse the repository at this point in the history
TODO: Add tests
  • Loading branch information
tjanez committed May 6, 2021
1 parent cfb3aee commit de103d6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
56 changes: 56 additions & 0 deletions go/common/crypto/sakg/sakg.go
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
}
1 change: 1 addition & 0 deletions go/common/crypto/sakg/sakg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package sakg
1 change: 1 addition & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/tendermint/tendermint v0.34.9
github.com/tendermint/tm-db v0.6.4
github.com/thepudds/fzgo v0.2.2
github.com/tyler-smith/go-bip39 v1.1.0
github.com/uber/jaeger-client-go v2.25.0+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/whyrusleeping/go-logging v0.0.1
Expand Down
2 changes: 2 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,8 @@ github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8=
github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U=
github.com/uber/jaeger-client-go v2.25.0+incompatible h1:IxcNZ7WRY1Y3G4poYlx24szfsn/3LvK9QHCq9oQw8+U=
github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
Expand Down

0 comments on commit de103d6

Please sign in to comment.