-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
f026ad1
commit d939de2
Showing
10 changed files
with
886 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { HubAsyncResult, HubError } from "../../errors"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { verifyTypedData, bytesToHex } from "viem"; | ||
|
||
export type IdGatewayRegisterMessage = { | ||
to: `0x${string}`; | ||
recovery: `0x${string}`; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export const ID_GATEWAY_ADDRESS = "0x00000000Fc25870C6eD6b6c7E41Fb078b7656f69" as const; | ||
|
||
export const ID_GATEWAY_EIP_712_DOMAIN = { | ||
name: "Farcaster IdGateway", | ||
version: "1", | ||
chainId: 10, | ||
verifyingContract: ID_GATEWAY_ADDRESS, | ||
} as const; | ||
|
||
export const ID_GATEWAY_REGISTER_TYPE = [ | ||
{ name: "to", type: "address" }, | ||
{ name: "recovery", type: "address" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const verifyRegister = async ( | ||
message: IdGatewayRegisterMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: ID_GATEWAY_EIP_712_DOMAIN, | ||
types: { Register: ID_GATEWAY_REGISTER_TYPE }, | ||
primaryType: "Register", | ||
message, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { HubAsyncResult, HubError } from "../../errors"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { verifyTypedData, bytesToHex } from "viem"; | ||
|
||
export type IdRegistryTransferMessage = { | ||
fid: bigint; | ||
to: `0x${string}`; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export type IdRegistryTransferAndChangeRecoveryMessage = { | ||
fid: bigint; | ||
to: `0x${string}`; | ||
recovery: `0x${string}`; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export type IdRegistryChangeRecoveryAddressMessage = { | ||
fid: bigint; | ||
from: `0x${string}`; | ||
to: `0x${string}`; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export const ID_REGISTRY_ADDRESS = "0x00000000Fc6c5F01Fc30151999387Bb99A9f489b" as const; | ||
|
||
export const ID_REGISTRY_EIP_712_DOMAIN = { | ||
name: "Farcaster IdRegistry", | ||
version: "1", | ||
chainId: 10, | ||
verifyingContract: ID_REGISTRY_ADDRESS, | ||
} as const; | ||
|
||
export const ID_REGISTRY_TRANSFER_TYPE = [ | ||
{ name: "fid", type: "uint256" }, | ||
{ name: "to", type: "address" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const ID_REGISTRY_TRANSFER_AND_CHANGE_RECOVERY_TYPE = [ | ||
{ name: "fid", type: "uint256" }, | ||
{ name: "to", type: "address" }, | ||
{ name: "recovery", type: "address" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const ID_REGISTRY_CHANGE_RECOVERY_ADDRESS_TYPE = [ | ||
{ name: "fid", type: "uint256" }, | ||
{ name: "from", type: "address" }, | ||
{ name: "to", type: "address" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const verifyTransfer = async ( | ||
message: IdRegistryTransferMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: ID_REGISTRY_EIP_712_DOMAIN, | ||
types: { Transfer: ID_REGISTRY_TRANSFER_TYPE }, | ||
primaryType: "Transfer", | ||
message, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; | ||
|
||
export const verifyTransferAndChangeRecovery = async ( | ||
message: IdRegistryTransferAndChangeRecoveryMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: ID_REGISTRY_EIP_712_DOMAIN, | ||
types: { TransferAndChangeRecovery: ID_REGISTRY_TRANSFER_AND_CHANGE_RECOVERY_TYPE }, | ||
primaryType: "TransferAndChangeRecovery", | ||
message, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; | ||
|
||
export const verifyChangeRecoveryAddress = async ( | ||
message: IdRegistryChangeRecoveryAddressMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: ID_REGISTRY_EIP_712_DOMAIN, | ||
types: { ChangeRecoveryAddress: ID_REGISTRY_CHANGE_RECOVERY_ADDRESS_TYPE }, | ||
primaryType: "ChangeRecoveryAddress", | ||
message, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { HubAsyncResult, HubError } from "../../errors"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { verifyTypedData, bytesToHex } from "viem"; | ||
|
||
export type KeyGatewayAddMessage = { | ||
owner: `0x${string}`; | ||
keyType: number; | ||
key: Uint8Array; | ||
metadataType: number; | ||
metadata: `0x${string}`; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export const KEY_GATEWAY_ADDRESS = "0x00000000fC56947c7E7183f8Ca4B62398CaAdf0B" as const; | ||
|
||
export const KEY_GATEWAY_EIP_712_DOMAIN = { | ||
name: "Farcaster KeyGateway", | ||
version: "1", | ||
chainId: 10, | ||
verifyingContract: KEY_GATEWAY_ADDRESS, | ||
} as const; | ||
|
||
export const KEY_GATEWAY_ADD_TYPE = [ | ||
{ name: "owner", type: "address" }, | ||
{ name: "keyType", type: "uint32" }, | ||
{ name: "key", type: "bytes" }, | ||
{ name: "metadataType", type: "uint8" }, | ||
{ name: "metadata", type: "bytes" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const verifyAdd = async ( | ||
message: KeyGatewayAddMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: KEY_GATEWAY_EIP_712_DOMAIN, | ||
types: { Add: KEY_GATEWAY_ADD_TYPE }, | ||
primaryType: "Add", | ||
message: { ...message, key: bytesToHex(message.key) }, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { HubAsyncResult, HubError } from "../../errors"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { verifyTypedData, bytesToHex } from "viem"; | ||
|
||
export type KeyRegistryRemoveMessage = { | ||
owner: `0x${string}`; | ||
key: Uint8Array; | ||
nonce: bigint; | ||
deadline: bigint; | ||
}; | ||
|
||
export const KEY_REGISTRY_ADDRESS = "0x00000000fc1237824fb747abde0ff18990e59b7e" as const; | ||
|
||
export const KEY_REGISTRY_EIP_712_DOMAIN = { | ||
name: "Farcaster KeyRegistry", | ||
version: "1", | ||
chainId: 10, | ||
verifyingContract: KEY_REGISTRY_ADDRESS, | ||
} as const; | ||
|
||
export const KEY_REGISTRY_REMOVE_TYPE = [ | ||
{ name: "owner", type: "address" }, | ||
{ name: "key", type: "bytes" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const verifyRemove = async ( | ||
message: KeyRegistryRemoveMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: KEY_REGISTRY_EIP_712_DOMAIN, | ||
types: { Remove: KEY_REGISTRY_REMOVE_TYPE }, | ||
primaryType: "Remove", | ||
message: { ...message, key: bytesToHex(message.key) }, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/core/src/eth/contracts/signedKeyRequestValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { HubAsyncResult, HubError } from "../../errors"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { verifyTypedData, bytesToHex } from "viem"; | ||
|
||
export type SignedKeyRequestMessage = { | ||
requestFid: bigint; | ||
key: Uint8Array; | ||
deadline: bigint; | ||
}; | ||
|
||
export const SIGNED_KEY_REQUEST_VALIDATOR_ADDRESS = "0x00000000FC700472606ED4fA22623Acf62c60553" as const; | ||
|
||
export const SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN = { | ||
name: "Farcaster SignedKeyRequestValidator", | ||
version: "1", | ||
chainId: 10, | ||
verifyingContract: SIGNED_KEY_REQUEST_VALIDATOR_ADDRESS, | ||
} as const; | ||
|
||
export const SIGNED_KEY_REQUEST_TYPE = [ | ||
{ name: "requestFid", type: "uint256" }, | ||
{ name: "key", type: "bytes" }, | ||
{ name: "deadline", type: "uint256" }, | ||
] as const; | ||
|
||
export const verifyKeyRequest = async ( | ||
message: SignedKeyRequestMessage, | ||
signature: Uint8Array, | ||
address: Uint8Array, | ||
): HubAsyncResult<boolean> => { | ||
const valid = await ResultAsync.fromPromise( | ||
verifyTypedData({ | ||
address: bytesToHex(address), | ||
domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN, | ||
types: { SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE }, | ||
primaryType: "SignedKeyRequest", | ||
message: { ...message, key: bytesToHex(message.key) }, | ||
signature, | ||
}), | ||
(e) => new HubError("unknown", e as Error), | ||
); | ||
|
||
return valid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters