From a76b6c0eda25e51124da2e0cdba8251375263cfc Mon Sep 17 00:00:00 2001 From: hh Date: Fri, 5 Jan 2024 15:19:02 +0800 Subject: [PATCH] use panda --- package-lock.json | 14 ++++----- package.json | 2 +- src/App.css | 3 -- src/App.tsx | 52 +++++++++++--------------------- src/Game.tsx | 75 +++++++++++++++++++++-------------------------- src/TitleBar.tsx | 6 ++-- 6 files changed, 61 insertions(+), 91 deletions(-) diff --git a/package-lock.json b/package-lock.json index d40414e..b525440 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", - "scrypt-ts": "^1.3.18", + "scrypt-ts": "^1.3.22", "typescript": "^4.9.5", "web-vitals": "^2.1.4" }, @@ -14813,9 +14813,9 @@ } }, "node_modules/scrypt-ts": { - "version": "1.3.18", - "resolved": "https://registry.npmjs.org/scrypt-ts/-/scrypt-ts-1.3.18.tgz", - "integrity": "sha512-hlg2radnxPcbr+UwJWk+HcQRDL9ZXyQgh05geym3oL4WaeZvnL62VnAv4aUG/0kuYn4shwFcD51L1LJvocIMMA==", + "version": "1.3.22", + "resolved": "https://registry.npmjs.org/scrypt-ts/-/scrypt-ts-1.3.22.tgz", + "integrity": "sha512-/jBSOaqVGJIflAaDEPdsvdHgqoU/g+ZVrHou2wugZHgKFlbtcYrxgoZdmQfJcSWFFN+A2RYeRyXE0Mk/w1NmuA==", "dependencies": { "deep-equal": "^2.2.0", "fast-diff": "^1.2.0", @@ -28095,9 +28095,9 @@ } }, "scrypt-ts": { - "version": "1.3.18", - "resolved": "https://registry.npmjs.org/scrypt-ts/-/scrypt-ts-1.3.18.tgz", - "integrity": "sha512-hlg2radnxPcbr+UwJWk+HcQRDL9ZXyQgh05geym3oL4WaeZvnL62VnAv4aUG/0kuYn4shwFcD51L1LJvocIMMA==", + "version": "1.3.22", + "resolved": "https://registry.npmjs.org/scrypt-ts/-/scrypt-ts-1.3.22.tgz", + "integrity": "sha512-/jBSOaqVGJIflAaDEPdsvdHgqoU/g+ZVrHou2wugZHgKFlbtcYrxgoZdmQfJcSWFFN+A2RYeRyXE0Mk/w1NmuA==", "requires": { "deep-equal": "^2.2.0", "fast-diff": "^1.2.0", diff --git a/package.json b/package.json index fe46686..602e0d0 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", - "scrypt-ts": "^1.3.18", + "scrypt-ts": "^1.3.22", "typescript": "^4.9.5", "web-vitals": "^2.1.4" }, diff --git a/src/App.css b/src/App.css index 6299080..57a9b6c 100644 --- a/src/App.css +++ b/src/App.css @@ -93,9 +93,6 @@ margin-top: 20px; } -.sensilet { - -} .wallet label { font-size: 1rem; diff --git a/src/App.tsx b/src/App.tsx index f246106..6b0a3c9 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,7 @@ import "./App.css"; import Game from "./Game"; import { useState, useRef } from "react"; import TitleBar from "./TitleBar"; -import { DefaultProvider, SensiletSigner, PubKey, toHex } from "scrypt-ts"; +import { DefaultProvider, PandaSigner, PubKey, toHex } from "scrypt-ts"; import { TicTacToe } from "./contracts/tictactoe"; const initialGameData = { amount: 0, @@ -23,7 +23,7 @@ function App() { const [gameData, setGameData] = useState(initialGameData); const [isConnected, setConnected] = useState(false); - const signerRef = useRef(); + const signerRef = useRef(); const [contract, setContract] = useState(undefined) const [deployedTxId, setDeployedTxId] = useState("") const [alicePubkey, setAlicePubkey] = useState(""); @@ -42,7 +42,7 @@ function App() { try { - const signer = signerRef.current as SensiletSigner; + const signer = signerRef.current as PandaSigner; const instance = new TicTacToe( @@ -72,11 +72,11 @@ function App() { setGameData(Object.assign({}, gameData, initialGameData)) }; - const sensiletLogin = async () => { - console.log('sensiletLogin...') + const login = async () => { + console.log('login...') try { const provider = new DefaultProvider(); - const signer = new SensiletSigner(provider); + const signer = new PandaSigner(provider); signerRef.current = signer; @@ -86,35 +86,17 @@ function App() { } const pubkey = await signer.getDefaultPubKey(); - const changeAccountMessage = "Please change your account in Sensilet wallet, click again to get bob PublicKey"; - - if (!alicePubkey) { - - setAlicePubkey(toHex(pubkey)) - - signer.getBalance().then(balance => { - setAliceBalance(balance.confirmed + balance.unconfirmed) - }) - - alert(changeAccountMessage) - - } else { - if (toHex(pubkey) !== alicePubkey) { - setBobPubkey(toHex(pubkey)) - - signer.getBalance().then(balance => { - setBobBalance(balance.confirmed + balance.unconfirmed) - }) - - setConnected(true); - } else { - alert(changeAccountMessage) - } - } + setAlicePubkey(toHex(pubkey)) + setBobPubkey(toHex(pubkey)) + signer.getBalance().then(balance => { + setAliceBalance(balance.confirmed + balance.unconfirmed) + setBobBalance(balance.confirmed + balance.unconfirmed) + }) + setConnected(true); } catch (error) { - console.error("sensiletLogin failed", error); - alert("sensiletLogin failed") + console.error("Panda Wallet login failed", error); + alert("Panda Wallet login failed") } }; @@ -145,8 +127,8 @@ function App() { : diff --git a/src/Game.tsx b/src/Game.tsx index 02b3bff..f487ae3 100644 --- a/src/Game.tsx +++ b/src/Game.tsx @@ -44,16 +44,6 @@ function Game(props: any) { return true; } - async function isRightSensiletAccount() { - const current = props.contract as TicTacToe; - - const expectedPubkey = current.isAliceTurn ? props.alicePubkey : props.bobPubkey; - - const pubkey = await current.signer.getDefaultPubKey(); - - return toHex(pubkey) === expectedPubkey; - } - async function move(i: number) { const current = props.contract as TicTacToe; @@ -81,13 +71,7 @@ function Game(props: any) { const history = gameData.history.slice(0, gameData.currentStepNumber + 1); const current = history[history.length - 1]; const squares = current.squares.slice(); - - const isRightAccount = await isRightSensiletAccount(); - - if (!isRightAccount) { - alert(`Please switch Sensilet to ${gameData.isAliceTurn ? "Alice" : "Bob"} account!`) - return; - } + if (!canMove(i, squares)) { console.error('can not move now!') @@ -100,33 +84,40 @@ function Game(props: any) { }; // Call smart contract move method. - const { tx, nexts } = await move(i); - - const square = squares[i] as SquareData; - if (square) { - square.tx = tx.id; - } - - console.log('move txid:', tx.id) - // update states - if (nexts && nexts[0]) { - const instance = nexts[0].instance - props.setContract(instance) + try { + const { tx, nexts } = await move(i); + + const square = squares[i] as SquareData; + if (square) { + square.tx = tx.id; + } + + console.log('move txid:', tx.id) + + // update states + if (nexts && nexts[0]) { + const instance = nexts[0].instance + props.setContract(instance) + } + const winner = calculateWinner(squares).winner; + setGameData({ + ...gameData, + history: history.concat([ + { + squares + }, + ]), + isAliceTurn: winner ? gameData.isAliceTurn : !gameData.isAliceTurn, + currentStepNumber: history.length, + start: true + }) + setLastTxId(tx.id) + } catch (error) { + console.error("error:", error); + alert("ERROR:" + error.message) } - const winner = calculateWinner(squares).winner; - setGameData({ - ...gameData, - history: history.concat([ - { - squares - }, - ]), - isAliceTurn: winner ? gameData.isAliceTurn : !gameData.isAliceTurn, - currentStepNumber: history.length, - start: true - }) - setLastTxId(tx.id) + } diff --git a/src/TitleBar.tsx b/src/TitleBar.tsx index d427917..c745263 100644 --- a/src/TitleBar.tsx +++ b/src/TitleBar.tsx @@ -9,8 +9,8 @@ function TitleBar(props : any) { let amount = parseInt(amountRef.current.value); - if (amount < 10000) { - alert("invalid amount, at least 10000 satoshis") + if (amount < 1) { + alert("invalid amount, at least 1 satoshis") return; } @@ -39,7 +39,7 @@ function TitleBar(props : any) { return (