diff --git a/examples/go.mod b/examples/go.mod index 458eab3..34dd12d 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -1,12 +1,12 @@ module github.com/bitcoin-sv/spv-wallet-go-client/examples -go 1.21 +go 1.22.4 replace github.com/bitcoin-sv/spv-wallet-go-client => ../ require ( github.com/bitcoin-sv/spv-wallet-go-client v0.0.0-00010101000000-000000000000 - github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 + github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573 ) require ( diff --git a/examples/go.sum b/examples/go.sum index fdd302c..be29a03 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,5 +1,5 @@ -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 h1:rBscs3Gbz0RWY03eI3Z9AwD7/MxajdJF54oy3xMqKRQ= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573 h1:lWWSZefre67rhyUistJjTH2BHZyNpczRjp2MHYibt7c= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573/go.mod h1:VsUb0ZRA6Emr8+VDEq5SbOyzwvfKb+32Lkb9dM+n20o= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= diff --git a/examples/webhooks/webhooks.go b/examples/webhooks/webhooks.go index 7f12df8..c10f17c 100644 --- a/examples/webhooks/webhooks.go +++ b/examples/webhooks/webhooks.go @@ -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, @@ -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) } diff --git a/go.mod b/go.mod index 5e1d077..f44665f 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ module github.com/bitcoin-sv/spv-wallet-go-client -go 1.21 +go 1.22.4 require ( - github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 + github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573 github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 github.com/libsv/go-bk v0.1.6 github.com/libsv/go-bt/v2 v2.2.5 diff --git a/go.sum b/go.sum index 5413a3d..d201127 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 h1:rBscs3Gbz0RWY03eI3Z9AwD7/MxajdJF54oy3xMqKRQ= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573 h1:lWWSZefre67rhyUistJjTH2BHZyNpczRjp2MHYibt7c= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240702094836-4955eed44573/go.mod h1:VsUb0ZRA6Emr8+VDEq5SbOyzwvfKb+32Lkb9dM+n20o= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= diff --git a/notifications/options.go b/notifications/options.go index 9f09f27..626f80d 100644 --- a/notifications/options.go +++ b/notifications/options.go @@ -1,6 +1,10 @@ package notifications -import "context" +import ( + "context" + + "github.com/bitcoin-sv/spv-wallet/models" +) type WebhookOptions struct { TokenHeader string @@ -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 } diff --git a/notifications/registerer.go b/notifications/registerer.go index 2c40c44..8022838 100644 --- a/notifications/registerer.go +++ b/notifications/registerer.go @@ -3,6 +3,8 @@ package notifications import ( "fmt" "reflect" + + "github.com/bitcoin-sv/spv-wallet/models" ) type eventHandler struct { @@ -10,7 +12,7 @@ type eventHandler struct { 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") diff --git a/notifications/webhook.go b/notifications/webhook.go index 3574b07..7d002fe 100644 --- a/notifications/webhook.go +++ b/notifications/webhook.go @@ -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 { @@ -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(), } @@ -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 @@ -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 -}