Skip to content

Commit

Permalink
Merge pull request #14 from hollow-leaf/feat/contractInteract
Browse files Browse the repository at this point in the history
feat: contract interact
  • Loading branch information
kidneyweakx authored Dec 12, 2023
2 parents ff4875b + 680f2e4 commit 1b8c5ab
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 14 deletions.
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "module",
"scripts": {
"build": "tsc",
"start": "ts-node ./src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand Down
22 changes: 11 additions & 11 deletions apps/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import express from "express"
import cors from "cors"
import { UserKey } from "./services/mina.js"
import { UserKey, contractInteract } from "./services/mina.js"
import { gameState, latestGame } from "./services/game.js"

const app = express()

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get("/test", async (req, res) => {
await contractInteract()
res.json({ done: true })
})

app.get("/latest_game", async (req, res) => {
const latest_game = await latestGame()
const state = {
game_id: 1,
game_id: latest_game,
}
res.json(state)
})

app.get("/get_game/:id", async (req, res) => {
const id = req.params.id

const state = {
game_id: id,
game_state: "draw",
draw_count: 3,
max: 10,
prize_list: [],
}

const state = await gameState(id)
res.json(state)
})

app.get("/get_game_user/:id", async (req, res) => {
app.post("/get_game_user/:id", async (req, res) => {
const id = req.params.id
const address = req.body.user_address

Expand Down
84 changes: 84 additions & 0 deletions apps/backend/src/contract/mental.js

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

File renamed without changes.
38 changes: 38 additions & 0 deletions apps/backend/src/services/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { get, insert, update } from "./db.js"

export const latestGame = async () => {
const num = await get("latest_game")
if (!num) {
await insert({ _id: "latest_game", game_id: 0 })
}
const { game_id } = await get("latest_game") as any
return game_id
}

const gamePlus = async () => {
const { game_id } = await get("latest_game") as any
await update("latest_game", {
game_id: game_id + 1,
})
}

export const gameState = async (game_id: string) => {
const game = await get(game_id)
if (!game) {
await insert({
_id: game_id,
game_state: "not_start",
draw_count: 0,
max: 0,
prize_list: [],
})
}
const { game_state, draw_count, max, prize_list } = await get(game_id) as any
return {
game_id,
game_state,
draw_count,
max,
prize_list,
}
}
40 changes: 37 additions & 3 deletions apps/backend/src/services/mina.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { Mina, PrivateKey } from "o1js"
import { Mina, PrivateKey, PublicKey } from "o1js"
import { get, insert } from "./db.js"
import { Mental } from "../contract/mental.js"
import { ElGamalFF } from "o1js-elgamal"
// import
const GenerateKey = () => {
const key = PrivateKey.random()
return key
}

const setNetwork = () => {
let isCompiled = false
const setNetwork = async () => {
const Berkeley = Mina.Network(
"https://proxy.berkeley.minaexplorer.com/graphql",
)
Mina.setActiveInstance(Berkeley)
if (!isCompiled) {
console.log("compiling...")
const a = await Mental.compile()
console.log(a)
console.log(a.verificationKey.hash)
isCompiled = true
}
}

const setTestAccount = () => {
const key = PrivateKey.fromBase58("EKERrsjtN4us8y9Y2Q3EDFKAju8rkKRynGFmybkG7Ufd6MMvPRbL")
return {
privateKey: key,
publicKey: key.toPublicKey(),
}
}

export const UserKey = async (address: string) => {
Expand All @@ -28,3 +44,21 @@ export const UserKey = async (address: string) => {
const userKey: any = await get(address)
return { pub: userKey.pub, key: userKey.key }
}
const transactionFee = 100_000_000
export const contractInteract = async () => {
await setNetwork()
const { privateKey: senderKey, publicKey: sender } = setTestAccount()
console.log(sender.toBase58())
const contractAddr = PublicKey.fromBase58("B62qrmRifvNnkRaKqw62Z84JGS5dn6cAgvZtLXsDpRLRH4zrxjLhCti")
const zkapp = new Mental(contractAddr)
console.log(zkapp)
await Mental.compile()
const { pk, sk } = ElGamalFF.generateKeys()
const tx = await Mina.transaction({ sender, fee: transactionFee }, () => {
zkapp.setPubKey(pk)
})
await tx.prove()
await tx.sign([senderKey]).send()

console.log(zkapp.pk.get())
}
1 change: 1 addition & 0 deletions packages/contract/src/mental.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { privateKey: userKey, publicKey: user } = Local.testAccounts[1]
const zkappKey = PrivateKey.random()
const zkappAddress = zkappKey.toPublicKey()

console.log(zkappAddress)
const zkapp = new Mental(zkappAddress)
const { pk, sk } = ElGamalFF.generateKeys()

Expand Down

0 comments on commit 1b8c5ab

Please sign in to comment.