Skip to content

Instantly share code, notes, and snippets.

@abdhilabs
Created February 12, 2024 07:09
Show Gist options
  • Save abdhilabs/90b9f8f03e234fa79862a23642487ef2 to your computer and use it in GitHub Desktop.
Save abdhilabs/90b9f8f03e234fa79862a23642487ef2 to your computer and use it in GitHub Desktop.
Generating A Signature For Farcaster Signer in Swift
import web3 //https://github.com/argentlabs/web3.swift
private func generateSignature() {
let privateKey = "0xd5071223dcbf1cb824090bd98e0ddc807be00f1874fdd74bbd9225773a824397" // PrivateKey of your wallet must be matched with FID. Note: Store it in a safe place
let requestFid: Double = 232312
let deadline = Double(Date().timeIntervalSince1970) + 86400
let publicKey = "0x85d2c4199a7fae484739ec9d280b28e5214a343298049745c108942690574a8e"
// Start | Domain SignedKeyRequest | Don't change this value
let name = "Farcaster SignedKeyRequestValidator"
let version = "1"
let chainId: Double = 10
let verifyingContract = "0x00000000fc700472606ed4fa22623acf62c60553"
// End
let keyStorage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(
replacing: keyStorage,
privateKey: privateKey,
keystorePassword: "MY_PASSWORD")
let typedData = TypedData(
types: [
"EIP712Domain": [
.init(name: "name", type: "string"),
.init(name: "version", type: "string"),
.init(name: "chainId", type: "uint256"),
.init(name: "verifyingContract", type: "address")
],
"SignedKeyRequest": [
.init(name: "requestFid", type: "uint256"),
.init(name: "key", type: "bytes"),
.init(name: "deadline", type: "uint256")
]
],
primaryType: "SignedKeyRequest",
domain: .object([
"name" : .string(name),
"version" : .string(version),
"chainId" : .number(chainId),
"verifyingContract" : .string(verifyingContract),
]),
message: .object([
"requestFid": .number(requestFid),
"key": .string(publicKey),
"deadline": .number(deadline)
]))
let signedKey = try! account.signMessage(message: typedData)
print("|DEBUG| Signer: \(signedKey)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment