@@ -49,33 +49,32 @@ type WebhookEventCommon struct {
4949 WebhookID string `json:"webhook_id"`
5050 Created int `json:"created"`
5151 APIVersion int `json:"api_version,omitempty"` // omitempty because test fixtures do not include it
52+ Type string `json:"type"` // block, transaction, delegation, epoch
5253}
5354
5455type WebhookEventBlock struct {
5556 WebhookEventCommon
56- Type string `json:"type"` // "block"
57- Payload Block `json:"payload"`
57+ Payload Block `json:"payload"`
5858}
5959
6060type WebhookEventTransaction struct {
6161 WebhookEventCommon
62- Type string `json:"type"` // "transaction"
6362 Payload []TransactionPayload `json:"payload"`
6463}
6564
6665type WebhookEventEpoch struct {
6766 WebhookEventCommon
68- Type string `json:"type"` // "epoch"
6967 Payload EpochPayload `json:"payload"`
7068}
7169
7270type WebhookEventDelegation struct {
7371 WebhookEventCommon
74- Type string `json:"type"` // "delegation"
7572 Payload []StakeDelegationPayload `json:"payload"`
7673}
7774
78- type WebhookEvent interface {}
75+ type WebhookEvent struct {
76+ WebhookEventCommon
77+ }
7978
8079const (
8180 // Signatures older than this will be rejected by ConstructEvent
@@ -152,67 +151,47 @@ func parseSignatureHeader(header string) (*signedHeader, error) {
152151 return sh , nil
153152}
154153
155- func VerifyWebhookSignature (payload []byte , header string , secret string ) (WebhookEvent , error ) {
154+ func VerifyWebhookSignature (payload []byte , header string , secret string ) (* WebhookEvent , error ) {
156155 return VerifyWebhookSignatureWithTolerance (payload , header , secret , DefaultTolerance )
157156}
158157
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 ) {
160159 return verifyWebhookSignature (payload , header , secret , tolerance , true )
161160}
162161
163- func VerifyWebhookSignatureIgnoringTolerance (payload []byte , header string , secret string ) (WebhookEvent , error ) {
162+ func VerifyWebhookSignatureIgnoringTolerance (payload []byte , header string , secret string ) (* WebhookEvent , error ) {
164163 return verifyWebhookSignature (payload , header , secret , 0 * time .Second , false )
165164}
166165
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 ) {
168167 // First unmarshal into a generic map to inspect the type
169168 var genericEvent map [string ]interface {}
170169 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 )
178171 }
179172
180173 var event WebhookEvent
181174
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-
196175 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 )
198177 }
199178
200179 header , err := parseSignatureHeader (sigHeader )
201180 if err != nil {
202- return event , err
181+ return & event , err
203182 }
204183
205184 expectedSignature := computeSignature (header .timestamp , payload , secret )
206185 expiredTimestamp := time .Since (header .timestamp ) > tolerance
207186 if enforceTolerance && expiredTimestamp {
208- return event , ErrTooOld
187+ return & event , ErrTooOld
209188 }
210189
211190 for _ , sig := range header .signatures {
212191 if hmac .Equal (expectedSignature , sig ) {
213- return event , nil
192+ return & event , nil
214193 }
215194 }
216195
217- return event , ErrNoValidSignature
196+ return & event , ErrNoValidSignature
218197}
0 commit comments