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

Feat/auth #27

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions examples/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"dependencies": {
"@json-rpc-tools/utils": "1.7.6",
"@nextui-org/react": "1.0.0-beta.12",
"@walletconnect/core": "2.11.2",
"@walletconnect/se-sdk": "1.7.1-canary.1",
"@walletconnect/utils": "2.11.2",
"@walletconnect/core": "2.12.1",
"@walletconnect/se-sdk": "1.7.1-auth.0",
"@walletconnect/utils": "2.12.1",
"ethers": "5.7.2",
"framer-motion": "9.0.2",
"next": "12.2.0",
Expand Down
9 changes: 9 additions & 0 deletions examples/wallet/src/hooks/useWalletConnectEventsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export default function useWalletConnectEventsManager() {
[],
);

const onSessionAuthenticate = useCallback(
(request: SingleEthereumTypes.EventArguments["session_authenticate"]) => {
ModalStore.open("SessionAuthenticateModal", { sessionAuthenticate: request });
},
[],
);

/******************************************************************************
* Set up WalletConnect event listeners
*****************************************************************************/
Expand All @@ -88,6 +95,7 @@ export default function useWalletConnectEventsManager() {
console.log("delete", data);
});
web3wallet.on("auth_request", onAuthRequest);
web3wallet.on("session_authenticate", onSessionAuthenticate);
}

return () => {
Expand All @@ -98,6 +106,7 @@ export default function useWalletConnectEventsManager() {
console.log("delete", data);
});
web3wallet.off("auth_request", onAuthRequest);
web3wallet.off("session_authenticate", onSessionAuthenticate);
}
};
}, [web3WalletReady, onSessionProposal, onSessionRequest]);
Expand Down
4 changes: 3 additions & 1 deletion examples/wallet/src/store/ModalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ModalData {
requestEvent?: SingleEthereumTypes.EventArguments["session_request"];
requestSession?: SessionTypes.Struct;
authRequest?: SingleEthereumTypes.AuthRequest;
sessionAuthenticate?: SingleEthereumTypes.EventArguments["session_authenticate"];
}
interface State {
open: boolean;
Expand All @@ -20,7 +21,8 @@ interface State {
| "SessionSendTransactionModal"
| "SessionUnsuportedMethodModal"
| "AuthRequestModal"
| "SwitchChainModal";
| "SwitchChainModal"
| "SessionAuthenticateModal";
data?: ModalData;
}

Expand Down
3 changes: 1 addition & 2 deletions examples/wallet/src/utils/WalletConnectUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import SettingsStore from "@/store/SettingsStore";
import { Core } from "@walletconnect/core";
import { SingleEthereum, ISingleEthereum } from "@walletconnect/se-sdk";
import { EIP155_MAINNET_CHAINS } from "@/data/EIP155Data";
import { ICore } from "@walletconnect/types";

export let core: any;
export let web3wallet: ISingleEthereum;
export let core: ICore;

export async function createWeb3Wallet() {
if (!SettingsStore.state.web3WalletReady && typeof window !== "undefined") {
Expand Down
124 changes: 124 additions & 0 deletions examples/wallet/src/views/SessionAuthenticateModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import ProjectInfoCard from "@/components/ProjectInfoCard";
import RequesDetailsCard from "@/components/RequestDetalilsCard";
import RequestMethodCard from "@/components/RequestMethodCard";
import RequestModalContainer from "@/components/RequestModalContainer";
import { EIP155_CHAINS, EIP155_SIGNING_METHODS } from "@/data/EIP155Data";
import ModalStore from "@/store/ModalStore";
import SettingsStore from "@/store/SettingsStore";
import { eip155Addresses, eip155Wallets } from "@/utils/EIP155WalletUtil";
import { Button, Checkbox, Code, Col, Divider, Grid, Modal, Row } from "@nextui-org/react";
import { getSdkError, buildAuthObject, populateAuthPayload } from "@walletconnect/utils";
import { Fragment, useCallback, useEffect, useState } from "react";
import { useSnapshot } from "valtio";
import { web3wallet } from "@/utils/WalletConnectUtil";

export default function SessionAuthenticateModal() {
// Get request and wallet data from store
const authRequest = ModalStore.state.data?.sessionAuthenticate;

const { account } = useSnapshot(SettingsStore.state);
const [messages, setMessages] = useState<
{ authPayload: any; message: string; id: number; iss: string }[]
>([]);
const [supportedChains] = useState<string[]>(Object.keys(EIP155_CHAINS));
const [supportedMethods] = useState<string[]>(Object.values(EIP155_SIGNING_METHODS));
// Ensure request and wallet are defined

const address = eip155Addresses[account];

useEffect(() => {
if (!authRequest?.params.authPayload) return;
console.log("authRequest", authRequest);
console.log("supportedChains", supportedChains);
const newAuthPayload = populateAuthPayload({
authPayload: authRequest.params.authPayload,
chains: supportedChains,
methods: supportedMethods,
});

const messagesToSign: any[] = [];
newAuthPayload.chains.forEach((chain: string) => {
const iss = `${chain}:${address}`;
const message = web3wallet.formatAuthMessage({
payload: newAuthPayload,
address,
});
messagesToSign.push({
authPayload: newAuthPayload,
message,
iss,
id: authRequest.id,
});
});
setMessages(messagesToSign);
}, [address, authRequest, supportedChains, supportedMethods]);

// Handle approve action (logic varies based on request method)
const onApprove = useCallback(async () => {
if (messages.length) {
const signedAuths = [];
for (const message of messages) {
const signature = await eip155Wallets[address].signMessage(message.message);
const signedCacao = buildAuthObject(
message.authPayload,
{
t: "eip191",
s: signature,
},
message.iss,
);
signedAuths.push(signedCacao);
}

await web3wallet.approveSessionAuthenticate({
id: messages[0].id,
auths: signedAuths,
});

ModalStore.close();
}
}, [address, messages]);

// Handle reject action
const onReject = useCallback(async () => {
if (authRequest?.params?.authPayload) {
await web3wallet.rejectSessionAuthenticate({
id: authRequest.id,
reason: getSdkError("USER_REJECTED"),
});
ModalStore.close();
}
}, [authRequest]);

return (
<Fragment>
<RequestModalContainer title="Sign Message">
<ProjectInfoCard metadata={authRequest?.params.requester.metadata as any} />

<Divider y={2} />
<Row>
<Col>
<p>Messages to Sign ({messages.length})</p>
{messages.map((message, index) => {
console.log("@loop messageToSign", message);
return (
<Code key={index}>
<p>{message.message}</p>
</Code>
);
})}
</Col>
</Row>
</RequestModalContainer>

<Modal.Footer>
<Button auto flat color="error" onPress={onReject}>
Reject
</Button>
<Button auto flat color="success" onPress={onApprove}>
Approve
</Button>
</Modal.Footer>
</Fragment>
);
}
123 changes: 44 additions & 79 deletions examples/wallet/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1898,47 +1898,24 @@
events "^3.3.0"
isomorphic-unfetch "^3.1.0"

"@walletconnect/[email protected]":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.11.2.tgz#35286be92c645fa461fecc0dfe25de9f076fca8f"
integrity sha512-bB4SiXX8hX3/hyBfVPC5gwZCXCl+OPj+/EDVM71iAO3TDsh78KPbrVAbDnnsbHzZVHlsMohtXX3j5XVsheN3+g==
dependencies:
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "1.0.3"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "1.0.14"
"@walletconnect/keyvaluestorage" "^1.1.1"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/relay-auth" "^1.0.4"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/utils" "2.11.2"
events "^3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
uint8arrays "^3.1.0"

"@walletconnect/[email protected]":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.11.3.tgz#c81855722cb9afd411f91f5345c7874f48bade0b"
integrity sha512-/9m4EqiggFUwkQDv5PDWbcTI+yCVnBd/iYW5iIHEkivg2/mnBr2bQz2r/vtPjp19r/ZK62Dx0+UN3U+BWP8ulQ==
"@walletconnect/[email protected]":
version "2.12.1"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.12.1.tgz#e905e42f6c2a5117a1166c1a1d35e40aa98e76d3"
integrity sha512-CIxWNRNvmFNwn+8kPbKyBXS1JHBFJpDE8f73dXtUIElVnZhmXzEOSE5fug91EX57wTrv4/qW66H9kNB3c7Pp5g==
dependencies:
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "1.0.3"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "1.0.14"
"@walletconnect/keyvaluestorage" "^1.1.1"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/logger" "^2.1.0"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/relay-auth" "^1.0.4"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.3"
"@walletconnect/utils" "2.11.3"
"@walletconnect/types" "2.12.1"
"@walletconnect/utils" "2.12.1"
events "^3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
Expand Down Expand Up @@ -2062,6 +2039,14 @@
pino "7.11.0"
tslib "1.14.1"

"@walletconnect/logger@^2.1.0":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@walletconnect/logger/-/logger-2.1.2.tgz#813c9af61b96323a99f16c10089bfeb525e2a272"
integrity sha512-aAb28I3S6pYXZHQm5ESB+V6rDqIYfsnHaQyzFbwUUBFY4H0OXx/YtTl8lvhUNhMMfb9UxbwEBS253TlXUYJWSw==
dependencies:
"@walletconnect/safe-json" "^1.0.2"
pino "7.11.0"

"@walletconnect/relay-api@^1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@walletconnect/relay-api/-/relay-api-1.0.9.tgz#f8c2c3993dddaa9f33ed42197fc9bfebd790ecaf"
Expand Down Expand Up @@ -2089,26 +2074,26 @@
dependencies:
tslib "1.14.1"

"@walletconnect/[email protected]canary.1":
version "1.7.1-canary.1"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.7.1-canary.1.tgz#9b50fd5c36bc953e5de5bbf249b15112475791f3"
integrity sha512-FZx1a66VW7U07iUq0SwJH/dyEnX88GQIUHl1ceN5wSfL2HDAvJEjqHVWm4cLvozTLSA3Fd2ZU9iG3q7FQSmOwA==
"@walletconnect/[email protected]auth.0":
version "1.7.1-auth.0"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.7.1-auth.0.tgz#34160a84cf08d93fc9e3ad0d52532e485fe81051"
integrity sha512-BYsmXgm45PYQR7UawkpuwJ4C+PtKSwA2X+lWFHzCAr5NZHNjrUfpW4p871eyWAMfk4OG3i1wp+iUkqVDcEJIhQ==
dependencies:
"@walletconnect/web3wallet" "1.10.3"
"@walletconnect/web3wallet" "1.11.1"

"@walletconnect/sign-client@2.11.3":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.11.3.tgz#3ea7b3acf92ee31cc42b45d42e66c44b4720b28b"
integrity sha512-JVjLTxN/3NjMXv5zalSGKuSYLRyU2yX6AWEdq17cInlrwODpbWZr6PS1uxMWdH4r90DXBLhdtwDbEq/pfd0BPg==
"@walletconnect/sign-client@2.12.1":
version "2.12.1"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.12.1.tgz#a10f316f5681b7547db2714666a159574f31126f"
integrity sha512-6PegtNZgqmOX2G022fyrHjyN3PW6Ov2GVFvG8f+80uqikEO3IAL3dgazlnUYtuaUNYs+Hx7sSvjNVanMiJsE1Q==
dependencies:
"@walletconnect/core" "2.11.3"
"@walletconnect/core" "2.12.1"
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.3"
"@walletconnect/utils" "2.11.3"
"@walletconnect/types" "2.12.1"
"@walletconnect/utils" "2.12.1"
events "^3.3.0"

"@walletconnect/time@^1.0.2":
Expand Down Expand Up @@ -2142,10 +2127,10 @@
"@walletconnect/logger" "^2.0.1"
events "^3.3.0"

"@walletconnect/types@2.11.3":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.11.3.tgz#8ce43cb77e8fd9d5269847cdd73bcfa7cce7dd1a"
integrity sha512-JY4wA9MVosDW9dcJMTpnwliste0aJGJ1X6Q4ulLsQsgWRSEBRkLila0oUT01TDBW9Yq8uUp7uFOUTaKx6KWVAg==
"@walletconnect/types@2.12.1":
version "2.12.1"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.12.1.tgz#a3cb49bdac43f5cff1d9543bcdbb65f75e19cacc"
integrity sha512-mPzGj5ssgcOJKqwn8qsdCr+J9swsjTmDPAV10CghXIe3GGQKOb4noTUhOofb4LDbFaio1GBql8+Xfy+6bulobw==
dependencies:
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
Expand Down Expand Up @@ -2174,30 +2159,10 @@
query-string "7.1.3"
uint8arrays "^3.1.0"

"@walletconnect/[email protected]":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.2.tgz#dee0f19adf5e38543612cbe9fa4de7ed28eb7e85"
integrity sha512-LyfdmrnZY6dWqlF4eDrx5jpUwsB2bEPjoqR5Z6rXPiHJKUOdJt7az+mNOn5KTSOlRpd1DmozrBrWr+G9fFLYVw==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
"@stablelib/random" "^1.0.2"
"@stablelib/sha256" "1.0.1"
"@stablelib/x25519" "^1.0.3"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/window-getters" "^1.0.1"
"@walletconnect/window-metadata" "^1.0.1"
detect-browser "5.3.0"
query-string "7.1.3"
uint8arrays "^3.1.0"

"@walletconnect/[email protected]":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.3.tgz#3731809b54902655cf202e0bf0e8f268780e8b54"
integrity sha512-jsdNkrl/IcTkzWFn0S2d0urzBXg6RxVJtUYRsUx3qI3wzOGiABP9ui3yiZ3SgZOv9aRe62PaNp1qpbYZ+zPb8Q==
"@walletconnect/[email protected]":
version "2.12.1"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.12.1.tgz#5fced674e0a732eb62f30391943e79abbf3d5d1f"
integrity sha512-v2Oc8mTb+3y8MW94Rnj9hxVjJU3wdnE1g8eLZXmcNf7zAvsm1iJPtHl7ZxZsjpVpo1Vg79Oo1rS9gWq9z0kKKw==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
Expand All @@ -2207,26 +2172,26 @@
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.3"
"@walletconnect/types" "2.12.1"
"@walletconnect/window-getters" "^1.0.1"
"@walletconnect/window-metadata" "^1.0.1"
detect-browser "5.3.0"
query-string "7.1.3"
uint8arrays "^3.1.0"

"@walletconnect/web3wallet@1.10.3":
version "1.10.3"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.10.3.tgz#8195308757bd298ccc9caa6e3fe9f4ff82b94607"
integrity sha512-1Dr2P8KIDCqEWZ+s4coKGJz/+pj87ogFs+icPDXPu9QpzTgY5Y1WSzuAHaqoY5gTlL7WS58YP49s0E7iacUz4g==
"@walletconnect/web3wallet@1.11.1":
version "1.11.1"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.11.1.tgz#0716f493e0fa3923938ed0e31dffe275bff1c3bf"
integrity sha512-R50v3Ez73cLEaXPJOGMEBeEw5xN8/V6tya7+49OqwLLlZ6VR3tdJm8Az8TrvTrbRZ38oXP0cIl9ky+bSMCJtYw==
dependencies:
"@walletconnect/auth-client" "2.1.2"
"@walletconnect/core" "2.11.3"
"@walletconnect/core" "2.12.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.0.1"
"@walletconnect/sign-client" "2.11.3"
"@walletconnect/types" "2.11.3"
"@walletconnect/utils" "2.11.3"
"@walletconnect/sign-client" "2.12.1"
"@walletconnect/types" "2.12.1"
"@walletconnect/utils" "2.12.1"

"@walletconnect/window-getters@^1.0.1":
version "1.0.1"
Expand Down
Loading