Skip to content

Commit af2dc61

Browse files
Merge pull request #998 from dev-protocol/fix/signin
Fix: Frequent signin required to prove admin rights
2 parents ffe33b2 + e7d2d1b commit af2dc61

File tree

3 files changed

+76
-26
lines changed

3 files changed

+76
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devprotocol/clubs-plugin-posts",
3-
"version": "0.20.11",
3+
"version": "0.20.12",
44
"type": "module",
55
"description": "Template repository for using TypeScript",
66
"main": "dist/index.js",

src/components/Page/Index.vue

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import {
2525
hasWritePermission,
2626
} from '../../fixtures/memberships'
2727
import { JsonRpcProvider } from 'ethers'
28-
import { getSignature, getMessage, consoleWarn } from '../../fixtures/session'
28+
import {
29+
getSignature,
30+
getMessage,
31+
checkSession,
32+
consoleWarn,
33+
} from '../../fixtures/session'
2934
import { Strings } from '../../i18n'
3035
3136
type Props = {
@@ -108,25 +113,30 @@ const testPermission = async (
108113
}
109114
110115
const handleConnection = async (signer: UndefinedOr<Signer>) => {
116+
let newSigner: Signer = signer as Signer
117+
console.log({ signer })
111118
if (!signer) {
112-
return
119+
const { connection: conct } = await import(
120+
'@devprotocol/clubs-core/connection'
121+
)
122+
connection.value = conct
123+
newSigner = conct().signer.value
124+
console.log({ newSigner })
113125
}
114-
126+
console.log('inside Handle Connection')
115127
// get wallet address
116-
const connectedAddress = await signer.getAddress()
117-
// const connectedAddress = '0x57E21bd98612DE0Bd1723F4bf81A944eF7BfF526'
118-
walletAddress.value = connectedAddress
119-
120-
const hash = getMessage(connectedAddress)
128+
const connectedAddress = signer
129+
? await signer.getAddress()
130+
: await newSigner.getAddress()
121131
122-
let sig = await getSignature(connectedAddress, signer)
123-
124-
fetchPosts({ hash, sig })
132+
walletAddress.value = connectedAddress
125133
126-
hasEditableRole.value = await testPermission(
127-
walletAddress.value,
128-
new JsonRpcProvider(props.rpcUrl),
129-
)
134+
if (isVerified.value) {
135+
hasEditableRole.value = await testPermission(
136+
walletAddress.value,
137+
new JsonRpcProvider(props.rpcUrl),
138+
)
139+
}
130140
}
131141
132142
const fetchPosts = async ({ hash, sig }: { hash?: string; sig?: string }) => {
@@ -172,27 +182,32 @@ onMounted(async () => {
172182
'@devprotocol/clubs-core/connection'
173183
)
174184
connection.value = conct
175-
conct().signer.subscribe((signer: UndefinedOr<Signer>) => {
176-
walletSigner = signer
185+
conct().signer.subscribe(async (signer: UndefinedOr<Signer>) => {
186+
if (signer) {
187+
const connectedAddress = await signer.getAddress()
188+
walletAddress.value = connectedAddress
189+
isVerified.value = await checkSession(connectedAddress)
190+
console.log('isverfied', isVerified.value)
191+
walletSigner = signer
192+
handleConnection(signer)
193+
}
177194
})
178-
const signer = conct().signer.value
179-
if (signer) {
180-
const connectedAddress = await signer.getAddress()
181-
walletAddress.value = connectedAddress
182-
isVerified.value = true
183-
}
184-
185195
i18n.value = i18nBase(navigator.languages)
186196
})
187197
188198
const isVerified = ref(false)
189199
190200
const handleVerify = async () => {
191-
handleConnection(walletSigner)
201+
const walletAddres = (await walletSigner?.getAddress()) as string
202+
const hash = getMessage(walletAddres)
203+
let sig = await getSignature(walletAddres, walletSigner as Signer)
204+
fetchPosts({ hash, sig })
192205
//
193206
if (connection.value?.().signer.value) {
194207
isVerified.value = true
195208
}
209+
210+
handleConnection(walletSigner)
196211
}
197212
198213
const handlePostSuccess = (post: Posts) => {

src/fixtures/session.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { type UndefinedOr } from '@devprotocol/util-ts'
33
import { type Signer } from 'ethers'
44
import { encode, decode } from '@devprotocol/clubs-core'
5+
import { verifyMessage } from 'ethers'
6+
import type { Address } from 'cluster'
57

68
const maxValidity = 1 * 60 * 60 * 1000 // 1 hour
79

@@ -38,6 +40,39 @@ export const getSignature = async (
3840
return decode(sig) as string
3941
}
4042

43+
export const getSessionAddress = async (hash: string, sig: string) => {
44+
console.log('inside getSessionAddress')
45+
const address = verifyMessage(hash, sig)
46+
return address
47+
}
48+
49+
export const checkSession = async (address: string) => {
50+
console.log('inside checkSession')
51+
const hash = sessionStorage.getItem(`hash-of-${address}`)
52+
const sigItem = sessionStorage.getItem(`sig-of-${address}`)
53+
const sig = sigItem ? (decode(sigItem) as string) : undefined
54+
console.log({ hash, sig })
55+
56+
if (!hash || !sig) {
57+
return false
58+
}
59+
60+
try {
61+
const SessoionAddress = await getSessionAddress(hash, sig)
62+
console.log({ SessoionAddress, address })
63+
64+
if (SessoionAddress !== address) {
65+
console.log('inside here')
66+
return false
67+
}
68+
return true
69+
} catch (error) {
70+
console.log('inside catch')
71+
console.error('Error checking session:', error)
72+
return false
73+
}
74+
}
75+
4176
export const consoleWarn = () => {
4277
console.log('%cWarning!!', 'color: red; font-size: 20px; background: yellow;')
4378
console.log(

0 commit comments

Comments
 (0)