Skip to content

Commit

Permalink
fix: check accounter interface implemented by account & add support f…
Browse files Browse the repository at this point in the history
…or execute_outside_v2 tx
  • Loading branch information
irisdv committed Jun 17, 2024
1 parent 37680b9 commit 894a231
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 22 deletions.
9 changes: 3 additions & 6 deletions app/components/recoverTokenModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ const RecoverTokenModal: FunctionComponent<RecoverTokenModalProps> = ({
you&apos;ll need to regenerate a signature with your ethereum wallet.
</>
) : (
<>
You can now install one of our partner wallets to get your additional
click!
</>
<>You can now install your Argent wallet to get your additional click!</>
);

return (
Expand Down Expand Up @@ -97,13 +94,13 @@ const RecoverTokenModal: FunctionComponent<RecoverTokenModalProps> = ({
>
Argent
</Button>
<Button
{/* <Button
icon={<img src={getBraavosIcon()} width={22} />}
width={300}
onClick={() => window.open(getBraavosWebsite())}
>
Braavos
</Button>
</Button> */}
</div>
)}

Expand Down
6 changes: 2 additions & 4 deletions app/components/tryAgainModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import WalletIcon from "./iconComponents/walletIcon";
import {
getArgentIcon,
getArgentWebsite,
getBraavosIcon,
getBraavosWebsite,
} from "@/utils/starknetConnectorsWrapper";

type TryAgainModalProps = {
Expand Down Expand Up @@ -76,13 +74,13 @@ const TryAgainModal: FunctionComponent<TryAgainModalProps> = ({
>
Argent
</Button>
<Button
{/* <Button
icon={<img src={getBraavosIcon()} width={22} />}
width={300}
onClick={() => window.open(getBraavosWebsite())}
>
Braavos
</Button>
</Button> */}
</div>
) : (
<div>
Expand Down
25 changes: 17 additions & 8 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "@/utils/dataService";
import WelcomeModal from "./components/welcomeModal";
import { getOutsideExecution } from "@/services/clickService";
import { getTypedData } from "@/utils/callData/typedData";
import { getTypedData, getTypedDataV2 } from "@/utils/callData/typedData";
import {
altStarknetNewAccount,
ethResetButton,
Expand All @@ -36,7 +36,7 @@ import {
starknetResetButtonFromEth,
trackId,
} from "@/services/apiService";
import { Signature, TypedData } from "starknet";
import { Signature, TypedData, WeierstrassSignatureType } from "starknet";
import {
addEthToken,
clearEthTokens,
Expand All @@ -50,6 +50,7 @@ import Notification from "./components/notification";
import { hexToDecimal } from "@/utils/feltService";
import getPriceValue from "@/hooks/getEthQuote";
import RecoverTokenModal from "./components/recoverTokenModal";
import getTxVersion from "@/hooks/getTxVersion";

export default function Home() {
const [isLoaded, setIsLoaded] = useState(false);
Expand Down Expand Up @@ -87,6 +88,8 @@ export default function Home() {
const { hasEthTokens, ethTokens } = canPlayOnStarknet(network);
const deploymentData = isStarknetDeployed(network, address);
const isFinished = isOver5mn(countdownTimestamp);
const txVersion = getTxVersion(network, address);
// console.log("txVersion", txVersion);

useEffect(() => {
if (evmConnected) {
Expand Down Expand Up @@ -238,14 +241,13 @@ export default function Home() {
) => {
try {
if (!starknetAccount) return;
console.log("starknetAccount", starknetAccount);
const signature = await starknetAccount.signMessage(typedData);
console.log("signature", signature);
const virtualTxId = await starknetResetButton(
starknetAccount?.address as string,
signature as Signature,
signature,
nonce,
executeBefore
executeBefore,
txVersion as number
);
storeVirtualTxId(virtualTxId.virtual_tx_id);
setTrackingList([...trackingList, virtualTxId.virtual_tx_id]);
Expand All @@ -272,7 +274,8 @@ export default function Home() {
signature as Signature,
nonce,
executeBefore,
availableDomain
availableDomain,
txVersion as number
);
storeVirtualTxId(virtualTxId.virtual_tx_id);
setTrackingList([...trackingList, virtualTxId.virtual_tx_id]);
Expand All @@ -295,6 +298,7 @@ export default function Home() {
ethTokens,
nonce,
executeBefore,
txVersion as number,
deploymentData
);
console.log("virtualTxId", virtualTxId);
Expand All @@ -318,6 +322,7 @@ export default function Home() {
ethSig.sig,
nonce,
executeBefore,
txVersion as number,
deploymentData
);
console.log("virtualTxId", virtualTxId);
Expand All @@ -337,7 +342,11 @@ export default function Home() {
const nonce = Math.floor(Math.random() * 1000000000000);
const executeBefore = Math.floor(Date.now() / 1000) + 3600 * 48; // + 48h for testing
const outsideExecution = getOutsideExecution(nonce, executeBefore);
const typedData = getTypedData(outsideExecution);
if (!txVersion) return;
const typedData =
txVersion === 1
? getTypedData(outsideExecution)
: getTypedDataV2(outsideExecution);

if (remainingClicks.eligibilityAmt && remainingClicks.eligibilityAmt > 0) {
await handleEligibleStarknetReset(typedData, nonce, executeBefore);
Expand Down
48 changes: 48 additions & 0 deletions hooks/getTxVersion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NetworkType } from "@/constants/types";
import { useAccount } from "@starknet-react/core";
import { useEffect, useState } from "react";

export default function getTxVersion(network?: NetworkType, address?: string) {
const { account } = useAccount();
const [txVersion, setVersion] = useState<number | undefined>();

useEffect(() => {
if (!network || network === NetworkType.EVM || !account) {
console.log("1");
setVersion(undefined);
return;
}

const checkIsDeployed = async () => {
try {
const supports_v1 = await account.callContract({
contractAddress: account.address,
entrypoint: "supports_interface",
calldata: [
"0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181",
],
});

const supports_v2 = await account.callContract({
contractAddress: account.address,
entrypoint: "supports_interface",
calldata: [
"0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872",
],
});

// @ts-ignore
if (Number(supports_v1.result[0])) setVersion(1);
// @ts-ignore
else if (Number(supports_v2.result[0])) setVersion(2);
else setVersion(undefined);
} catch (error) {
setVersion(undefined);
}
};

checkIsDeployed();
}, [network, address]);

return txVersion;
}
24 changes: 20 additions & 4 deletions services/apiService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { EthToken, GetDeploymentDataResult } from "@/constants/types";
import { Signature, stark } from "starknet";
import {
ArraySignatureType,
Signature,
WeierstrassSignatureType,
num,
stark,
} from "starknet";

const baseurl = process.env.NEXT_PUBLIC_ETH_BUTTON_API;
const starknetIdBaseUrl = process.env.NEXT_PUBLIC_STARKNET_ID_API;
Expand Down Expand Up @@ -67,8 +73,11 @@ export const starknetResetButton = async (
address: string,
sig: Signature,
nonce: number,
executeBefore: number
executeBefore: number,
version: number
) => {
const sigHex = stark.signatureToHexArray(sig);
console.log("sigHex reset button", sigHex);
try {
const response = await fetch(`${baseurl}/starknet_reset_button`, {
method: "POST",
Expand All @@ -77,9 +86,10 @@ export const starknetResetButton = async (
},
body: JSON.stringify({
addr: address,
sig: stark.signatureToHexArray(sig),
sig: [sigHex[1], sigHex[2]],
nonce,
execute_before: executeBefore,
version,
}),
});
return await response.json();
Expand All @@ -92,7 +102,8 @@ export const starknetDomainResetButton = async (
sig: Signature,
nonce: number,
executeBefore: number,
domain: string
domain: string,
version: number
) => {
try {
const response = await fetch(`${baseurl}/starknet_domain_reset_button`, {
Expand All @@ -105,6 +116,7 @@ export const starknetDomainResetButton = async (
sig: stark.signatureToHexArray(sig),
nonce,
execute_before: executeBefore,
version,
}),
});
return await response.json();
Expand All @@ -119,6 +131,7 @@ export const starknetResetButtonFromEth = async (
tokens: EthToken[],
nonce: number,
executeBefore: number,
version: number,
deploymentData?: GetDeploymentDataResult
) => {
try {
Expand All @@ -136,6 +149,7 @@ export const starknetResetButtonFromEth = async (
class_hash: deploymentData?.class_hash,
salt: deploymentData?.salt,
deployment_calldata: deploymentData?.calldata,
version,
}),
});
return await response.json();
Expand All @@ -151,6 +165,7 @@ export const altStarknetNewAccount = async (
eth_sig: String,
nonce: number,
executeBefore: number,
version: number,
deploymentData?: GetDeploymentDataResult
) => {
try {
Expand All @@ -169,6 +184,7 @@ export const altStarknetNewAccount = async (
class_hash: deploymentData?.class_hash,
salt: deploymentData?.salt,
deployment_calldata: deploymentData?.calldata,
version,
}),
});
return await response.json();
Expand Down
53 changes: 53 additions & 0 deletions utils/callData/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,56 @@ export function getTypedData(outsideExecution: OutsideExecution) {
},
};
}

const typesV2 = {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
OutsideExecution: [
{ name: "Caller", type: "ContractAddress" },
{ name: "Nonce", type: "felt" },
{ name: "Execute After", type: "u128" },
{ name: "Execute Before", type: "u128" },
{ name: "Calls", type: "Call*" },
],
Call: [
{ name: "To", type: "ContractAddress" },
{ name: "Selector", type: "selector" },
{ name: "Calldata", type: "felt*" },
],
};

export function getDomainV2(chainId: string) {
return {
name: "Account.execute_from_outside",
version: "2",
chainId: chainId,
revision: "1",
};
}

export function getTypedDataV2(outsideExecution: OutsideExecution) {
const chainId =
process.env.NEXT_PUBLIC_IS_TESTNET === "true" ? "SN_SEPOLIA" : "SN_MAIN";
return {
types: typesV2,
primaryType: "OutsideExecution",
domain: getDomainV2(chainId),
message: {
Caller: outsideExecution.caller,
Nonce: outsideExecution.nonce,
"Execute After": outsideExecution.execute_after,
"Execute Before": outsideExecution.execute_before,
Calls: outsideExecution.calls.map((call) => {
return {
To: call.to,
Selector: call.selector,
Calldata: call.calldata,
};
}),
},
};
}

0 comments on commit 894a231

Please sign in to comment.