Skip to content

Commit

Permalink
add cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
zRich committed Nov 3, 2022
1 parent 52a93d3 commit d08d361
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 1,909 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"mode": "auto",
"program": "${fileDirname}",
"showLog": true,
"args": ["did", "create", "--public=public.pem", "--output=did.json"]
}
]
}
661 changes: 0 additions & 661 deletions LICENSE

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"crypto/ed25519"
"encoding/json"
"encoding/pem"
"fmt"
"log"
"os"

ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("create called")
pubKey, _ := cmd.Flags().GetString("public")
didFile, _ := cmd.Flags().GetString("output")
createDIDDoc(pubKey, didFile)
},
}

func init() {
didCmd.AddCommand(createCmd)

// Here you will define your flags and configuration settings.

var pubKey string
var didFile string
createCmd.Flags().StringVarP(&pubKey, "public", "p", "public.pem", "public key in JsonWebKey2020 format.")
createCmd.MarkFlagRequired("public")
createCmd.Flags().StringVarP(&didFile, "output", "o", "", "DID document output file.")
createCmd.MarkFlagRequired("output")
}

func createDIDDoc(pubKey string, didFile string) error {
raw, _ := os.ReadFile(pubKey)
block, rest := pem.Decode(raw)
if block == nil || block.Type != "PUBLIC KEY" {
log.Fatal(rest)
}

pub := ed25519.PublicKey(block.Bytes)

didID, _ := did.ParseDID("did:example:123")
doc := &did.Document{
Context: []ssi.URI{did.DIDContextV1URI()},
ID: *didID,
}

keyID, _ := did.ParseDIDURL("did:example:123#key-1")
vm, _ := did.NewVerificationMethod(*keyID, ssi.JsonWebKey2020, did.DID{}, pub)
doc.AddAssertionMethod(vm)
didJson, _ := json.MarshalIndent(doc, "", " ")

// fmt.Println(string(didJson))
// parsedDID := did.Document{}

// err := json.Unmarshal(didJson, &parsedDID)
// if err != nil {
// log.Fatal(err)
// }
// parsedDID.AssertionMethod[0].JWK()
// parsedDID.AssertionMethod[0].PublicKey()

// fmt.Printf("public key arg = %s \n", pubKey)
// fmt.Printf("out put did document file = %s\n", didFile)
// // err = os.WriteFile(didFile, didJson, 0644)
err := os.WriteFile(didFile, didJson, 0644)
return err
}
40 changes: 40 additions & 0 deletions cmd/did.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// didCmd represents the did command
var didCmd = &cobra.Command{
Use: "did",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("did called")
},
}

func init() {
rootCmd.AddCommand(didCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// didCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// didCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
40 changes: 40 additions & 0 deletions cmd/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2022 ZHAO Zhenhua <[email protected]>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// nodeCmd represents the node command
var nodeCmd = &cobra.Command{
Use: "node",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("node called")
},
}

func init() {
rootCmd.AddCommand(nodeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// nodeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// nodeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
46 changes: 46 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "fusion",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) {},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.fusion.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
3 changes: 0 additions & 3 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ type BHPCSP interface {
//KeyGen generates a new key
KeyGen(opts KeyGenOpts) (Key, error)

//GetKey returns the key
GetKey(keyInstance []byte) (Key, error)

//Hash hashes a message
Hash(msg []byte, opts HashOpts) ([]byte, error)

Expand Down
22 changes: 11 additions & 11 deletions crypto/ed29919csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package crypto
import (
"crypto/ed25519"
"crypto/rand"
"crypto/sha512"
"errors"
"hash"
)
Expand All @@ -43,37 +44,36 @@ func (k *Ed25519PrivateKey) generateEd25519Key() (Key, error) {
return key, nil
}

// GetKey returns the key
func (e *Ed25519PrivateKey) GetKey(keyInstance []byte) (Key, error) {
panic("not implemented") // TODO: Implement
}

// Hash hashes a message
func (e *Ed25519PrivateKey) Hash(msg []byte, opts HashOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
h, err := e.GetHash(opts)
if err != nil {
return nil, err
}
return h.Sum(msg), nil
}

// GetHash returns the instance of hash function
func (e *Ed25519PrivateKey) GetHash(opt HashOpts) (hash.Hash, error) {
panic("not implemented") // TODO: Implement
return sha512.New(), nil
}

func (e *Ed25519PrivateKey) Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
return nil, errors.New("this key does NOT support encrypt")
}

func (e *Ed25519PrivateKey) Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
return nil, errors.New("this key does NOT support decrypt")
}

// Sign signs a message's hash
func (e *Ed25519PrivateKey) Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
return ed25519.Sign(e.csp, digest), nil
}

// Verify verifies a signature
func (e *Ed25519PrivateKey) Verify(k Key, signature []byte, digest []byte, opts SignerOpts) (bool, error) {
panic("not implemented") // TODO: Implement
return ed25519.Verify(e.pub.csi, digest, signature), nil
}

// the key's raw byte
Expand Down
44 changes: 44 additions & 0 deletions crypto/ed29919csp_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
// The AGPLv3 License (AGPLv3)

// Copyright (c) 2022 ZHAO Zhenhua <[email protected]>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package crypto

import "testing"

func TestKeyGen(t *testing.T) {
keyGenOpts := &Ed25519PrivateKey{}
key, err := keyGenOpts.KeyGen(nil)
b, _ := key.Bytes()
if err != nil {
t.Logf("key = %x", b)
}
}

func TestSign(t *testing.T) {
keyGenOpts := &Ed25519PrivateKey{}
key, err := keyGenOpts.KeyGen(nil)
if err != nil {
t.Logf("key failed")
}

msg := []byte("Rich Zhao")
signature, err := key.(*Ed25519PrivateKey).Sign(key, msg, nil)
if err != nil {
t.Logf("sign failed")
}

t.Logf("signature = %x", signature)
}
24 changes: 0 additions & 24 deletions crypto/opts.go

This file was deleted.

6 changes: 6 additions & 0 deletions did/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ type DID did.DID

type Document did.Document

type URI ssi.URI

type KeyType ssi.KeyType

const (
DIIDContext = did.DIDContextV1
)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
Expand All @@ -28,6 +29,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shengdoushi/base58 v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
Expand All @@ -40,6 +42,7 @@ require (
github.com/ethereum/go-ethereum v1.10.25
github.com/gin-gonic/gin v1.8.1
github.com/nuts-foundation/go-did v0.3.0
github.com/spf13/cobra v1.6.1
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
golang.org/x/net v0.0.0-20220923203811-8be639271d50
google.golang.org/grpc v1.50.1
Expand Down
Loading

0 comments on commit d08d361

Please sign in to comment.