@@ -64,3 +64,60 @@ export async function generateAttestationData(
6464 new Uint8Array ( [ sig . v ] ) ,
6565 ] )
6666}
67+
68+ /**
69+ * Verifies the signer of an attestation data.
70+ * @param attestationData The attestation data to verify.
71+ * @param expectedSigner The expected signer address.
72+ * @param disputeManagerAddress The address of the dispute manager contract.
73+ * @param chainId The chain ID.
74+ * @returns True if the signer matches the expected signer, false otherwise.
75+ */
76+ export function verifyAttestationSigner (
77+ attestationData : string ,
78+ expectedSigner : string ,
79+ disputeManagerAddress : string ,
80+ chainId : number ,
81+ ) : boolean {
82+ try {
83+ // Extract components from attestation data
84+ const data = ethers . getBytes ( attestationData )
85+
86+ // Each CID and deployment ID is 32 bytes
87+ const requestCID = ethers . hexlify ( data . slice ( 0 , 32 ) )
88+ const responseCID = ethers . hexlify ( data . slice ( 32 , 64 ) )
89+ const subgraphDeploymentID = ethers . hexlify ( data . slice ( 64 , 96 ) )
90+
91+ // Extract signature components
92+ const r = ethers . hexlify ( data . slice ( 96 , 128 ) )
93+ const s = ethers . hexlify ( data . slice ( 128 , 160 ) )
94+ const v = data [ 160 ]
95+
96+ // Create the domain for the EIP712 signature
97+ const domain = {
98+ name : 'Graph Protocol' ,
99+ version : '0' ,
100+ chainId : chainId ,
101+ verifyingContract : disputeManagerAddress ,
102+ salt : EIP712_DISPUTE_MANAGER_DOMAIN_SALT ,
103+ }
104+
105+ // Create receipt struct
106+ const receipt = {
107+ requestCID,
108+ responseCID,
109+ subgraphDeploymentID,
110+ }
111+
112+ // Recover the signer address
113+ const signature = ethers . Signature . from ( { r, s, v } )
114+ const recoveredAddress = ethers . verifyTypedData ( domain , EIP712_ATTESTATION_PROOF_TYPES , receipt , signature )
115+ console . log ( `recoveredAddress: ${ recoveredAddress } ` )
116+
117+ // Compare addresses (case-insensitive)
118+ return recoveredAddress . toLowerCase ( ) === expectedSigner . toLowerCase ( )
119+ } catch ( error ) {
120+ console . log ( error )
121+ return false
122+ }
123+ }
0 commit comments