This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e446a66
commit 1da2ba7
Showing
258 changed files
with
65,284 additions
and
6,791 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
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,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 | ||
} |
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
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,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 | ||
} |
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
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 | ||
} |
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,6 @@ | ||
package mnemonic | ||
|
||
const ( | ||
// DefaultEntropy ... | ||
DefaultEntropy int = 128 | ||
) |
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,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 | ||
} |
Oops, something went wrong.