Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
adds common/mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Jan 24, 2019
1 parent e446a66 commit 1da2ba7
Show file tree
Hide file tree
Showing 258 changed files with 65,284 additions and 6,791 deletions.
6 changes: 3 additions & 3 deletions common/keyring/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
keytypes "github.com/c3systems/go-substrate/common/keyring/types"
)

type KeyRingInterface interface {
type InterfaceKeyRing interface {
DecodeAddress(encoded []byte) ([]byte, error)
EncodeAddress(key []byte) ([]byte, error)
EncodeAddress(key []byte) (string, error)
SetAddressPrefix(prefix address.PrefixEnum) error
AddPair(pair *pair.Pair) (*pair.Pair, error)
AddFromAddress(address []byte, meta *keytypes.Meta) (*pair.Pair, error)
AddFromAddress(address []byte, meta *keytypes.Meta, defaultEncoded []byte) (*pair.Pair, error)
AddFromMnemonic(mnemonic string, meta *keytypes.Meta) (*pair.Pair, error)
AddFromSeed(seed []byte, meta *keytypes.Meta) (*pair.Pair, error)
AddFromJson(pairJSON []byte) (*pair.Pair, error)
Expand Down
100 changes: 100 additions & 0 deletions common/keyring/keyring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package keyring

import (
"errors"

"github.com/c3systems/go-substrate/common/keyring/address"
"github.com/c3systems/go-substrate/common/keyring/pair"
keytypes "github.com/c3systems/go-substrate/common/keyring/types"
)

// New ...
func New() (*KeyRing, error) {
return &KeyRing{
Pairs: new(pair.Pairs),
}, nil
}

// DecodeAddres ...
func (k *KeyRing) DecodeAddress(encoded []byte) ([]byte, error) {
return address.Decode(string(encoded), nil)
}

// EncodeAddress ...
func (k *KeyRing) EncodeAddress(key []byte) (string, error) {
return address.Encode(string(key), nil)
}

// SetAddressPrefix ...
func (k *KeyRing) SetAddressPrefix(prefix address.PrefixEnum) error {
// note: is this really what we want? We won't be able to create multiple keyrings with different default prefixes...
address.SetDefaultPrefix(prefix)
return nil
}

// AddPair ...
func (k *KeyRing) AddPair(pair *pair.Pair) (*pair.Pair, error) {
if k.Pairs == nil {
return nil, errors.New("pairs is nil")
}

return k.Pairs.Add(pair)
}

// AddFromAddress ...
func (k *KeyRing) AddFromAddress(addr []byte, meta *keytypes.Meta, defaultEncoded []byte) (*pair.Pair, error) {
tmp, err := address.Decode(addr)
if err != nil {
return nil, err
}
var pub [32]byte
copy(pub[:], tmp)

// note: this pair will be locked bc no secret key ...
pair, err := pair.NewPair(pub, [64]byte{}, meta, defaultEncoded)
if err != nil {
return nil, err
}

return k.AddPair(pair)
}

// AddFromMnemonic ...
func (k *KeyRing) AddFromMnemonic(mnemonic string, meta *keytypes.Meta) (*pair.Pair, error) {
return nil, nil
}

// AddFromSeed ...
func (k *KeyRing) AddFromSeed(seed []byte, meta *keytypes.Meta) (*pair.Pair, error) {
return nil, nil
}

// AddFromJSON ...
func (k *KeyRing) AddFromJSON(pairJSON []byte) (*pair.Pair, error) {
return nil, nil
}

// GetPair ...
func (k *KeyRing) GetPair(address []byte) (*pair.Pair, error) {
return nil, nil
}

// GetPairs ...
func (k *KeyRing) GetPairs() []*pair.Pair {
return nil
}

// GetPublicKeys ...
func (k *KeyRing) GetPublicKeys() ([][]byte, error) {
return nil, nil
}

// RemovePair ...
func (k *KeyRing) RemovePair(address []byte) error {
return nil
}

// ToJSON ...
func (k *KeyRing) ToJSON(address []byte, passphrase *string) ([]byte, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion common/keyring/pair/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type InterfacePair interface {
// InterfacePairs ...
type InterfacePairs interface {
Add(pair *Pair) (*Pair, error)
All() []*Pair
All() ([]*Pair, error)
Get(address []byte) (*Pair, error)
Remove(address []byte) error
}
8 changes: 5 additions & 3 deletions common/keyring/pair/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/c3systems/go-substrate/logger"
)

// New ...
func New(naclPub [32]byte, naclPriv [64]byte, meta *ktypes.Meta, defaultEncoded []byte) (*Pair, error) {
// NewPair ...
func NewPair(naclPub [32]byte, naclPriv [64]byte, meta *ktypes.Meta, defaultEncoded []byte) (*Pair, error) {
state := &State{
Meta: meta,
PublicKey: naclPub,
Expand Down Expand Up @@ -68,7 +68,9 @@ func (p *Pair) GetMeta() (*ktypes.Meta, error) {

// IsLocked ...
func (p *Pair) IsLocked() bool {
return len(p.secretKey) == 0
// note: string comparison is slow, better method? reflect.DeepEqual will probably also be slow...
blankSecret := [64]byte{}
return string(p.secretKey[:]) == string(blankSecret[:])
}

// Lock ...
Expand Down
85 changes: 85 additions & 0 deletions common/keyring/pair/pairs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pair

import (
"errors"

"github.com/c3systems/go-substrate/common/keyring/address"
)

// NewPairs ...
func NewPairs() (*Pairs, error) {
return &Pairs{
PairMap: make(MapPair),
}, nil
}

// Add ...
func (p *Pairs) Add(pair *Pair) (*Pair, error) {
if pair == nil {
return nil, errors.New("nil pair")
}
if pair.State == nil {
return nil, errors.New("nil state")
}
// note: just make the map?
if p.PairMap == nil {
return nil, errors.New("nil pair map")
}

p.PairMap[string(pair.State.PublicKey[:])] = pair

return pair, nil
}

// All ...
func (p *Pairs) All() ([]*Pair, error) {
// note: just make the map?
if p.PairMap == nil {
return nil, errors.New("nil pair map")
}

var pairs []*Pair
for k := range p.PairMap {
pairs = append(pairs, p.PairMap[k])
}

return pairs, nil
}

// Get ...
func (p *Pairs) Get(addr []byte) (*Pair, error) {
// note: just make the map?
if p.PairMap == nil {
return nil, errors.New("nil pair map")
}

decoded, err := address.Decode(string(addr), nil)
if err != nil {
return nil, err
}

pair, ok := p.PairMap[string(decoded)]
if !ok {
// note: or just return nil, nil?
return nil, errors.New("no such pair")
}

return pair, nil
}

// Remove ...
func (p *Pairs) Remove(addr []byte) error {
// note: just make the map?
if p.PairMap == nil {
return errors.New("nil pair map")
}

decoded, err := address.Decode(string(addr), nil)
if err != nil {
return err
}

delete(p.PairMap, string(decoded))

return nil
}
13 changes: 11 additions & 2 deletions common/keyring/pair/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

var (
// note: ensure the pair struct implements the interface at compile time
_ InterfacePair = (*Pair)(nil)
// note: ensure the struct(s) implement the interface(s) at compile time
_ InterfacePair = (*Pair)(nil)
_ InterfacePairs = (*Pairs)(nil)
)

// State ...
Expand Down Expand Up @@ -35,5 +36,13 @@ type encoding struct {
Version string
}

// Pairs ...
type Pairs struct {
PairMap MapPair
}

// MapPair ...
type MapPair map[string]*Pair

// note: implement nobody and everybody?
// https://github.com/polkadot-js/common/blob/master/packages/keyring/src/pair/nobody.ts
13 changes: 13 additions & 0 deletions common/keyring/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package keyring

import "github.com/c3systems/go-substrate/common/keyring/pair"

var (
// note: ensure the struct(s) implement the interface(s) at compile time
_ InterfaceKeyRing = (*KeyRing)(nil)
)

// KeyRing ...
type KeyRing struct {
Pairs *pair.Pairs
}
6 changes: 6 additions & 0 deletions common/mnemonic/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mnemonic

const (
// DefaultEntropy ...
DefaultEntropy int = 128
)
49 changes: 49 additions & 0 deletions common/mnemonic/mnemonic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mnemonic

import (
"errors"

"github.com/tyler-smith/go-bip39"
)

// Generate ...
func Generate(entropy *int) (string, error) {
if entropy == nil {
entropy = new(int)
*entropy = DefaultEntropy
}

// note: already checked for nil pointer
ent, err := bip39.NewEntropy(*entropy)
if err != nil {
return "", err
}

return bip39.NewMnemonic(ent)
}

// Validate ...
func Validate(mnemonic string) bool {
return bip39.IsMnemonicValid(mnemonic)
}

// ToSecret ...
func ToSecret(mnemonic, password string) ([]byte, error) {
// TODO: is this correct? rather 'bip32.NewMasterKey(seed)'?
return bip39.NewSeed(mnemonic, password), nil
}

// ToSeed ...
func ToSeed(mnemonic, password string) ([]byte, error) {
tmp, err := ToSecret(mnemonic, password)
if err != nil {
return nil, err
}

if len(tmp) < 32 {
return nil, errors.New("invalid secret generated")
}

// pair.DEFAULT_KEY_LENGTH?
return tmp[:32], nil
}
Loading

0 comments on commit 1da2ba7

Please sign in to comment.