Skip to content

Commit

Permalink
Issue #47
Browse files Browse the repository at this point in the history
  • Loading branch information
DVitaliy committed Jan 10, 2024
1 parent 05c766f commit 15ae4dd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
19 changes: 9 additions & 10 deletions src/hooks/useSiwe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo } from "react"
import { useMemo } from "react"
import { useThePoolz } from "./useThePoolz"

interface ISiwe {
Expand All @@ -11,16 +11,16 @@ interface ISiwe {
IssuedAt: string
ExpirationAt: string
}
type TProps = () => Partial<ISiwe>

export const useSiwe = (props: TProps) => {
/**
* @deprecated since version 1.12.0
*/
export const useSiwe = ({ Domain, URI, Statement, Version, ChainId, Nonce, IssuedAt, ExpirationAt }: Partial<ISiwe>) => {
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:
const { host: domain, href: uri } = window.location
const eip4361message = `${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"}
Expand All @@ -31,12 +31,11 @@ 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
const signature = (await web3.currentProvider.request({
method: "personal_sign",
Expand All @@ -46,5 +45,5 @@ Expiration Time: ${ExpirationAt ?? new Date(new Date().getTime() + 1000 * 60 * 6
return { eip4361message, signature, formatedMessage: eip4361message.replace(/\n/g, "\\n") }
}
return { signInWithEthereum }
}, [web3, templateEip4361message, account])
}, [web3, eip4361message, account])
}
50 changes: 50 additions & 0 deletions src/hooks/useTheSiwe.ts
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

View check run for this annotation

codefactor.io / CodeFactor

src/hooks/useTheSiwe.ts#L40

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free. (@typescript-eslint/ban-ts-comment)
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])
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import ThePoolzProvider from "./components/Provider"
import { useThePoolz } from "./hooks/useThePoolz"
import { useConnectWallet } from "./hooks/useConnectWallet"
import { useSiwe } from "./hooks/useSiwe"
import { useTheSiwe } from "./hooks/useTheSiwe"
import { useSidNameForAddress } from "./hooks/useSid"

export { ThePoolzProvider, useThePoolz, useConnectWallet, useSiwe, useSidNameForAddress }
export { ThePoolzProvider, useThePoolz, useConnectWallet, useSiwe, useSidNameForAddress, useTheSiwe }
export type { IThePoolzInterface, IERC20Info } from "./types/IThePoolzInterface"
export * from "./utils"
export * from "./constants"
Expand Down

0 comments on commit 15ae4dd

Please sign in to comment.