Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reset. #4

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/deploy-vue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'

- name: Install dependencies
run: yarn install

- name: Build
run: yarn build-only

- name: Deploy
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
12 changes: 7 additions & 5 deletions src/components/PlayGame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ const submit = async () => {

const reset = async () => {
try {
await game.resetState()
opponentCommited.value = false
players.value = {}
opponentRevealed.value = false
winnerIdx.value = null
await game.reset() // To clear contract's state
} catch (error) {
console.error(error)
}

game = new Game(nodeUrl.value, applicationId.value)
opponentCommited.value = false
players.value = {}
opponentRevealed.value = false
winnerIdx.value = null
}

const reveal = async () => {
Expand Down
42 changes: 26 additions & 16 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface VersionRequest {
}

interface CreateKeyPairRequest {
seed: number[]
seed: Uint8Array
}

interface KeyComponents {
Expand All @@ -22,7 +22,7 @@ interface KeyComponents {
interface PrepareRequest {
signing_key: string
choice: string
nonce: number[]
nonce: Uint8Array
}

interface JoinRequest {
Expand All @@ -36,11 +36,19 @@ interface CommitRequest {
signature: string
}

interface ResetRequest {
player_idx: number
commitment: string
signature: string
}

export class Game {
constructor(nodeUrl: string, applicationId: string) {
this.applicationId = applicationId
this.seed = [...Array(32)].map(() => ~~(Math.random() * 255))
this.nonce = [...Array(32)].map(() => ~~(Math.random() * 255))
this.seed = new Uint8Array(32)
crypto.getRandomValues(this.seed)
this.nonce = new Uint8Array(32)
crypto.getRandomValues(this.nonce)
this.jsonRpcClient = new JsonRpcClient(nodeUrl, '/jsonrpc')
}

Expand Down Expand Up @@ -94,8 +102,16 @@ export class Game {
return response.result?.output
}

async resetState() {
await this.mutate('reset_state', {})
async reset() {
if (!(this.playerIdx && this.commitment && this.signature)) {
throw new Error('Unable to call reset.')
}

await this.mutate<ResetRequest, {}>('reset', {
player_idx: this.playerIdx,
commitment: this.commitment,
signature: this.signature
})
}

async join(playerName: string): Promise<number | undefined> {
Expand All @@ -114,9 +130,6 @@ export class Game {
public_key: keys!.pk
}
const joinResponse = await this.mutate<JoinRequest, number>('join', joinParams)
console.log('**************************')
console.log(joinResponse)
console.log('**************************')

this.playerIdx = joinResponse.result?.output
return this.playerIdx
Expand All @@ -132,26 +145,23 @@ export class Game {

this.commitment = prepareResponse.result?.output![0]
this.signature = prepareResponse.result?.output![1]
const commitResponse = await this.mutate<CommitRequest, {}>('commit', {
await this.mutate<CommitRequest, {}>('commit', {
player_idx: this.playerIdx!,
commitment: this.commitment!,
signature: this.signature!
})

console.log(commitResponse)
}

async reveal() {
const revealResponse = await this.mutate('reveal', {
await this.mutate('reveal', {
player_idx: this.playerIdx,
nonce: this.nonce
})
console.log(revealResponse)
}

applicationId: string
seed: number[]
nonce: number[]
seed: Uint8Array
nonce: Uint8Array
jsonRpcClient: JsonRpcClient
keys: KeyComponents | undefined
commitment: string | undefined
Expand Down