Skip to content

Commit

Permalink
Merge branch 'BUX-705/Gitbook' of https://github.com/bitcoin-sv/spv-w…
Browse files Browse the repository at this point in the history
…allet-go-client into BUX-705/Gitbook
  • Loading branch information
Nazarii-4chain committed Apr 11, 2024
2 parents 1374d3e + 2abf260 commit 644afb3
Show file tree
Hide file tree
Showing 14 changed files with 529 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
updates:
# Maintain dependencies for the core library
- package-ecosystem: "gomod"
target-branch: "master"
target-branch: "main"
directory: "/"
schedule:
interval: "daily"
Expand All @@ -19,7 +19,7 @@ updates:

# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
target-branch: "master"
target-branch: "main"
directory: "/"
schedule:
interval: "weekly"
Expand Down
2 changes: 1 addition & 1 deletion .github/mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pull_request_rules:

- name: Close stale pull request
conditions:
- base=master
- base=main
- -closed
- updated-at<21 days ago
actions:
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ name: "CodeQL"

on:
push:
branches: [master]
branches:
- master
- main
pull_request:
# The branches below must be a subset of the branches above
branches: [master]
branches:
- master
- main
# schedule:
# - cron: '0 23 * * 0'

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/sync-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- master
- main
paths:
- .github/labels.yml
jobs:
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# SPV Wallet: Go Client

[![Release](https://img.shields.io/github/release-pre/bitcoin-sv/spv-wallet-go-client.svg?logo=github&style=flat&v=2)](https://github.com/bitcoin-sv/spv-wallet-go-client/releases)
[![Build Status](https://img.shields.io/github/actions/workflow/status/bitcoin-sv/spv-wallet-go-client/run-tests.yml?branch=master&v=2)](https://github.com/bitcoin-sv/spv-wallet-go-client/actions)
[![Build Status](https://img.shields.io/github/actions/workflow/status/bitcoin-sv/spv-wallet-go-client/run-tests.yml?branch=main&v=2)](https://github.com/bitcoin-sv/spv-wallet-go-client/actions)
[![Report](https://goreportcard.com/badge/github.com/bitcoin-sv/spv-wallet-go-client?style=flat&v=2)](https://goreportcard.com/report/github.com/bitcoin-sv/spv-wallet-go-client)
[![codecov](https://codecov.io/gh/bitcoin-sv/spv-wallet-go-client/branch/master/graph/badge.svg?v=2)](https://codecov.io/gh/bitcoin-sv/spv-wallet-go-client)
[![codecov](https://codecov.io/gh/bitcoin-sv/spv-wallet-go-client/branch/main/graph/badge.svg?v=2)](https://codecov.io/gh/bitcoin-sv/spv-wallet-go-client)
[![Mergify Status](https://img.shields.io/endpoint.svg?url=https://api.mergify.com/v1/badges/bitcoin-sv/spv-wallet-go-client&style=flat&v=2)](https://mergify.io)
<br>

Expand All @@ -14,6 +14,9 @@
[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat&v=2)](https://github.com/RichardLitt/standard-readme)
[![Makefile Included](https://img.shields.io/badge/Makefile-Supported%20-brightgreen?=flat&logo=probot&v=2)](Makefile)

## Documentation

For in-depth information and guidance, please refer to the [SPV Wallet Documentation](https://bsvblockchain.gitbook.io/docs).
<br/>
</div>

Expand Down
49 changes: 49 additions & 0 deletions contacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package walletclient

import (
"context"
"errors"
"fmt"

"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/bitcoin-sv/spv-wallet/models"
)

// 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.
func (b *WalletClient) UpsertContact(ctx context.Context, paymail, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) {
return b.transport.UpsertContact(ctx, paymail, fullName, metadata, "")
}

// UpsertContactForPaymail 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 specified paymail in their contacts.
func (b *WalletClient) UpsertContactForPaymail(ctx context.Context, paymail, fullName string, metadata *models.Metadata, requesterPaymail string) (*models.Contact, transports.ResponseError) {
return b.transport.UpsertContact(ctx, paymail, fullName, metadata, requesterPaymail)
}

// AcceptContact will accept the contact associated with the paymail
func (b *WalletClient) AcceptContact(ctx context.Context, paymail string) transports.ResponseError {
return b.transport.AcceptContact(ctx, paymail)
}

// RejectContact will reject the contact associated with the paymail
func (b *WalletClient) RejectContact(ctx context.Context, paymail string) transports.ResponseError {
return b.transport.RejectContact(ctx, paymail)
}

// ConfirmContact will try to confirm the contact
func (b *WalletClient) ConfirmContact(ctx context.Context, contact *models.Contact, passcode string, period, digits uint) transports.ResponseError {
isTotpValid, err := b.ValidateTotpForContact(contact, passcode, period, digits)
if err != nil {
return transports.WrapError(fmt.Errorf("totp validation failed: %w", err))
}

if !isTotpValid {
return transports.WrapError(errors.New("totp is invalid"))
}

return b.transport.ConfirmContact(ctx, contact.Paymail)
}

// GetContacts will get contacts by conditions
func (b *WalletClient) GetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *transports.QueryParams) ([]*models.Contact, transports.ResponseError) {
return b.transport.GetContacts(ctx, conditions, metadata, queryParams)
}
145 changes: 145 additions & 0 deletions contacts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package walletclient

import (
"context"
"testing"

"github.com/bitcoin-sv/spv-wallet-go-client/fixtures"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestContactActionsRouting will test routing
func TestContactActionsRouting(t *testing.T) {
tcs := []struct {
name string
route string
responsePayload string
f func(c *WalletClient) error
}{
{
name: "RejectContact",
route: "/contact/rejected/",
responsePayload: "{}",
f: func(c *WalletClient) error { return c.RejectContact(context.Background(), fixtures.PaymailAddress) },
},
{
name: "AcceptContact",
route: "/contact/accepted/",
responsePayload: "{}",
f: func(c *WalletClient) error { return c.AcceptContact(context.Background(), fixtures.PaymailAddress) },
},
{
name: "GetContacts",
route: "/contact/search/",
responsePayload: "[]",
f: func(c *WalletClient) error {
_, err := c.GetContacts(context.Background(), nil, nil, nil)
return err
},
},
{
name: "UpsertContact",
route: "/contact/",
responsePayload: "{}",
f: func(c *WalletClient) error {
_, err := c.UpsertContact(context.Background(), "", "", nil)
return err
},
},
{
name: "UpsertContactForPaymail",
route: "/contact/",
responsePayload: "{}",
f: func(c *WalletClient) error {
_, err := c.UpsertContactForPaymail(context.Background(), "", "", nil, "")
return err
},
},
{
name: "ConfirmContact",
route: "/contact/confirmed/",
responsePayload: "{}",
f: func(c *WalletClient) error {
contact := models.Contact{PubKey: fixtures.PubKey}

passcode, _ := c.GenerateTotpForContact(&contact, 30, 2)
return c.ConfirmContact(context.Background(), &contact, passcode, 30, 2)
},
},
}

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)
})
}

}

func TestConfirmContact(t *testing.T) {
t.Run("TOTP is valid - call Confirm Action", func(t *testing.T) {
// given
tmq := testTransportHandler{
Type: fixtures.RequestType,
Path: "/contact/confirmed/",
Result: "{}",
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}

client := getTestWalletClientWithOpts(tmq, WithXPriv(fixtures.XPrivString))

contact := &models.Contact{PubKey: fixtures.PubKey}
totp, err := client.GenerateTotpForContact(contact, 30, 2)
require.NoError(t, err)

// when
result := client.ConfirmContact(context.Background(), contact, totp, 30, 2)

// then
require.Nil(t, result)
})

t.Run("TOTP is invalid - do not call Confirm Action", func(t *testing.T) {
// given
tmq := testTransportHandler{
Type: fixtures.RequestType,
Path: "/unknown/",
Result: "{}",
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}

client := getTestWalletClientWithOpts(tmq, WithXPriv(fixtures.XPrivString))

alice := &models.Contact{PubKey: "034252e5359a1de3b8ec08e6c29b80594e88fb47e6ae9ce65ee5a94f0d371d2cde"}
a_totp, err := client.GenerateTotpForContact(alice, 30, 2)
require.NoError(t, err)

bob := &models.Contact{PubKey: "02dde493752f7bc89822ed8a13e0e4aa04550c6c4430800e4be1e5e5c2556cf65b"}

// when
result := client.ConfirmContact(context.Background(), bob, a_totp, 30, 2)

// then
require.NotNil(t, result)
require.Equal(t, result.Error(), "totp is invalid")
})
}
1 change: 1 addition & 0 deletions fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
XPrivString = "xprv9s21ZrQH143K3N6qVJQAu4EP51qMcyrKYJLkLgmYXgz58xmVxVLSsbx2DfJUtjcnXK8NdvkHMKfmmg5AJT2nqqRWUrjSHX29qEJwBgBPkJQ"
AccessKeyString = "7779d24ca6f8821f225042bf55e8f80aa41b08b879b72827f51e41e6523b9cd0"
PaymailAddress = "[email protected]"
PubKey = "034252e5359a1de3b8ec08e6c29b80594e88fb47e6ae9ce65ee5a94f0d371d2cde"
)

func MarshallForTestHandler(object any) string {
Expand Down
4 changes: 3 additions & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 644afb3

Please sign in to comment.