Skip to content

Commit

Permalink
refactor: change signature for verification message
Browse files Browse the repository at this point in the history
  • Loading branch information
Wazzymandias committed Jan 31, 2024
1 parent 3e90808 commit 1afc542
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 171 deletions.
2 changes: 1 addition & 1 deletion apps/hubble/src/console/warpcastTestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class WarpcastTestCommand implements ConsoleCommandInterface {
data: {
fid,
network,
verificationRemoveBody: { address: verificationAdd.data.verificationAddEthAddressBody.address },
verificationRemoveBody: { address: verificationAdd.data.verificationAddAddressBody.address },
},
},
{ transient: { signer } },
Expand Down
10 changes: 4 additions & 6 deletions apps/hubble/src/rpc/test/httpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,17 +676,15 @@ describe("httpServer", () => {
test("getVerification", async () => {
expect((await engine.mergeMessage(verificationAdd)).isOk()).toBeTruthy();

const address = verificationAdd.data.verificationAddEthAddressBody.address;
const address = verificationAdd.data.verificationAddAddressBody.address;
const url = getFullUrl(`/v1/verificationsByFid?fid=${fid}&address=${bytesToHexString(address)._unsafeUnwrap()}`);
const response = await axiosGet(url);

expect(response.status).toBe(200);
expect(response.data).toEqual(protoToJSON(verificationAdd, Message));
expect(response.data.data.verificationAddEthAddressBody.address).toEqual(
bytesToHexString(address)._unsafeUnwrap(),
);
expect(response.data.data.verificationAddEthAddressBody.blockHash).toEqual(
bytesToHexString(verificationAdd.data.verificationAddEthAddressBody.blockHash)._unsafeUnwrap(),
expect(response.data.data.verificationAddAddressBody.address).toEqual(bytesToHexString(address)._unsafeUnwrap());
expect(response.data.data.verificationAddAddressBody.blockHash).toEqual(
bytesToHexString(verificationAdd.data.verificationAddAddressBody.blockHash)._unsafeUnwrap(),
);

// Get via fid
Expand Down
6 changes: 3 additions & 3 deletions apps/hubble/src/rpc/test/verificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe("getVerification", () => {
const result = await client.getVerification(
VerificationRequest.create({
fid,
address: verificationAdd.data.verificationAddEthAddressBody.address ?? new Uint8Array(),
address: verificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
}),
);
expect(Message.toJSON(result._unsafeUnwrap())).toEqual(Message.toJSON(verificationAdd));
Expand All @@ -86,7 +86,7 @@ describe("getVerification", () => {
const result = await client.getVerification(
VerificationRequest.create({
fid,
address: verificationAdd.data.verificationAddEthAddressBody.address ?? new Uint8Array(),
address: verificationAdd.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.verificationAddEthAddressBody.address ?? new Uint8Array(),
address: verificationAdd.data.verificationAddAddressBody.address ?? new Uint8Array(),
}),
);
expect(result._unsafeUnwrapErr()).toEqual(new HubError("bad_request.validation_failure", "fid is missing"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("uniqueVerifications migration", () => {
const tsHash = makeTsHash(message.data?.timestamp, message.hash)._unsafeUnwrap();
putMessageTransaction(txn, message);
if (isVerificationAddAddressMessage(message)) {
const addKey = makeVerificationAddsKey(message.data.fid, message.data.verificationAddEthAddressBody.address);
const addKey = makeVerificationAddsKey(message.data.fid, message.data.verificationAddAddressBody.address);
txn.put(addKey, Buffer.from(tsHash));
} else {
const removeKey = makeVerificationRemovesKey(message.data.fid, message.data.verificationRemoveBody.address);
Expand All @@ -37,7 +37,7 @@ describe("uniqueVerifications migration", () => {
const ethSignerKey = (await ethSigner.getSignerKey())._unsafeUnwrap();
const verificationAdd1 = await Factories.VerificationAddEthAddressMessage.create(
{
data: { fid, verificationAddEthAddressBody: { address: ethSignerKey } },
data: { fid, verificationAddAddressBody: { address: ethSignerKey } },
},
{ transient: { ethSigner } },
);
Expand Down
18 changes: 11 additions & 7 deletions apps/hubble/src/storage/engine/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe("mergeMessage", () => {
test("succeeds", async () => {
await expect(engine.mergeMessage(verificationAdd)).resolves.toBeInstanceOf(Ok);
await expect(
engine.getVerification(fid, verificationAdd.data.verificationAddEthAddressBody.address),
engine.getVerification(fid, verificationAdd.data.verificationAddAddressBody.address),
).resolves.toEqual(ok(verificationAdd));
expect(mergedMessages).toEqual([verificationAdd]);
});
Expand All @@ -197,14 +197,18 @@ describe("mergeMessage", () => {
data: {
fid,
network: FarcasterNetwork.TESTNET,
verificationAddEthAddressBody: { address: address, blockHash: blockHash, ethSignature: claimSignature },
verificationAddAddressBody: {
address: address,
blockHash: blockHash,
protocolSignature: claimSignature,
},
},
},
{ transient: { signer: signer, ethSigner: custodySigner } },
);
const result = await engine.mergeMessage(testnetVerificationAdd);
// Signature will not match because we're attempting to recover the address based on the wrong network
expect(result).toEqual(err(new HubError("bad_request.validation_failure", "invalid ethSignature")));
expect(result).toEqual(err(new HubError("bad_request.validation_failure", "invalid protocolSignature")));
});

describe("validateOrRevokeMessage", () => {
Expand All @@ -223,7 +227,7 @@ describe("mergeMessage", () => {
{
data: {
fid,
verificationAddEthAddressBody: Factories.VerificationAddEthAddressBody.build({
verificationAddAddressBody: Factories.VerificationAddEthAddressBody.build({
chainId: 1,
verificationType: 1,
}),
Expand Down Expand Up @@ -631,7 +635,7 @@ describe("mergeMessage", () => {
test("succeeds for valid proof for verified eth address", async () => {
await engine.mergeMessage(verificationAdd);
const verificationAddress = bytesToHexString(
verificationAdd.data.verificationAddEthAddressBody.address,
verificationAdd.data.verificationAddAddressBody.address,
)._unsafeUnwrap();
jest.spyOn(publicClient, "getEnsAddress").mockImplementation(() => {
return Promise.resolve(verificationAddress);
Expand Down Expand Up @@ -675,7 +679,7 @@ describe("mergeMessage", () => {
beforeEach(async () => {
const custodyAddress = bytesToHexString(custodyEvent.idRegisterEventBody.to)._unsafeUnwrap();
const verificationAddress = bytesToHexString(
verificationAdd.data.verificationAddEthAddressBody.address,
verificationAdd.data.verificationAddAddressBody.address,
)._unsafeUnwrap();

jest.spyOn(publicClient, "getEnsAddress").mockImplementation((opts) => {
Expand Down Expand Up @@ -746,7 +750,7 @@ describe("mergeMessage", () => {
data: {
fid,
timestamp: verificationAdd.data.timestamp + 2,
verificationRemoveBody: { address: verificationAdd.data.verificationAddEthAddressBody.address },
verificationRemoveBody: { address: verificationAdd.data.verificationAddAddressBody.address },
},
},
{ transient: { signer } },
Expand Down
19 changes: 9 additions & 10 deletions apps/hubble/src/storage/stores/verificationStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ beforeAll(async () => {
ethSignerKey = (await ethSigner.getSignerKey())._unsafeUnwrap();
verificationAdd = await Factories.VerificationAddEthAddressMessage.create(
{
data: { fid, verificationAddEthAddressBody: { address: ethSignerKey } },
data: { fid, verificationAddAddressBody: { address: ethSignerKey } },
},
{ transient: { ethSigner } },
);
Expand Down Expand Up @@ -499,7 +499,7 @@ describe("revoke", () => {
expect(result.isOk()).toBeTruthy();
expect(result._unsafeUnwrap()).toBeGreaterThan(0);
await expect(
set.getVerificationAdd(fid, verificationAdd.data.verificationAddEthAddressBody.address),
set.getVerificationAdd(fid, verificationAdd.data.verificationAddAddressBody.address),
).rejects.toThrow();
expect(revokedMessages).toEqual([verificationAdd]);
});
Expand All @@ -520,7 +520,7 @@ describe("revoke", () => {
expect(result.isOk()).toBeTruthy();
expect(result._unsafeUnwrap()).toBeGreaterThan(0);
await expect(
set.getVerificationAdd(fid, verificationAdd.data.verificationAddEthAddressBody.address),
set.getVerificationAdd(fid, verificationAdd.data.verificationAddAddressBody.address),
).rejects.toThrow();
expect(revokedMessages).toEqual([verificationAdd]);
});
Expand Down Expand Up @@ -580,11 +580,11 @@ describe("pruneMessages", () => {
add5 = await generateAddWithTimestamp(fid, time + 5);
addOld1 = await generateAddWithTimestamp(fid, time - 60 * 60);

remove1 = await generateRemoveWithTimestamp(fid, time + 1, add1.data.verificationAddEthAddressBody.address);
remove2 = await generateRemoveWithTimestamp(fid, time + 2, add2.data.verificationAddEthAddressBody.address);
remove3 = await generateRemoveWithTimestamp(fid, time + 3, add3.data.verificationAddEthAddressBody.address);
remove4 = await generateRemoveWithTimestamp(fid, time + 4, add4.data.verificationAddEthAddressBody.address);
remove5 = await generateRemoveWithTimestamp(fid, time + 5, add5.data.verificationAddEthAddressBody.address);
remove1 = await generateRemoveWithTimestamp(fid, time + 1, add1.data.verificationAddAddressBody.address);
remove2 = await generateRemoveWithTimestamp(fid, time + 2, add2.data.verificationAddAddressBody.address);
remove3 = await generateRemoveWithTimestamp(fid, time + 3, add3.data.verificationAddAddressBody.address);
remove4 = await generateRemoveWithTimestamp(fid, time + 4, add4.data.verificationAddAddressBody.address);
remove5 = await generateRemoveWithTimestamp(fid, time + 5, add5.data.verificationAddAddressBody.address);
});

describe("with size limit", () => {
Expand All @@ -608,8 +608,7 @@ describe("pruneMessages", () => {
expect(prunedMessages).toEqual([add1, add2]);

for (const message of prunedMessages as VerificationAddAddressMessage[]) {
const getAdd = () =>
sizePrunedStore.getVerificationAdd(fid, message.data.verificationAddEthAddressBody.address);
const getAdd = () => sizePrunedStore.getVerificationAdd(fid, message.data.verificationAddAddressBody.address);
await expect(getAdd()).rejects.toThrow(HubError);
}
});
Expand Down
14 changes: 7 additions & 7 deletions apps/hubble/src/storage/stores/verificationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
override makeAddKey(msg: VerificationAddAddressMessage) {
return makeVerificationAddsKey(
msg.data.fid,
(msg.data.verificationAddEthAddressBody || msg.data.verificationRemoveBody).address,
(msg.data.verificationAddAddressBody || msg.data.verificationRemoveBody).address,
) as Buffer;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
return err(tsHash.error);
}

const address = message.data.verificationAddEthAddressBody.address;
const address = message.data.verificationAddAddressBody.address;

if (address.length === 0) {
return err(new HubError("bad_request.invalid_param", "address empty"));
Expand All @@ -195,7 +195,7 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
txn: Transaction,
message: VerificationAddAddressMessage,
): HubAsyncResult<void> {
const address = message.data.verificationAddEthAddressBody.address;
const address = message.data.verificationAddAddressBody.address;

if (address.length === 0) {
return err(new HubError("bad_request.invalid_param", "address empty"));
Expand Down Expand Up @@ -223,7 +223,7 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
// For adds, we also need to check for conflicts across all fids (by eth address)
const conflicts = res.value;

const byAddressKey = makeVerificationByAddressKey(message.data.verificationAddEthAddressBody.address);
const byAddressKey = makeVerificationByAddressKey(message.data.verificationAddAddressBody.address);
const fidResult = await ResultAsync.fromPromise(this._db.get(byAddressKey), () => undefined);
if (fidResult.isOk()) {
const fid = readFidKey(fidResult.value);
Expand All @@ -233,7 +233,7 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
data: {
fid,
verificationAddAddressBody: {
address: message.data.verificationAddEthAddressBody.address,
address: message.data.verificationAddAddressBody.address,
},
},
});
Expand Down Expand Up @@ -294,9 +294,9 @@ class VerificationStore extends Store<VerificationAddAddressMessage, Verificatio
}

const fid = message.value.data.fid;
const verificationAdd = message.value.data.verificationAddEthAddressBody;
const verificationAdd = message.value.data.verificationAddAddressBody;
const txn = this._db.transaction();
const byAddressKey = makeVerificationByAddressKey(message.value.data.verificationAddEthAddressBody.address);
const byAddressKey = makeVerificationByAddressKey(message.value.data.verificationAddAddressBody.address);
const existingFidRes = await ResultAsync.fromPromise(this._db.get(byAddressKey), () => undefined);
if (existingFidRes.isOk()) {
const existingFid = readFidKey(existingFidRes.value);
Expand Down
4 changes: 2 additions & 2 deletions apps/replicator/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ export type ReactionBodyJson = ReactionBodyCastJson | ReactionBodyUrlJson;

export type VerificationAddEthAddressBodyJson = {
address: Hex;
ethSignature: Hex;
protocolSignature: Hex;
blockHash: Hex;
};

export type VerificationAddSolAddressBodyJson = {
address: Hex;
solSignature: Hex;
protocolSignature: Hex;
blockHash: Hex;
};

Expand Down
8 changes: 4 additions & 4 deletions apps/replicator/src/processors/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { processAdd: processAddEthereum, processRemove } = buildAddRemoveMessageP
withConflictId(message) {
const ethAddress =
message.data.type === MessageType.VERIFICATION_ADD_ADDRESS
? message.data.verificationAddEthAddressBody.address
? message.data.verificationAddAddressBody.address
: message.data.verificationRemoveBody.address;

return ({ eb, and }) => {
Expand All @@ -25,7 +25,7 @@ const { processAdd: processAddEthereum, processRemove } = buildAddRemoveMessageP
async getDerivedRow(message, trx) {
const ethAddress =
message.data.type === MessageType.VERIFICATION_ADD_ADDRESS
? message.data.verificationAddEthAddressBody.address
? message.data.verificationAddAddressBody.address
: message.data.verificationRemoveBody.address;

return await executeTakeFirst(
Expand All @@ -52,7 +52,7 @@ const { processAdd: processAddEthereum, processRemove } = buildAddRemoveMessageP
},
async mergeDerivedRow(message, deleted, trx) {
const {
data: { fid, verificationAddEthAddressBody: verificationAddBody },
data: { fid, verificationAddAddressBody: verificationAddBody },
} = message;

const timestamp = farcasterTimeToDate(message.data.timestamp);
Expand All @@ -62,7 +62,7 @@ const { processAdd: processAddEthereum, processRemove } = buildAddRemoveMessageP
hash: message.hash,
signerAddress: verificationAddBody.address,
blockHash: verificationAddBody.blockHash,
signature: verificationAddBody.ethSignature,
signature: verificationAddBody.protocolSignature,
};

// Upsert the verification, if it's shadowed by a remove, mark it as deleted
Expand Down
8 changes: 4 additions & 4 deletions apps/replicator/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,21 @@ export function convertProtobufMessageBodyToJson(message: Message): MessageBodyJ
if (!message.data.verificationAddAddressBody) {
throw new Error("Missing verificationAddEthAddressBody");
}
const { address, ethSignature, blockHash } = message.data.verificationAddAddressBody;
const { address, protocolSignature, blockHash } = message.data.verificationAddAddressBody;
return {
address: bytesToHex(address),
ethSignature: bytesToHex(ethSignature),
protocolSignature: bytesToHex(protocolSignature),
blockHash: bytesToHex(blockHash),
} satisfies VerificationAddEthAddressBodyJson;
}
case MessageType.VERIFICATION_ADD_SOL_ADDRESS: {
if (!message.data.verificationAddAddressBody) {
throw new Error("Missing verificationAddSolAddressBody");
}
const { address, solSignature, blockHash } = message.data.verificationAddAddressBody;
const { address, protocolSignature, blockHash } = message.data.verificationAddAddressBody;
return {
address: bytesToHex(address),
solSignature: bytesToHex(solSignature),
protocolSignature: bytesToHex(protocolSignature),
blockHash: bytesToHex(blockHash),
} satisfies VerificationAddSolAddressBodyJson;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/builders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe("makeVerificationAddEthAddressData", () => {

test("succeeds", async () => {
const data = await builders.makeVerificationAddEthAddressData(
{ address: ethSignerKey, blockHash, ethSignature },
{ address: ethSignerKey, blockHash: blockHash, protocolSignature: ethSignature, protocol: Protocol.ETHEREUM },
{ fid, network },
);
expect(data.isOk()).toBeTruthy();
Expand Down Expand Up @@ -189,7 +189,7 @@ describe("makeVerificationAddEthAddress", () => {

test("succeeds", async () => {
const message = await builders.makeVerificationAddEthAddress(
{ address: ethSignerKey, blockHash, ethSignature },
{ address: ethSignerKey, blockHash: blockHash, protocolSignature: ethSignature, protocol: Protocol.ETHEREUM },
{ fid, network },
ed25519Signer,
);
Expand All @@ -200,7 +200,11 @@ describe("makeVerificationAddEthAddress", () => {

describe("makeVerificationRemove", () => {
test("succeeds", async () => {
const message = await builders.makeVerificationRemove({ address: ethSignerKey }, { fid, network }, ed25519Signer);
const message = await builders.makeVerificationRemove(
{ address: ethSignerKey, protocol: Protocol.ETHEREUM },
{ fid, network },
ed25519Signer,
);
const isValid = await validations.validateMessage(message._unsafeUnwrap());
expect(isValid.isOk()).toBeTruthy();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export const makeVerificationAddEthAddressData = (
body: protobufs.VerificationAddAddressBody,
dataOptions: MessageDataOptions,
publicClients: PublicClients = defaultPublicClients,
): HubAsyncResult<protobufs.VerificationAddEthAddressData> => {
): HubAsyncResult<protobufs.VerificationAddAddressData> => {
return makeMessageData(
{ verificationAddAddressBody: body },
protobufs.MessageType.VERIFICATION_ADD_ADDRESS,
Expand Down
Loading

0 comments on commit 1afc542

Please sign in to comment.