@@ -20,6 +20,7 @@ import (
20
20
"fmt"
21
21
"os"
22
22
23
+ "github.com/blinklabs-io/gouroboros/cbor"
23
24
"github.com/blinklabs-io/snek/event"
24
25
"github.com/blinklabs-io/snek/fcm"
25
26
"github.com/blinklabs-io/snek/input/chainsync"
@@ -139,16 +140,42 @@ func (p *PushOutput) Start() error {
139
140
140
141
// Create notification message
141
142
title := "Snek"
142
- body := fmt .Sprintf (
143
- "New Transaction!\n BlockNumber: %d, SlotNumber: %d\n Inputs: %d, Outputs: %d\n Fee: %d\n Hash: %s" ,
144
- tc .BlockNumber ,
145
- tc .SlotNumber ,
146
- len (te .Inputs ),
147
- len (te .Outputs ),
148
- te .Fee ,
149
- tc .TransactionHash ,
150
- )
151
143
144
+ // Get metadata
145
+ var cip20Message string
146
+ if te .Metadata != nil {
147
+ jsonMessage , err := extractCIP20FromMetadata (te .Metadata )
148
+ if err != nil {
149
+ fmt .Println ("Error:" , err )
150
+ } else {
151
+ cip20Message = jsonMessage
152
+ fmt .Println ("JSON CIP20 Message:" , cip20Message )
153
+ }
154
+ }
155
+
156
+ var body string
157
+ if cip20Message != "" {
158
+ body = fmt .Sprintf (
159
+ "New Transaction!\n BlockNumber: %d, SlotNumber: %d\n Inputs: %d, Outputs: %d\n Fee: %d\n Hash: %s\n Metadata: %s" ,
160
+ tc .BlockNumber ,
161
+ tc .SlotNumber ,
162
+ len (te .Inputs ),
163
+ len (te .Outputs ),
164
+ te .Fee ,
165
+ tc .TransactionHash ,
166
+ cip20Message ,
167
+ )
168
+ } else {
169
+ body = fmt .Sprintf (
170
+ "New Transaction!\n BlockNumber: %d, SlotNumber: %d\n Inputs: %d, Outputs: %d\n Fee: %d\n Hash: %s" ,
171
+ tc .BlockNumber ,
172
+ tc .SlotNumber ,
173
+ len (te .Inputs ),
174
+ len (te .Outputs ),
175
+ te .Fee ,
176
+ tc .TransactionHash ,
177
+ )
178
+ }
152
179
// Send notification
153
180
p .processFcmNotifications (title , body )
154
181
@@ -273,3 +300,47 @@ func (p *PushOutput) InputChan() chan<- event.Event {
273
300
func (p * PushOutput ) OutputChan () <- chan event.Event {
274
301
return nil
275
302
}
303
+
304
+ // This should probably go in gouroboros module
305
+ // extractCIP20FromMetadata extracts the CIP20 message from the transaction metadata
306
+ // and returns it as a JSON string.
307
+ func extractCIP20FromMetadata (metadata * cbor.Value ) (string , error ) {
308
+ if metadata == nil {
309
+ return "" , fmt .Errorf ("metadata is nil" )
310
+ }
311
+
312
+ metadataMap , ok := metadata .Value ().(map [any ]any )
313
+ if ! ok {
314
+ return "" , fmt .Errorf ("metadata value is not of the expected map type" )
315
+ }
316
+
317
+ // Extract the nested value for key 674
318
+ nestedValue , found := metadataMap [uint64 (674 )]
319
+ if ! found {
320
+ return "" , fmt .Errorf ("key 674 not found in metadata" )
321
+ }
322
+
323
+ // Assert the nested value is a map
324
+ nestedMap , ok := nestedValue .(map [any ]any )
325
+ if ! ok {
326
+ return "" , fmt .Errorf ("nested value for key 674 is not a map" )
327
+ }
328
+
329
+ msgValue , found := nestedMap ["msg" ]
330
+ if ! found {
331
+ return "" , fmt .Errorf ("key 'msg' not found in nested metadata map" )
332
+ }
333
+
334
+ msgStruct := map [string ]any {
335
+ "674" : map [string ]any {
336
+ "msg" : msgValue ,
337
+ },
338
+ }
339
+
340
+ jsonBytes , err := json .Marshal (msgStruct )
341
+ if err != nil {
342
+ return "" , fmt .Errorf ("error marshalling message to JSON: %v" , err )
343
+ }
344
+
345
+ return string (jsonBytes ), nil
346
+ }
0 commit comments