Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayprabhu committed Feb 2, 2024
1 parent 0b5b0a5 commit b750e68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
10 changes: 5 additions & 5 deletions .changeset/itchy-llamas-act.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
"@farcaster/hub-nodejs": minor
"@farcaster/hub-web": minor
"@farcaster/replicator": minor
"@farcaster/core": minor
"@farcaster/hubble": minor
"@farcaster/hub-nodejs": patch
"@farcaster/hub-web": patch
"@farcaster/replicator": patch
"@farcaster/core": patch
"@farcaster/hubble": patch
---

Add Solana verification support
12 changes: 6 additions & 6 deletions .changeset/old-dolphins-pump.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
"@farcaster/hub-nodejs": minor
"@farcaster/hub-web": minor
"@farcaster/replicator": minor
"@farcaster/core": minor
"@farcaster/hubble": minor
"@farcaster/hub-nodejs": patch
"@farcaster/hub-web": patch
"@farcaster/replicator": patch
"@farcaster/core": patch
"@farcaster/hubble": patch
---

Rename verification message
Rename signature field in verification message
2 changes: 1 addition & 1 deletion apps/hubble/src/rpc/test/verificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("getVerification", () => {
await engine.mergeOnChainEvent(storageEvent);
});

test.only("succeeds", async () => {
test("succeeds", async () => {
const solanaMerge = await engine.mergeMessage(solVerificationAdd);
expect(solanaMerge.isOk()).toBeTruthy();

Expand Down
57 changes: 30 additions & 27 deletions packages/core/src/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getFarcasterTime, toFarcasterTime } from "./time";
import { makeVerificationAddressClaim } from "./verifications";
import { normalize } from "viem/ens";
import { defaultPublicClients, PublicClients } from "./eth/clients";
import { verify } from "noble-ed25519";
import { toBigInt } from "ethers";
import bs58 from "bs58";

Expand Down Expand Up @@ -636,30 +635,7 @@ export const validateVerificationAddAddressBody = async (
case protobufs.Protocol.ETHEREUM:
return await validateVerificationAddEthAddressBody(body, fid, network, publicClients);
case protobufs.Protocol.SOLANA: {
const response = validateVerificationAddSolAddressBody(body);
if (response.isErr()) {
return err(response.error);
}
const message = {
fid: toBigInt(fid),
network: network,
blockHash: bs58.encode(body.blockHash),
address: bs58.encode(body.address),
protocol: body.protocol,
};
const encodedMessage = new TextEncoder().encode(
JSON.parse(
JSON.stringify(
message,
(_key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
),
),
);
const isVerified = await verify(body.addressVerificationSignature, encodedMessage, body.address);
if (!isVerified) {
return err(new HubError("bad_request.validation_failure", "invalid signature"));
}
return ok(body);
return validateVerificationAddSolAddressBody(body, fid, network);

Check warning on line 638 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L637-L638

Added lines #L637 - L638 were not covered by tests
}
default:
return err(new HubError("bad_request.validation_failure", "invalid verification protocol"));

Check warning on line 641 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L640-L641

Added lines #L640 - L641 were not covered by tests
Expand Down Expand Up @@ -694,9 +670,11 @@ export const validateVerificationAddEthAddressBody = async (
return ok(body);
};

export const validateVerificationAddSolAddressBody = (
export const validateVerificationAddSolAddressBody = async (
body: protobufs.VerificationAddAddressBody,
): HubResult<protobufs.VerificationAddAddressBody> => {
fid: number,
network: protobufs.FarcasterNetwork,

Check warning on line 676 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L676

Added line #L676 was not covered by tests
): HubAsyncResult<protobufs.VerificationAddAddressBody> => {
if (body.protocol !== protobufs.Protocol.SOLANA) {
return err(new HubError("bad_request.validation_failure", "invalid verification protocol"));

Check warning on line 679 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L679

Added line #L679 was not covered by tests
}
Expand All @@ -713,6 +691,31 @@ export const validateVerificationAddSolAddressBody = (
return err(new HubError("bad_request.validation_failure", "addressVerificationSignature != 64 bytes"));

Check warning on line 691 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L691

Added line #L691 was not covered by tests
}

const message = {

Check warning on line 694 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L694

Added line #L694 was not covered by tests
fid: toBigInt(fid),
network: network,
blockHash: bs58.encode(body.blockHash),
address: bs58.encode(body.address),
protocol: body.protocol,
};
const encodedMessage = new TextEncoder().encode(

Check warning on line 701 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L701

Added line #L701 was not covered by tests
JSON.parse(
JSON.stringify(
message,
(_key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
),
),
);
// These messages are pretty rare, so it's fine performance wise to use the pure JS implementation
const isVerified = pureJSValidationMethods.ed25519_verify(

Check warning on line 710 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L710

Added line #L710 was not covered by tests
body.addressVerificationSignature,
encodedMessage,
body.address,
);
if (!isVerified) {
return err(new HubError("bad_request.validation_failure", "invalid signature"));

Check warning on line 716 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L716

Added line #L716 was not covered by tests
}

return ok(body);

Check warning on line 719 in packages/core/src/validations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/validations.ts#L719

Added line #L719 was not covered by tests
};

Expand Down

0 comments on commit b750e68

Please sign in to comment.