1- import { BytesLike , ethers , id , Signature , Wallet } from 'ethers'
1+ import { BytesLike , ethers , Signature , Wallet } from 'ethers'
22
3- import type { RAV , Receipt } from './types'
3+ import type { RAV } from './types'
44
55export const EIP712_DOMAIN_NAME = 'GraphTallyCollector'
66export const EIP712_DOMAIN_VERSION = '1'
7-
8- export const EIP712_RAV_PROOF_TYPEHASH = id (
9- 'ReceiptAggregateVoucher(bytes32 collectionId,address payer,address serviceProvider,address dataService,uint64 timestampNs,uint128 valueAggregate,bytes metadata)' ,
10- )
11- export const EIP712_RAV_PROOF_TYPES = {
7+ export const EIP712_RAV_TYPES = {
128 ReceiptAggregateVoucher : [
139 { name : 'collectionId' , type : 'bytes32' } ,
1410 { name : 'payer' , type : 'address' } ,
@@ -20,22 +16,6 @@ export const EIP712_RAV_PROOF_TYPES = {
2016 ] ,
2117}
2218
23- export const EIP712_RECEIPT_PROOF_TYPEHASH = id (
24- 'Receipt(bytes32 collection_id,address payer,address data_service,address service_provider,uint64 timestamp_ns,uint64 nonce,uint128 value)' ,
25- )
26-
27- export const EIP712_RECEIPT_PROOF_TYPES = {
28- Receipt : [
29- { name : 'collection_id' , type : 'bytes32' } ,
30- { name : 'payer' , type : 'address' } ,
31- { name : 'data_service' , type : 'address' } ,
32- { name : 'service_provider' , type : 'address' } ,
33- { name : 'timestamp_ns' , type : 'uint64' } ,
34- { name : 'nonce' , type : 'uint64' } ,
35- { name : 'value' , type : 'uint128' } ,
36- ] ,
37- }
38-
3919/**
4020 * Generates a signed RAV
4121 * @param allocationId The allocation ID
@@ -83,12 +63,27 @@ export async function generateSignedRAV(
8363
8464 // Sign the RAV data
8565 const signer = new Wallet ( signerPrivateKey )
86- const signature = await signer . signTypedData ( domain , EIP712_RAV_PROOF_TYPES , ravData )
66+ const signature = await signer . signTypedData ( domain , EIP712_RAV_TYPES , ravData )
8767
8868 // Return the signed RAV
8969 return { rav : ravData , signature : signature }
9070}
9171
72+ export function recoverRAVSigner (
73+ rav : RAV ,
74+ signature : string ,
75+ graphTallyCollectorAddress : string ,
76+ chainId : number ,
77+ ) : string {
78+ const domain = {
79+ name : EIP712_DOMAIN_NAME ,
80+ version : EIP712_DOMAIN_VERSION ,
81+ chainId,
82+ verifyingContract : graphTallyCollectorAddress ,
83+ }
84+ return ethers . verifyTypedData ( domain , EIP712_RAV_TYPES , rav , signature )
85+ }
86+
9287/**
9388 * Generates a signer proof for authorizing a signer in the Graph Tally Collector
9489 * @param graphTallyCollector The Graph Tally Collector contract
@@ -120,26 +115,3 @@ export function generateSignerProof(
120115 const signer = new Wallet ( signerPrivateKey )
121116 return Signature . from ( signer . signingKey . sign ( proofToDigest ) ) . serialized
122117}
123-
124- export function recoverReceiptSigner (
125- receipt : Receipt ,
126- signature : Uint8Array | string ,
127- graphTallyCollectorAddress : string ,
128- chainId : number ,
129- ) : string {
130- // Create the domain for the EIP712 signature
131- const domain = {
132- name : EIP712_DOMAIN_NAME ,
133- version : EIP712_DOMAIN_VERSION ,
134- chainId,
135- verifyingContract : graphTallyCollectorAddress ,
136- }
137-
138- // Normalize signature to 0x-hex string
139- const sigHex = typeof signature === 'string' ? signature : ethers . hexlify ( signature )
140-
141- // Recover the signer address from the signature
142- const signerAddress = ethers . verifyTypedData ( domain , EIP712_RECEIPT_PROOF_TYPES , receipt , sigHex )
143-
144- return signerAddress
145- }
0 commit comments