forked from WalletConnect/web3inbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiwe.ts
141 lines (111 loc) · 3.55 KB
/
siwe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { getAccount } from '@wagmi/core'
import { composeDidPkh } from '@walletconnect/did-jwt'
import { Web3InboxClient } from '@web3inbox/core'
import {
SIWECreateMessageArgs,
SIWEVerifyMessageArgs,
formatMessage,
getAddressFromMessage,
getChainIdFromMessage
} from '@web3modal/siwe'
import { waitFor } from '@/utils/general'
import { wagmiConfig } from '@/utils/wagmiConfig'
let registerParams: Awaited<ReturnType<Web3InboxClient['prepareRegistrationViaRecaps']>> | null =
null
export const getMessageParams = async (client: Web3InboxClient | null) => {
if (!client) {
throw new Error('Client not ready yet')
}
registerParams = await client.prepareRegistrationViaRecaps({
domain: window.location.hostname,
allApps: true
})
const { cacaoPayload } = registerParams
return {
chains: wagmiConfig.chains.map(chain => chain.id),
domain: cacaoPayload.domain,
statement: cacaoPayload.statement ?? undefined,
uri: cacaoPayload.uri,
resources: cacaoPayload.resources
}
}
export const createMessage = ({ address, ...args }: SIWECreateMessageArgs) => {
if (!registerParams) {
throw new Error("Can't create message if registerParams is undefined")
}
registerParams = {
...registerParams,
cacaoPayload: {
...registerParams?.cacaoPayload,
...args
}
}
const message = formatMessage(registerParams.cacaoPayload, address)
// statement is generated in format message and not part of original payload.
const statement = message.split('\n')[3]
registerParams.cacaoPayload.statement = statement
return message
}
export const getNonce = async () => {
return registerParams?.cacaoPayload.nonce ?? 'FAILED_NONCE'
}
export const getSession = async (client: Web3InboxClient | null) => {
const { address, chainId } = getAccount({ ...wagmiConfig })
await waitFor(async () => !!client)
const account = `eip155:${chainId}:${address}`
const identityKey = await client?.getAccountIsRegistered(account)
const invalidSession = !(address && chainId && identityKey)
if (invalidSession) throw new Error('Failed to get account')
return {
address,
chainId
}
}
const verifySiweMessage = async (params: SIWEVerifyMessageArgs, client: Web3InboxClient | null) => {
if (!client) {
throw new Error('Failed to verify message - no client')
}
if (!registerParams) {
throw new Error('Failed to verify message - no registerParams saved')
}
// Start signing the signature modal so it does not show up
// in sign 2.5
const account = composeDidPkh(
`${getChainIdFromMessage(params.message)}:${getAddressFromMessage(params.message)}`
)
if (await client.getAccountIsRegistered(account)) {
return true
}
// Unregister account if registered with a faulty registration.
try {
await client.unregister({ account })
} catch (e) {}
await client.register({
registerParams: {
allApps: true,
cacaoPayload: {
...registerParams.cacaoPayload,
...params.cacao?.p,
iss: account
},
privateIdentityKey: registerParams.privateIdentityKey
},
signature: params.signature
})
return true
}
export const verifyMessage = async (
params: SIWEVerifyMessageArgs,
client: Web3InboxClient | null
) => {
try {
const messageIsValid = await verifySiweMessage(params, client)
const account = `${getChainIdFromMessage(params.message)}:${getAddressFromMessage(params.message)}`
if (messageIsValid) {
await waitFor(() => client!.getAccountIsRegistered(account))
}
return messageIsValid
} catch (e) {
return false
}
}