Miscellaneous
Signature verification
The idea
Example implementation
import { useWeb3React } from '@web3-react/core'
import {
safeAccess,
getRawSignature,
solidityKeccak,
toUnixTimestamp
} from '../utils'
const { library } = useWeb3React()
const BACKEND_ADDRESS = "..."
const getHeaderFields = async (account, library) => {
const currentTimestamp = toUnixTimestamp(new Date())
const msg = solidityKeccak( ['address', 'uint256'], [account, currentTimestamp] )
const signature = await getRawSignature(msg, library, account)
return { address, signature, timestamp }
}
export async function getProtectedEndpoint(account, data) {
const headers = await getHeaderFields(account, library)
return axios.post(`${BACKEND_ADDRESS}/protected/`, data, {
headers: {
'Content-Type': 'application/json',
'x-address': headers.address,
'x-timestamp': headers.timestamp!.toString(),
'x-signature': headers.signature
}
}).then((r) => {
if (r.status === 200) {
return r.data
}
return undefined
}).catch((e: Error) => {
console.log(e)
return undefined
})
}Getting a smart contract instance with ethers
Last updated