From d5688f12fe4b9839c2c2c050d134d7016c1dcea8 Mon Sep 17 00:00:00 2001 From: Allen Iype P Cherian Date: Mon, 9 Dec 2024 13:20:36 +0530 Subject: [PATCH 1/2] Add api: /api/fetch-nft and command : fetch-nft --- client/nft.go | 20 +++++++++++++++++ command/command.go | 4 ++++ command/nft.go | 37 +++++++++++++++++++++++++++++++ docs/docs.go | 31 ++++++++++++++++++++++++++ docs/swagger.json | 31 ++++++++++++++++++++++++++ docs/swagger.yaml | 20 +++++++++++++++++ server/nft.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ server/server.go | 1 + setup/setup.go | 1 + 9 files changed, 200 insertions(+) diff --git a/client/nft.go b/client/nft.go index 6b3198fb..1d7bb058 100644 --- a/client/nft.go +++ b/client/nft.go @@ -13,6 +13,11 @@ type CreateNFTReq struct { Artifact string } +type FetchNFTRequest struct { + NFT string + NFTPath string +} + func (c *Client) CreateNFT(createNFTReq *CreateNFTReq) (*model.BasicResponse, error) { fields := make(map[string]string) files := make(map[string]string) @@ -81,3 +86,18 @@ func (c *Client) GetAllNFTs(did string) (*model.NFTTokens, error) { } return &tkns, nil } + +func (c *Client) FetchNFT(fetchNft *FetchNFTRequest) (*model.BasicResponse, error) { + fields := make(map[string]string) + if fetchNft.NFT != "" { + fields["nft"] = fetchNft.NFT + } + + var basicResponse model.BasicResponse + err := c.sendJSONRequest("GET", setup.APIFetchNft, fields, nil, &basicResponse) + if err != nil { + return nil, err + } + return &basicResponse, nil + +} diff --git a/command/command.go b/command/command.go index 73dfeb01..9d66886a 100644 --- a/command/command.go +++ b/command/command.go @@ -97,6 +97,7 @@ const ( DeployNFTCmd string = "deploy-nft" ExecuteNFTCmd string = "execute-nft" SubscribeNFTCmd string = "subscribe-nft" + FetchNft string = "fetch-nft" ) var commands = []string{VersionCmd, @@ -156,6 +157,7 @@ var commands = []string{VersionCmd, DeployNFTCmd, ExecuteNFTCmd, SubscribeNFTCmd, + FetchNft, } var commandsHelp = []string{"To get tool version", @@ -688,6 +690,8 @@ func Run(args []string) { cmd.dumpNFTTokenChain() case SubscribeNFTCmd: cmd.SubscribeNFT() + case FetchNft: + cmd.fetchNFT() default: cmd.log.Error("Invalid command") } diff --git a/command/nft.go b/command/nft.go index 88eb8456..2234955a 100644 --- a/command/nft.go +++ b/command/nft.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/rubixchain/rubixgoplatform/client" + "github.com/rubixchain/rubixgoplatform/core" "github.com/rubixchain/rubixgoplatform/core/model" ) @@ -193,3 +194,39 @@ func (cmd *Command) getAllNFTs() { } cmd.log.Info("Got all NFTs successfully") } + +func (cmd *Command) fetchNFT() { + if cmd.nft == "" { + cmd.log.Info("nft id cannot be empty") + fmt.Print("Enter NFT Token Id : ") + _, err := fmt.Scan(&cmd.nft) + if err != nil { + cmd.log.Error("Failed to get NFT Token ID") + return + } + } + isAlphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(cmd.nft) + + if len(cmd.nft) != 46 || !strings.HasPrefix(cmd.nft, "Qm") || !isAlphanumeric { + cmd.log.Error("Invalid smart contract token") + return + } + nftRequest := core.FetchNFTRequest{ + NFT: cmd.nft, + } + + request := client.FetchNFTRequest{ + NFT: nftRequest.NFT, + } + + basicResponse, err := cmd.c.FetchNFT(&request) + if err != nil { + cmd.log.Error("Failed to fetch nft", "err", err) + return + } + if !basicResponse.Status { + cmd.log.Error("Failed to fetch nft", "err", err) + return + } + cmd.log.Info("NFT fetched successfully") +} diff --git a/docs/docs.go b/docs/docs.go index 0c7513c8..332d95b3 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -368,6 +368,37 @@ const docTemplate = `{ } } }, + "/api/fetch-nft": { + "get": { + "description": "This API will Fetch NFT", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "NFT" + ], + "summary": "Fetch NFT", + "operationId": "fetch-nft", + "parameters": [ + { + "type": "string", + "name": "nft", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.BasicResponse" + } + } + } + } + }, "/api/fetch-smart-contract": { "get": { "description": "This API will Fetch smart contract", diff --git a/docs/swagger.json b/docs/swagger.json index 731f5087..64df6e7c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -359,6 +359,37 @@ } } }, + "/api/fetch-nft": { + "get": { + "description": "This API will Fetch NFT", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "NFT" + ], + "summary": "Fetch NFT", + "operationId": "fetch-nft", + "parameters": [ + { + "type": "string", + "name": "nft", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.BasicResponse" + } + } + } + } + }, "/api/fetch-smart-contract": { "get": { "description": "This API will Fetch smart contract", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 02041c2e..7f0ab000 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -448,6 +448,26 @@ paths: summary: Execute Smart Contract tags: - Smart Contract + /api/fetch-nft: + get: + consumes: + - application/json + description: This API will Fetch NFT + operationId: fetch-nft + parameters: + - in: query + name: nft + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/model.BasicResponse' + summary: Fetch NFT + tags: + - NFT /api/fetch-smart-contract: get: consumes: diff --git a/server/nft.go b/server/nft.go index b318e16d..d8be23a8 100644 --- a/server/nft.go +++ b/server/nft.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "net/http" "os" "path/filepath" @@ -261,3 +262,57 @@ func (s *Server) APISubscribeNFT(request *ensweb.Request) *ensweb.Result { go s.c.SubscribeNFTSetup(request.ID, topic) return s.BasicResponse(request, true, "NFT subscribed successfully", nil) } + +type FetchNFTSwaggoInput struct { + NFT string `json:"nft"` +} + +// NFT godoc +// @Summary Fetch NFT +// @Description This API will Fetch NFT +// @Tags NFT +// @ID fetch-nft +// @Accept json +// @Produce json +// @Param input query FetchNFTSwaggoInput true "Fetch nft" +// @Success 200 {object} model.BasicResponse +// @Router /api/fetch-nft [get] +func (s *Server) APIFetchNft(req *ensweb.Request) *ensweb.Result { + var fetchNft core.FetchNFTRequest + var err error + + fetchNft.NFT = s.GetQuerry(req, "nft") + + is_alphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(fetchNft.NFT) + if len(fetchNft.NFT) != 46 || !strings.HasPrefix(fetchNft.NFT, "Qm") || !is_alphanumeric { + s.log.Error("Invalid nft") + return s.BasicResponse(req, false, "Invalid nft", nil) + } + + fetchNft.NFTPath, err = s.c.CreateSCTempFolder() + if err != nil { + s.log.Error("Fetch nft failed, failed to create nft folder", "err", err) + return s.BasicResponse(req, false, "Fetch nft failed, failed to create nft folder", nil) + } + + fetchNft.NFTPath, err = s.c.RenameSCFolder(fetchNft.NFTPath, fetchNft.NFT) + if err != nil { + s.log.Error("Fetch nft failed, failed to rename nft folder", "err", err) + return s.BasicResponse(req, false, "Fetch nft failed, failed to rename nft folder", nil) + } else { + // The following condition indicates that the Smart Contract directory + // already exists in the node directory + if fetchNft.NFTPath == "" { + s.log.Debug("NFT directory already exists") + return s.BasicResponse(req, true, "NFT directory already exists", nil) + } + } + + s.c.AddWebReq(req) + go func() { + basicResponse := s.c.FetchNFT(req.ID, &fetchNft) + fmt.Printf("Basic Response server: %+v\n", basicResponse.Message) + }() + return s.BasicResponse(req, true, "NFT fetched successfully", nil) + +} diff --git a/server/server.go b/server/server.go index 5aa8703c..c3dfbc1b 100644 --- a/server/server.go +++ b/server/server.go @@ -175,6 +175,7 @@ func (s *Server) RegisterRoutes() { s.AddRoute(setup.APIDumpNFTTokenChain, "GET", s.AuthHandle(s.APIDumpNFTTokenChain, true, s.AuthError, false)) s.AddRoute(setup.APISubscribeNFT, "POST", s.AuthHandle(s.APISubscribeNFT, true, s.AuthError, false)) s.AddRoute(setup.APIGetNFTTokenChainData, "GET", s.AuthHandle(s.APIGetNFTTokenChainData, true, s.AuthError, false)) + s.AddRoute(setup.APIFetchNft, "GET", s.AuthHandle(s.APIFetchNft, true, s.AuthError, false)) } func (s *Server) ExitFunc() error { diff --git a/setup/setup.go b/setup/setup.go index 1d976373..4875ae66 100644 --- a/setup/setup.go +++ b/setup/setup.go @@ -82,6 +82,7 @@ const ( APIDumpNFTTokenChain string = "/api/dump-nft-token-chain" APISubscribeNFT string = "/api/subscribe-nft" APIGetNFTTokenChainData string = "/api/get-nft-token-chain-data" + APIFetchNft string = "/api/fetch-nft" ) // jwt.RegisteredClaims From b84225fa950625ef9586b0f6aa8b33a81927343a Mon Sep 17 00:00:00 2001 From: Allen Iype P Cherian Date: Mon, 9 Dec 2024 14:01:22 +0530 Subject: [PATCH 2/2] Add api: /api/get-nfts-by-did and command get-nfts-by-did --- client/nft.go | 11 +++++++++++ command/command.go | 14 +++++++++----- command/nft.go | 16 ++++++++++++++++ core/nft.go | 25 +++++++++++++++++++++++++ core/wallet/nft.go | 12 +++++++++++- docs/docs.go | 30 ++++++++++++++++++++++++++++++ docs/swagger.json | 30 ++++++++++++++++++++++++++++++ docs/swagger.yaml | 19 +++++++++++++++++++ server/nft.go | 25 ++++++++++++++++++++----- server/server.go | 1 + setup/setup.go | 1 + 11 files changed, 173 insertions(+), 11 deletions(-) diff --git a/client/nft.go b/client/nft.go index 1d7bb058..90154b17 100644 --- a/client/nft.go +++ b/client/nft.go @@ -87,6 +87,17 @@ func (c *Client) GetAllNFTs(did string) (*model.NFTTokens, error) { return &tkns, nil } +func (c *Client) GetNFTsByDid(did string) (*model.NFTTokens, error) { + q := make(map[string]string) + q["did"] = did + var tkns model.NFTTokens + err := c.sendJSONRequest("GET", setup.APIGetNftsByDid, q, nil, &tkns) + if err != nil { + return nil, err + } + return &tkns, nil +} + func (c *Client) FetchNFT(fetchNft *FetchNFTRequest) (*model.BasicResponse, error) { fields := make(map[string]string) if fetchNft.NFT != "" { diff --git a/command/command.go b/command/command.go index 9d66886a..3a643297 100644 --- a/command/command.go +++ b/command/command.go @@ -97,7 +97,8 @@ const ( DeployNFTCmd string = "deploy-nft" ExecuteNFTCmd string = "execute-nft" SubscribeNFTCmd string = "subscribe-nft" - FetchNft string = "fetch-nft" + FetchNftCmd string = "fetch-nft" + GetNftsByDidCmd string = "get-nfts-by-did" ) var commands = []string{VersionCmd, @@ -157,7 +158,8 @@ var commands = []string{VersionCmd, DeployNFTCmd, ExecuteNFTCmd, SubscribeNFTCmd, - FetchNft, + FetchNftCmd, + GetNftsByDidCmd, } var commandsHelp = []string{"To get tool version", @@ -634,8 +636,8 @@ func Run(args []string) { cmd.SubscribeContract() case CreateNFTCmd: cmd.createNFT() - // case GetAllNFTCmd: - // cmd.getAllNFTs() + case GetAllNFTCmd: + cmd.getAllNFTs() case DeploySmartContractCmd: cmd.deploySmartcontract() case GenerateSmartContractToken: @@ -690,8 +692,10 @@ func Run(args []string) { cmd.dumpNFTTokenChain() case SubscribeNFTCmd: cmd.SubscribeNFT() - case FetchNft: + case FetchNftCmd: cmd.fetchNFT() + case GetNftsByDidCmd: + cmd.getNFTsByDid() default: cmd.log.Error("Invalid command") } diff --git a/command/nft.go b/command/nft.go index 2234955a..50fa37d2 100644 --- a/command/nft.go +++ b/command/nft.go @@ -195,6 +195,22 @@ func (cmd *Command) getAllNFTs() { cmd.log.Info("Got all NFTs successfully") } +func (cmd *Command) getNFTsByDid() { + if cmd.did == "" { + cmd.log.Error("Failed to get NFTs, DID is required to get NFTs") + return + } + tkns, err := cmd.c.GetNFTsByDid(cmd.did) + if err != nil { + cmd.log.Error("Failed to get NFTs, " + err.Error()) + return + } + for _, tkn := range tkns.Tokens { + fmt.Printf("NFT : %s, Status : %d\n", tkn.Token, tkn.TokenStatus) + } + cmd.log.Info("Got all NFTs successfully") +} + func (cmd *Command) fetchNFT() { if cmd.nft == "" { cmd.log.Info("nft id cannot be empty") diff --git a/core/nft.go b/core/nft.go index cc2a0e2b..2e2dd04c 100644 --- a/core/nft.go +++ b/core/nft.go @@ -550,3 +550,28 @@ func (c *Core) GetAllNFT() model.NFTList { return response } + +func (c *Core) GetNFTsByDid(did string) model.NFTList { + response := model.NFTList{ + BasicResponse: model.BasicResponse{ + Status: false, + }, + } + nftList, err := c.w.GetNFTsByDid(did) + if err != nil { + errorMsg := fmt.Sprintf("Failed to get NFT list of the did: ", did, "err", err) + c.log.Error(errorMsg) + response.Message = errorMsg + return response + } + nftDetails := make([]model.NFTInfo, 0) + for _, nft := range nftList { + nftDetails = append(nftDetails, model.NFTInfo{NFTId: nft.TokenID, Owner: nft.DID, Value: nft.TokenValue}) + } + response.NFTs = nftDetails + response.Status = true + response.Message = "Got All NFTs" + + return response + +} diff --git a/core/wallet/nft.go b/core/wallet/nft.go index 61a99cdf..f71ffef3 100644 --- a/core/wallet/nft.go +++ b/core/wallet/nft.go @@ -28,7 +28,7 @@ func (w *Wallet) CreateNFT(nt *NFT, local bool) error { return nil } -// GetNFTByDid get all NFTs from db +// GetAllNFT get all NFTs from db func (w *Wallet) GetAllNFT() ([]NFT, error) { var tkns []NFT err := w.s.Read(NFTTokenStorage, &tkns, "token_id != ?", "") @@ -38,6 +38,16 @@ func (w *Wallet) GetAllNFT() ([]NFT, error) { return tkns, nil } +// GetNFTsByDid get all the NFTs of that did from db +func (w *Wallet) GetNFTsByDid(did string) ([]NFT, error) { + var tkns []NFT + err := w.s.Read(NFTTokenStorage, &tkns, "did=?", did) + if err != nil { + return nil, err + } + return tkns, nil +} + // GetNFT get NFT from db func (w *Wallet) GetNFT(did string, nft string, lock bool) (*NFT, error) { var tkns NFT diff --git a/docs/docs.go b/docs/docs.go index 332d95b3..cb3b44e9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -727,6 +727,36 @@ const docTemplate = `{ } } }, + "/api/get-nfts-by-did": { + "get": { + "description": "This API will get all NFTs owned by the particular did", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "NFT" + ], + "summary": "Get NFTs owned by the particular did", + "parameters": [ + { + "type": "string", + "name": "did", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.NFTList" + } + } + } + } + }, "/api/get-pledgedtoken-details": { "get": { "description": "This API allows the user to get details about the tokens the quorums have pledged i.e. which token is pledged for which token state", diff --git a/docs/swagger.json b/docs/swagger.json index 64df6e7c..772816c5 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -718,6 +718,36 @@ } } }, + "/api/get-nfts-by-did": { + "get": { + "description": "This API will get all NFTs owned by the particular did", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "NFT" + ], + "summary": "Get NFTs owned by the particular did", + "parameters": [ + { + "type": "string", + "name": "did", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.NFTList" + } + } + } + } + }, "/api/get-pledgedtoken-details": { "get": { "description": "This API allows the user to get details about the tokens the quorums have pledged i.e. which token is pledged for which token state", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 7f0ab000..b62f9377 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -686,6 +686,25 @@ paths: summary: Get NFT Token Chain Data tags: - NFT + /api/get-nfts-by-did: + get: + consumes: + - application/json + description: This API will get all NFTs owned by the particular did + parameters: + - in: query + name: did + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/model.NFTList' + summary: Get NFTs owned by the particular did + tags: + - NFT /api/get-pledgedtoken-details: get: description: This API allows the user to get details about the tokens the quorums diff --git a/server/nft.go b/server/nft.go index d8be23a8..25223522 100644 --- a/server/nft.go +++ b/server/nft.go @@ -166,6 +166,25 @@ func (s *Server) APIGetAllNFT(req *ensweb.Request) *ensweb.Result { return s.RenderJSON(req, resp, http.StatusOK) } +type GetNFTSwaggoInput struct { + Did string `json:"did"` +} + +// ShowAccount godoc +// @Summary Get NFTs owned by the particular did +// @Description This API will get all NFTs owned by the particular did +// @Tags NFT +// @Accept json +// @Produce json +// @Param input query GetNFTSwaggoInput true "Get nfts by did" +// @Success 200 {object} model.NFTList +// @Router /api/get-nfts-by-did [get] +func (s *Server) APIGetNFTsByDid(req *ensweb.Request) *ensweb.Result { + did := s.GetQuerry(req, "did") + resp := s.c.GetNFTsByDid(did) + return s.RenderJSON(req, resp, http.StatusOK) +} + // ShowAccount godoc // @Summary Add NFTs // @Description This API will put NFTs for sale @@ -263,10 +282,6 @@ func (s *Server) APISubscribeNFT(request *ensweb.Request) *ensweb.Result { return s.BasicResponse(request, true, "NFT subscribed successfully", nil) } -type FetchNFTSwaggoInput struct { - NFT string `json:"nft"` -} - // NFT godoc // @Summary Fetch NFT // @Description This API will Fetch NFT @@ -274,7 +289,7 @@ type FetchNFTSwaggoInput struct { // @ID fetch-nft // @Accept json // @Produce json -// @Param input query FetchNFTSwaggoInput true "Fetch nft" +// @Param input query NewNFTSwaggoInput true "Fetch nft" // @Success 200 {object} model.BasicResponse // @Router /api/fetch-nft [get] func (s *Server) APIFetchNft(req *ensweb.Request) *ensweb.Result { diff --git a/server/server.go b/server/server.go index c3dfbc1b..ca48064a 100644 --- a/server/server.go +++ b/server/server.go @@ -176,6 +176,7 @@ func (s *Server) RegisterRoutes() { s.AddRoute(setup.APISubscribeNFT, "POST", s.AuthHandle(s.APISubscribeNFT, true, s.AuthError, false)) s.AddRoute(setup.APIGetNFTTokenChainData, "GET", s.AuthHandle(s.APIGetNFTTokenChainData, true, s.AuthError, false)) s.AddRoute(setup.APIFetchNft, "GET", s.AuthHandle(s.APIFetchNft, true, s.AuthError, false)) + s.AddRoute(setup.APIGetNftsByDid, "GET", s.AuthHandle(s.APIGetNFTsByDid, true, s.AuthError, false)) } func (s *Server) ExitFunc() error { diff --git a/setup/setup.go b/setup/setup.go index 4875ae66..9d71bc28 100644 --- a/setup/setup.go +++ b/setup/setup.go @@ -83,6 +83,7 @@ const ( APISubscribeNFT string = "/api/subscribe-nft" APIGetNFTTokenChainData string = "/api/get-nft-token-chain-data" APIFetchNft string = "/api/fetch-nft" + APIGetNftsByDid string = "/api/get-nfts-by-did" ) // jwt.RegisteredClaims