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

New explorer structure #233

Merged
merged 18 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions client/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ func (c *Client) GetAllExplorer() ([]string, string, bool) {
}
return rm.Result.Links, rm.Message, rm.Status
}

func (c *Client) AddUserAPIKey(did string, apiKey string) (string, bool) {
q := make(map[string]string)
q["did"] = did
q["apiKey"] = apiKey
var rm model.BasicResponse
err := c.sendJSONRequest("POST", setup.APIAddUserAPIKey, q, nil, &rm)
if err != nil {
return err.Error(), false
}
return rm.Message, rm.Status
}
5 changes: 5 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const (
RecoverTokensCmd string = "recoverToken"
ValidateTokenchainCmd string = "validatetokenchain"
ValidateTokenCmd string = "validatetoken"
AddUserAPIKeyCmd string = "adduserapikey"
)

var commands = []string{VersionCmd,
Expand Down Expand Up @@ -285,6 +286,7 @@ type Command struct {
pinningAddress string
blockCount int
smartContractChainValidation bool
apiKey string
}

func showVersion() {
Expand Down Expand Up @@ -485,6 +487,7 @@ func Run(args []string) {
flag.StringVar(&cmd.pinningAddress, "pinningAddress", "", "Pinning address")
flag.IntVar(&cmd.blockCount, "blockCount", 0, "Number of blocks of the tokenchain to validate")
flag.BoolVar(&cmd.smartContractChainValidation, "sctValidation", false, "Validate smart contract token chain")
flag.StringVar(&cmd.apiKey, "apikey", "", "Give the API Key corresponding to the DID")

if len(os.Args) < 2 {
fmt.Println("Invalid Command")
Expand Down Expand Up @@ -664,6 +667,8 @@ func Run(args []string) {
cmd.ValidateTokenchain()
case ValidateTokenCmd:
cmd.ValidateToken()
case AddUserAPIKeyCmd:
cmd.addUserAPIKey()
default:
cmd.log.Error("Invalid command")
}
Expand Down
29 changes: 28 additions & 1 deletion command/explorer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package command

import "fmt"
import (
"fmt"
"regexp"
"strings"
)

func (cmd *Command) addExplorer() {
if len(cmd.links) == 0 {
Expand Down Expand Up @@ -41,3 +45,26 @@ func (cmd *Command) getAllExplorer() {
}
}
}

func (cmd *Command) addUserAPIKey() {
isAlphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(cmd.did)
if !isAlphanumeric {
cmd.log.Error("Invalid DID. Please provide valid DID")
return
}
if !strings.HasPrefix(cmd.did, "bafybmi") || len(cmd.did) != 59 {
cmd.log.Error("Invalid DID")
return
}
if cmd.apiKey == "" {
cmd.log.Error("API Key cannot be empty")
return
}
msg, status := cmd.c.AddUserAPIKey(cmd.did, cmd.apiKey)

if !status {
cmd.log.Error("API Key could not be added, " + msg)
} else {
cmd.log.Info("API Key added successfully")
}
}
2 changes: 1 addition & 1 deletion core/data_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (c *Core) commitDataToken(reqID string, did string, batchID string) *model.
SenderPeerID: c.peerID,
ContractBlock: sc.GetBlock(),
}
td, pl, err := c.initiateConsensus(cr, sc, dc)
td, pl, _, err := c.initiateConsensus(cr, sc, dc)
if err != nil {
c.log.Error("Consensus failed", "err", err)
br.Message = "Consensus failed" + err.Error()
Expand Down
15 changes: 13 additions & 2 deletions core/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ func (c *Core) CreateDID(didCreate *did.DIDCreate) (string, error) {
// if err != nil {
// return "", err
// }
newDID := &ExplorerDID{
PeerID: c.peerID,
DID: did,
Balance: 0,
DIDType: didCreate.Type,
}
if !c.testNet {
c.ec.ExplorerCreateDID(c.peerID, did)
c.ec.ExplorerCreateDID(newDID)
}
return did, nil
}
Expand Down Expand Up @@ -178,7 +184,12 @@ func (c *Core) AddDID(dc *did.DIDCreate) *model.BasicResponse {
br.Message = err.Error()
return br
}
c.ec.ExplorerCreateDID(c.peerID, ds)
newDID := &ExplorerDID{
PeerID: c.peerID,
DID: ds,
DIDType: dc.Type,
}
c.ec.ExplorerCreateDID(newDID)
br.Status = true
br.Message = "DID added successfully"
br.Result = ds
Expand Down
Loading