Skip to content

Commit

Permalink
Updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
br0wnD3v committed Feb 6, 2024
1 parent 0dcd59a commit dd2ceec
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 89 deletions.
9 changes: 5 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"version": 1,
"deployAliases": {
"berkeley": {
"url": "https://proxy.berkeley.minaexplorer.com/graphql",
"networkId": "testnet",
"url": "https://api.minascan.io/node/berkeley/v1/graphql",
"keyPath": "keys/berkeley.json",
"feepayerKeyPath": "/home/botvenom/.cache/zkapp-cli/keys/deployer.json",
"feepayerAlias": "deployer",
"fee": "1",
"feepayerKeyPath": "/home/botvenom/.cache/zkapp-cli/keys/oracle.json",
"feepayerAlias": "oracle",
"fee": "0.1",
"smartContract": "Doot"
}
}
Expand Down
62 changes: 23 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
"ts-jest": "^27.0.7",
"typescript": "^4.7.2"
},
"peerDependencies": {
"o1js": "0.14.*"
},
"dependencies": {
"dotenv": "^16.3.1",
"o1js-pack": "^0.4.3"
"o1js": "^0.16.0",
"o1js-pack": "^0.5.1"
}
}
2 changes: 0 additions & 2 deletions src/Doot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ describe('Doot.js', () => {
const onChainCommitment = dootZkApp.commitment.get();
const onChainSecret = dootZkApp.secretToken.get();

console.log('INITIAL VALUES --->>>');
console.log(onChainIpfsCid.toString(), onChainCommitment.toBigInt());
expect(onChainIpfsCid.toString()).toEqual(expectedIpfsCid);
expect(onChainCommitment).toEqual(updatedCommitment);
expect(onChainSecret).toEqual(Poseidon.hash([secret]));
Expand Down
61 changes: 21 additions & 40 deletions src/Doot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import { MultiPackedStringFactory } from 'o1js-pack';

export class IpfsCID extends MultiPackedStringFactory(4) {}
export class IpfsCID extends MultiPackedStringFactory(2) {}

export class Doot extends SmartContract {
/// @notice Merkle Map Root to make sure the values are valid.
Expand All @@ -39,23 +39,16 @@ export class Doot extends SmartContract {
updatedCID: IpfsCID,
secret: Field
) {
const currentOracle = this.oraclePublicKey.get();
this.oraclePublicKey.assertEquals(currentOracle);

const currentSecretToken = this.secretToken.get();
this.secretToken.assertEquals(currentSecretToken);

const currentCommitment = this.commitment.get();
this.commitment.assertEquals(currentCommitment);

const currentCID = this.ipfsCID.get();
this.ipfsCID.assertEquals(currentCID);
this.oraclePublicKey.getAndRequireEquals();
this.secretToken.getAndRequireEquals();
this.commitment.getAndRequireEquals();
this.ipfsCID.getAndRequireEquals();

const sentSecret = Poseidon.hash([secret]);
this.secretToken.assertEquals(sentSecret);

const [previousCommitment, key] = keyWitness.computeRootAndKey(valueBefore);
previousCommitment.assertEquals(currentCommitment);
previousCommitment.assertEquals(this.commitment.get());
key.assertEquals(keyToChange);

const updatedCommitment = keyWitness.computeRootAndKey(valueToChange)[0];
Expand All @@ -69,20 +62,13 @@ export class Doot extends SmartContract {
updatedIpfsCID: IpfsCID,
secret: Field
) {
const currentSecretToken = this.secretToken.get();
this.secretToken.assertEquals(currentSecretToken);

const currentOracle = this.oraclePublicKey.get();
this.oraclePublicKey.assertEquals(currentOracle);

const currentCommitment = this.commitment.get();
this.commitment.assertEquals(currentCommitment);

const currentCID = this.ipfsCID.get();
this.ipfsCID.assertEquals(currentCID);
this.oraclePublicKey.getAndRequireEquals();
this.secretToken.getAndRequireEquals();
this.commitment.getAndRequireEquals();
this.ipfsCID.getAndRequireEquals();

const sentSecret = Poseidon.hash([secret]);
this.secretToken.assertEquals(sentSecret);
this.secretToken.requireEquals(sentSecret);

this.commitment.set(updatedCommitment);
this.ipfsCID.set(updatedIpfsCID);
Expand All @@ -93,20 +79,13 @@ export class Doot extends SmartContract {
updatedIpfsCID: IpfsCID,
updatedSecret: Field
) {
const currentSecretToken = this.secretToken.get();
this.secretToken.assertEquals(currentSecretToken);

const currentOracle = this.oraclePublicKey.get();
this.oraclePublicKey.assertEquals(currentOracle);

const currentCommitment = this.commitment.get();
this.commitment.assertEquals(currentCommitment);

const currentCID = this.ipfsCID.get();
this.ipfsCID.assertEquals(currentCID);
this.oraclePublicKey.getAndRequireEquals();
this.secretToken.getAndRequireEquals();
this.commitment.getAndRequireEquals();
this.ipfsCID.getAndRequireEquals();

/// Can only be called once
this.secretToken.assertEquals(Field.from(0));
this.secretToken.requireEquals(Field.from(0));

this.commitment.set(updatedCommitment);
this.ipfsCID.set(updatedIpfsCID);
Expand All @@ -115,10 +94,12 @@ export class Doot extends SmartContract {

@method verify(signature: Signature, Price: Field) {
// Get the oracle public key from the contract state
const oraclePublicKey = this.oraclePublicKey.get();
this.oraclePublicKey.assertEquals(oraclePublicKey);
this.oraclePublicKey.getAndRequireEquals();

// Evaluate whether the signature is valid for the provided data
const validSignature = signature.verify(oraclePublicKey, [Price]);
const validSignature = signature.verify(this.oraclePublicKey.get(), [
Price,
]);
// Check that the signature is valid
validSignature.assertTrue();
}
Expand Down
50 changes: 50 additions & 0 deletions src/initBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const ORACLE_KEY = '';
const DOOT_KEY = '';
const MINA_SECRET = '';

import { Doot, IpfsCID } from './Doot.js';
import { Mina, PrivateKey, Field, fetchAccount } from 'o1js';

const COMMITMENT = Field.from('');
const IPFS_HASH: IpfsCID = IpfsCID.fromString('');
const SECRET: Field = Field.from(MINA_SECRET);

const doot = PrivateKey.fromBase58(DOOT_KEY);
const oracle = PrivateKey.fromBase58(ORACLE_KEY);
const oraclePub = oracle.toPublicKey();
const dootPub = doot.toPublicKey();

const Berkeley = Mina.Network(
'https://api.minascan.io/node/berkeley/v1/graphql'
);
Mina.setActiveInstance(Berkeley);

let accountInfo = {
publicKey: dootPub,
};
await fetchAccount(accountInfo);
accountInfo = {
publicKey: oraclePub,
};
await fetchAccount(accountInfo);

// const cacheFiles = await fetchFiles();
// await Doot.compile({ cache: DootFileSystem(cacheFiles) });
// const zkapp = new Doot(dootPub);
await Doot.compile();
const zkapp = new Doot(dootPub);

const transactionFee = 100_000_000;

console.log('Proving and sending txn...');
const txn = await Mina.transaction(
{ sender: oraclePub, fee: transactionFee },
() => {
zkapp.initBase(COMMITMENT, IPFS_HASH, SECRET);
}
);
await txn.prove();
console.log('Proved.');

const res = await txn.sign([oracle]).send();
console.log('Sent txn:', res.hash());

0 comments on commit dd2ceec

Please sign in to comment.