@@ -49,33 +49,32 @@ type WebhookEventCommon struct {
49
49
WebhookID string `json:"webhook_id"`
50
50
Created int `json:"created"`
51
51
APIVersion int `json:"api_version,omitempty"` // omitempty because test fixtures do not include it
52
+ Type string `json:"type"` // block, transaction, delegation, epoch
52
53
}
53
54
54
55
type WebhookEventBlock struct {
55
56
WebhookEventCommon
56
- Type string `json:"type"` // "block"
57
- Payload Block `json:"payload"`
57
+ Payload Block `json:"payload"`
58
58
}
59
59
60
60
type WebhookEventTransaction struct {
61
61
WebhookEventCommon
62
- Type string `json:"type"` // "transaction"
63
62
Payload []TransactionPayload `json:"payload"`
64
63
}
65
64
66
65
type WebhookEventEpoch struct {
67
66
WebhookEventCommon
68
- Type string `json:"type"` // "epoch"
69
67
Payload EpochPayload `json:"payload"`
70
68
}
71
69
72
70
type WebhookEventDelegation struct {
73
71
WebhookEventCommon
74
- Type string `json:"type"` // "delegation"
75
72
Payload []StakeDelegationPayload `json:"payload"`
76
73
}
77
74
78
- type WebhookEvent interface {}
75
+ type WebhookEvent struct {
76
+ WebhookEventCommon
77
+ }
79
78
80
79
const (
81
80
// Signatures older than this will be rejected by ConstructEvent
@@ -152,67 +151,47 @@ func parseSignatureHeader(header string) (*signedHeader, error) {
152
151
return sh , nil
153
152
}
154
153
155
- func VerifyWebhookSignature (payload []byte , header string , secret string ) (WebhookEvent , error ) {
154
+ func VerifyWebhookSignature (payload []byte , header string , secret string ) (* WebhookEvent , error ) {
156
155
return VerifyWebhookSignatureWithTolerance (payload , header , secret , DefaultTolerance )
157
156
}
158
157
159
- func VerifyWebhookSignatureWithTolerance (payload []byte , header string , secret string , tolerance time.Duration ) (WebhookEvent , error ) {
158
+ func VerifyWebhookSignatureWithTolerance (payload []byte , header string , secret string , tolerance time.Duration ) (* WebhookEvent , error ) {
160
159
return verifyWebhookSignature (payload , header , secret , tolerance , true )
161
160
}
162
161
163
- func VerifyWebhookSignatureIgnoringTolerance (payload []byte , header string , secret string ) (WebhookEvent , error ) {
162
+ func VerifyWebhookSignatureIgnoringTolerance (payload []byte , header string , secret string ) (* WebhookEvent , error ) {
164
163
return verifyWebhookSignature (payload , header , secret , 0 * time .Second , false )
165
164
}
166
165
167
- func verifyWebhookSignature (payload []byte , sigHeader string , secret string , tolerance time.Duration , enforceTolerance bool ) (WebhookEvent , error ) {
166
+ func verifyWebhookSignature (payload []byte , sigHeader string , secret string , tolerance time.Duration , enforceTolerance bool ) (* WebhookEvent , error ) {
168
167
// First unmarshal into a generic map to inspect the type
169
168
var genericEvent map [string ]interface {}
170
169
if err := json .Unmarshal (payload , & genericEvent ); err != nil {
171
- return nil , fmt .Errorf ("failed to parse webhook body json: %s" , err )
172
- }
173
-
174
- // Determine the specific event type
175
- eventType , ok := genericEvent ["type" ].(string )
176
- if ! ok {
177
- return nil , errors .New ("event type not found" )
170
+ return nil , fmt .Errorf ("Failed to parse webhook body json: %s" , err )
178
171
}
179
172
180
173
var event WebhookEvent
181
174
182
- // Unmarshal into the specific event type based on the eventType
183
- switch eventType {
184
- case string (WebhookEventTypeBlock ):
185
- event = new (WebhookEventBlock )
186
- case string (WebhookEventTypeTransaction ):
187
- event = new (WebhookEventTransaction )
188
- case string (WebhookEventTypeEpoch ):
189
- event = new (WebhookEventEpoch )
190
- case string (WebhookEventTypeDelegation ):
191
- event = new (WebhookEventDelegation )
192
- default :
193
- return nil , fmt .Errorf ("unknown event type: %s" , eventType )
194
- }
195
-
196
175
if err := json .Unmarshal (payload , & event ); err != nil {
197
- return nil , fmt .Errorf ("failed to parse specific webhook event json: %s" , err )
176
+ return nil , fmt .Errorf ("Failed to parse specific webhook event json: %s" , err )
198
177
}
199
178
200
179
header , err := parseSignatureHeader (sigHeader )
201
180
if err != nil {
202
- return event , err
181
+ return & event , err
203
182
}
204
183
205
184
expectedSignature := computeSignature (header .timestamp , payload , secret )
206
185
expiredTimestamp := time .Since (header .timestamp ) > tolerance
207
186
if enforceTolerance && expiredTimestamp {
208
- return event , ErrTooOld
187
+ return & event , ErrTooOld
209
188
}
210
189
211
190
for _ , sig := range header .signatures {
212
191
if hmac .Equal (expectedSignature , sig ) {
213
- return event , nil
192
+ return & event , nil
214
193
}
215
194
}
216
195
217
- return event , ErrNoValidSignature
196
+ return & event , ErrNoValidSignature
218
197
}
0 commit comments