Skip to content

Commit

Permalink
refactor: refactor messages
Browse files Browse the repository at this point in the history
  • Loading branch information
horsefacts committed Dec 20, 2023
1 parent 5ec3e60 commit dcd7d49
Show file tree
Hide file tree
Showing 23 changed files with 570 additions and 620 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAppClient } from "../clients/createAppClient";
import { createAppClient } from "../../clients/createAppClient";
import { jest } from "@jest/globals";

describe("connect", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Client } from "../clients/createClient";
import { AsyncHttpResponse, post } from "../clients/transports/http";
import { Client } from "../../clients/createClient";
import { AsyncHttpResponse, post } from "../../clients/transports/http";

export type ConnectArgs = ConnectRequest;
export type ConnectResponse = AsyncHttpResponse<ConnectAPIResponse>;

interface ConnectRequest {
siweUri: string;
Expand All @@ -12,13 +13,13 @@ interface ConnectRequest {
requestId?: string;
}

export interface ConnectResponse {
interface ConnectAPIResponse {
channelToken: string;
connectURI: string;
}

const path = "connect";

export const connect = async (client: Client, { ...request }: ConnectArgs): AsyncHttpResponse<ConnectResponse> => {
return post<ConnectRequest, ConnectResponse>(client, path, request);
export const connect = async (client: Client, { ...request }: ConnectArgs): ConnectResponse => {
return post<ConnectRequest, ConnectAPIResponse>(client, path, request);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAppClient } from "../clients/createAppClient";
import { createAppClient } from "../../clients/createAppClient";
import { jest } from "@jest/globals";

describe("status", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Client } from "../clients/createClient";
import { get, AsyncHttpResponse } from "../clients/transports/http";
import { Client } from "../../clients/createClient";
import { get, AsyncHttpResponse } from "../../clients/transports/http";

export interface StatusArgs {
channelToken: string;
}

export interface StatusResponse {
export type StatusResponse = AsyncHttpResponse<StatusAPIResponse>;

interface StatusAPIResponse {
state: "pending" | "completed";
nonce: string;
connectURI: string;
Expand All @@ -20,6 +22,6 @@ export interface StatusResponse {

const path = "connect/status";

export const status = async (client: Client, { channelToken }: StatusArgs): AsyncHttpResponse<StatusResponse> => {
export const status = async (client: Client, { channelToken }: StatusArgs): StatusResponse => {
return get(client, path, { authToken: channelToken });
};
21 changes: 21 additions & 0 deletions packages/connect/src/actions/app/verifySignInMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Client } from "../../clients/createClient";
import { SignInResponse, verify } from "../../messages/verify";

export interface VerifySignInMessageArgs {
message: string;
signature: `0x${string}`;
}

export type VerifySignInMessageResponse = Promise<SignInResponse>;

export const verifySignInMessage = async (
_client: Client,
{ message, signature }: VerifySignInMessageArgs,
): VerifySignInMessageResponse => {
const result = await verify(message, signature);
if (result.isErr()) {
throw result.error;
} else {
return result.value;
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAuthClient } from "../clients/createAuthClient";
import { createAuthClient } from "../../clients/createAuthClient";
import { jest } from "@jest/globals";

describe("authenticate", () => {
Expand Down
30 changes: 30 additions & 0 deletions packages/connect/src/actions/auth/authenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { StatusResponse } from "../app/status";
import { post, AsyncHttpResponse } from "../../clients/transports/http";
import { Client } from "../../clients/createClient";

export interface AuthenticateArgs extends AuthenticateRequest {
channelToken: string;
}

export type AuthenticateResponse = AsyncHttpResponse<AuthenticateAPIResponse>;

interface AuthenticateRequest {
message: string;
signature: `0x${string}`;
fid: number;
username: string;
bio: string;
displayName: string;
pfpUrl: string;
}

type AuthenticateAPIResponse = StatusResponse;

const path = "connect/authenticate";

export const authenticate = async (
client: Client,
{ channelToken, ...request }: AuthenticateArgs,
): AuthenticateResponse => {
return post<AuthenticateRequest, AuthenticateAPIResponse>(client, path, request, { authToken: channelToken });
};
15 changes: 15 additions & 0 deletions packages/connect/src/actions/auth/buildSignInMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client } from "clients/createClient";
import { build, SignInMessageParams } from "../../messages/build";
import { SiweMessage } from "siwe";

export type BuildSignInMessageArgs = SignInMessageParams;
export type BuildSignInMessageResponse = SiweMessage;

export const buildSignInMessage = (_client: Client, args: BuildSignInMessageArgs): BuildSignInMessageResponse => {
const result = build(args);
if (result.isErr()) {
throw result.error;
} else {
return result.value;
}
};
28 changes: 0 additions & 28 deletions packages/connect/src/actions/authenticate.ts

This file was deleted.

16 changes: 11 additions & 5 deletions packages/connect/src/clients/createAppClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { connect, ConnectArgs, ConnectResponse } from "../actions/connect";
import { status, StatusArgs, StatusResponse } from "../actions/status";
import { connect, ConnectArgs, ConnectResponse } from "../actions/app/connect";
import { status, StatusArgs, StatusResponse } from "../actions/app/status";
import {
verifySignInMessage,
VerifySignInMessageArgs,
VerifySignInMessageResponse,
} from "../actions/app/verifySignInMessage";
import { Client, ClientConfig, createClient } from "./createClient";
import { AsyncHttpResponse } from "./transports/http";

export interface AppClient extends Client {
connect: (args: ConnectArgs) => AsyncHttpResponse<ConnectResponse>;
status: (args: StatusArgs) => AsyncHttpResponse<StatusResponse>;
connect: (args: ConnectArgs) => ConnectResponse;
status: (args: StatusArgs) => StatusResponse;
verifySignInMessage: (args: VerifySignInMessageArgs) => VerifySignInMessageResponse;
}

export const createAppClient = (config: ClientConfig): AppClient => {
Expand All @@ -14,5 +19,6 @@ export const createAppClient = (config: ClientConfig): AppClient => {
...client,
connect: (args: ConnectArgs) => connect(client, args),
status: (args: StatusArgs) => status(client, args),
verifySignInMessage: (args: VerifySignInMessageArgs) => verifySignInMessage(client, args),
};
};
12 changes: 9 additions & 3 deletions packages/connect/src/clients/createAuthClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { authenticate, AuthenticateArgs, AuthenticateResponse } from "../actions/authenticate";
import { authenticate, AuthenticateArgs, AuthenticateResponse } from "../actions/auth/authenticate";
import {
buildSignInMessage,
BuildSignInMessageArgs,
BuildSignInMessageResponse,
} from "../actions/auth/buildSignInMessage";
import { Client, ClientConfig, createClient } from "./createClient";
import { AsyncHttpResponse } from "./transports/http";

export interface AuthClient extends Client {
authenticate: (args: AuthenticateArgs) => AsyncHttpResponse<AuthenticateResponse>;
authenticate: (args: AuthenticateArgs) => AuthenticateResponse;
buildSignInMessage: (args: BuildSignInMessageArgs) => BuildSignInMessageResponse;
}

export const createAuthClient = (config: ClientConfig): AuthClient => {
const client = createClient(config);
return {
...client,
authenticate: (args: AuthenticateArgs) => authenticate(client, args),
buildSignInMessage: (args: BuildSignInMessageArgs) => buildSignInMessage(client, args),
};
};
6 changes: 3 additions & 3 deletions packages/connect/src/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export { createAuthClient } from "./createAuthClient";
export type { AppClient } from "./createAppClient";
export type { AuthClient } from "./createAuthClient";
export type { ClientConfig } from "./createClient";
export type { ConnectArgs, ConnectResponse } from "../actions/connect";
export type { StatusArgs, StatusResponse } from "../actions/status";
export type { ConnectArgs, ConnectResponse } from "../actions/app/connect";
export type { StatusArgs, StatusResponse } from "../actions/app/status";
export type {
AuthenticateArgs,
AuthenticateResponse,
} from "../actions/authenticate";
} from "../actions/auth/authenticate";
export type { AsyncHttpResponse, HttpResponse } from "./transports/http";
1 change: 0 additions & 1 deletion packages/connect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./errors";
export * from "./messages";
export * from "./clients";
41 changes: 41 additions & 0 deletions packages/connect/src/messages/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { build } from "./build";

const siweParams = {
domain: "example.com",
address: "0x63C378DDC446DFf1d831B9B96F7d338FE6bd4231",
uri: "https://example.com/login",
version: "1",
nonce: "12345678",
issuedAt: "2023-10-01T00:00:00.000Z",
};

describe("build", () => {
test("adds connect-specific parameters", () => {
const result = build({
...siweParams,
fid: 5678,
});
expect(result.isOk()).toBe(true);
expect(result._unsafeUnwrap()).toMatchObject({
...siweParams,
statement: "Farcaster Connect",
chainId: 10,
resources: ["farcaster://fid/5678"],
});
});

test("handles additional resources", () => {
const result = build({
...siweParams,
fid: 5678,
resources: ["https://example.com/resource"],
});
expect(result.isOk()).toBe(true);
expect(result._unsafeUnwrap()).toMatchObject({
...siweParams,
statement: "Farcaster Connect",
chainId: 10,
resources: ["farcaster://fid/5678", "https://example.com/resource"],
});
});
});
22 changes: 22 additions & 0 deletions packages/connect/src/messages/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SiweMessage } from "siwe";
import { ConnectResult } from "../errors";
import { validate } from "./validate";
import { STATEMENT, CHAIN_ID } from "./constants";

export type FarcasterResourceParams = {
fid: number;
};
export type SignInMessageParams = Partial<SiweMessage> & FarcasterResourceParams;

export const build = (params: SignInMessageParams): ConnectResult<SiweMessage> => {
const { fid, ...siweParams } = params;
const resources = siweParams.resources ?? [];
siweParams.statement = STATEMENT;
siweParams.chainId = CHAIN_ID;
siweParams.resources = [buildFidResource(fid), ...resources];
return validate(siweParams);
};

const buildFidResource = (fid: number): string => {
return `farcaster://fid/${fid}`;
};
2 changes: 2 additions & 0 deletions packages/connect/src/messages/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const STATEMENT = "Farcaster Connect";
export const CHAIN_ID = 10;
1 change: 0 additions & 1 deletion packages/connect/src/messages/index.ts

This file was deleted.

Loading

0 comments on commit dcd7d49

Please sign in to comment.