Skip to content

Commit

Permalink
Merge pull request #2610 from oasislabs/david-yan/2609
Browse files Browse the repository at this point in the history
cmd/signer: Add extract command, add entity signer pk check
  • Loading branch information
david-yan authored Jan 31, 2020
2 parents c36c6f0 + b24088b commit 8b88a33
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changelog/2609.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Export signer public key to entity

We added a command to export entities from existing signers, and a check to ensure
that the entity and signer public keys match.
This makes it so that a dummy entity cannot be used for signers backed by Ledger.
5 changes: 5 additions & 0 deletions go/common/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func Load(baseDir string, signerFactory signature.SignerFactory) (*Entity, signa
return nil, nil, err
}

if !ent.ID.Equal(signer.Public()) {
signer.Reset()
return nil, nil, fmt.Errorf("public key mismatch (signer: %s, entity: %s)", signer.Public(), ent.ID)
}

return ent, signer, nil
}

Expand Down
16 changes: 16 additions & 0 deletions go/oasis-node/cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,19 @@ func LoadEntity(signerBackend string, entityDir string) (*entity.Entity, signatu

return entity.Load(entityDir, factory)
}

// ExportEntity creates an empty entity from the public key of the signer
// generated with the specified backend, and writes it to a file in entityDir.
func ExportEntity(signerBackend string, entityDir string) error {
factory, err := SignerFactory(signerBackend, entityDir)
if err != nil {
return err
}
signer, err := factory.Load(signature.SignerEntity)
if err != nil {
return err
}
var entity entity.Entity
entity.ID = signer.Public()
return entity.Save(entityDir)
}
35 changes: 35 additions & 0 deletions go/oasis-node/cmd/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
package signer

import (
"os"

"github.com/spf13/cobra"

"github.com/oasislabs/oasis-core/go/common/logging"
cmdCommon "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common"
cmdFlags "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common/flags"
"github.com/oasislabs/oasis-core/go/oasis-node/cmd/signer/ledger"
)

Expand All @@ -12,14 +17,44 @@ var (
Use: "signer",
Short: "signer backend utilities",
}

exportCmd = &cobra.Command{
Use: "export",
Short: "export the public key from signer as an empty entity",
Run: doExport,
}

logger = logging.GetLogger("cmd/signer")
)

func doExport(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}
entityDir, err := cmdFlags.SignerDirOrPwd()
if err != nil {
logger.Error("failed to retrieve signer dir",
"err", err,
)
os.Exit(1)
}
if err := cmdCommon.ExportEntity(cmdFlags.Signer(), entityDir); err != nil {
logger.Error("failed to export entity",
"err", err,
)
os.Exit(1)
}
}

func Register(parentCmd *cobra.Command) {
for _, v := range []func(*cobra.Command){
ledger.Register,
} {
v(signerCmd)
}

exportCmd.Flags().AddFlagSet(cmdFlags.SignerFlags)

signerCmd.AddCommand(exportCmd)
parentCmd.AddCommand(signerCmd)
}

0 comments on commit 8b88a33

Please sign in to comment.