diff --git a/.evn.template b/.evn.template index 4358e6d..0dda49c 100644 --- a/.evn.template +++ b/.evn.template @@ -4,4 +4,5 @@ INFURA_DEV_KEY= PORT= DESIRED_CHAIN_ID= START_BLOCK_HEIGHT= -SUBGRAPH_URI= \ No newline at end of file +SUBGRAPH_URI= +REVOKE_CONSENT_TEXT= \ No newline at end of file diff --git a/resolvers.ts b/resolvers.ts index 23441bb..ac5ad73 100644 --- a/resolvers.ts +++ b/resolvers.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { StoredPendingMetadata, StoredPendingMetadataModel } from './models/StoredPendingMetadataModel'; -import { getConsentMessageToSign, addSignedConsent as handleAddSignedConsent, consentNeeded as handleConsentNeeded } from './services/consentService'; +import { getConsentMessageToSign, handleAddSignedConsent, consentNeeded as handleConsentNeeded, getRevokeConsentMessageToSign, revokeSignedConsent } from './services/consentService'; import { multiply } from './services/demoService'; -import { addPendingMetadataFromClient, getMetadataUploadMessageToSign, verifyMetadataSignature } from './services/metadataService'; -import { MultiplyPayloadDemo, Result } from './types'; +import { addPendingMetadataFromClient, getMetadataUploadMessageToSign } from './services/metadataService'; +import { MultiplyPayloadDemo } from './types'; export const resolvers = { Query: { @@ -34,6 +33,10 @@ export const resolvers = { getConsentMessageToSign(_root: any, _args: any) { const text = getConsentMessageToSign(); return text; + }, + getRevokeConsentMessageToSign(_root: any, _args: any) { + const text = getRevokeConsentMessageToSign(); + return text; } }, Mutation: { @@ -44,6 +47,8 @@ export const resolvers = { const signature = args.signature as string; const result = await addPendingMetadataFromClient(pendingTxHash, metadata, signingAddress, signature); + console.log(`addPendingMetadata: pendingTxHash ${pendingTxHash} result ${result.success} ${result.message} metadata ${metadata} signingAddress ${signingAddress} signature ${signature}`); + return result; }, async addSignedConsent(_root: any, args: any) { @@ -53,6 +58,14 @@ export const resolvers = { const result = await handleAddSignedConsent(signingAddress, signature, consentText); return result; + }, + async revokeSignedConsent(_root: any, args: any) { + const signingAddress = args.signingAddress as string; + const signature = args.signature as string; + const consentText = args.consentText as string; + + const result = await revokeSignedConsent(signingAddress, signature, consentText); + return result; } } }; \ No newline at end of file diff --git a/schema.ts b/schema.ts index c1ba99e..8b42301 100644 --- a/schema.ts +++ b/schema.ts @@ -21,6 +21,7 @@ export const schemaDefs: TypeSource = gql` multiply(value1: Int!, value2: Int!): MultiplyResult! getMetadataUploadMessageToSign(txHash: String!, metadata: String!): String! getConsentMessageToSign: String! + getRevokeConsentMessageToSign: String! consentNeeded(address: String!): Boolean! } @@ -36,5 +37,10 @@ export const schemaDefs: TypeSource = gql` signature: String!, consentText: String! ): Result! + revokeSignedConsent( + signingAddress: String!, + signature: String!, + consentText: String! + ): Result! } `; diff --git a/services/consentService.ts b/services/consentService.ts index 2b3a9d5..c767ef8 100644 --- a/services/consentService.ts +++ b/services/consentService.ts @@ -8,7 +8,13 @@ export const getConsentMessageToSign = () => { else throw new Error('CONSENT_TEXT variable not set'); }; -export const addSignedConsent = async (signingAddress: string, signature: string, consentText: string): Promise => { +export const getRevokeConsentMessageToSign = () => { + const text = process.env.REVOKE_CONSENT_TEXT; + if (text) return text; + else throw new Error('REVOKE_CONSENT_TEXT variable not set'); +}; + +export const handleAddSignedConsent = async (signingAddress: string, signature: string, consentText: string): Promise => { if (consentText !== getConsentMessageToSign()) { return { success: false, message: 'Consent text does not match current consent' }; @@ -31,9 +37,36 @@ export const addSignedConsent = async (signingAddress: string, signature: string }; +export const revokeSignedConsent = async (signingAddress: string, signature: string, consentText: string): Promise => { + + if (consentText !== getRevokeConsentMessageToSign()) { + return { success: false, message: 'Consent text does not match current consent' }; + } + + const signatureMatch = verifyMessageSafe(signingAddress,consentText, signature); + + if (signatureMatch) { + if (await consentExists(signingAddress)) { + await StoredConsentModel.deleteMany({ address: signingAddress }); + return { success: true }; + } + else { + return { success: false, message: 'Consent for this address does not exists' }; + } + } + else { + return { success: false, message: 'Signature is not matching consent and address' }; + } + +}; + export const consentNeeded = async (address: string) => { const result = await StoredConsentModel.findOne({ address }); if (result) return false; else return true; +}; + +export const consentExists = async (address: string) => { + return ! await consentNeeded(address); }; \ No newline at end of file diff --git a/tests/services/metadataServiceEventsCheck.test.ts b/tests/services/metadataServiceEventsCheck.test.ts index 194298f..a668ab7 100644 --- a/tests/services/metadataServiceEventsCheck.test.ts +++ b/tests/services/metadataServiceEventsCheck.test.ts @@ -9,6 +9,8 @@ import { initWeb3Provider, web3provider } from '../../web3/web3provider'; describe('metadata update on share event', () => { beforeAll(async () => { + process.env.START_BLOCK_HEIGHT = '12258810'; + await initMongoose(); initWeb3Provider(); });