-
Notifications
You must be signed in to change notification settings - Fork 0
/
prover.ts
159 lines (137 loc) · 4.37 KB
/
prover.ts
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
import { ethers } from 'ethers'
import { abi as proverAbi } from '@relicprotocol/contracts/abi/IProver.json'
import { abi as batchProverAbi } from '@relicprotocol/contracts/abi/IBatchProver.json'
import { abi as ephemeralFactsAbi } from '@relicprotocol/contracts/abi/IEphemeralFacts.json'
import {
BlockProof,
Prover,
EphemeralProver,
RelicAddresses,
} from '@relicprotocol/types'
import type { RelicClient } from '../client'
import { RelicAPI } from '../api'
import { isProxyL2Deployment } from '../utils'
export interface ReceiverContext {
initiator: string
receiver: string
extra?: string
gasLimit: ethers.BigNumberish
}
export type ProofData = {
proof: string
sigData: string
}
export type BatchProofData = {
proof: string
sigDatas: Array<string>
}
// wrap a RelicAPI with one that ensures blockProofs can be
// validated on the target chain
function wrapAPI(client: RelicClient): RelicAPI {
if (!isProxyL2Deployment(client.chainId, client.dataChainId)) {
return client.api
}
function isBlockProof(obj: any): obj is BlockProof {
return (obj.blockNum && obj.header && obj.blockProof) !== undefined
}
return new Proxy(client.api, {
get(target: RelicAPI, p: keyof RelicAPI) {
if (p.startsWith('_')) return target[p]
if (target[p] instanceof Function) {
return async (...args: any[]) => {
const lastVerifiable =
await client.blockHistory.getLastVerifiableBlock()
target.setBaseBlock(lastVerifiable.toNumber())
let result = await (target[p] as Function).call(target, ...args)
if (isBlockProof(result)) {
await client.blockHistory.ensureValidProof(result)
}
return result
}
}
},
})
}
export abstract class ProverImpl<Params> implements Prover {
readonly client: RelicClient
readonly contract: ethers.Contract
readonly api: RelicAPI
constructor(client: RelicClient, key: keyof RelicAddresses) {
this.client = client
this.contract = new ethers.Contract(
client.addresses[key],
proverAbi,
client.provider
)
this.api = wrapAPI(client)
}
abstract getProofData(params: Params): Promise<ProofData>
async prove(params: Params): Promise<ethers.PopulatedTransaction> {
const { proof } = await this.getProofData(params)
return await this.contract.populateTransaction.prove(proof, true, {
value: await this.fee(),
})
}
fee(): Promise<ethers.BigNumber> {
return this.client.reliquary.getFee(this.contract.address)
}
}
export abstract class EphemeralProverImpl<Params>
extends ProverImpl<Params>
implements EphemeralProver
{
readonly ephemeralFacts: ethers.Contract
constructor(client: RelicClient, key: keyof RelicAddresses) {
super(client, key)
this.ephemeralFacts = new ethers.Contract(
client.addresses.ephemeralFacts,
ephemeralFactsAbi,
client.provider
)
}
/**
* Proves a fact ephemerally and deliver it to the receiver
* If context.extra is undefined, it will default to the fact signature data
* for compatibility with the RelicReceiver SDK contracts
*/
async proveEphemeral(
context: ReceiverContext,
params: Params
): Promise<ethers.PopulatedTransaction> {
const { proof, sigData } = await this.getProofData(params)
// default value for extra = fact signature data
if (!context.extra) {
context = { ...context, extra: sigData }
}
return await this.ephemeralFacts.populateTransaction.proveEphemeral(
context,
this.contract.address,
proof,
{ value: await this.fee() }
)
}
}
export abstract class BatchProverImpl<Params> implements Prover {
readonly client: RelicClient
readonly contract: ethers.Contract
readonly api: RelicAPI
constructor(client: RelicClient, key: keyof RelicAddresses) {
this.client = client
this.contract = new ethers.Contract(
client.addresses[key],
batchProverAbi,
client.provider
)
this.api = wrapAPI(client)
}
abstract getProofData(params: Params): Promise<BatchProofData>
async prove(params: Params): Promise<ethers.PopulatedTransaction> {
const { proof } = await this.getProofData(params)
return await this.contract.populateTransaction.proveBatch(proof, true, {
value: await this.fee(),
})
}
fee(): Promise<ethers.BigNumber> {
return this.client.reliquary.getFee(this.contract.address)
}
}