-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
11 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
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,50 @@ | ||
import { useCallback, useMemo } from "react" | ||
import { useThePoolz } from "./useThePoolz" | ||
|
||
interface ISiwe { | ||
Domain: string | ||
URI: string | ||
Statement: string | ||
Version: number | ||
ChainId: number | ||
Nonce: number | ||
IssuedAt: string | ||
ExpirationAt: string | ||
} | ||
type TProps = () => Partial<ISiwe> | ||
|
||
export const useTheSiwe = (props: TProps) => { | ||
const thePoolz = useThePoolz() | ||
const { web3, account } = thePoolz | ||
|
||
const templateEip4361message = useCallback(() => { | ||
const { Domain, URI, Statement, Version, ChainId, Nonce, IssuedAt, ExpirationAt } = props() | ||
const { host: domain, href: uri } = window.location | ||
return `${Domain ?? domain} wants you to sign in with your Ethereum account: | ||
${account} | ||
${Statement ?? "I accept the Poolz Terms & Conditions: https://www.poolz.finance/terms-conditions"} | ||
URI: ${URI ?? uri} | ||
Version: ${Version ?? 1} | ||
Chain ID: ${ChainId ?? 56} | ||
Nonce: ${Nonce ?? new Date().getTime()} | ||
Issued At: ${IssuedAt ?? new Date().toISOString()} | ||
Expiration Time: ${ExpirationAt ?? new Date(new Date().getTime() + 1000 * 60 * 60 * 24).toISOString()}` | ||
}, [props]) | ||
|
||
return useMemo(() => { | ||
const signInWithEthereum = async () => { | ||
if (!web3.currentProvider || !account) throw new Error("No web3 provider or account") | ||
const eip4361message = templateEip4361message() | ||
// @ts-ignore | ||
Check warning on line 40 in src/hooks/useTheSiwe.ts codefactor.io / CodeFactorsrc/hooks/useTheSiwe.ts#L40
|
||
const signature = (await web3.currentProvider.request({ | ||
method: "personal_sign", | ||
params: [eip4361message.replace(/[\r\n]/g, "\n"), account] | ||
})) as string | ||
|
||
return { eip4361message, signature, formatedMessage: eip4361message.replace(/\n/g, "\\n") } | ||
} | ||
return { signInWithEthereum } | ||
}, [web3, templateEip4361message, account]) | ||
} |
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