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

Commit

Permalink
Add subkey network flag (#484)
Browse files Browse the repository at this point in the history
- Adds `--network` for `accounts generate` and `accounts import`
- Updates sr25519 methos to accept network parameter
- Bump GSRPC version
  • Loading branch information
ansermino authored Jul 17, 2020
1 parent a92ea6f commit 306cdbb
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 37 deletions.
19 changes: 8 additions & 11 deletions cmd/chainbridge/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func handleGenerateCmd(ctx *cli.Context, dHandler *dataHandler) error {
password = []byte(pwdflag)
}

_, err := generateKeypair(keytype, dHandler.datadir, password)
_, err := generateKeypair(keytype, dHandler.datadir, password, ctx.String(config.SubkeyNetworkFlag.Name))
if err != nil {
return fmt.Errorf("failed to generate key: %s", err)
}
Expand All @@ -72,12 +72,8 @@ func handleGenerateCmd(ctx *cli.Context, dHandler *dataHandler) error {

// handleImportCmd imports external keystores into the bridge
func handleImportCmd(ctx *cli.Context, dHandler *dataHandler) error {

// import key

var err error

log.Info("Importing key...")
var err error

// check if --ed25519 or --sr25519 is set
keytype := crypto.Secp256k1Type
Expand Down Expand Up @@ -105,7 +101,7 @@ func handleImportCmd(ctx *cli.Context, dHandler *dataHandler) error {
password = []byte(pwdflag)
}

_, err = importPrivKey(keytype, dHandler.datadir, privkeyflag, password)
_, err = importPrivKey(ctx, keytype, dHandler.datadir, privkeyflag, password)
} else {
if keyimport := ctx.Args().First(); keyimport != "" {
_, err = importKey(keyimport, dHandler.datadir)
Expand Down Expand Up @@ -147,7 +143,7 @@ func getDataDir(ctx *cli.Context) (string, error) {
}

//importPrivKey imports a private key into a keypair
func importPrivKey(keytype, datadir, key string, password []byte) (string, error) {
func importPrivKey(ctx *cli.Context, keytype, datadir, key string, password []byte) (string, error) {
if password == nil {
password = keystore.GetPassword("Enter password to encrypt keystore file:")
}
Expand All @@ -162,7 +158,8 @@ func importPrivKey(keytype, datadir, key string, password []byte) (string, error

if keytype == crypto.Sr25519Type {
// generate sr25519 keys
kp, err = sr25519.NewKeypairFromSeed(key)
network := ctx.String(config.SubkeyNetworkFlag.Name)
kp, err = sr25519.NewKeypairFromSeed(key, network)
if err != nil {
return "", fmt.Errorf("could not generate sr25519 keypair from given string: %s", err)
}
Expand Down Expand Up @@ -338,7 +335,7 @@ func getKeyFiles(datadir string) ([]string, error) {
// generateKeypair create a new keypair with the corresponding type and saves it to datadir/keystore/[public key].key
// in json format encrypted using the specified password
// it returns the resulting filepath of the new key
func generateKeypair(keytype, datadir string, password []byte) (string, error) {
func generateKeypair(keytype, datadir string, password []byte, subNetwork string) (string, error) {
if password == nil {
password = keystore.GetPassword("Enter password to encrypt keystore file:")
}
Expand All @@ -353,7 +350,7 @@ func generateKeypair(keytype, datadir string, password []byte) (string, error) {

if keytype == crypto.Sr25519Type {
// generate sr25519 keys
kp, err = sr25519.GenerateKeypair()
kp, err = sr25519.GenerateKeypair(subNetwork)
if err != nil {
return "", fmt.Errorf("could not generate sr25519 keypair: %s", err)
}
Expand Down
19 changes: 7 additions & 12 deletions cmd/chainbridge/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,11 @@ func TestAccountCommands(t *testing.T) {
}

keypath := "."

srFile, err := generateKeypair("sr25519", keypath, testPassword)
srFile, err := generateKeypair("sr25519", keypath, testPassword, "substrate")
if err != nil {
t.Fatal(err)
}
secpFile, err := generateKeypair("secp256k1", keypath, testPassword)
if err != nil {
t.Fatal(err)
}

secpFile, err := generateKeypair("secp256k1", keypath, testPassword, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -225,7 +220,7 @@ func TestGetDatadir(t *testing.T) {
}

func TestGenerateKey_NoType(t *testing.T) {
keyfile, err := generateKeypair("", testKeystoreDir, testPassword)
keyfile, err := generateKeypair("", testKeystoreDir, testPassword, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -258,7 +253,7 @@ func TestImportKey_ShouldFail(t *testing.T) {
func TestImportKey(t *testing.T) {
keypath := "../../"

importkeyfile, err := generateKeypair("sr25519", keypath, testPassword)
importkeyfile, err := generateKeypair("sr25519", keypath, testPassword, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -318,7 +313,7 @@ func TestImportEthKey(t *testing.T) {
}

func TestImportKey_withPk(t *testing.T) {
keyfile, err := importPrivKey("", testKeystoreDir, "000000000000000000000000000000000000000000000000000000416c696365", testPassword)
keyfile, err := importPrivKey(cli.NewContext(app, nil, nil), "", testKeystoreDir, "000000000000000000000000000000000000000000000000000000416c696365", testPassword)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -347,12 +342,12 @@ func TestListKeys(t *testing.T) {
var err error
var keyfile string
if i%2 == 0 {
keyfile, err = generateKeypair(crypto.Sr25519Type, testKeystoreDir, testPassword)
keyfile, err = generateKeypair(crypto.Sr25519Type, testKeystoreDir, testPassword, "")
if err != nil {
t.Fatal(err)
}
} else {
keyfile, err = generateKeypair(crypto.Secp256k1Type, testKeystoreDir, testPassword)
keyfile, err = generateKeypair(crypto.Secp256k1Type, testKeystoreDir, testPassword, "")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/chainbridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var generateFlags = []cli.Flag{
config.PasswordFlag,
config.Sr25519Flag,
config.Secp256k1Flag,
config.SubkeyNetworkFlag,
}

var devFlags = []cli.Flag{
Expand All @@ -48,6 +49,7 @@ var importFlags = []cli.Flag{
config.Sr25519Flag,
config.Secp256k1Flag,
config.PasswordFlag,
config.SubkeyNetworkFlag,
}

var accountCommand = cli.Command{
Expand Down
5 changes: 5 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ var (
Name: "privateKey",
Usage: "Import a hex representation of a private key into a keystore.",
}
SubkeyNetworkFlag = &cli.StringFlag{
Name: "network",
Usage: "Specify the network to use for the address encoding (substrate/polkadot/centrifuge)",
DefaultText: "substrate",
}
)

// Test Setting Flags
Expand Down
8 changes: 4 additions & 4 deletions crypto/sr25519/sr25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ type Keypair struct {
keyringPair *signature.KeyringPair
}

func GenerateKeypair() (*Keypair, error) {
func GenerateKeypair(network string) (*Keypair, error) {
data := make([]byte, 32)
_, err := rand.Read(data)
if err != nil {
return nil, err
}
return NewKeypairFromSeed("//" + hexutil.Encode(data))
return NewKeypairFromSeed("//"+hexutil.Encode(data), network)
}

func NewKeypairFromSeed(seed string) (*Keypair, error) {
kp, err := signature.KeyringPairFromSecret(seed)
func NewKeypairFromSeed(seed, network string) (*Keypair, error) {
kp, err := signature.KeyringPairFromSecret(seed, network)
return &Keypair{&kp}, err
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/sr25519/sr25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestNewKeypairFromSeed(t *testing.T) {
kp, err := NewKeypairFromSeed("//Alice")
kp, err := NewKeypairFromSeed("//Alice", "substrate")
if err != nil {
t.Fatal(err)
}
Expand All @@ -22,7 +22,7 @@ func TestNewKeypairFromSeed(t *testing.T) {
}

func TestKeypair_AsKeyringPair(t *testing.T) {
kp, err := NewKeypairFromSeed("//Alice")
kp, err := NewKeypairFromSeed("//Alice", "substrate")
if err != nil {
t.Fatal(err)
}
Expand All @@ -38,7 +38,7 @@ func TestKeypair_AsKeyringPair(t *testing.T) {
}

func TestEncodeAndDecodeKeypair(t *testing.T) {
kp, err := NewKeypairFromSeed("//Alice")
kp, err := NewKeypairFromSeed("//Alice", "substrate")
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ go 1.13
require (
github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e
github.com/ChainSafe/log15 v1.0.0
github.com/aristanetworks/goarista v0.0.0-20200609010056-95bcf8053598 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.4+incompatible
github.com/ethereum/go-ethereum v1.9.13
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.5+incompatible
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/ethereum/go-ethereum v1.9.16
github.com/gorilla/websocket v1.4.2 // indirect
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/rs/cors v1.7.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/urfave/cli/v2 v2.2.0
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
)
Loading

0 comments on commit 306cdbb

Please sign in to comment.