File size: 12,352 Bytes
2793f59
 
 
 
 
 
 
 
 
626cc51
 
ead55d3
3e1fbcf
 
 
 
bf45e8c
 
3e1fbcf
 
 
 
 
df9d848
2793f59
adfd98e
2793f59
 
 
c5c280b
 
 
bc653cd
2793f59
 
164f1bd
3e1fbcf
 
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
 
 
 
bf45e8c
ead55d3
 
4cf625f
ead55d3
41d7cd3
ead55d3
 
 
8ebd2f7
cc830e6
8ebd2f7
 
 
 
 
 
 
 
 
 
ead55d3
 
 
bf45e8c
 
 
 
 
 
aad2369
bf45e8c
aad2369
bf45e8c
 
3aa4e60
98d121f
52bec60
bf45e8c
 
 
ead55d3
2793f59
 
 
c5c280b
21dcb75
 
c5c280b
 
 
 
 
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38cbdbe
9de5a60
2793f59
 
 
 
 
 
 
 
 
 
38cbdbe
9de5a60
38cbdbe
 
2793f59
9e73eae
 
 
 
 
 
 
 
 
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
a0558d6
2793f59
 
 
 
 
 
 
 
 
9b9289d
 
 
 
 
 
 
2793f59
 
 
 
38cbdbe
 
2793f59
3278551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c80b44
3278551
 
 
 
 
2176387
3278551
a0558d6
4575e19
a0558d6
3278551
f4d66d4
 
3278551
 
405a1cd
c7d925f
3278551
 
 
 
4575e19
 
 
34af4b2
 
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
34af4b2
 
 
 
 
 
 
 
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
867da82
 
 
2793f59
 
34af4b2
3fce22f
 
 
 
 
 
2793f59
 
b062448
2793f59
 
 
 
 
 
 
dca2589
9e73eae
2793f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c22e76e
 
2793f59
9cde465
 
 
 
 
 
 
 
 
 
2793f59
 
 
 
 
 
 
38cbdbe
7b15919
 
2793f59
 
38cbdbe
 
2793f59
 
 
c5c280b
2793f59
c5c280b
 
 
38cbdbe
 
 
 
a0558d6
79dadaf
38cbdbe
c5c280b
a0558d6
c5c280b
 
 
 
 
 
 
9cde465
2793f59
 
9cde465
2793f59
 
 
 
 
 
 
 
 
ff43b57
2793f59
 
9cde465
 
 
 
5f001ee
9cde465
 
 
 
2793f59
 
 
 
 
 
 
 
 
38cbdbe
7b15919
 
2793f59
 
 
 
 
 
c5c280b
 
 
2793f59
c5c280b
 
4575e19
c5c280b
 
38cbdbe
 
 
 
a0558d6
79dadaf
38cbdbe
c5c280b
38cbdbe
4575e19
 
c5c280b
 
 
 
 
 
9cde465
2793f59
 
9cde465
2793f59
 
 
 
 
 
 
 
c5c280b
9e73eae
 
 
 
 
c5c280b
 
2793f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0558d6
2793f59
 
 
 
 
 
 
 
9b9289d
 
 
 
 
016b551
b4aba90
9b9289d
2793f59
 
 
9b9289d
 
 
016b551
b4aba90
9b9289d
 
 
 
 
 
2793f59
 
 
 
 
 
973a992
8cb15e9
973a992
2793f59
973a992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2793f59
62a1685
973a992
2793f59
 
 
 
 
 
3278551
 
 
 
b279269
3278551
 
 
76e5a34
2793f59
 
 
adfd98e
 
21dcb75
 
 
 
 
 
 
 
 
 
 
adfd98e
 
 
 
 
 
 
 
 
 
 
 
 
 
2793f59
34af4b2
2793f59
 
 
 
 
 
 
 
34af4b2
2793f59
 
34af4b2
2793f59
34af4b2
2793f59
 
34af4b2
 
 
 
2793f59
34af4b2
 
 
2793f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
from fastapi import FastAPI,Request,HTTPException,Response
from pydantic import BaseModel
from fastapi.responses import JSONResponse
from components.mongodbconnection import provideClient
from components.checkaccountexists import checkAccount
from components.endpointscheck import checkEndpoints
from components.dynamicendpointscheck import checkDynamicEndpoints
from components.updatedynamicendpoint import UpdateDynamicEndpoint
from components.getinvoices import GetInvoices
from components.updateendpointprice import UpdateEndpointPrice
from components.updatedynamicendpointprice import UpdateDynamicEndpointPrice
from openkitx403 import OpenKit403Middleware
import uuid
from fastapi.middleware.cors import CORSMiddleware
import json
import base64
from components.checktoken import TokenCheck
from components.middleware import x401Kit
















app=FastAPI()





client=provideClient()
origins=[
    "http://localhost:3000",
    "http://localhost:3001"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

'''
app.add_middleware(
    OpenKit403Middleware,
    audience="https://itsvelocity-velocity.hf.space/",
    issuer="my-api-v1",
    #token_gate=TokenCheck,
    excluded_paths=[
                    "/setupaccount",
                    "/checkaccount", 
                    "/register_endpoint",
                    "/register_dynamic_endpoint",
                    "/delete_endpoint",
                    "/delete_dynamic_endpoint",
                    "/checkendpoints",
                    "/checkdynamicendpoints",
                    "/update_dynamic_endpoint",
                    "/update_endpoint_price",
                    "/update_dynamic_endpoint_price",
                    "/docs",
                    "/openapi.json",
                    "/invoices"
                   
                   ]
                )
'''



app.add_middleware(
    x401Kit,
    protected_paths=[
        "/custom_account_login"
    ],
    required_mint="3d4XyPWkUJzruF5c2qc1QfpLgsaNaDLMtTya1bWBpump",
    mint_amount=100000,
    helius_api_key="",
    geo_code=False,
    geo_code_locs=["IN"]
)




db=client["x402ify_db"]
coll=db["x402ify_col"]
marketplace=db["x402marketplace"]
sdkdb=db["sdklogs_db"]
sdkcol=sdkdb["sdklogs_col"]











class SetupPayload(BaseModel):
        owner:str




class RegisterPayload(BaseModel):

        endpoint:str
        owner:str
        description:str
        meta:str
        price:str
        #pay_per_request:bool



class RegisterPayloadDynamic(BaseModel):
        
        endpoint:str
        owner:str
        description:str
        tag:str
        meta:str
        price:str
        #pay_per_request:bool



class RegisterAgent(BaseModel):
        agent:str
        owner:str
        description:str
        meta:str
        price:str
    






class Update(BaseModel):
       owner:str
       tag:str
       newendpoint:str




class Owner(BaseModel):
      owner:str


class Delete(BaseModel):
      owner:str
      endpoint_linker:str


class Invoice(BaseModel):
      owner:str


class UpdatePrice(BaseModel):
        owner:str
        endpoint_linker:str
        price:str










def saveToMarket(payload):

        
        marketplace.update_one(
                    {"owner": "system"},
                    {"$push": {"market": payload}}
                
                )
        




def UpdateMarketData(endpoint_method,endpoint_linker,status_code):

    
    call_success = 200 <= status_code < 300
    
    marketplace.update_one(
           { "owner": "system" },
                {
                "$set": { 
                    "market.$[elem].endpoint_method":endpoint_method,
                },
                "$inc": { 
                    "market.$[elem].calls": 1,
                    "market.$[elem].failures": 0 if call_success else 1
            }
        },
        array_filters=[{ "elem.endpoint_linker": endpoint_linker }]
        
    );










#POST requests proxy




        

#GET proxy with query params 













#'''post proxy tester'''

@app.post("/tester")
async def Tester(request:Request):
     body=await request.json()
     return {"data":body.get("name")}





@app.get("/accountlogin")
def AccountLogin(request:Request):
    return {"message":"login"}



@app.get("/custom_account_login")
def CustomAccountLogin(request:Request):
    return {"address":""}



#'''Account Setup'''

@app.post("/setupaccount")
def SetupAccount(data:Owner):
        
       
        payload={
                
                "owner":data.owner,
                "endpoints":[],
                "dynamicendpoints":[],
                "agents":[]

        }
        coll.insert_one(payload)
        return {"status":True}






#'''Account Check '''


@app.post("/checkaccount")
def checkaccount(data:Owner):
    status=checkAccount(data.owner)
    print(data)
    if status==True:
        return {'status':True}
    else:
        return {"status":False}
    





#''' Regular Endpoint Registration '''



@app.post("/register_endpoint")
def RegisterEndpoint(data:RegisterPayload):

    
        try:


            document=coll.find_one({'owner':data.owner})
            endpoints=document["endpoints"]
            if len(endpoints) > 0:
                for endpointobject in endpoints:
                    if endpointobject["endpoint"]==data.endpoint:
                        return {"status":False,"message":"endpoint already present"}
            
            
            short_id = str(uuid.uuid4())[:5]
            short_id_e=short_id+"x402"
            payload={
                "owner":data.owner,
                "endpoint":data.endpoint,
                "meta":data.meta,
                "endpoint_linker":short_id_e,
                 "price":data.price,
                "invoices":[]
                #"pay_per_request":data.pay_per_request
            }

            

            coll.update_one(
                    {"owner": data.owner},
                    {"$push": {"endpoints": payload}}
                
                )

            marketpayload={
                
                 "owner":data.owner,
                 "endpoint_linker":short_id_e,
                 "meta":data.meta,
                 "description":data.description,
                 "endpoint_method":"",
                  "price":data.price,
                 "endpoint_mcp_base":"https://mcpv100-production.up.railway.app/mcp:8080",
                 "calls":0,
                "failures":0
                
                
            }

            saveToMarket(marketpayload)

            
            return {"status":True,"x402ify_tag":short_id_e,"message":"done"}
        
        except Exception as e:
            return {"status":False,"x402ify_tag":str(e),"message":"error"}






#''' Dynamic Endpoint Creation '''

@app.post("/register_dynamic_endpoint")
async def RegisterDynamicEndpoint(data:RegisterPayloadDynamic):
        
        try:



            document=coll.find_one({'owner':data.owner})
            endpoints=document["dynamicendpoints"]
            if len(endpoints) > 0:
                for endpointobject in endpoints:
                    if endpointobject["endpoint"]==data.endpoint:
                        return {"status":False,"message":"endpoint already present"}
        
            payload={
                  
                
                "owner":data.owner,
                "endpoint":data.endpoint,
                "description":data.description,
                "meta":data.meta,
                "endpoint_linker":f"{data.tag}",
                "price":data.price,
                "invoices":[]
                #"pay_per_request":data.pay_per_request
                }
            

            coll.update_one(
                    {"owner": data.owner},
                    {"$push": {"dynamicendpoints": payload}}


                
                )



            marketpayload={
                
                 "owner":data.owner,
                 "endpoint_linker":short_id_e,
                 "meta":data.meta,
                 "description":data.description,
                 "endpoint_method":"",
                  "price":data.price,
                 "endpoint_mcp_base":"https://mcpv100-production.up.railway.app/mcp:8080",
                 "calls":0,
                 "failures":0
                
                
            }
            

            saveToMarket(marketpayload)

            
            return {"status":True,"x402ify_tag":f"{data.tag}","message":"done"}
        
        except Exception as e:
            return {"status":False,"x402ify_tag":str(e),"message":"error"}
















#'''Regular Endpoint Deletion'''

@app.post("/delete_endpoint")
def DeleteEndpoint(data:Delete):

    try:
        coll.update_one(
        {"owner": data.owner},
        {"$pull": {"endpoints": {"endpoint_linker": data.endpoint_linker}}})
        return {"status":True}

    except Exception as e:
          return {"status":False}


        

#'''Dynamic Endpoint Deletion '''



@app.post("/delete_dynamic_endpoint")
def DeleteDynamicEndpoint(data:Delete):

    try:
        coll.update_one(
        {"owner": data.owner},
        {"$pull": {"dynamicendpoints": {"endpoint_linker": data.endpoint_linker}}})
        return {"status":True}

    except Exception as e:
          return {"status":False}







#'''Regular Endpoint Fetching'''


@app.post("/checkendpoints")
def checkEP(data:Owner):
     print(data.owner)
     endpoints=checkEndpoints(data.owner)
     return endpoints  


#''' Dynamic Endpoint Fetching '''


@app.post("/checkdynamicendpoints")
def checkDEP(data:Owner):
      dendpoints=checkDynamicEndpoints(data.owner)
      return dendpoints





#''' Dynamic Endpoint Updation  '''

@app.post("/update_dynamic_endpoint")
def UpdateDynamic(data:Update):
      print(data.tag)
      status=UpdateDynamicEndpoint(data.owner,data.tag,data.newendpoint)
      return status



@app.post("/update_endpoint_price")
def UpdateEndpointPriceRoute(data:UpdatePrice):
    
      
      status=UpdateEndpointPrice(data.owner,data.endpoint_linker,data.price)
      return status



@app.post("/update_dynamic_endpoint_price")
def UpdateDynamicEndpointPriceRoute(data:UpdatePrice):
    
      
      status=UpdateDynamicEndpointPrice(data.owner,data.endpoint_linker,data.price)
      return status



    

#'''Fetch Invoices '''


@app.post("/invoices")
def getaudit(data:Invoice):
    invoices=GetInvoices(data.owner)
    result = []

    for item in invoices:
        
        owner = item.get("owner")
        endpoint = item.get("endpoint")
        endpoint_linker = item.get("endpoint_linker")
        signature = item.get("signature")

        #Navigate into nested fields
        payment = item["details"]["payment_request"]

        #Some entries use "accepts", some use flat structure
        if "accepts" in payment:
            pay_info = payment["accepts"][0]
        else:
            pay_info = payment

        payTo = pay_info.get("payTo")
        asset = pay_info.get("asset")
        amount = pay_info.get("maxAmountRequired")

        result.append({
            "owner": owner,
            "endpoint_linker": endpoint_linker,
            "endpoint": endpoint,
            "signature": signature,
            "payTo": payTo,
            "asset": asset,
            "amount": amount
        })
        
    return {"invoices":result}

    





@app.get("/marketdata")
def MarketData(request:Request):
    marketdocument=marketplace.find_one({"owner":"system"})
    marketdata=marketdocument["market"]
    return  {"market":marketdata}
    


#'''GET Endpoints Proxy endpoint'''   





@app.get("/sdklogs/{id}")
def SDKLog(id):
    document=col.find_one({"owner":"system"})
    details=None
    if document:
        logs=document["logs"]
        for data in logs:
            if data.signer==id:
                details=data
                
    return JSONResponse(content={"details":details})