-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lookup methods to controller pkg (#1106)
* Add lookup methods to controller pkg * fix warning * use workspace
- Loading branch information
Showing
11 changed files
with
154 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use client"; | ||
|
||
import { useAccount } from "@starknet-react/core"; | ||
import { useCallback, useState } from "react"; | ||
import ControllerConnector from "@cartridge/connector/controller"; | ||
import { lookupAddresses, lookupUsernames } from "@cartridge/controller"; | ||
import { Button } from "@cartridge/ui-next"; | ||
|
||
export function LookupControllers() { | ||
const { address, connector } = useAccount(); | ||
const [error, setError] = useState<Error>(); | ||
const [addressToUsername, setAddressToUsername] = | ||
useState<Map<string, string>>(); | ||
const [usernameToAddress, setUsernameToAddress] = | ||
useState<Map<string, string>>(); | ||
const cartridgeConnector = connector as never as ControllerConnector; | ||
|
||
const onClick = useCallback(async () => { | ||
if (!address) return; | ||
|
||
setError(undefined); | ||
try { | ||
const username = await cartridgeConnector.username()!; | ||
const [addressResults, usernameResults] = await Promise.all([ | ||
lookupAddresses([address]), | ||
lookupUsernames([username]), | ||
]); | ||
|
||
setAddressToUsername(addressResults); | ||
setUsernameToAddress(usernameResults); | ||
} catch (e) { | ||
setError(e as Error); | ||
} | ||
}, [address, cartridgeConnector]); | ||
|
||
if (!address) return null; | ||
|
||
return ( | ||
<div> | ||
<h2>Lookup Controllers</h2> | ||
<h3>Address to Username</h3> | ||
{addressToUsername && | ||
[...addressToUsername].map(([addr, username]) => ( | ||
<p key={addr}> | ||
{addr}: {username} | ||
</p> | ||
))} | ||
<h3>Username to Address</h3> | ||
{usernameToAddress && | ||
[...usernameToAddress].map(([username, addr]) => ( | ||
<p key={username}> | ||
{username}: {addr} | ||
</p> | ||
))} | ||
{error && <p className="error">{error.message}</p>} | ||
<Button onClick={onClick}>Lookup Controllers</Button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const KEYCHAIN_URL = "https://x.cartridge.gg"; | ||
export const PROFILE_URL = "https://profile.cartridge.gg"; | ||
export const API_URL = "https://api.cartridge.gg"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export { default } from "./controller"; | ||
export * from "./errors"; | ||
export * from "./types"; | ||
export * from "./lookup"; | ||
|
||
export { toWasmPolicies, toSessionPolicies, toArray } from "./utils"; | ||
export * from "@cartridge/presets"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { LookupRequest, LookupResponse } from "./types"; | ||
import { num } from "starknet"; | ||
import { API_URL } from "./constants"; | ||
|
||
const cache = new Map<string, string>(); | ||
|
||
async function lookup(request: LookupRequest): Promise<LookupResponse> { | ||
if (!request.addresses?.length && !request.usernames?.length) { | ||
return { results: [] }; | ||
} | ||
|
||
const response = await fetch(`${API_URL}/lookup`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(request), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
export async function lookupUsernames( | ||
usernames: string[], | ||
): Promise<Map<string, string>> { | ||
const uncachedUsernames = usernames.filter((name) => !cache.has(name)); | ||
|
||
if (uncachedUsernames.length > 0) { | ||
const response = await lookup({ usernames: uncachedUsernames }); | ||
|
||
response.results.forEach((result) => { | ||
cache.set(result.username, result.addresses[0]); // TODO: handle multiple controller addresses | ||
}); | ||
} | ||
|
||
return new Map( | ||
usernames | ||
.map((name) => [name, cache.get(name)] as [string, string]) | ||
.filter((entry): entry is [string, string] => entry[1] !== undefined), | ||
); | ||
} | ||
|
||
export async function lookupAddresses( | ||
addresses: string[], | ||
): Promise<Map<string, string>> { | ||
addresses = addresses.map(num.toHex); | ||
const uncachedAddresses = addresses.filter((addr) => !cache.has(addr)); | ||
|
||
if (uncachedAddresses.length > 0) { | ||
const response = await lookup({ | ||
addresses: uncachedAddresses, | ||
}); | ||
|
||
response.results.forEach((result) => { | ||
cache.set(result.addresses[0], result.username); // TODO: handle multiple controller addresses | ||
}); | ||
} | ||
|
||
return new Map( | ||
addresses | ||
.map((addr) => [addr, cache.get(addr)] as [string, string]) | ||
.filter((entry): entry is [string, string] => entry[1] !== undefined), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.