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

Switch to panda wallet. #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
65 changes: 32 additions & 33 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, DefaultProviderOption, PandaSigner, PubKey, Signer, bsv, toHex } from "scrypt-ts";
import { TicTacToe } from "./contracts/tictactoe";
const initialGameData = {
amount: 0,
Expand All @@ -19,11 +19,10 @@ const initialGameData = {
}

function App() {

const [gameData, setGameData] = useState(initialGameData);
const [isConnected, setConnected] = useState(false);

const signerRef = useRef<SensiletSigner>();
const signerRef = useRef<Signer>();
const [contract, setContract] = useState<TicTacToe | undefined>(undefined)
const [deployedTxId, setDeployedTxId] = useState<string>("")
const [alicePubkey, setAlicePubkey] = useState("");
Expand All @@ -39,11 +38,8 @@ function App() {
return
}



try {
const signer = signerRef.current as SensiletSigner;

const signer = signerRef.current;

const instance = new TicTacToe(
PubKey(toHex(alicePubkey)),
Expand Down Expand Up @@ -72,11 +68,13 @@ function App() {
setGameData(Object.assign({}, gameData, initialGameData))
};

const sensiletLogin = async () => {
console.log('sensiletLogin...')
const handleConnect = async () => {
try {
const provider = new DefaultProvider();
const signer = new SensiletSigner(provider);
const provider = new DefaultProvider({
network: bsv.Networks.testnet
} as DefaultProviderOption);

const signer = new PandaSigner(provider)

signerRef.current = signer;

Expand All @@ -86,7 +84,7 @@ function App() {
}

const pubkey = await signer.getDefaultPubKey();
const changeAccountMessage = "Please change your account in Sensilet wallet, click again to get bob PublicKey";
const changeAccountMessage = "Please switch wallet and hit the connect button again to get Bob's public key.";

if (!alicePubkey) {

Expand All @@ -113,8 +111,8 @@ function App() {
}

} catch (error) {
console.error("sensiletLogin failed", error);
alert("sensiletLogin failed")
console.error("Connecting wallet failed", error);
alert("Connecting wallet failed")
}
};

Expand All @@ -128,28 +126,29 @@ function App() {
onCancel={cancelGame}
started={gameData.start}
/>
<Game gameData={gameData}
setGameData={setGameData}
deployedTxId={deployedTxId}
contract={contract}
setContract={setContract}
alicePubkey={alicePubkey}
bobPubkey={bobPubkey} />
<Game gameData={gameData}
setGameData={setGameData}
deployedTxId={deployedTxId}
contract={contract}
setContract={setContract}
alicePubkey={alicePubkey}
bobPubkey={bobPubkey} />

{
isConnected ?
<div>
<label>Alice Balance: {alicebalance} <span> (satoshis)</span></label>
<br/>
<label>Bob Balance: {bobbalance} <span> (satoshis)</span></label>
</div>
:
<button
className="pure-button button-large sensilet"
onClick={sensiletLogin}
>
Connect Wallet
</button>
<div>
<label>Alice Balance: {alicebalance} <span> (satoshis)</span></label>
<br />
<label>Bob Balance: {bobbalance} <span> (satoshis)</span></label>
</div>
: (
<button
className="pure-button button-large"
onClick={handleConnect}
>
Connect Wallet
</button>
)
}
</header>
</div>
Expand Down
6 changes: 2 additions & 4 deletions src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Game(props: any) {
return true;
}

async function isRightSensiletAccount() {
async function isRightAccount() {
const current = props.contract as TicTacToe;

const expectedPubkey = current.isAliceTurn ? props.alicePubkey : props.bobPubkey;
Expand Down Expand Up @@ -81,10 +81,8 @@ 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) {
if (!(await isRightAccount())) {
alert(`Please switch Sensilet to ${gameData.isAliceTurn ? "Alice" : "Bob"} account!`)
return;
}
Expand Down