Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-1319] Add helper to convert ed25519 pubkeys to jwk #321

Merged
merged 3 commits into from
May 16, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions cmd/cheqd-noded/cmd/debug_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/base64"
"encoding/json"
"fmt"

"github.com/lestrrat-go/jwx/jwk"
"github.com/multiformats/go-multibase"
"github.com/spf13/cobra"
)
Expand All @@ -24,7 +26,7 @@ func ed25519Cmd() *cobra.Command {
Short: "ed25519 tools",
}

cmd.AddCommand(ed25519RandomCmd())
cmd.AddCommand(ed25519RandomCmd(), ed25519PubKeyBase64ToJwkCmd())

return cmd
}
Expand All @@ -41,10 +43,10 @@ func ed25519RandomCmd() *cobra.Command {
}

keyInfo := struct {
PubKeyBase64 string `json:"pub_key_base_64"`
PrivKeyBase64 string `json:"priv_key_base_64"`
PubKeyBase64 string `json:"pub_key_base_64"`
PrivKeyBase64 string `json:"priv_key_base_64"`
}{
PubKeyBase64: base64.StdEncoding.EncodeToString(pubKey),
PubKeyBase64: base64.StdEncoding.EncodeToString(pubKey),
PrivKeyBase64: base64.StdEncoding.EncodeToString(privKey),
}

Expand All @@ -61,6 +63,39 @@ func ed25519RandomCmd() *cobra.Command {
return cmd
}

// ed25519PubKeyBase64ToJwk returns cobra Command.
func ed25519PubKeyBase64ToJwkCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pubkey-base64-to-jwk",
Short: "Convert ed25519 pubkey base64 to jwk",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pubKeyBase64 := args[0]
pubKeyBytes, err := base64.StdEncoding.DecodeString(pubKeyBase64)
if err != nil {
return err
}

pubKey := ed25519.PublicKey(pubKeyBytes)

pubKeyJwk, err := jwk.New(pubKey)
if err != nil {
return err
}

pubKeyJwkJson, err := json.Marshal(pubKeyJwk)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), string(pubKeyJwkJson))
return err
},
}

return cmd
}

// encoding returns cobra Command.
func encodingCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -78,7 +113,7 @@ func base64toMultibase58Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "base64-multibase58 [input]",
Short: "Convert base64 string to multibase58 string",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
base64Str := args[0]
bytes, err := base64.StdEncoding.DecodeString(base64Str)
Expand Down