Skip to content

Commit

Permalink
fix:Switch to viem (#64)
Browse files Browse the repository at this point in the history
* switch to viem

* update package

* pnpm i

* upgrade types

* fix build
  • Loading branch information
andy-t-wang authored Sep 2, 2024
1 parent 6403f32 commit 6dc47cc
Show file tree
Hide file tree
Showing 9 changed files with 1,914 additions and 1,747 deletions.
4 changes: 3 additions & 1 deletion demo/with-next/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default async function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<SessionProvider session={session}>{children}</SessionProvider>
<MiniKitProvider>
<SessionProvider session={session}>{children}</SessionProvider>
</MiniKitProvider>
</body>
</html>
);
Expand Down
76 changes: 40 additions & 36 deletions demo/with-next/components/ClientContent/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
MiniKit,
SignMessageErrorCodes,
ResponseEvent,
MiniAppSignMessagePayload,
SignMessageInput,
} from "@worldcoin/minikit-js";
import { useCallback, useEffect, useState } from "react";
import { validateSchema } from "./helpers/validate-schema";
import * as yup from "yup";
import { SignMessageInput } from "../../../../src/types/commands";
import { verifyMessage } from "@wagmi/core";
import { config } from "../config";
import { optimism } from "@wagmi/core/chains";
Expand Down Expand Up @@ -52,45 +53,48 @@ export const SignMessage = () => {
return;
}

MiniKit.subscribe(ResponseEvent.MiniAppSignMessage, async (payload) => {
console.log("MiniAppSignMessage, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
signMessageErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSignMessagePayloadValidationMessage("Payload is valid");
} else {
setSignMessagePayloadValidationMessage(errorMessage);
}
} else {
const errorMessage = await validateSchema(
signMessageSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSignMessagePayloadValidationMessage("Payload is valid");
MiniKit.subscribe(
ResponseEvent.MiniAppSignMessage,
async (payload: MiniAppSignMessagePayload) => {
console.log("MiniAppSignMessage, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
signMessageErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSignMessagePayloadValidationMessage("Payload is valid");
} else {
setSignMessagePayloadValidationMessage(errorMessage);
}
} else {
setSignMessagePayloadValidationMessage(errorMessage);
const errorMessage = await validateSchema(
signMessageSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSignMessagePayloadValidationMessage("Payload is valid");
} else {
setSignMessagePayloadValidationMessage(errorMessage);
}
}
}

const isValid = await verifyMessage(config, {
address: "0x4564420674EA68fcc61b463C0494807C759d47e6",
message: "hello world",
signature:
"0x654c6c04ba9496731e26f92b74a0de100e2dc72e0ae646698d5f8ed68c2b9db03bb46a772843608717d8ba3d8ae1d4a330bc97315b14397d9216b45b3834351d1b",
});
const isValid = await verifyMessage(config, {
address: "0x4564420674EA68fcc61b463C0494807C759d47e6",
message: "hello world",
signature:
"0x654c6c04ba9496731e26f92b74a0de100e2dc72e0ae646698d5f8ed68c2b9db03bb46a772843608717d8ba3d8ae1d4a330bc97315b14397d9216b45b3834351d1b",
});

setSignMessageAppPayload(JSON.stringify(payload, null, 2));
setSignMessagePayloadVerificationMessage(
isValid ? "Signature is valid" : "Signature is invalid"
);
});
setSignMessageAppPayload(JSON.stringify(payload, null, 2));
setSignMessagePayloadVerificationMessage(
isValid ? "Signature is valid" : "Signature is invalid"
);
}
);

return () => {
MiniKit.unsubscribe(ResponseEvent.MiniAppSignMessage);
Expand Down
79 changes: 41 additions & 38 deletions demo/with-next/components/ClientContent/SignTypedMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
MiniKit,
SignTypedDataErrorCodes,
ResponseEvent,
MiniAppSignTypedDataPayload,
SignTypedDataInput,
} from "@worldcoin/minikit-js";
import { useCallback, useEffect, useState } from "react";
import { validateSchema } from "./helpers/validate-schema";
import * as yup from "yup";
import { SignTypedDataInput } from "../../../../src/types/commands";
import { verifyMessage } from "@wagmi/core";
import { config } from "../config";
import { optimism } from "@wagmi/core/chains";

const signTypedDataSuccessPayloadSchema = yup.object({
message: yup.string().required(),
Expand Down Expand Up @@ -38,7 +38,7 @@ export const SignTypedData = () => {
] = useState<string | null>();

const [
signTypedDataPayloadVerificaitonMessage,
signTypedDataPayloadVerificationMessage,
setSignTypedDataPayloadVerificationMessage,
] = useState<string | null>();

Expand All @@ -50,45 +50,48 @@ export const SignTypedData = () => {
return;
}

MiniKit.subscribe(ResponseEvent.MiniAppSignTypedData, async (payload) => {
console.log("MiniAppSignTypedData, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
signTypedDataErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSignTypedDataPayloadValidationMessage("Payload is valid");
MiniKit.subscribe(
ResponseEvent.MiniAppSignTypedData,
async (payload: MiniAppSignTypedDataPayload) => {
console.log("MiniAppSignTypedData, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
signTypedDataErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSignTypedDataPayloadValidationMessage("Payload is valid");
} else {
setSignTypedDataPayloadValidationMessage(errorMessage);
}
} else {
setSignTypedDataPayloadValidationMessage(errorMessage);
const errorMessage = await validateSchema(
signTypedDataSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSignTypedDataPayloadValidationMessage("Payload is valid");
} else {
setSignTypedDataPayloadValidationMessage(errorMessage);
}
}
} else {
const errorMessage = await validateSchema(
signTypedDataSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSignTypedDataPayloadValidationMessage("Payload is valid");
} else {
setSignTypedDataPayloadValidationMessage(errorMessage);
}
}
const isValid = await verifyMessage(config, {
address: "0x4564420674EA68fcc61b463C0494807C759d47e6",
message: "hello world",
signature:
"0x654c6c04ba9496731e26f92b74a0de100e2dc72e0ae646698d5f8ed68c2b9db03bb46a772843608717d8ba3d8ae1d4a330bc97315b14397d9216b45b3834351d1b",
});

const isValid = await verifyMessage(config, {
address: "0x4564420674EA68fcc61b463C0494807C759d47e6",
message: "hello world",
signature:
"0x654c6c04ba9496731e26f92b74a0de100e2dc72e0ae646698d5f8ed68c2b9db03bb46a772843608717d8ba3d8ae1d4a330bc97315b14397d9216b45b3834351d1b",
});

setSignTypedDataAppPayload(JSON.stringify(payload, null, 2));
setSignTypedDataPayloadVerificationMessage(
isValid ? "Signature is valid" : "Signature is invalid"
);
});
setSignTypedDataAppPayload(JSON.stringify(payload, null, 2));
setSignTypedDataPayloadVerificationMessage(
isValid ? "Signature is valid" : "Signature is invalid"
);
}
);

return () => {
MiniKit.unsubscribe(ResponseEvent.MiniAppSignTypedData);
Expand Down
94 changes: 49 additions & 45 deletions demo/with-next/components/ClientContent/Transaction.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
MiniAppSendTransactionPayload,
MiniKit,
ResponseEvent,
SendTransactionErrorCodes,
Expand Down Expand Up @@ -55,55 +56,58 @@ export const SendTransaction = () => {
return;
}

MiniKit.subscribe(ResponseEvent.MiniAppSendTransaction, async (payload) => {
console.log("MiniAppSendTransaction, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
sendTransactionErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSendTransactionPayloadValidationMessage("Payload is valid");
} else {
setSendTransactionPayloadValidationMessage(errorMessage);
}
} else {
const errorMessage = await validateSchema(
sendTransactionSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSendTransactionPayloadValidationMessage("Payload is valid");
MiniKit.subscribe(
ResponseEvent.MiniAppSendTransaction,
async (payload: MiniAppSendTransactionPayload) => {
console.log("MiniAppSendTransaction, SUBSCRIBE PAYLOAD", payload);

if (payload.status === "error") {
const errorMessage = await validateSchema(
sendTransactionErrorPayloadSchema,
payload
);

if (!errorMessage) {
setSendTransactionPayloadValidationMessage("Payload is valid");
} else {
setSendTransactionPayloadValidationMessage(errorMessage);
}
} else {
setSendTransactionPayloadValidationMessage(errorMessage);
const errorMessage = await validateSchema(
sendTransactionSuccessPayloadSchema,
payload
);

if (!errorMessage) {
setSendTransactionPayloadValidationMessage("Payload is valid");
} else {
setSendTransactionPayloadValidationMessage(errorMessage);
}

// // Call the API to verify the message
// const response = await fetch("/api/verify-siwe", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// siweResponsePayload: payload,
// }),
// });

// const responseJson = await response.json();

// setSendTransactionVerificationMessage(
// responseJson.isValid
// ? "Valid! Successful Transaction"
// : `Failed: ${responseJson.message}`
// );
}
setSendTransactionVerificationMessage("TODO");

// // Call the API to verify the message
// const response = await fetch("/api/verify-siwe", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// siweResponsePayload: payload,
// }),
// });

// const responseJson = await response.json();

// setSendTransactionVerificationMessage(
// responseJson.isValid
// ? "Valid! Successful Transaction"
// : `Failed: ${responseJson.message}`
// );
setReceivedSendTransactionPayload(payload);
}
setSendTransactionVerificationMessage("TODO");

setReceivedSendTransactionPayload(payload);
});
);

return () => {
MiniKit.unsubscribe(ResponseEvent.MiniAppSendTransaction);
Expand Down
3 changes: 2 additions & 1 deletion demo/with-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"prettier-plugin-sort-imports-desc": "^1.0.0",
"react": "^18",
"react-dom": "^18",
"yup": "^1.4.0"
"yup": "^1.4.0",
"viem": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
Loading

0 comments on commit 6dc47cc

Please sign in to comment.