Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
chore(BUX-175): define paymail payload formats
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Sep 11, 2023
1 parent e709f46 commit df98654
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
33 changes: 20 additions & 13 deletions model_transaction_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions paymail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand Down

0 comments on commit df98654

Please sign in to comment.