0x0x230x

Untitled

May 30th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import 'server-only';
  2.  
  3. import { env } from '../config/env';
  4. import { ReclaimProofRequest } from '@reclaimprotocol/js-sdk';
  5. import { supabaseClient } from '../config/supabaseClient';
  6. import { sign } from 'jsonwebtoken';
  7.  
  8. interface GigResponse {
  9. action_params: Record<string, any>;
  10. platform: string;
  11. gig_type: {
  12. id: string;
  13. required_params: { keys: string[] };
  14. display: {
  15. [key: string]: any;
  16. };
  17. };
  18. }
  19.  
  20. class ReclaimService {
  21. static instance: ReclaimService;
  22.  
  23. static getInstance() {
  24. if (!ReclaimService.instance) {
  25. ReclaimService.instance = new ReclaimService();
  26. }
  27. return ReclaimService.instance;
  28. }
  29.  
  30. async getReclaimConfig({ gigId, userId }: { gigId: number; userId: number }) {
  31. const { data: gig, error } = await supabaseClient
  32. .from('gigbot_gigs')
  33. .select('action_params,platform,gig_type(id,required_params,display)')
  34. .eq('id', gigId)
  35. .single();
  36.  
  37. if (error) {
  38. throw new Error('Gig not found');
  39. }
  40.  
  41. const _gig = gig as unknown as GigResponse;
  42. const platformConfig = _gig?.gig_type?.display?.[_gig.platform];
  43.  
  44. if (!platformConfig) {
  45. throw new Error(
  46. `Could not find configuration for platform '${_gig.platform}' for this gig type '${_gig.gig_type.id}'.`,
  47. );
  48. }
  49.  
  50. if (!platformConfig.reclaim_params_key) {
  51. throw new Error('Missing reclaim_params_key in platform config.');
  52. }
  53. if (!platformConfig.reclaim_params_value) {
  54. throw new Error('Missing reclaim_params_value in platform config.');
  55. }
  56. if (!platformConfig.reclaim_provider_id) {
  57. throw new Error('Missing reclaim_provider_id in platform config.');
  58. }
  59.  
  60. const actionParams = gig?.action_params as Record<string, any> | null;
  61.  
  62. const reclaimKey = platformConfig.reclaim_params_key;
  63. const actionParamsReclaimKey = platformConfig.reclaim_params_key;
  64. const actionValue = actionParams
  65. ? actionParams[actionParamsReclaimKey]
  66. : '';
  67.  
  68. let reclaimValue = platformConfig.reclaim_params_value;
  69.  
  70. if (reclaimValue && actionParamsReclaimKey && actionValue) {
  71. reclaimValue = reclaimValue.replace(
  72. `<${actionParamsReclaimKey}>`,
  73. actionValue,
  74. );
  75. }
  76.  
  77. const reclaimProofRequest = await ReclaimProofRequest.init(
  78. env.RECLAIM_APPLICATION_ID,
  79. env.RECLAIM_APPLICATION_SECRET,
  80. platformConfig.reclaim_provider_id,
  81. );
  82.  
  83. const token = sign(
  84. {
  85. gigId,
  86. userId,
  87. },
  88. env.RECLAIM_JWT_SECRET,
  89. { expiresIn: '1h' },
  90. );
  91.  
  92. reclaimProofRequest.addContext('metadata', token);
  93. reclaimProofRequest.setParams({
  94. [reclaimKey]: reclaimValue,
  95. });
  96. reclaimProofRequest.setAppCallbackUrl(
  97. `${env.NEXT_PUBLIC_APP_BASE_URL}api/reclaim/verify-proof`,
  98. );
  99.  
  100. const jsonString = reclaimProofRequest.toJsonString();
  101. console.log('jsonString', jsonString);
  102. return jsonString;
  103. }
  104. }
  105.  
  106. export const reclaimService = new ReclaimService();
  107.  
Advertisement
Add Comment
Please, Sign In to add comment