-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp2p_receive_transaction.go
109 lines (91 loc) · 2.72 KB
/
p2p_receive_transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package server
import (
"github.com/bitcoin-sv/go-paymail/errors"
"github.com/gin-gonic/gin"
"net/http"
"github.com/bitcoin-sv/go-paymail"
"github.com/bitcoin-sv/go-paymail/spv"
)
type p2pPayloadFormat uint
const (
basicP2pPayload p2pPayloadFormat = iota
beefP2pPayload
)
/*
Incoming Data Object Example:
{
"hex": "01000000012adda020db81f2155ebba69e7.........154888ac00000000",
"metadata": {
"sender": "[email protected]",
"pubkey": "<sender-pubkey>",
"signature": "signature(txid)",
"note": "Human readable information related to the tx."
},
"reference": "someRefId"
}
*/
// p2pReceiveTx will receive a P2P transaction (from previous request: P2P Payment Destination)
//
// Specs: https://docs.moneybutton.com/docs/paymail-06-p2p-transactions.html
func (c *Configuration) p2pReceiveTx(context *gin.Context) {
p2pFormat := basicP2pPayload
incomingPaymail := context.Param(PaymailAddressParamName)
requestPayload, _, md, err := processP2pReceiveTxRequest(c, context.Request, incomingPaymail, p2pFormat)
if err != nil {
errors.ErrorResponse(context, err, c.Logger)
return
}
if len(requestPayload.Hex) == 0 {
panic("empty hex after parsing!")
}
var response *paymail.P2PTransactionPayload
if response, err = c.actions.RecordTransaction(
context.Request.Context(), requestPayload.P2PTransaction, md,
); err != nil {
errors.ErrorResponse(context, err, c.Logger)
return
}
context.JSON(http.StatusOK, response)
}
/*
Incoming Data Object Example:
{
"beef": "01000000012adda020db81f2155ebba69e7.........154888ac00000000",
"metadata": {
"sender": "[email protected]",
"pubkey": "<sender-pubkey>",
"signature": "signature(txid)",
"note": "Human readable information related to the tx."
},
"reference": "someRefId"
}
*/
// p2pReceiveBeefTx will receive a P2P transaction in BEEF format
func (c *Configuration) p2pReceiveBeefTx(context *gin.Context) {
p2pFormat := beefP2pPayload
incomingPaymail := context.Param(PaymailAddressParamName)
requestPayload, dBeef, md, err := processP2pReceiveTxRequest(c, context.Request, incomingPaymail, p2pFormat)
if err != nil {
errors.ErrorResponse(context, err, c.Logger)
return
}
if len(requestPayload.Hex) == 0 {
panic("empty hex after parsing!")
}
if dBeef == nil {
panic("empty beef after parsing!")
}
err = spv.ExecuteSimplifiedPaymentVerification(context.Request.Context(), dBeef, c.actions)
if err != nil {
errors.ErrorResponse(context, errors.ErrSPVFailed, c.Logger)
return
}
var response *paymail.P2PTransactionPayload
if response, err = c.actions.RecordTransaction(
context.Request.Context(), requestPayload.P2PTransaction, md,
); err != nil {
errors.ErrorResponse(context, err, c.Logger)
return
}
context.JSON(http.StatusOK, response)
}