Skip to content

Commit

Permalink
feat(SPV-848): use models from spv-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Jul 2, 2024
1 parent 59c6c72 commit a26b756
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 46 deletions.
4 changes: 2 additions & 2 deletions examples/go.mod

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

4 changes: 2 additions & 2 deletions examples/go.sum

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

12 changes: 7 additions & 5 deletions examples/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"github.com/bitcoin-sv/spv-wallet-go-client/notifications"
"github.com/bitcoin-sv/spv-wallet/models"
)

func main() {
defer examples.HandlePanic()

client := walletclient.NewWithAdminKey("http://localhost:3003/v1", "xprv9s21ZrQH143K2pmNeAHBzU4JHNDaFaPTbzKbBCw55ErhMDLsxDwKqcaDVV3PwmEmRZa9qUaU261iJaUx8eBiBF77zrPxTH8JGXC7LZQnsgA")
//"Authorization", "this-is-the-token", 3
examples.CheckIfAdminKeyExists()

client := walletclient.NewWithAdminKey("http://localhost:3003/v1", examples.ExampleAdminKey)
wh := notifications.NewWebhook(
context.Background(),
client,
Expand All @@ -33,16 +35,16 @@ func main() {

http.Handle("/notification", wh.HTTPHandler())

if err = notifications.RegisterHandler(wh, func(gpe *notifications.StringEvent) {
if err = notifications.RegisterHandler(wh, func(gpe *models.StringEvent) {
time.Sleep(50 * time.Millisecond) // simulate processing time
fmt.Printf("Processing event-string: %s\n", gpe.Value)
}); err != nil {
panic(err)
}

if err = notifications.RegisterHandler(wh, func(gpe *notifications.TransactionEvent) {
if err = notifications.RegisterHandler(wh, func(gpe *models.TransactionEvent) {
time.Sleep(50 * time.Millisecond) // simulate processing time
fmt.Printf("Processing event-transaction: XPubID: %s, TxID: %s\n", gpe.XPubID, gpe.TransactionID)
fmt.Printf("Processing event-transaction: XPubID: %s, TxID: %s, Status: %s\n", gpe.XPubID, gpe.TransactionID, gpe.Status)
}); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod

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

4 changes: 2 additions & 2 deletions go.sum

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

8 changes: 6 additions & 2 deletions notifications/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package notifications

import "context"
import (
"context"

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

type WebhookOptions struct {
TokenHeader string
Expand Down Expand Up @@ -50,7 +54,7 @@ func WithProcessors(count int) WebhookOpts {
type Webhook struct {
URL string
options *WebhookOptions
buffer chan *RawEvent
buffer chan *models.RawEvent
subscriber WebhookSubscriber
handlers *eventsMap
}
4 changes: 3 additions & 1 deletion notifications/registerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package notifications
import (
"fmt"
"reflect"

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

type eventHandler struct {
Caller reflect.Value
ModelType reflect.Type
}

func RegisterHandler[EventType Events](nd *Webhook, handlerFunction func(event *EventType)) error {
func RegisterHandler[EventType models.Events](nd *Webhook, handlerFunction func(event *EventType)) error {
handlerValue := reflect.ValueOf(handlerFunction)
if handlerValue.Kind() != reflect.Func {
return fmt.Errorf("Not a function")
Expand Down
34 changes: 4 additions & 30 deletions notifications/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"reflect"
"time"

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

func NewWebhook(ctx context.Context, subscriber WebhookSubscriber, url string, opts ...WebhookOpts) *Webhook {
Expand All @@ -18,7 +20,7 @@ func NewWebhook(ctx context.Context, subscriber WebhookSubscriber, url string, o
wh := &Webhook{
URL: url,
options: options,
buffer: make(chan *RawEvent, options.BufferSize),
buffer: make(chan *models.RawEvent, options.BufferSize),
subscriber: subscriber,
handlers: newEventsMap(),
}
Expand All @@ -42,7 +44,7 @@ func (w *Webhook) HTTPHandler() http.Handler {
http.Error(rw, "Unauthorized", http.StatusUnauthorized)
return
}
var events []*RawEvent
var events []*models.RawEvent
if err := json.NewDecoder(r.Body).Decode(&events); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
Expand Down Expand Up @@ -87,31 +89,3 @@ func (nd *Webhook) process() {
}
}
}

////////////////////// BELOW it should be imported from spv-wallet models

// RawEvent - the base event type
type RawEvent struct {
Type string `json:"type"`
Content json.RawMessage `json:"content"`
}

// StringEvent - event with string value; can be used for generic messages and it's used for testing
type StringEvent struct {
Value string `json:"value"`
}

type UserEvent struct {
XPubID string `json:"xpubId"`
}

type TransactionEvent struct {
UserEvent `json:",inline"`

TransactionID string `json:"transactionId"`
}

// Events - interface for all supported events
type Events interface {
StringEvent | TransactionEvent
}

0 comments on commit a26b756

Please sign in to comment.