From 59a55c2ac5eba16f95d6531aeffd6591d3b053d1 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Wed, 8 May 2024 07:55:25 +0200 Subject: [PATCH 1/7] adding admin contacts methods --- admin_contacts.go | 29 +++++++++++++++++++++++++ transports/http_admin.go | 47 ++++++++++++++++++++++++++++++++++++++++ transports/interface.go | 5 +++++ 3 files changed, 81 insertions(+) create mode 100644 admin_contacts.go diff --git a/admin_contacts.go b/admin_contacts.go new file mode 100644 index 0000000..7dae43e --- /dev/null +++ b/admin_contacts.go @@ -0,0 +1,29 @@ +package walletclient + +import ( + "context" + + "github.com/bitcoin-sv/spv-wallet/models" + + "github.com/bitcoin-sv/spv-wallet-go-client/transports" +) + +func (wc *WalletClient) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *transports.QueryParams) ([]*models.Contact, transports.ResponseError) { + return wc.transport.AdminGetContacts(ctx, conditions, metadata, queryParams) +} + +func (wc *WalletClient) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminUpdateContact(ctx, id, fullName, metadata) +} + +func (wc *WalletClient) AdminDeleteContact(ctx context.Context, id string) transports.ResponseError { + return wc.transport.AdminDeleteContact(ctx, id) +} + +func (wc *WalletClient) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminAcceptContact(ctx, id) +} + +func (wc *WalletClient) AdminRejectContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminRejectContact(ctx, id) +} diff --git a/transports/http_admin.go b/transports/http_admin.go index 3c9548f..7ae2d7e 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -3,6 +3,7 @@ package transports import ( "context" "encoding/json" + "fmt" "net/http" "github.com/bitcoin-sv/spv-wallet/models" @@ -316,3 +317,49 @@ func (h *TransportHTTP) AdminGetSharedConfig(ctx context.Context) (*models.Share return model, nil } + +func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + FieldConditions: conditions, + FieldMetadata: processMetadata(metadata), + FieldQueryParams: queryParams, + }) + if err != nil { + return nil, WrapError(err) + } + + var contacts []*models.Contact + err = h.doHTTPRequest(ctx, http.MethodPost, "admin/contact/search", jsonStr, h.adminXPriv, h.IsSignRequest(), contacts) + return contacts, WrapError(err) +} + +func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + "fullName": fullName, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, WrapError(err) + } + var contact *models.Contact + err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/%s", id), jsonStr, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} + +func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) ResponseError { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodDelete, "", nil, h.adminXPriv, h.IsSignRequest(), contact) + return WrapError(err) +} + +func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/accepted/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} + +func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/rejected/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} diff --git a/transports/interface.go b/transports/interface.go index 3ae040b..a7ceeea 100644 --- a/transports/interface.go +++ b/transports/interface.go @@ -82,6 +82,11 @@ type AdminService interface { AdminGetXPubsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError) AdminRecordTransaction(ctx context.Context, hex string) (*models.Transaction, ResponseError) AdminGetSharedConfig(ctx context.Context) (*models.SharedConfig, ResponseError) + AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) + AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) + AdminDeleteContact(ctx context.Context, id string) ResponseError + AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) + AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) } // TransportService the transport service interface From 5e6155f7d0c0b93fc36d6b7c4c44a146896ca0c5 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Thu, 9 May 2024 08:30:31 +0200 Subject: [PATCH 2/7] adding admin contacts methods test --- admin_contacts_test.go | 51 ++++++++++++ contacts.go | 3 +- contacts_test.go | 3 +- examples/http/http_admin.go | 158 ++++++++++++++++++++++++++++++++++++ transports/http_admin.go | 11 ++- 5 files changed, 218 insertions(+), 8 deletions(-) create mode 100644 admin_contacts_test.go create mode 100644 examples/http/http_admin.go diff --git a/admin_contacts_test.go b/admin_contacts_test.go new file mode 100644 index 0000000..58f6a4b --- /dev/null +++ b/admin_contacts_test.go @@ -0,0 +1,51 @@ +package walletclient + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" +) + +func TestAdminContacts(t *testing.T) { + tcs := []struct { + name string + route string + responsePayload string + f func(c *WalletClient) error + }{ + { + name: "AdminRejectContact", + route: "admin/contact/rejected/", + responsePayload: "{}", + f: func(c *WalletClient) error { + _, err := c.AdminRejectContact(context.Background(), "admin") + return err + }, + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + // given + tmq := testTransportHandler{ + Type: fixtures.RequestType, + Path: tc.route, + Result: tc.responsePayload, + ClientURL: fixtures.ServerURL, + Client: WithHTTPClient, + } + + client := getTestWalletClientWithOpts(tmq, WithXPriv(fixtures.XPrivString)) + + // when + err := tc.f(client) + + // then + assert.NoError(t, err) + }) + } + +} diff --git a/contacts.go b/contacts.go index 989270e..e1e47e5 100644 --- a/contacts.go +++ b/contacts.go @@ -5,8 +5,9 @@ import ( "errors" "fmt" - "github.com/bitcoin-sv/spv-wallet-go-client/transports" "github.com/bitcoin-sv/spv-wallet/models" + + "github.com/bitcoin-sv/spv-wallet-go-client/transports" ) // UpsertContact add or update contact. When adding a new contact, the system utilizes Paymail's PIKE capability to dispatch an invitation request, asking the counterparty to include the current user in their contacts. diff --git a/contacts_test.go b/contacts_test.go index 3ed81fc..08d87a6 100644 --- a/contacts_test.go +++ b/contacts_test.go @@ -5,9 +5,10 @@ import ( "strings" "testing" - "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" ) // TestContactActionsRouting will test routing diff --git a/examples/http/http_admin.go b/examples/http/http_admin.go new file mode 100644 index 0000000..7326a2e --- /dev/null +++ b/examples/http/http_admin.go @@ -0,0 +1,158 @@ +package main + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/bitcoin-sv/spv-wallet/models" + + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" + "github.com/bitcoin-sv/spv-wallet-go-client/transports" + "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" +) + +const serverURL = "http://localhost:3003/v1" +const adminKey = "you can get admin key from spv-wallet default settings" + +func main() { + + // Create a clientAdmin + clientAdmin, _ := walletclient.New( + walletclient.WithXPriv(adminKey), + walletclient.WithAdminKey(adminKey), + walletclient.WithHTTP(serverURL), + walletclient.WithSignRequest(true), + ) + log.Println("is sign req: ", clientAdmin.IsSignRequest()) + ctx := context.Background() + + aliceName := "alice" + bobName := "bob" + aliceClient, _ := createUser(ctx, aliceName) + bobClient, bobPaymail := createUser(ctx, bobName) + + if _, err := aliceClient.UpsertContact(ctx, bobPaymail, bobName, nil); err != nil { + panic(err) + } + + fmt.Println() + log.Println(" **** Admin Get Contacts ****") + c, err := clientAdmin.AdminGetContacts(ctx, nil, nil, &transports.QueryParams{}) + if err != nil { + log.Printf("admin error for get contacts - %v", err) + } + if len(c) == 0 { + log.Printf("empty list of contacts") + return + } + log.Printf("admin got contacts: %#v\n", c) + + fmt.Println() + log.Println(" **** Admin Update Contact ****") + bobToUpdate := c[0] + log.Printf("Bob before update [%v]", c[0].FullName) + updatedContact, err := clientAdmin.AdminUpdateContact(ctx, bobToUpdate.ID, fmt.Sprintf("%s %s", bobName, time.Now().Local().String()), nil) + if err != nil { + log.Panicf("error updating contact id: [%s] name: [%s] - %s", bobToUpdate.ID, bobToUpdate.FullName, err) + } + log.Printf("updated contact full name [%v]", updatedContact.FullName) + + fmt.Println() + log.Println(" **** Admin Reject Contact ****") + aliceContacts, err := bobClient.GetContacts(ctx, nil, nil, nil) + if err != nil { + log.Printf("admin error for get contacts - %v", err) + } + if len(aliceContacts) == 0 { + log.Printf("empty list of contacts") + return + } + log.Printf("status should be awaiting == [%v]", aliceContacts[0].Status) + + rejectAliceContact, err := clientAdmin.AdminRejectContact(ctx, aliceContacts[0].ID) + if err != nil { + log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) + } + log.Printf("status should change to rejected == [%v]", rejectAliceContact.Status) + + fmt.Println() + log.Println(" **** Admin Accept Contact ****") + if _, err := aliceClient.UpsertContact(ctx, bobPaymail, bobName, nil); err != nil { + panic(err) + } + + aliceContacts, err = bobClient.GetContacts(ctx, nil, nil, nil) + if err != nil { + log.Printf("admin error for get contacts - %v", err) + } + if len(aliceContacts) == 0 { + log.Printf("empty list of contacts") + return + } + + log.Printf("status should be awaiting == [%v]", aliceContacts[0].Status) + acceptAliceContact, err := clientAdmin.AdminAcceptContact(ctx, aliceContacts[0].ID) + if err != nil { + log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) + } + log.Printf("status should change to unconfirmed == [%v]", acceptAliceContact.Status) + + fmt.Println() + log.Println(" **** Admin Delete Contact ****") + + err = clientAdmin.AdminDeleteContact(ctx, aliceContacts[0].ID) + if err != nil { + log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) + } + if len(aliceContacts) > 0 { + log.Println("removed id:", aliceContacts[0].ID) + } else { + log.Println("alice contacts are empty") + } + aliceContacts, err = bobClient.GetContacts(ctx, nil, nil, nil) + if err != nil { + log.Printf("admin error for get contacts - %v", err) + } + if len(aliceContacts) == 0 { + log.Printf("empty list of contacts") + } + for _, c := range aliceContacts { + log.Println("alice contacts id:", c.ID, c.FullName, c.DeletedAt) + } +} + +func createUser(ctx context.Context, name string) (*walletclient.WalletClient, string) { + keys, _ := xpriv.Generate() + + timestamp := time.Now().UnixMicro() + examplePaymail := fmt.Sprintf("contacttest_%d_%s@auggie.4chain.space", timestamp, name) + + adminClient, _ := walletclient.New( + walletclient.WithXPriv(adminKey), + walletclient.WithAdminKey(adminKey), + walletclient.WithHTTP(serverURL), + walletclient.WithSignRequest(true), + ) + + metadata := make(models.Metadata) + metadata["name"] = name + + err := adminClient.AdminNewXpub(ctx, keys.XPub().String(), &metadata) + if err != nil { + panic(err) + } + _, err = adminClient.AdminCreatePaymail(context.Background(), keys.XPub().String(), examplePaymail, name, "") + if err != nil { + panic(err) + } + + userClient, _ := walletclient.New( + walletclient.WithXPriv(keys.XPriv()), + walletclient.WithHTTP(serverURL), + walletclient.WithSignRequest(true), + ) + + return userClient, examplePaymail +} diff --git a/transports/http_admin.go b/transports/http_admin.go index 7ae2d7e..1eb8f6e 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -329,7 +329,7 @@ func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[str } var contacts []*models.Contact - err = h.doHTTPRequest(ctx, http.MethodPost, "admin/contact/search", jsonStr, h.adminXPriv, h.IsSignRequest(), contacts) + err = h.doHTTPRequest(ctx, http.MethodPost, "/admin/contact/search", jsonStr, h.adminXPriv, true, &contacts) return contacts, WrapError(err) } @@ -342,24 +342,23 @@ func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName str return nil, WrapError(err) } var contact *models.Contact - err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/%s", id), jsonStr, h.adminXPriv, h.IsSignRequest(), contact) + err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/%s", id), jsonStr, h.adminXPriv, true, &contact) return contact, WrapError(err) } func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) ResponseError { - var contact *models.Contact - err := h.doHTTPRequest(ctx, http.MethodDelete, "", nil, h.adminXPriv, h.IsSignRequest(), contact) + err := h.doHTTPRequest(ctx, http.MethodDelete, fmt.Sprintf("/admin/contact/%s", id), nil, h.adminXPriv, true, nil) return WrapError(err) } func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { var contact *models.Contact - err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/accepted/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/accepted/%s", id), nil, h.adminXPriv, true, &contact) return contact, WrapError(err) } func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { var contact *models.Contact - err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/rejected/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/rejected/%s", id), nil, h.adminXPriv, true, &contact) return contact, WrapError(err) } From c39965b93a71f2909fc31d7c376250d906ad2877 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Thu, 9 May 2024 08:33:10 +0200 Subject: [PATCH 3/7] removed unit test --- admin_contacts_test.go | 51 ------------------------------------------ 1 file changed, 51 deletions(-) delete mode 100644 admin_contacts_test.go diff --git a/admin_contacts_test.go b/admin_contacts_test.go deleted file mode 100644 index 58f6a4b..0000000 --- a/admin_contacts_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package walletclient - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" -) - -func TestAdminContacts(t *testing.T) { - tcs := []struct { - name string - route string - responsePayload string - f func(c *WalletClient) error - }{ - { - name: "AdminRejectContact", - route: "admin/contact/rejected/", - responsePayload: "{}", - f: func(c *WalletClient) error { - _, err := c.AdminRejectContact(context.Background(), "admin") - return err - }, - }, - } - - for _, tc := range tcs { - t.Run(tc.name, func(t *testing.T) { - // given - tmq := testTransportHandler{ - Type: fixtures.RequestType, - Path: tc.route, - Result: tc.responsePayload, - ClientURL: fixtures.ServerURL, - Client: WithHTTPClient, - } - - client := getTestWalletClientWithOpts(tmq, WithXPriv(fixtures.XPrivString)) - - // when - err := tc.f(client) - - // then - assert.NoError(t, err) - }) - } - -} From ab6798fc7f9c06d1f47ff0fcc590d1884c63b71c Mon Sep 17 00:00:00 2001 From: ac4ch Date: Thu, 9 May 2024 08:36:20 +0200 Subject: [PATCH 4/7] refactored example for admins contacts --- examples/{http => http_admin}/http_admin.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{http => http_admin}/http_admin.go (100%) diff --git a/examples/http/http_admin.go b/examples/http_admin/http_admin.go similarity index 100% rename from examples/http/http_admin.go rename to examples/http_admin/http_admin.go From 9bc226f33b3c95b2af6993f1151540a211e99e7c Mon Sep 17 00:00:00 2001 From: ac4ch Date: Fri, 10 May 2024 04:25:05 +0200 Subject: [PATCH 5/7] removing example --- examples/http_admin/http_admin.go | 158 ------------------------------ 1 file changed, 158 deletions(-) delete mode 100644 examples/http_admin/http_admin.go diff --git a/examples/http_admin/http_admin.go b/examples/http_admin/http_admin.go deleted file mode 100644 index 7326a2e..0000000 --- a/examples/http_admin/http_admin.go +++ /dev/null @@ -1,158 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "time" - - "github.com/bitcoin-sv/spv-wallet/models" - - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "github.com/bitcoin-sv/spv-wallet-go-client/transports" - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" -) - -const serverURL = "http://localhost:3003/v1" -const adminKey = "you can get admin key from spv-wallet default settings" - -func main() { - - // Create a clientAdmin - clientAdmin, _ := walletclient.New( - walletclient.WithXPriv(adminKey), - walletclient.WithAdminKey(adminKey), - walletclient.WithHTTP(serverURL), - walletclient.WithSignRequest(true), - ) - log.Println("is sign req: ", clientAdmin.IsSignRequest()) - ctx := context.Background() - - aliceName := "alice" - bobName := "bob" - aliceClient, _ := createUser(ctx, aliceName) - bobClient, bobPaymail := createUser(ctx, bobName) - - if _, err := aliceClient.UpsertContact(ctx, bobPaymail, bobName, nil); err != nil { - panic(err) - } - - fmt.Println() - log.Println(" **** Admin Get Contacts ****") - c, err := clientAdmin.AdminGetContacts(ctx, nil, nil, &transports.QueryParams{}) - if err != nil { - log.Printf("admin error for get contacts - %v", err) - } - if len(c) == 0 { - log.Printf("empty list of contacts") - return - } - log.Printf("admin got contacts: %#v\n", c) - - fmt.Println() - log.Println(" **** Admin Update Contact ****") - bobToUpdate := c[0] - log.Printf("Bob before update [%v]", c[0].FullName) - updatedContact, err := clientAdmin.AdminUpdateContact(ctx, bobToUpdate.ID, fmt.Sprintf("%s %s", bobName, time.Now().Local().String()), nil) - if err != nil { - log.Panicf("error updating contact id: [%s] name: [%s] - %s", bobToUpdate.ID, bobToUpdate.FullName, err) - } - log.Printf("updated contact full name [%v]", updatedContact.FullName) - - fmt.Println() - log.Println(" **** Admin Reject Contact ****") - aliceContacts, err := bobClient.GetContacts(ctx, nil, nil, nil) - if err != nil { - log.Printf("admin error for get contacts - %v", err) - } - if len(aliceContacts) == 0 { - log.Printf("empty list of contacts") - return - } - log.Printf("status should be awaiting == [%v]", aliceContacts[0].Status) - - rejectAliceContact, err := clientAdmin.AdminRejectContact(ctx, aliceContacts[0].ID) - if err != nil { - log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) - } - log.Printf("status should change to rejected == [%v]", rejectAliceContact.Status) - - fmt.Println() - log.Println(" **** Admin Accept Contact ****") - if _, err := aliceClient.UpsertContact(ctx, bobPaymail, bobName, nil); err != nil { - panic(err) - } - - aliceContacts, err = bobClient.GetContacts(ctx, nil, nil, nil) - if err != nil { - log.Printf("admin error for get contacts - %v", err) - } - if len(aliceContacts) == 0 { - log.Printf("empty list of contacts") - return - } - - log.Printf("status should be awaiting == [%v]", aliceContacts[0].Status) - acceptAliceContact, err := clientAdmin.AdminAcceptContact(ctx, aliceContacts[0].ID) - if err != nil { - log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) - } - log.Printf("status should change to unconfirmed == [%v]", acceptAliceContact.Status) - - fmt.Println() - log.Println(" **** Admin Delete Contact ****") - - err = clientAdmin.AdminDeleteContact(ctx, aliceContacts[0].ID) - if err != nil { - log.Panicf("contact not accepted id: %v - %s", aliceContacts[0].ID, err) - } - if len(aliceContacts) > 0 { - log.Println("removed id:", aliceContacts[0].ID) - } else { - log.Println("alice contacts are empty") - } - aliceContacts, err = bobClient.GetContacts(ctx, nil, nil, nil) - if err != nil { - log.Printf("admin error for get contacts - %v", err) - } - if len(aliceContacts) == 0 { - log.Printf("empty list of contacts") - } - for _, c := range aliceContacts { - log.Println("alice contacts id:", c.ID, c.FullName, c.DeletedAt) - } -} - -func createUser(ctx context.Context, name string) (*walletclient.WalletClient, string) { - keys, _ := xpriv.Generate() - - timestamp := time.Now().UnixMicro() - examplePaymail := fmt.Sprintf("contacttest_%d_%s@auggie.4chain.space", timestamp, name) - - adminClient, _ := walletclient.New( - walletclient.WithXPriv(adminKey), - walletclient.WithAdminKey(adminKey), - walletclient.WithHTTP(serverURL), - walletclient.WithSignRequest(true), - ) - - metadata := make(models.Metadata) - metadata["name"] = name - - err := adminClient.AdminNewXpub(ctx, keys.XPub().String(), &metadata) - if err != nil { - panic(err) - } - _, err = adminClient.AdminCreatePaymail(context.Background(), keys.XPub().String(), examplePaymail, name, "") - if err != nil { - panic(err) - } - - userClient, _ := walletclient.New( - walletclient.WithXPriv(keys.XPriv()), - walletclient.WithHTTP(serverURL), - walletclient.WithSignRequest(true), - ) - - return userClient, examplePaymail -} From 8f5c980b04fab30ee4e747acd5419c5f0db77c00 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Fri, 10 May 2024 04:33:06 +0200 Subject: [PATCH 6/7] adding comments to methods --- admin_contacts.go | 5 +++++ transports/http_admin.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/admin_contacts.go b/admin_contacts.go index 7dae43e..e8c2d53 100644 --- a/admin_contacts.go +++ b/admin_contacts.go @@ -8,22 +8,27 @@ import ( "github.com/bitcoin-sv/spv-wallet-go-client/transports" ) +// AdminGetContacts retrieves a list of contacts based on the provided conditions, metadata, and query parameters. func (wc *WalletClient) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *transports.QueryParams) ([]*models.Contact, transports.ResponseError) { return wc.transport.AdminGetContacts(ctx, conditions, metadata, queryParams) } +// AdminUpdateContact updates a contact's details such as their full name using the specified contact ID and metadata. func (wc *WalletClient) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) { return wc.transport.AdminUpdateContact(ctx, id, fullName, metadata) } +// AdminDeleteContact removes a contact from the system using the specified contact ID. func (wc *WalletClient) AdminDeleteContact(ctx context.Context, id string) transports.ResponseError { return wc.transport.AdminDeleteContact(ctx, id) } +// AdminAcceptContact marks a contact as accepted using the specified contact ID. func (wc *WalletClient) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { return wc.transport.AdminAcceptContact(ctx, id) } +// AdminRejectContact marks a contact as rejected using the specified contact ID. func (wc *WalletClient) AdminRejectContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { return wc.transport.AdminRejectContact(ctx, id) } diff --git a/transports/http_admin.go b/transports/http_admin.go index 1eb8f6e..21bf02a 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -318,6 +318,7 @@ func (h *TransportHTTP) AdminGetSharedConfig(ctx context.Context) (*models.Share return model, nil } +// AdminGetContacts executes an HTTP POST request to search for contacts based on specified conditions, metadata, and query parameters. func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) { jsonStr, err := json.Marshal(map[string]interface{}{ FieldConditions: conditions, @@ -333,6 +334,7 @@ func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[str return contacts, WrapError(err) } +// AdminUpdateContact executes an HTTP PATCH request to update a specific contact's full name using their ID. func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) { jsonStr, err := json.Marshal(map[string]interface{}{ "fullName": fullName, @@ -346,17 +348,20 @@ func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName str return contact, WrapError(err) } +// AdminDeleteContact executes an HTTP DELETE request to remove a contact using their ID. func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) ResponseError { err := h.doHTTPRequest(ctx, http.MethodDelete, fmt.Sprintf("/admin/contact/%s", id), nil, h.adminXPriv, true, nil) return WrapError(err) } +// AdminAcceptContact executes an HTTP PATCH request to mark a contact as accepted using their ID. func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { var contact *models.Contact err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/accepted/%s", id), nil, h.adminXPriv, true, &contact) return contact, WrapError(err) } +// AdminRejectContact executes an HTTP PATCH request to mark a contact as rejected using their ID. func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { var contact *models.Contact err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/rejected/%s", id), nil, h.adminXPriv, true, &contact) From e35b5473e5fb27715819abe7e960be5a9ca47fb4 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Fri, 10 May 2024 08:09:37 +0200 Subject: [PATCH 7/7] removing pointer in var declaration as we need to retun it anyway --- transports/http_admin.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/transports/http_admin.go b/transports/http_admin.go index 21bf02a..02ab6b1 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -343,9 +343,9 @@ func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName str if err != nil { return nil, WrapError(err) } - var contact *models.Contact + var contact models.Contact err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/%s", id), jsonStr, h.adminXPriv, true, &contact) - return contact, WrapError(err) + return &contact, WrapError(err) } // AdminDeleteContact executes an HTTP DELETE request to remove a contact using their ID. @@ -356,14 +356,14 @@ func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) Respo // AdminAcceptContact executes an HTTP PATCH request to mark a contact as accepted using their ID. func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { - var contact *models.Contact + var contact models.Contact err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/accepted/%s", id), nil, h.adminXPriv, true, &contact) - return contact, WrapError(err) + return &contact, WrapError(err) } // AdminRejectContact executes an HTTP PATCH request to mark a contact as rejected using their ID. func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { - var contact *models.Contact + var contact models.Contact err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/rejected/%s", id), nil, h.adminXPriv, true, &contact) - return contact, WrapError(err) + return &contact, WrapError(err) }