From df98654da1e067581d2215a96b28107b3c87006b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= Date: Wed, 6 Sep 2023 17:48:06 +0200 Subject: [PATCH] chore(BUX-175): define paymail payload formats --- model_transaction_config.go | 33 ++++++++++++++++++++------------- paymail.go | 7 +++++-- paymail_test.go | 12 ++++++------ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/model_transaction_config.go b/model_transaction_config.go index e1a26982..66b760a0 100644 --- a/model_transaction_config.go +++ b/model_transaction_config.go @@ -70,17 +70,24 @@ type TransactionOutput struct { UseForChange bool `json:"use_for_change,omitempty" toml:"use_for_change" yaml:"use_for_change" bson:"use_for_change,omitempty"` // if set, no change destinations will be created, but all outputs flagged will get the change } +type PaymailPayloadFormat uint32 + +const ( + BasicPaymailPayloadFormat PaymailPayloadFormat = iota + BeefPaymailPayloadFormat +) + // PaymailP4 paymail configuration for the p2p payments on this output type PaymailP4 struct { - Alias string `json:"alias" toml:"alias" yaml:"alias" bson:"alias,omitempty"` // Alias of the paymail {alias}@domain.com - Domain string `json:"domain" toml:"domain" yaml:"domain" bson:"domain,omitempty"` // Domain of the paymail alias@{domain.com} - FromPaymail string `json:"from_paymail,omitempty" toml:"from_paymail" yaml:"from_paymail" bson:"from_paymail,omitempty"` // From paymail address: alias@domain.com - Note string `json:"note,omitempty" toml:"note" yaml:"note" bson:"note,omitempty"` // Friendly readable note to the paymail receiver - PubKey string `json:"pub_key,omitempty" toml:"pub_key" yaml:"pub_key" bson:"pub_key,omitempty"` // Used for validating the signature - ReceiveEndpoint string `json:"receive_endpoint,omitempty" toml:"receive_endpoint" yaml:"receive_endpoint" bson:"receive_endpoint,omitempty"` // P2P endpoint when notifying - ReferenceID string `json:"reference_id,omitempty" toml:"reference_id" yaml:"reference_id" bson:"reference_id,omitempty"` // Reference ID saved from P2P request - ResolutionType string `json:"resolution_type" toml:"resolution_type" yaml:"resolution_type" bson:"resolution_type,omitempty"` // Type of address resolution (basic vs p2p) - UseBEEF bool `json:"use_beef,omitempty" toml:"use_beef" yaml:"use_beef" bson:"use_beef,omitempty"` // Use beef format for the transaction + Alias string `json:"alias" toml:"alias" yaml:"alias" bson:"alias,omitempty"` // Alias of the paymail {alias}@domain.com + Domain string `json:"domain" toml:"domain" yaml:"domain" bson:"domain,omitempty"` // Domain of the paymail alias@{domain.com} + FromPaymail string `json:"from_paymail,omitempty" toml:"from_paymail" yaml:"from_paymail" bson:"from_paymail,omitempty"` // From paymail address: alias@domain.com + Note string `json:"note,omitempty" toml:"note" yaml:"note" bson:"note,omitempty"` // Friendly readable note to the paymail receiver + PubKey string `json:"pub_key,omitempty" toml:"pub_key" yaml:"pub_key" bson:"pub_key,omitempty"` // Used for validating the signature + ReceiveEndpoint string `json:"receive_endpoint,omitempty" toml:"receive_endpoint" yaml:"receive_endpoint" bson:"receive_endpoint,omitempty"` // P2P endpoint when notifying + ReferenceID string `json:"reference_id,omitempty" toml:"reference_id" yaml:"reference_id" bson:"reference_id,omitempty"` // Reference ID saved from P2P request + ResolutionType string `json:"resolution_type" toml:"resolution_type" yaml:"resolution_type" bson:"resolution_type,omitempty"` // Type of address resolution (basic vs p2p) + Format PaymailPayloadFormat `json:"format,omitempty" toml:"format" yaml:"format" bson:"format,omitempty"` // Use beef format for the transaction } // Types of resolution methods @@ -219,10 +226,10 @@ func (t *TransactionOutput) processPaymailOutput(ctx context.Context, cacheStore } // Does the provider support P2P? - success, useBEEF, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities) + success, p2pDestinationURL, p2pSubmitTxURL, format := hasP2P(capabilities) if success { return t.processPaymailViaP2P( - paymailClient, p2pDestinationURL, p2pSubmitTxURL, fromPaymail, useBEEF, + paymailClient, p2pDestinationURL, p2pSubmitTxURL, fromPaymail, format, ) } @@ -274,7 +281,7 @@ func (t *TransactionOutput) processPaymailViaAddressResolution(ctx context.Conte } // processPaymailViaP2P will process the output for P2P Paymail resolution -func (t *TransactionOutput) processPaymailViaP2P(client paymail.ClientInterface, p2pDestinationURL, p2pSubmitTxURL string, fromPaymail string, useBEEF bool) error { +func (t *TransactionOutput) processPaymailViaP2P(client paymail.ClientInterface, p2pDestinationURL, p2pSubmitTxURL string, fromPaymail string, format PaymailPayloadFormat) error { // todo: this is a hack since paymail providers will complain if satoshis are empty (SendToAll has 0 satoshi) satoshis := t.Satoshis @@ -315,7 +322,7 @@ func (t *TransactionOutput) processPaymailViaP2P(client paymail.ClientInterface, t.PaymailP4.ReferenceID = destinationInfo.Reference t.PaymailP4.ResolutionType = ResolutionTypeP2P t.PaymailP4.FromPaymail = fromPaymail - t.PaymailP4.UseBEEF = useBEEF + t.PaymailP4.Format = format return nil } diff --git a/paymail.go b/paymail.go index f398e039..a90e09dc 100644 --- a/paymail.go +++ b/paymail.go @@ -67,15 +67,18 @@ func getCapabilities(ctx context.Context, cs cachestore.ClientInterface, client } // hasP2P will return the P2P urls and true if they are both found -func hasP2P(capabilities *paymail.CapabilitiesPayload) (success, useBEEF bool, p2pDestinationURL, p2pSubmitTxURL string) { +func hasP2P(capabilities *paymail.CapabilitiesPayload) (success bool, p2pDestinationURL, p2pSubmitTxURL string, format PaymailPayloadFormat) { p2pDestinationURL = capabilities.GetString(paymail.BRFCP2PPaymentDestination, "") p2pSubmitTxURL = capabilities.GetString(paymail.BRFCP2PTransactions, "") p2pBeefSubmitTxURL := capabilities.GetString(paymail.BRFCBeefTransaction, "") if len(p2pBeefSubmitTxURL) > 0 { p2pSubmitTxURL = p2pBeefSubmitTxURL - useBEEF = true + format = BeefPaymailPayloadFormat } + //else { + // format = BasicPaymailPayloadFormat + //} if len(p2pSubmitTxURL) > 0 && len(p2pDestinationURL) > 0 { success = true diff --git a/paymail_test.go b/paymail_test.go index ebb0f17d..be084017 100644 --- a/paymail_test.go +++ b/paymail_test.go @@ -110,7 +110,7 @@ func Test_hasP2P(t *testing.T) { t.Run("no p2p capabilities", func(t *testing.T) { capabilities := server.GenericCapabilities(paymail.DefaultBsvAliasVersion, false) - success, _, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities) + success, p2pDestinationURL, p2pSubmitTxURL, _ := hasP2P(capabilities) assert.Equal(t, false, success) assert.Equal(t, "", p2pDestinationURL) assert.Equal(t, "", p2pSubmitTxURL) @@ -123,7 +123,7 @@ func Test_hasP2P(t *testing.T) { capabilities.Capabilities[paymail.BRFCP2PTransactions] = "/receive-transaction/{alias}@{domain.tld}" capabilities.Capabilities[paymail.BRFCP2PPaymentDestination] = "/p2p-payment-destination/{alias}@{domain.tld}" - success, _, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities) + success, p2pDestinationURL, p2pSubmitTxURL, _ := hasP2P(capabilities) assert.Equal(t, true, success) assert.Equal(t, capabilities.Capabilities[paymail.BRFCP2PPaymentDestination], p2pDestinationURL) assert.Equal(t, capabilities.Capabilities[paymail.BRFCP2PTransactions], p2pSubmitTxURL) @@ -136,9 +136,9 @@ func Test_hasP2P_beefCapabilities(t *testing.T) { t.Run("no beef capabilities", func(t *testing.T) { capabilities := server.GenericCapabilities(paymail.DefaultBsvAliasVersion, false) - success, useBeef, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities) + success, p2pDestinationURL, p2pSubmitTxURL, format := hasP2P(capabilities) assert.Equal(t, false, success) - assert.Equal(t, false, useBeef) + assert.Equal(t, BasicPaymailPayloadFormat, format) assert.Equal(t, "", p2pDestinationURL) assert.Equal(t, "", p2pSubmitTxURL) }) @@ -150,9 +150,9 @@ func Test_hasP2P_beefCapabilities(t *testing.T) { capabilities.Capabilities[paymail.BRFCP2PPaymentDestination] = "/p2p-payment-destination/{alias}@{domain.tld}" capabilities.Capabilities[paymail.BRFCBeefTransaction] = "/receive-beef-transaction/{alias}@{domain.tld}" - success, useBeef, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities) + success, p2pDestinationURL, p2pSubmitTxURL, format := hasP2P(capabilities) assert.Equal(t, true, success) - assert.Equal(t, true, useBeef) + assert.Equal(t, BeefPaymailPayloadFormat, format) assert.Equal(t, capabilities.Capabilities[paymail.BRFCP2PPaymentDestination], p2pDestinationURL) assert.Equal(t, capabilities.Capabilities[paymail.BRFCBeefTransaction], p2pSubmitTxURL) })