Skip to content

Commit

Permalink
Add protocol to verifications, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wazzymandias committed Jan 31, 2024
1 parent 08290fa commit 1cbc91d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
18 changes: 9 additions & 9 deletions apps/hubble/src/rpc/test/verificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let custodyEvent: OnChainEvent;
let signerEvent: OnChainEvent;
let storageEvent: OnChainEvent;

let verificationAdd: VerificationAddAddressMessage;
let ethVerificationAdd: VerificationAddAddressMessage;

beforeAll(async () => {
const signerKey = (await signer.getSignerKey())._unsafeUnwrap();
Expand All @@ -56,7 +56,7 @@ beforeAll(async () => {
signerEvent = Factories.SignerOnChainEvent.build({ fid }, { transient: { signer: signerKey } });
storageEvent = Factories.StorageRentOnChainEvent.build({ fid });

verificationAdd = await Factories.VerificationAddEthAddressMessage.create(
ethVerificationAdd = await Factories.VerificationAddEthAddressMessage.create(
{ data: { fid, network } },
{ transient: { signer } },
);
Expand All @@ -70,23 +70,23 @@ describe("getVerification", () => {
});

test("succeeds", async () => {
const r = await engine.mergeMessage(verificationAdd);
const r = await engine.mergeMessage(ethVerificationAdd);
expect(r.isOk()).toBeTruthy();

const result = await client.getVerification(
VerificationRequest.create({
fid,
address: verificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
address: ethVerificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
}),
);
expect(Message.toJSON(result._unsafeUnwrap())).toEqual(Message.toJSON(verificationAdd));
expect(Message.toJSON(result._unsafeUnwrap())).toEqual(Message.toJSON(ethVerificationAdd));
});

test("fails if verification is missing", async () => {
const result = await client.getVerification(
VerificationRequest.create({
fid,
address: verificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
address: ethVerificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
}),
);
expect(result._unsafeUnwrapErr().errCode).toEqual("not_found");
Expand All @@ -107,7 +107,7 @@ describe("getVerification", () => {
test("fails without fid", async () => {
const result = await client.getVerification(
VerificationRequest.create({
address: verificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
address: ethVerificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
}),
);
expect(result._unsafeUnwrapErr()).toEqual(new HubError("bad_request.validation_failure", "fid is missing"));
Expand All @@ -122,12 +122,12 @@ describe("getVerificationsByFid", () => {
});

test("succeeds", async () => {
const result = await engine.mergeMessage(verificationAdd);
const result = await engine.mergeMessage(ethVerificationAdd);
expect(result.isOk()).toBeTruthy();

const verifications = await client.getVerificationsByFid(FidRequest.create({ fid }));
expect(verifications._unsafeUnwrap().messages.map((m) => Message.toJSON(m))).toEqual(
[verificationAdd].map((m) => Message.toJSON(m)),
[ethVerificationAdd].map((m) => Message.toJSON(m)),
);
});

Expand Down
2 changes: 2 additions & 0 deletions apps/hubble/src/storage/engine/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MessageType,
OnChainEvent,
OnChainEventType,
Protocol,
ReactionAddMessage,
ReactionType,
RevokeMessageHubEvent,
Expand Down Expand Up @@ -190,6 +191,7 @@ describe("mergeMessage", () => {
address,
FarcasterNetwork.MAINNET,
blockHash,
Protocol.ETHEREUM,
)._unsafeUnwrap();
const claimSignature = (await custodySigner.signVerificationEthAddressClaim(mainnetClaim))._unsafeUnwrap();
const testnetVerificationAdd = await Factories.VerificationAddEthAddressMessage.create(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/builders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("makeVerificationAddEthAddress", () => {
let claim: VerificationAddressClaim;

beforeAll(async () => {
claim = makeVerificationAddressClaim(fid, ethSignerKey, network, blockHash)._unsafeUnwrap();
claim = makeVerificationAddressClaim(fid, ethSignerKey, network, blockHash, Protocol.ETHEREUM)._unsafeUnwrap();
const signatureHex = (await eip712Signer.signVerificationEthAddressClaim(claim))._unsafeUnwrap();
expect(signatureHex).toBeTruthy();
ethSignature = signatureHex;
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/crypto/eip712.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { bytesToHex, verifyTypedData } from "viem";
import { ResultAsync } from "neverthrow";
import { HubAsyncResult, HubError } from "../errors";
import { VerificationAddressClaim } from "../verifications";
import { VerificationAddressClaim, VerificationAddressClaimEthereum } from "../verifications";
import { UserNameProofClaim } from "../userNameProof";
import { PublicClients, defaultPublicClients } from "../eth/clients";
import { defaultPublicClients, PublicClients } from "../eth/clients";
import { CHAIN_IDS } from "../eth/chains";

export const EIP_712_FARCASTER_DOMAIN = {
Expand Down Expand Up @@ -76,18 +76,17 @@ export const verifyVerificationClaimEOASignature = async (
() => new HubError("bad_request.invalid_param", "Invalid chain ID"),
);
}
const valid = await ResultAsync.fromPromise(
return ResultAsync.fromPromise(
verifyTypedData({
address: bytesToHex(address),
domain: EIP_712_FARCASTER_DOMAIN,
types: { VerificationClaim: EIP_712_FARCASTER_VERIFICATION_CLAIM },
primaryType: "VerificationClaim",
message: claim,
message: claim as VerificationAddressClaimEthereum,
signature,
}),
(e) => new HubError("unknown", e as Error),
);
return valid;
};

export const verifyVerificationClaimContractSignature = async (
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ const VerificationEthAddressClaimFactory = Factory.define<VerificationAddressCla
address,
network: FarcasterNetworkFactory.build(),
blockHash,
protocol: Protocol.ETHEREUM,
};
});

Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/validations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as protobufs from "./protobufs";
import { Protocol, UserNameType } from "./protobufs";
import { blake3 } from "@noble/hashes/blake3";
import { err, ok, Result } from "neverthrow";
import { bytesCompare, bytesToUtf8String, utf8StringToBytes } from "./bytes";
import { ed25519, eip712 } from "./crypto";
import { HubAsyncResult, HubError, HubResult } from "./errors";
import { getFarcasterTime, toFarcasterTime } from "./time";
import { makeVerificationAddressClaim } from "./verifications";
import { UserNameType } from "./protobufs";
import { normalize } from "viem/ens";
import { defaultPublicClients, PublicClients } from "./eth/clients";

Expand Down Expand Up @@ -331,7 +331,13 @@ export const validateVerificationAddEthAddressSignature = async (
return err(new HubError("bad_request.validation_failure", "addressVerificationSignature > 256 bytes"));
}

const reconstructedClaim = makeVerificationAddressClaim(fid, body.address, network, body.blockHash);
const reconstructedClaim = makeVerificationAddressClaim(
fid,
body.address,
network,
body.blockHash,
Protocol.ETHEREUM,
);
if (reconstructedClaim.isErr()) {
return err(reconstructedClaim.error);
}
Expand Down
23 changes: 18 additions & 5 deletions packages/core/src/verifications.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { FarcasterNetwork } from "./protobufs";
import { FarcasterNetwork, Protocol } from "./protobufs";
import { err, ok } from "neverthrow";
import { bytesToHexString } from "./bytes";
import { HubResult } from "./errors";
import { validateEthAddress, validateEthBlockHash } from "./validations";

export type VerificationAddressClaim = {
export type VerificationAddressClaim = VerificationAddressClaimEthereum | VerificationAddressClaimSolana;

export type VerificationAddressClaimEthereum = {
fid: bigint;
address: `0x${string}`;
network: FarcasterNetwork;
blockHash: `0x${string}`;
network: FarcasterNetwork;
protocol: Protocol.ETHEREUM;
};

export type VerificationAddressClaimSolana = {
fid: bigint;
address: string;
blockHash: string;
network: FarcasterNetwork;
protocol: Protocol.SOLANA;
};

export const makeVerificationAddressClaim = (
fid: number,
ethAddress: Uint8Array,
address: Uint8Array,
network: FarcasterNetwork,
blockHash: Uint8Array,
protocol: Protocol,
): HubResult<VerificationAddressClaim> => {
const ethAddressHex = validateEthAddress(ethAddress).andThen((validatedEthAddress) =>
const ethAddressHex = validateEthAddress(address).andThen((validatedEthAddress) =>
bytesToHexString(validatedEthAddress),
);
if (ethAddressHex.isErr()) {
Expand All @@ -36,5 +48,6 @@ export const makeVerificationAddressClaim = (
address: ethAddressHex.value,
network: network,
blockHash: blockHashHex.value,
protocol: protocol,
});
};

0 comments on commit 1cbc91d

Please sign in to comment.