Skip to content

Commit

Permalink
Merge branch 'master' into feat-576-request-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain authored Mar 14, 2024
2 parents 5c7bfa3 + a5563e0 commit fe26ba5
Show file tree
Hide file tree
Showing 28 changed files with 435 additions and 80 deletions.
5 changes: 0 additions & 5 deletions actions/access_keys/access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -29,9 +27,6 @@ func (ts *TestSuite) TearDownSuite() {
func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
routes := NewHandler(ts.AppConfig, ts.Services)
routes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
}
Expand Down
4 changes: 0 additions & 4 deletions actions/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
adminRoutes := NewHandler(ts.AppConfig, ts.Services)
adminRoutes.RegisterAdminEndpoints(ts.Router.Group("/" + config.APIVersion))
}
Expand Down
4 changes: 0 additions & 4 deletions actions/base/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -29,8 +27,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
routes := NewHandler()
routes.RegisterBaseEndpoints(ts.Router.Group(""))
}
Expand Down
1 change: 0 additions & 1 deletion actions/contacts/contacts.go

This file was deleted.

4 changes: 0 additions & 4 deletions actions/contacts/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
routes := NewHandler(ts.AppConfig, ts.Services)
routes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
}
Expand Down
18 changes: 13 additions & 5 deletions actions/contacts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
)

Expand All @@ -23,19 +24,26 @@ import (
// @Security bux-auth-xpub
func (a *Action) create(c *gin.Context) {

fullName := c.GetString("full_name")
paymail := c.GetString("paymail")
pubKey := c.GetString("pubKey")
var contact engine.Contact

err := c.ShouldBindJSON(&contact)

if err != nil {
c.JSON(http.StatusUnprocessableEntity, err.Error())
return
}

pubKey := c.GetString(auth.ParamXPubKey)

var requestBody CreateContact

contact, err := a.Services.SpvWalletEngine.NewContact(c.Request.Context(), fullName, paymail, pubKey, engine.WithMetadatas(requestBody.Metadata))
newContact, err := a.Services.SpvWalletEngine.NewContact(c.Request.Context(), contact.FullName, contact.Paymail, pubKey, engine.WithMetadatas(requestBody.Metadata))
if err != nil {
c.JSON(http.StatusUnprocessableEntity, err.Error())
return
}

contract := mappings.MapToContactContract(contact)
contract := mappings.MapToContactContract(newContact)

c.JSON(http.StatusCreated, contract)
}
15 changes: 14 additions & 1 deletion actions/contacts/models.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package contacts

import "github.com/bitcoin-sv/spv-wallet/engine"
import (
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/models"
)

// CreateContact is the model for creating a contact
type CreateContact struct {
Metadata engine.Metadata `json:"metadata"`
}

// UpdateContact is the model for updating a contact
type UpdateContact struct {
XPubID string `json:"xpub_id"`
FullName string `json:"full_name"`
Paymail string `json:"paymail"`
PubKey string `json:"pubKey"`
Status models.ContactStatus `json:"status"`
Metadata engine.Metadata `json:"metadata"`
}
5 changes: 5 additions & 0 deletions actions/contacts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func NewHandler(appConfig *config.AppConfig, services *config.AppServices) route
apiEndpoints := routes.APIEndpointsFunc(func(router *gin.RouterGroup) {
contactGroup := router.Group("/contact")
contactGroup.POST("", action.create)
contactGroup.PATCH("", action.update)

contactsGroup := router.Group("/contacts")
contactsGroup.GET("", action.search)

})

return apiEndpoints
Expand Down
5 changes: 3 additions & 2 deletions actions/contacts/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/stretchr/testify/assert"
)

// TestBaseRegisterRoutes will test routes
func (ts *TestSuite) TestRegisterRoutes() {
// TestContactsRegisterRoutes will test routes
func (ts *TestSuite) TestContactsRegisterRoutes() {
ts.T().Run("test routes", func(t *testing.T) {
testCases := []struct {
method string
url string
}{
{"POST", "/" + config.APIVersion + "/contact"},
{"GET", "/" + config.APIVersion + "/contacts"},
}

ts.Router.Routes()
Expand Down
64 changes: 64 additions & 0 deletions actions/contacts/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package contacts

import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
)

// Search will fetch a list of contacts
// Get contacts godoc
// @Summary Search contacts
// @Description Search contacts
// @Tags Contact
// @Produce json
// @Param page query int false "page"
// @Param page_size query int false "page_size"
// @Param order_by_field query string false "order_by_field"
// @Param sort_direction query string false "sort_direction"
// @Param conditions query string false "conditions"
// @Success 200
// @Router /v1/contacts [get]
// @Security x-auth-xpub
func (a *Action) search(c *gin.Context) {
reqXPubID := c.GetString(auth.ParamXPubHashKey)

params := c.Request.URL.Query()

queryParams, metadata, _, err := actions.GetQueryParameters(c)
if err != nil {
c.JSON(http.StatusExpectationFailed, err.Error())
return
}

dbConditions := make(map[string]interface{})

for key, value := range params {
dbConditions[key] = value
}

dbConditions["xpub_id"] = reqXPubID

var contacts []*engine.Contact
if contacts, err = a.Services.SpvWalletEngine.GetContacts(
c.Request.Context(),
metadata,
&dbConditions,
queryParams,
); err != nil {
c.JSON(http.StatusExpectationFailed, err.Error())
return
}

contactContracts := make([]*models.Contact, 0)
for _, contact := range contacts {
contactContracts = append(contactContracts, mappings.MapToContactContract(contact))
}

c.JSON(http.StatusOK, contactContracts)
}
47 changes: 47 additions & 0 deletions actions/contacts/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package contacts

import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
)

// update will update an existing model
// Update Contact godoc
// @Summary Update contact
// @Description Update contact
// @Tags Contacts
// @Produce json
// @Param metadata body string true "Contacts Metadata"
// @Success 200
// @Router /v1/contact [patch]
// @Security x-auth-xpub
func (a *Action) update(c *gin.Context) {
reqXPubID := c.GetString(auth.ParamXPubHashKey)

var requestBody UpdateContact

if err := c.ShouldBindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

if requestBody.XPubID == "" {
c.JSON(http.StatusBadRequest, "Id is missing")
}

contact, err := a.Services.SpvWalletEngine.UpdateContact(c.Request.Context(), requestBody.FullName, requestBody.PubKey, reqXPubID, requestBody.Paymail, engine.ContactStatus(requestBody.Status), engine.WithMetadatas(requestBody.Metadata))

if err != nil {
c.JSON(http.StatusExpectationFailed, err.Error())
return
}

contract := mappings.MapToContactContract(contact)

c.JSON(http.StatusOK, contract)

}
4 changes: 0 additions & 4 deletions actions/destinations/destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
basicRoutes, apiRoutes := NewHandler(ts.AppConfig, ts.Services)
basicRoutes.RegisterBasicEndpoints(ts.Router.Group("/" + config.APIVersion))
apiRoutes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
Expand Down
4 changes: 0 additions & 4 deletions actions/transactions/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
basicRoutes, apiRoutes, callbackRoutes := NewHandler(ts.AppConfig, ts.Services)
basicRoutes.RegisterBasicEndpoints(ts.Router.Group("/" + config.APIVersion))
apiRoutes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
Expand Down
4 changes: 0 additions & 4 deletions actions/utxos/utxo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
apiRoutes := NewHandler(ts.AppConfig, ts.Services)
apiRoutes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
}
Expand Down
4 changes: 0 additions & 4 deletions actions/xpubs/xpubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,8 +28,6 @@ func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
routes := NewHandler(ts.AppConfig, ts.Services)
routes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
}
Expand Down
7 changes: 4 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"context"
"testing"

"github.com/bitcoin-sv/spv-wallet/logging"
"github.com/mrz1836/go-cachestore"
"github.com/mrz1836/go-datastore"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// newTestConfig will make a new test config
func newTestConfig(t *testing.T) (ac *AppConfig) {
defaultLogger := logging.GetDefaultLogger()
ac, err := Load(defaultLogger)
nop := zerolog.Nop()
ac, err := Load(&nop)

require.NoError(t, err)
require.NotNil(t, ac)
return
Expand Down
Loading

0 comments on commit fe26ba5

Please sign in to comment.