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

Open
wants to merge 16 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ func (b *Block) GetCommitedTokenDetials(t string) ([]string, error) {
// if !ok {
// return nil
// }

// result := make(map[string]interface{})
// for k, v := range tokenPledgeMap {
// kStr, kOk := k.(string)
Expand All @@ -718,7 +717,6 @@ func (b *Block) GetCommitedTokenDetials(t string) ([]string, error) {
// }
// result[kStr] = v
// }

// return result
// }

Expand Down Expand Up @@ -826,3 +824,16 @@ func (b *Block) CalculateBlockHash() (string, error) {

return blockHash, nil
}

func (b *Block) GetTokenLevel(token string) (int, int) {
gtm := b.getGenesisTokenMap(token)
tokenLevel := util.GetIntFromMap(gtm, GITokenLevelKey)
tokenNum := util.GetIntFromMap(gtm, GITokenNumberKey)
return tokenLevel, tokenNum
}

func (b *Block) GetPledgedTokens() {
pledgedInfo := util.GetFromMap(b.bm, TCPledgeDetailsKey)
fmt.Println(pledgedInfo)
// return
}
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
}
14 changes: 14 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
SubscribeNFTCmd string = "subscribe-nft"
FetchNftCmd string = "fetch-nft"
GetNftsByDidCmd string = "get-nfts-by-did"
AddUserAPIKeyCmd string = "adduserapikey"
)

var commands = []string{VersionCmd,
Expand Down Expand Up @@ -321,6 +322,7 @@ type Command struct {
ftName string
ftCount int
creatorDID string
apiKey string
}

func showVersion() {
Expand Down Expand Up @@ -404,6 +406,14 @@ func (cmd *Command) runApp() {
cmd.log.Info("Core version : " + version)
cmd.log.Info("Starting server...")
go s.Start()
cmd.log.Info("Syncing Details...")
dids := c.ExplorerUserCreate() //Checking if all the DIDs are in the ExplorerUserDetailtable or not.
if len(dids) != 0 {
c.UpdateUserInfo(dids) //Updating the balance
c.GenerateUserAPIKey(dids) //Regenerating the API Key for DID
}
// c.UpdateTokenInfo()
cmd.log.Info("Syncing Complete...")

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
Expand All @@ -414,6 +424,7 @@ func (cmd *Command) runApp() {
}
s.Shutdown()
cmd.log.Info("Shutting down...")
c.ExpireUserAPIKey()
}

func (cmd *Command) validateOptions() bool {
Expand Down Expand Up @@ -528,6 +539,7 @@ func Run(args []string) {
flag.StringVar(&cmd.ftName, "ftName", "", "Name of FT to be created")
flag.IntVar(&cmd.ftCount, "ftCount", 0, "Number of FTs to be created")
flag.StringVar(&cmd.creatorDID, "creatorDID", "", "DID of creator of FT")
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 @@ -727,6 +739,8 @@ func Run(args []string) {
cmd.fetchNFT()
case GetNftsByDidCmd:
cmd.getNFTsByDid()
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
25 changes: 12 additions & 13 deletions core/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,13 @@ func (c *Core) CreateDID(didCreate *did.DIDCreate) (string, error) {
c.log.Error("Failed to create did in the wallet", "err", err)
return "", err
}
// exp := model.ExploreModel{
// Cmd: ExpDIDPeerMapCmd,
// DIDList: []string{did},
// PeerID: c.peerID,
// Message: "DID Created Successfully",
// }
// err = c.PublishExplorer(&exp)
// if err != nil {
// return "", err
// }
if !c.testNet {
c.ec.ExplorerCreateDID(c.peerID, did)
newDID := &ExplorerDID{
PeerID: c.peerID,
DID: did,
Balance: 0,
DIDType: didCreate.Type,
}
c.ec.ExplorerUserCreate(newDID)
return did, nil
}

Expand Down Expand Up @@ -178,7 +172,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.ExplorerUserCreate(newDID)
br.Status = true
br.Message = "DID added successfully"
br.Result = ds
Expand Down
Loading
Loading