-
Notifications
You must be signed in to change notification settings - Fork 7
/
DelegationOracleVerifier.ts
69 lines (55 loc) · 2.34 KB
/
DelegationOracleVerifier.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
/* This smart contract takes in an oracle source */
import {
Field,
SmartContract,
state,
State,
method,
DeployArgs,
Permissions,
PublicKey,
Signature,
UInt64,
UInt32,
} from 'snarkyjs';
// The public key of our trusted data provider
const ORACLE_PUBLIC_KEY =
'B62qphyUJg3TjMKi74T2rF8Yer5rQjBr1UyEG7Wg9XEYAHjaSiSqFv1';
export class DelegationOracle extends SmartContract {
@state(PublicKey) oraclePublicKey = State<PublicKey>();
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
});
}
@method init() {
// Initialize contract state
this.oraclePublicKey.set(PublicKey.fromBase58(ORACLE_PUBLIC_KEY));
}
// Define contract events
events = {
verified: PublicKey,
producerKey: PublicKey,
epoch: UInt32
};
// This method verifies that the amount received by the delegating key is greater than the amount owed and if so will emit an event that can be read on-chain
@method verify(epoch: UInt32, publicKey: PublicKey, producerKey: PublicKey, blocksWon: UInt32, delegatedBalance: UInt64, totalDelegatedBalance: UInt64, amountOwed: UInt64, amountSent: UInt64, signature: Signature) {
let oraclePublicKey = this.oraclePublicKey.get();
this.oraclePublicKey.assertEquals(oraclePublicKey);
// Hack - need to understand why this doesn't work as is but works like this. Otherwise error is https://gist.github.com/garethtdavies/b5deadda86f1fd4a3b5b9efb13a0284e
oraclePublicKey = PublicKey.fromBase58("B62qphyUJg3TjMKi74T2rF8Yer5rQjBr1UyEG7Wg9XEYAHjaSiSqFv1");
// Evaluate whether the signature is valid for the provided data
const signedData = epoch.toFields().concat(publicKey.toFields()).concat(producerKey.toFields()).concat(blocksWon.toFields()).concat(delegatedBalance.toFields()).concat(totalDelegatedBalance.toFields()).concat(amountOwed.toFields()).concat(amountSent.toFields());
const validSignature = signature.verify(oraclePublicKey, signedData);
// Check that the signature is valid
validSignature.assertTrue();
// Check that they have paid enough
amountSent.assertGte(amountOwed);
// Emit an event containing the verified users id
this.emitEvent('verified', publicKey);
this.emitEvent('producerKey', producerKey);
this.emitEvent('epoch', epoch);
}
}