Skip to content

Commit

Permalink
Expose username method from keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed May 30, 2024
1 parent 8c07892 commit 156aec1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
21 changes: 14 additions & 7 deletions examples/starknet-react-next/src/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useAccount, useConnect, useDisconnect } from "@starknet-react/core";
import CartridgeConnector from "@cartridge/connector"
import { useEffect, useState } from "react";

export function ConnectWallet() {
const { connect, connectors } = useConnect();
Expand All @@ -8,9 +9,21 @@ export function ConnectWallet() {

const connector = connectors[0] as unknown as CartridgeConnector;

const [username, setUsername] = useState<string>();
useEffect(() => {
if (!address) return
connector.username()?.then(n => setUsername(n))
}, [address, connector])

return (
<>
{address && <p>Account: {address} </p>}
{address && (
<>
<p>Account: {address} </p>
{username && <p>Username: {username}</p>}
</>
)}

<div style={{ display: "flex", gap: "10px" }}>
<button
onClick={() => {
Expand All @@ -20,12 +33,6 @@ export function ConnectWallet() {
{address ? "Disconnect" : "Connect"}
</button>

{address && (
<div>
<div>username: {connector.username()}</div>
<div>address: {address}</div>
</div>
)}
</div>
</>
);
Expand Down
12 changes: 12 additions & 0 deletions packages/keychain/src/methods/username.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Controller from "utils/controller";

export function username() {
return () => {
const controller = Controller.fromStore();
if (!controller) {
throw new Error("no controller");
}

return controller.username;
};
}
2 changes: 2 additions & 0 deletions packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "components";
import { useController } from "hooks/controller";
import { Status } from "utils/account";
import { username } from "methods/username";

type Context = Connect | Logout | Execute | SignMessage;

Expand Down Expand Up @@ -261,6 +262,7 @@ const Index: NextPage = () => {
),
sessions: normalize(sessions),
reset: normalize(() => () => setContext(undefined)),
username: normalize(username)
},
});

Expand Down

0 comments on commit 156aec1

Please sign in to comment.