Skip to content

Commit

Permalink
feat: parse sign in URI
Browse files Browse the repository at this point in the history
  • Loading branch information
horsefacts committed Dec 20, 2023
1 parent 3dadb62 commit 4254c8c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/connect/src/messages/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SiweMessage } from "siwe";
import { ConnectResult } from "../errors";
import { validate } from "./validate";
import { parseSignInURI } from "./parseSignInURI";
import { STATEMENT, CHAIN_ID } from "./constants";

export type FarcasterResourceParams = {
Expand All @@ -17,6 +18,10 @@ export const build = (params: SignInMessageParams): ConnectResult<SiweMessage> =
return validate(siweParams);
};

export const buildFromSignInURI = (signInUri: string, fid: number): ConnectResult<SiweMessage> => {
return parseSignInURI(signInUri).andThen(({ params }) => build({ ...params, fid }));
};

const buildFidResource = (fid: number): string => {
return `farcaster://fid/${fid}`;
};
17 changes: 17 additions & 0 deletions packages/connect/src/messages/parseSignInURI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { parseSignInURI } from "./parseSignInURI";

describe("parseSignInUri", () => {
test("parses protocol handler URI into message params", () => {
const signInUri =
"farcaster://connect?channelToken=76be6229-bdf7-4ad2-930a-540fb2de1e08&nonce=ESsxs6MaFio7OvqWb&siweUri=https%3A%2F%2Fexample.com%2Flogin&domain=example.com";
const result = parseSignInURI(signInUri);
expect(result._unsafeUnwrap()).toStrictEqual({
channelToken: "76be6229-bdf7-4ad2-930a-540fb2de1e08",
params: {
domain: "example.com",
siweUri: "https://example.com/login",
nonce: "ESsxs6MaFio7OvqWb",
},
});
});
});
31 changes: 31 additions & 0 deletions packages/connect/src/messages/parseSignInURI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { err, ok } from "neverthrow";
import { ConnectError, ConnectResult } from "../errors";
import { SignInMessageParams } from "./build";

export interface ParsedSignInURI {
channelToken: string;
params: Partial<SignInMessageParams>;
}

export const parseSignInURI = (signInUri: string): ConnectResult<ParsedSignInURI> => {
const url = new URL(signInUri);
const searchParams = Object.fromEntries(url.searchParams.entries());
const { channelToken, ...params } = searchParams;
if (!channelToken) {
return err(validationFail("No channel token provided"));
}
if (!params["nonce"]) {
return err(validationFail("No nonce provided"));
}
if (!params["siweUri"]) {
return err(validationFail("No SIWE URI provided"));
}
if (!params["domain"]) {
return err(validationFail("No domain provided"));
}
return ok({ channelToken, params });
};

const validationFail = (message: string): ConnectError => {
return new ConnectError("bad_request.validation_failure", message);
};

0 comments on commit 4254c8c

Please sign in to comment.