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

Commit

Permalink
feat(BUX-175): add beef capability
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Sep 11, 2023
1 parent 51c75e3 commit 6691145
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func WithPaymailSupport(domains []string, defaultFromPaymail, defaultNote string

// Add generic capabilities
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithP2PCapabilities())
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithBeefCapabilities())

// Add each domain
for _, domain := range domains {
Expand Down
1 change: 1 addition & 0 deletions model_sync_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ func notifyPaymailProviders(ctx context.Context, transaction *Transaction) ([]*S
out.PaymailP4.Note,
out.PaymailP4.FromPaymail,
transaction.Hex,
"", // TODO: set transaction in beef format if paymail provider can handle it
); err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions model_transaction_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type PaymailP4 struct {
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_beefy,omitempty" toml:"use_beefy" yaml:"use_beefy" bson:"use_beefy,omitempty"` // Use beef format for the transaction
}

// Types of resolution methods
Expand Down Expand Up @@ -218,10 +219,10 @@ func (t *TransactionOutput) processPaymailOutput(ctx context.Context, cacheStore
}

// Does the provider support P2P?
success, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities)
success, useBEEF, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities)
if success {
return t.processPaymailViaP2P(
paymailClient, p2pDestinationURL, p2pSubmitTxURL, fromPaymail,
paymailClient, p2pDestinationURL, p2pSubmitTxURL, fromPaymail, useBEEF,
)
}

Expand Down Expand Up @@ -273,7 +274,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) error {
func (t *TransactionOutput) processPaymailViaP2P(client paymail.ClientInterface, p2pDestinationURL, p2pSubmitTxURL string, fromPaymail string, useBEEF bool) 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 @@ -314,6 +315,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

return nil
}
Expand Down
23 changes: 18 additions & 5 deletions paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ 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 bool, p2pDestinationURL, p2pSubmitTxURL string) {
func hasP2P(capabilities *paymail.CapabilitiesPayload) (success, useBEEF bool, p2pDestinationURL, p2pSubmitTxURL string) {
p2pDestinationURL = capabilities.GetString(paymail.BRFCP2PPaymentDestination, "")
p2pSubmitTxURL = capabilities.GetString(paymail.BRFCP2PTransactions, "")
p2pBeefSubmitTxURL := capabilities.GetString(paymail.BRFCBeefTransaction, "")

if len(p2pBeefSubmitTxURL) > 0 {
p2pSubmitTxURL = p2pBeefSubmitTxURL
useBEEF = true
}

if len(p2pSubmitTxURL) > 0 && len(p2pDestinationURL) > 0 {
success = true
Expand Down Expand Up @@ -146,7 +152,7 @@ func startP2PTransaction(client paymail.ClientInterface,

// finalizeP2PTransaction will notify the paymail provider about the transaction
func finalizeP2PTransaction(client paymail.ClientInterface,
alias, domain, p2pSubmitURL, referenceID, note, senderPaymailAddress, txHex string) (*paymail.P2PTransactionPayload, error) {
alias, domain, p2pSubmitURL, referenceID, note, senderPaymailAddress, txHex, txBeef string) (*paymail.P2PTransactionPayload, error) {

// Submit the P2P transaction
/*logger.Data(2, logger.DEBUG, "sending p2p tx...",
Expand All @@ -158,14 +164,21 @@ func finalizeP2PTransaction(client paymail.ClientInterface,
logger.MakeParameter("referenceID", referenceID),
)*/

response, err := client.SendP2PTransaction(p2pSubmitURL, alias, domain, &paymail.P2PTransaction{
Hex: txHex,
p2pTransaction := &paymail.P2PTransaction{
MetaData: &paymail.P2PMetaData{
Note: note,
Sender: senderPaymailAddress,
},
Reference: referenceID,
})
}

if len(txBeef) > 0 {
p2pTransaction.Beef = txBeef
} else {
p2pTransaction.Hex = txHex
}

response, err := client.SendP2PTransaction(p2pSubmitURL, alias, domain, p2pTransaction)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions paymail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ 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, useBeef, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities)
assert.Equal(t, false, success)
assert.Equal(t, false, useBeef)
assert.Equal(t, "", p2pDestinationURL)
assert.Equal(t, "", p2pSubmitTxURL)
})
Expand All @@ -123,8 +124,9 @@ 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, useBeef, p2pDestinationURL, p2pSubmitTxURL := hasP2P(capabilities)
assert.Equal(t, true, success)
assert.Equal(t, true, useBeef)
assert.Equal(t, capabilities.Capabilities[paymail.BRFCP2PPaymentDestination], p2pDestinationURL)
assert.Equal(t, capabilities.Capabilities[paymail.BRFCP2PTransactions], p2pSubmitTxURL)
})
Expand Down

0 comments on commit 6691145

Please sign in to comment.