Skip to content

Commit

Permalink
feat(BUX-175): add beef capability (#5)
Browse files Browse the repository at this point in the history
* feat(BUX-175): add beef capability

* chore(BUX-175): update receive beef transaction url

* chore(BUX-175): update beef capability url
  • Loading branch information
pawellewandowski98 authored Sep 6, 2023
1 parent 698315e commit 3d6b2d8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
8 changes: 8 additions & 0 deletions brfc_definintions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
BRFCSFPAuthoriseAction = "95dddb461bff" // more info: https://docs.moneybutton.com/docs/sfp/paymail-10-sfp-authorise.html
BRFCSFPBuildAction = "189e32d93d28" // more info: https://docs.moneybutton.com/docs/sfp/paymail-09-sfp-build.html
BRFCVerifyPublicKeyOwner = "a9f510c16bde" // more info: http://bsvalias.org/05-verify-public-key-owner.html
BRFCBeefTransaction = "5c55a7fdb7bb" // more info: https://bsv.brc.dev/payments/0070
)

// BRFCKnownSpecifications is a running list of all known BRFC specifications
Expand Down Expand Up @@ -188,6 +189,13 @@ const BRFCKnownSpecifications = `
"title": "P2P Payment Destination with Tokens Support",
"url": "https://docs.moneybutton.com/docs/paymail/paymail-11-p2p-payment-destination-tokens.html",
"version": "1"
},
{
"author": "Darren Kellenschwiler",
"id": "5c55a7fdb7bb",
"title": "Background Evaluation Extended Format Transaction",
"url": "https://bsv.brc.dev/payments/0070",
"version": "1.0.0"
}
]
`
5 changes: 3 additions & 2 deletions p2p_send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Example:
// P2PTransaction is the request body for the P2P transaction request
type P2PTransaction struct {
Hex string `json:"hex"` // The raw transaction, encoded as a hexadecimal string
Beef string `json:"beef"` // The transaction in BEEF format
MetaData *P2PMetaData `json:"metadata"` // An object containing data associated with the transaction
Reference string `json:"reference"` // Reference for the payment (from previous P2P Destination request)
}
Expand Down Expand Up @@ -71,8 +72,8 @@ func (c *Client) SendP2PTransaction(p2pURL, alias, domain string,
if transaction == nil {
err = errors.New("transaction cannot be nil")
return
} else if len(transaction.Hex) == 0 {
err = errors.New("hex is required")
} else if len(transaction.Beef) == 0 && len(transaction.Hex) == 0 {
err = errors.New("beef or hex is required")
return
} else if len(transaction.Reference) == 0 {
err = errors.New("reference is required")
Expand Down
6 changes: 6 additions & 0 deletions server/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func P2PCapabilities(bsvAliasVersion string, senderValidation bool) *paymail.Cap
return c
}

// BeefCapabilities will add beef capabilities to given ones
func BeefCapabilities(c *paymail.CapabilitiesPayload) *paymail.CapabilitiesPayload {
c.Capabilities[paymail.BRFCBeefTransaction] = "/receive-beef-transaction/{alias}@{domain.tld}"
return c
}

// showCapabilities will return the service discovery results for the server
// and list all active capabilities of the Paymail server
//
Expand Down
7 changes: 7 additions & 0 deletions server/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func WithP2PCapabilities() ConfigOps {
}
}

// WithBeefCapabilities will load the beef capabilities
func WithBeefCapabilities() ConfigOps {
return func(c *Configuration) {
c.Capabilities = BeefCapabilities(c.Capabilities)
}
}

// WithCapabilities will modify the capabilities
func WithCapabilities(capabilities *paymail.CapabilitiesPayload) ConfigOps {
return func(c *Configuration) {
Expand Down
12 changes: 12 additions & 0 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,18 @@ func TestNewConfig(t *testing.T) {
assert.Equal(t, "test", c.Capabilities.BsvAlias)
})

t.Run("with beef capabilities", func(t *testing.T) {
c, err := NewConfig(
new(mockServiceProvider),
WithDomain("test.com"),
WithP2PCapabilities(),
WithBeefCapabilities(),
)
require.NoError(t, err)
require.NotNil(t, c)
assert.Equal(t, 8, len(c.Capabilities.Capabilities))
})

t.Run("with basic routes", func(t *testing.T) {
c, err := NewConfig(
new(mockServiceProvider),
Expand Down
3 changes: 2 additions & 1 deletion server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
ErrorUnknownDomain = "unknown-domain"
ErrorFailedMarshalJSON = "failed-marshal-json"
ErrorEncodingResponse = "error-encoding-response"
ErrorNotImplmented = "not-implemented"
)

var (
Expand Down Expand Up @@ -64,5 +65,5 @@ func ErrorResponse(w http.ResponseWriter, code, message string, statusCode int)
return
}

writeRespone(w, statusCode, "application/json", jsonData)
writeRespone(w, statusCode, "application/json", jsonData)
}
5 changes: 5 additions & 0 deletions server/p2p_receive_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ func (c *Configuration) p2pReceiveTx(w http.ResponseWriter, req *http.Request, _
// Set the response
writeJsonResponse(w, http.StatusOK, response)
}

// p2pReceiveBeefTx will receive a P2P transaction in BEEF format
func (c *Configuration) p2pReceiveBeefTx(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ErrorResponse(w, ErrorNotImplmented, "Receive BEEF transactions not implemented", http.StatusNotImplemented)
}
6 changes: 6 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ func (c *Configuration) registerPaymailRoutes(router *nrhttprouter.Router) {
"/"+c.APIVersion+"/"+c.ServiceName+"/receive-transaction/:paymailAddress",
c.p2pReceiveTx,
)

// P2P BEEF capability Receive Tx request
router.HTTPRouter.POST(
"/"+c.APIVersion+"/"+c.ServiceName+"/beef/:paymailAddress",
router.Request(c.p2pReceiveBeefTx),
)
}

0 comments on commit 3d6b2d8

Please sign in to comment.