-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4254c8c
commit 5313dda
Showing
22 changed files
with
311 additions
and
64 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
48 changes: 48 additions & 0 deletions
48
packages/connect/src/actions/app/verifySignInMessage.test.ts
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,48 @@ | ||
import { createAppClient } from "../../clients/createAppClient"; | ||
import { createAuthClient } from "../../clients/createAuthClient"; | ||
import { viem } from "../../clients/ethereum/viem"; | ||
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts"; | ||
import { ConnectError } from "../../errors"; | ||
|
||
describe("verifySignInMessage", () => { | ||
const client = createAppClient({ | ||
relayURI: "https://connect.farcaster.xyz", | ||
ethereum: viem(), | ||
}); | ||
|
||
const authClient = createAuthClient({ | ||
relayURI: "https://connect.farcaster.xyz", | ||
ethereum: viem(), | ||
}); | ||
|
||
const account = privateKeyToAccount(generatePrivateKey()); | ||
|
||
const siweParams = { | ||
domain: "example.com", | ||
uri: "https://example.com/login", | ||
version: "1", | ||
issuedAt: "2023-10-01T00:00:00.000Z", | ||
}; | ||
|
||
test("verifies sign in message", async () => { | ||
const message = authClient.buildSignInMessage({ | ||
...siweParams, | ||
address: account.address, | ||
fid: 1234, | ||
}); | ||
|
||
const signature = await account.signMessage({ | ||
message: message.toMessage(), | ||
}); | ||
|
||
const errMsg = `Invalid resource: signer ${account.address} does not own fid 1234.`; | ||
const error = new ConnectError("unauthorized", errMsg); | ||
|
||
await expect( | ||
client.verifySignInMessage({ | ||
message, | ||
signature, | ||
}), | ||
).rejects.toStrictEqual(error); | ||
}); | ||
}); |
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
31 changes: 31 additions & 0 deletions
31
packages/connect/src/actions/auth/buildSignInMessage.test.ts
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,31 @@ | ||
import { createAuthClient } from "../../clients/createAuthClient"; | ||
import { viem } from "../../clients/ethereum/viem"; | ||
|
||
describe("buildSignInMessage", () => { | ||
const client = createAuthClient({ | ||
relayURI: "https://connect.farcaster.xyz", | ||
ethereum: viem(), | ||
}); | ||
|
||
test("builds Siwe message from provided parameters", async () => { | ||
const message = client.buildSignInMessage({ | ||
address: "0x63C378DDC446DFf1d831B9B96F7d338FE6bd4231", | ||
uri: "https://example.com/login", | ||
domain: "example.com", | ||
nonce: "12345678", | ||
fid: 1, | ||
resources: ["https://example.com/resource"], | ||
}); | ||
|
||
expect(message).toMatchObject({ | ||
address: "0x63C378DDC446DFf1d831B9B96F7d338FE6bd4231", | ||
statement: "Farcaster Connect", | ||
chainId: 10, | ||
uri: "https://example.com/login", | ||
domain: "example.com", | ||
version: "1", | ||
nonce: "12345678", | ||
resources: ["farcaster://fid/1", "https://example.com/resource"], | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { createAuthClient } from "../../clients/createAuthClient"; | ||
import { viem } from "../../clients/ethereum/viem"; | ||
|
||
describe("parseSignInURI", () => { | ||
const client = createAuthClient({ | ||
relayURI: "https://connect.farcaster.xyz", | ||
ethereum: viem(), | ||
}); | ||
|
||
test("parses sign in params from protocol URI", async () => { | ||
const { channelToken, params } = client.parseSignInURI({ | ||
uri: "farcaster://connect?channelToken=76be6229-bdf7-4ad2-930a-540fb2de1e08&nonce=ESsxs6MaFio7OvqWb&siweUri=https%3A%2F%2Fexample.com%2Flogin&domain=example.com", | ||
}); | ||
|
||
expect(channelToken).toBe("76be6229-bdf7-4ad2-930a-540fb2de1e08"); | ||
expect(params).toStrictEqual({ | ||
domain: "example.com", | ||
siweUri: "https://example.com/login", | ||
nonce: "ESsxs6MaFio7OvqWb", | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { Client } from "../../clients/createClient"; | ||
import { parseSignInURI as parse, ParsedSignInURI } from "../../messages/parseSignInURI"; | ||
|
||
export interface ParseSignInURIArgs { | ||
uri: string; | ||
} | ||
export type ParseSignInURIResponse = ParsedSignInURI; | ||
|
||
export const parseSignInURI = (_client: Client, { uri }: ParseSignInURIArgs): ParseSignInURIResponse => { | ||
const result = parse(uri); | ||
if (result.isErr()) { | ||
throw result.error; | ||
} else { | ||
return result.value; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import { authenticate, AuthenticateArgs, AuthenticateResponse } from "../actions/auth/authenticate"; | ||
import { parseSignInURI, ParseSignInURIArgs, ParseSignInURIResponse } from "../actions/auth/parseSignInURI"; | ||
import { | ||
buildSignInMessage, | ||
BuildSignInMessageArgs, | ||
BuildSignInMessageResponse, | ||
} from "../actions/auth/buildSignInMessage"; | ||
import { Client, ClientConfig, createClient } from "./createClient"; | ||
import { Client, CreateClientArgs, createClient } from "./createClient"; | ||
|
||
export interface AuthClient extends Client { | ||
authenticate: (args: AuthenticateArgs) => AuthenticateResponse; | ||
buildSignInMessage: (args: BuildSignInMessageArgs) => BuildSignInMessageResponse; | ||
parseSignInURI: (args: ParseSignInURIArgs) => ParseSignInURIResponse; | ||
} | ||
|
||
export const createAuthClient = (config: ClientConfig): AuthClient => { | ||
export const createAuthClient = (config: CreateClientArgs): AuthClient => { | ||
const client = createClient(config); | ||
return { | ||
...client, | ||
authenticate: (args: AuthenticateArgs) => authenticate(client, args), | ||
buildSignInMessage: (args: BuildSignInMessageArgs) => buildSignInMessage(client, args), | ||
parseSignInURI: (args: ParseSignInURIArgs) => parseSignInURI(client, args), | ||
}; | ||
}; |
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,18 +1,28 @@ | ||
import { Ethereum } from "../clients/ethereum/viem"; | ||
|
||
export interface CreateClientArgs { | ||
relayURI: string; | ||
version?: string; | ||
ethereum: Ethereum; | ||
} | ||
|
||
export interface ClientConfig { | ||
relayURI: string; | ||
version?: string; | ||
} | ||
|
||
export interface Client { | ||
config: ClientConfig; | ||
ethereum: Ethereum; | ||
} | ||
|
||
const configDefaults = { | ||
version: "v1", | ||
}; | ||
|
||
export const createClient = (config: ClientConfig) => { | ||
export const createClient = ({ ethereum, ...config }: CreateClientArgs) => { | ||
return { | ||
config: { ...configDefaults, ...config }, | ||
ethereum: ethereum, | ||
}; | ||
}; |
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,31 @@ | ||
import { Hex, createPublicClient, http } from "viem"; | ||
import { optimism } from "viem/chains"; | ||
import { ID_REGISTRY_ADDRESS, idRegistryABI } from "@farcaster/hub-web"; | ||
|
||
export interface Ethereum { | ||
getFid: (custody: Hex) => Promise<BigInt>; | ||
} | ||
|
||
interface ViemConfigArgs { | ||
rpcUrl?: string; | ||
} | ||
|
||
export const viem = (args?: ViemConfigArgs): Ethereum => { | ||
const publicClient = createPublicClient({ | ||
chain: optimism, | ||
transport: http(args?.rpcUrl), | ||
}); | ||
|
||
const getFid = async (custody: Hex): Promise<BigInt> => { | ||
return publicClient.readContract({ | ||
address: ID_REGISTRY_ADDRESS, | ||
abi: idRegistryABI, | ||
functionName: "idOf", | ||
args: [custody], | ||
}); | ||
}; | ||
|
||
return { | ||
getFid, | ||
}; | ||
}; |
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
Oops, something went wrong.