From 535d3da891bba5d72a9b139e10fbf2675ffd98ba Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Tue, 4 Jun 2024 10:47:51 -0400 Subject: [PATCH] add API endpoint --- api/client.go | 8 +++++++- api/server.go | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api/client.go b/api/client.go index 370a14ee..f92a88b7 100644 --- a/api/client.go +++ b/api/client.go @@ -106,7 +106,7 @@ func (c *Client) AddressBalance(address types.Address) (resp AddressBalanceRespo // Contract returns the file contract with the specified ID. func (c *Client) Contract(id types.FileContractID) (resp explorer.FileContract, err error) { - err = c.c.GET(fmt.Sprintf("/explorer/contracts/%s", id), &resp) + err = c.c.GET(fmt.Sprintf("/explorer/contracts/id/%s", id), &resp) return } @@ -116,6 +116,12 @@ func (c *Client) Contracts(ids []types.FileContractID) (resp []explorer.FileCont return } +// ContractsKey returns the contracts for a particular ed25519 key. +func (c *Client) ContractsKey(key types.UnlockKey) (resp []explorer.FileContract, err error) { + err = c.c.GET(fmt.Sprintf("/explorer/contracts/key/%s", key), &resp) + return +} + // Metrics returns the most recent metrics about Sia. func (c *Client) Metrics() (resp explorer.Metrics, err error) { err = c.c.GET("/explorer/metrics", &resp) diff --git a/api/server.go b/api/server.go index 3529cb39..62ddc485 100644 --- a/api/server.go +++ b/api/server.go @@ -51,6 +51,7 @@ type ( UnspentSiafundOutputs(address types.Address, offset, limit uint64) ([]explorer.SiafundOutput, error) AddressEvents(address types.Address, offset, limit uint64) (events []explorer.Event, err error) Contracts(ids []types.FileContractID) (result []explorer.FileContract, err error) + ContractsKey(key types.UnlockKey) (result []explorer.FileContract, err error) } ) @@ -305,6 +306,23 @@ func (s *server) explorerContractIDHandler(jc jape.Context) { jc.Encode(fcs[0]) } +func (s *server) explorerContractKeyHandler(jc jape.Context) { + errNotFound := errors.New("no contract found") + + var key types.UnlockKey + if jc.DecodeParam("key", &key) != nil { + return + } + fcs, err := s.e.ContractsKey(key) + if jc.Check("failed to get contracts", err) != nil { + return + } else if len(fcs) == 0 { + jc.Error(errNotFound, http.StatusNotFound) + return + } + jc.Encode(fcs) +} + func (s *server) explorerContractsHandler(jc jape.Context) { var ids []types.FileContractID if jc.Decode(&ids) != nil { @@ -346,7 +364,8 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler { "GET /explorer/addresses/:address/utxos": srv.explorerAddressessAddressUtxosHandler, "GET /explorer/addresses/:address/events": srv.explorerAddressessAddressEventsHandler, "GET /explorer/addresses/:address/balance": srv.explorerAddressessAddressBalanceHandler, - "GET /explorer/contracts/:id": srv.explorerContractIDHandler, + "GET /explorer/contracts/id/:id": srv.explorerContractIDHandler, + "GET /explorer/contracts/key/:key": srv.explorerContractKeyHandler, "POST /explorer/contracts": srv.explorerContractsHandler, }) }