@redzone/taunt-logins 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/magic.ts +14 -4
- package/src/metamask.ts +3 -3
- package/.history/package_20251002102021.json +0 -24
- package/.history/package_20251002102142.json +0 -24
- package/.history/package_20251002102150.json +0 -24
- package/.history/package_20251002102201.json +0 -24
- package/.history/src/metamask_20251002095345.ts +0 -136
- package/.history/src/metamask_20251002101110.ts +0 -90
- package/.history/src/metamask_20251002101707.ts +0 -59
- package/.history/src/metamask_20251002101728.ts +0 -61
- package/.history/src/metamask_20251002101947.ts +0 -66
- package/tsup.config.ts +0 -10
package/package.json
CHANGED
package/src/magic.ts
CHANGED
|
@@ -7,24 +7,34 @@ const customNodeOptions = {
|
|
|
7
7
|
chainId: 137
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export function
|
|
10
|
+
export function emailOTPWithMagic(magicKey: string, email: string) {
|
|
11
11
|
const magic = new Magic(magicKey, {
|
|
12
12
|
network: customNodeOptions
|
|
13
13
|
})
|
|
14
14
|
return magic.auth.loginWithEmailOTP({ email })
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export async function
|
|
17
|
+
export async function tauntMagicEmailOTPLogin(
|
|
18
18
|
tauntServiceEndpoint: string,
|
|
19
19
|
magicKey: string,
|
|
20
20
|
email: string
|
|
21
21
|
) {
|
|
22
22
|
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
23
23
|
try {
|
|
24
|
-
const did = await
|
|
25
|
-
|
|
24
|
+
const did = await emailOTPWithMagic(magicKey, email)
|
|
25
|
+
if (!did) throw new Error("No DID returned")
|
|
26
|
+
console.log("Magic email OTP login", { did })
|
|
27
|
+
return taunt.loginWithMagicDid(did)
|
|
26
28
|
} catch (err) {
|
|
27
29
|
console.error("Magic login error:", err)
|
|
28
30
|
throw err
|
|
29
31
|
}
|
|
30
32
|
}
|
|
33
|
+
|
|
34
|
+
export async function tauntMagicDidLogin(
|
|
35
|
+
tauntServiceEndpoint: string,
|
|
36
|
+
did: string
|
|
37
|
+
) {
|
|
38
|
+
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
39
|
+
return taunt.loginWithMagicDid(did)
|
|
40
|
+
}
|
package/src/metamask.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare global {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export async function
|
|
11
|
+
export async function tauntSignWithMetamask(
|
|
12
12
|
tauntServiceEndpoint: string,
|
|
13
13
|
provider?: BaseProvider
|
|
14
14
|
) {
|
|
@@ -49,12 +49,12 @@ export async function withMetamask(
|
|
|
49
49
|
return { walletAddress, message, signature }
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export async function
|
|
52
|
+
export async function tauntMetamaskLogin(
|
|
53
53
|
tauntServiceEndpoint: string,
|
|
54
54
|
providerParam?: BaseProvider
|
|
55
55
|
) {
|
|
56
56
|
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
57
|
-
const { walletAddress, message, signature } = await
|
|
57
|
+
const { walletAddress, message, signature } = await tauntSignWithMetamask(
|
|
58
58
|
tauntServiceEndpoint,
|
|
59
59
|
providerParam
|
|
60
60
|
)
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@seaders/redzone-taunt-logins",
|
|
3
|
-
"version": "1.0.4",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.cjs",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rm -rf dist",
|
|
9
|
-
"prebuild": "npm run clean",
|
|
10
|
-
"build": "tsc"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@metamask/providers": "^22.1.1",
|
|
14
|
-
"magic-sdk": "^30.0.0"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
18
|
-
"@types/chrome": "^0.1.16",
|
|
19
|
-
"@types/node": "^24.6.1",
|
|
20
|
-
"prettier": "^3.6.2",
|
|
21
|
-
"tsup": "^8.5.0",
|
|
22
|
-
"typescript": "^5.9.3"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@redzone/taunt-logins",
|
|
3
|
-
"version": "1.0.4",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.cjs",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rm -rf dist",
|
|
9
|
-
"prebuild": "npm run clean",
|
|
10
|
-
"build": "tsc"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@metamask/providers": "^22.1.1",
|
|
14
|
-
"magic-sdk": "^30.0.0"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
18
|
-
"@types/chrome": "^0.1.16",
|
|
19
|
-
"@types/node": "^24.6.1",
|
|
20
|
-
"prettier": "^3.6.2",
|
|
21
|
-
"tsup": "^8.5.0",
|
|
22
|
-
"typescript": "^5.9.3"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@redzone/taunt-logins",
|
|
3
|
-
"version": "0.0.1",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.cjs",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rm -rf dist",
|
|
9
|
-
"prebuild": "npm run clean",
|
|
10
|
-
"build": "tsc"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@metamask/providers": "^22.1.1",
|
|
14
|
-
"magic-sdk": "^30.0.0"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
18
|
-
"@types/chrome": "^0.1.16",
|
|
19
|
-
"@types/node": "^24.6.1",
|
|
20
|
-
"prettier": "^3.6.2",
|
|
21
|
-
"tsup": "^8.5.0",
|
|
22
|
-
"typescript": "^5.9.3"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@redzone/taunt-logins",
|
|
3
|
-
"version": "0.0.1",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "index.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rm -rf dist",
|
|
9
|
-
"prebuild": "npm run clean",
|
|
10
|
-
"build": "tsc"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@metamask/providers": "^22.1.1",
|
|
14
|
-
"magic-sdk": "^30.0.0"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
18
|
-
"@types/chrome": "^0.1.16",
|
|
19
|
-
"@types/node": "^24.6.1",
|
|
20
|
-
"prettier": "^3.6.2",
|
|
21
|
-
"tsup": "^8.5.0",
|
|
22
|
-
"typescript": "^5.9.3"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { MetaMaskInpageProvider } from "@metamask/providers"
|
|
2
|
-
|
|
3
|
-
import { TauntApi } from "./taunt"
|
|
4
|
-
|
|
5
|
-
const CHROME_METAMASK_ID = "nkbihfbeogaeaoehlefnkodbefgpgknn"
|
|
6
|
-
|
|
7
|
-
export const createMetaMaskProvider = () =>
|
|
8
|
-
createWalletProvider(CHROME_METAMASK_ID)
|
|
9
|
-
|
|
10
|
-
declare global {
|
|
11
|
-
interface Window {
|
|
12
|
-
ethereum?: MetaMaskInpageProvider
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async function createWalletProvider(ident: string) {
|
|
17
|
-
let provider
|
|
18
|
-
try {
|
|
19
|
-
let metamaskPort
|
|
20
|
-
try {
|
|
21
|
-
metamaskPort = chrome.runtime.connect(ident)
|
|
22
|
-
} catch (error) {
|
|
23
|
-
console.log("Failed to connect to MetaMask", error)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (metamaskPort) {
|
|
27
|
-
const PortStream = await import("extension-port-stream").then(
|
|
28
|
-
(m) => m.default
|
|
29
|
-
)
|
|
30
|
-
const pluginStream = new PortStream(metamaskPort)
|
|
31
|
-
provider = new MetaMaskInpageProvider(pluginStream)
|
|
32
|
-
} else {
|
|
33
|
-
let eth
|
|
34
|
-
try {
|
|
35
|
-
eth = window?.ethereum
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.error("Failed to access window.ethereum", error)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (eth) {
|
|
41
|
-
provider = eth
|
|
42
|
-
if (!provider.isMetaMask) {
|
|
43
|
-
console.warn(
|
|
44
|
-
"Injected ethereum provider is not MetaMask! You may experience issues."
|
|
45
|
-
)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
} catch (e) {
|
|
50
|
-
console.dir(`Metamask connect error `, e)
|
|
51
|
-
throw e
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return provider
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export async function withMetamask(
|
|
58
|
-
tauntServiceEndpoint: string,
|
|
59
|
-
providerParam?: MetaMaskInpageProvider
|
|
60
|
-
) {
|
|
61
|
-
const provider = providerParam || (await createMetaMaskProvider())
|
|
62
|
-
if (!provider) throw new Error("MetaMask provider not found")
|
|
63
|
-
|
|
64
|
-
const accounts = (await provider.request({
|
|
65
|
-
method: "eth_requestAccounts"
|
|
66
|
-
})) as string[]
|
|
67
|
-
if (!accounts.length) throw new Error("No accounts returned")
|
|
68
|
-
|
|
69
|
-
console.log("MetaMask", { accounts })
|
|
70
|
-
|
|
71
|
-
const walletAddress = accounts[0]
|
|
72
|
-
const baseUrl = `${tauntServiceEndpoint}/v1/auth`
|
|
73
|
-
const cryptoValuesArray = new Uint32Array(3)
|
|
74
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()
|
|
75
|
-
|
|
76
|
-
const serverNonce = await fetch(`${baseUrl}/nonce/login`, {
|
|
77
|
-
method: "POST",
|
|
78
|
-
body: JSON.stringify({ walletAddress, clientNonce }),
|
|
79
|
-
headers: { "Content-Type": "application/json" }
|
|
80
|
-
}).then((res) => res.text())
|
|
81
|
-
|
|
82
|
-
const payload = JSON.stringify({ clientNonce, serverNonce })
|
|
83
|
-
const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`
|
|
84
|
-
const params = [message, walletAddress]
|
|
85
|
-
const method = "personal_sign"
|
|
86
|
-
|
|
87
|
-
const signature = await new Promise<string>((resolve, reject) => {
|
|
88
|
-
provider.sendAsync(
|
|
89
|
-
{
|
|
90
|
-
id: 1,
|
|
91
|
-
method,
|
|
92
|
-
params,
|
|
93
|
-
jsonrpc: "2.0"
|
|
94
|
-
},
|
|
95
|
-
(err, response) => {
|
|
96
|
-
if (err) {
|
|
97
|
-
console.error(err)
|
|
98
|
-
reject(err)
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const { result, error } = response as {
|
|
103
|
-
id: number
|
|
104
|
-
jsonrpc: string
|
|
105
|
-
result: string
|
|
106
|
-
error?: unknown
|
|
107
|
-
}
|
|
108
|
-
if (error) {
|
|
109
|
-
console.error(error)
|
|
110
|
-
reject(error)
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
resolve(result)
|
|
115
|
-
}
|
|
116
|
-
)
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
return { walletAddress, message, signature }
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export async function loginWithMetamask(
|
|
123
|
-
tauntServiceEndpoint: string,
|
|
124
|
-
providerParam?: MetaMaskInpageProvider
|
|
125
|
-
) {
|
|
126
|
-
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
127
|
-
const { walletAddress, message, signature } = await withMetamask(
|
|
128
|
-
tauntServiceEndpoint,
|
|
129
|
-
providerParam
|
|
130
|
-
)
|
|
131
|
-
return taunt.loginWithWeb3WalletSignature({
|
|
132
|
-
walletAddress,
|
|
133
|
-
message,
|
|
134
|
-
signature
|
|
135
|
-
})
|
|
136
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "@metamask/providers"
|
|
2
|
-
|
|
3
|
-
import { TauntApi } from "./taunt"
|
|
4
|
-
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
ethereum?: BaseProvider
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function withMetamask(
|
|
12
|
-
tauntServiceEndpoint: string,
|
|
13
|
-
provider?: BaseProvider
|
|
14
|
-
) {
|
|
15
|
-
provider = provider || window.ethereum
|
|
16
|
-
if (!provider) throw new Error("MetaMask provider not found")
|
|
17
|
-
|
|
18
|
-
const accounts = (await provider.request({
|
|
19
|
-
method: "eth_requestAccounts"
|
|
20
|
-
})) as string[]
|
|
21
|
-
if (!accounts.length) throw new Error("No accounts returned")
|
|
22
|
-
|
|
23
|
-
console.log("MetaMask", { accounts })
|
|
24
|
-
|
|
25
|
-
const walletAddress = accounts[0]
|
|
26
|
-
const baseUrl = `${tauntServiceEndpoint}/v1/auth`
|
|
27
|
-
const cryptoValuesArray = new Uint32Array(3)
|
|
28
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()
|
|
29
|
-
|
|
30
|
-
const serverNonce = await fetch(`${baseUrl}/nonce/login`, {
|
|
31
|
-
method: "POST",
|
|
32
|
-
body: JSON.stringify({ walletAddress, clientNonce }),
|
|
33
|
-
headers: { "Content-Type": "application/json" }
|
|
34
|
-
}).then((res) => res.text())
|
|
35
|
-
|
|
36
|
-
const payload = JSON.stringify({ clientNonce, serverNonce })
|
|
37
|
-
const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`
|
|
38
|
-
const params = [message, walletAddress]
|
|
39
|
-
const method = "personal_sign"
|
|
40
|
-
|
|
41
|
-
const signature = await new Promise<string>((resolve, reject) => {
|
|
42
|
-
provider.sendAsync(
|
|
43
|
-
{
|
|
44
|
-
id: 1,
|
|
45
|
-
method,
|
|
46
|
-
params,
|
|
47
|
-
jsonrpc: "2.0"
|
|
48
|
-
},
|
|
49
|
-
(err, response) => {
|
|
50
|
-
if (err) {
|
|
51
|
-
console.error(err)
|
|
52
|
-
reject(err)
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const { result, error } = response as {
|
|
57
|
-
id: number
|
|
58
|
-
jsonrpc: string
|
|
59
|
-
result: string
|
|
60
|
-
error?: unknown
|
|
61
|
-
}
|
|
62
|
-
if (error) {
|
|
63
|
-
console.error(error)
|
|
64
|
-
reject(error)
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
resolve(result)
|
|
69
|
-
}
|
|
70
|
-
)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
return { walletAddress, message, signature }
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export async function loginWithMetamask(
|
|
77
|
-
tauntServiceEndpoint: string,
|
|
78
|
-
providerParam?: MetaMaskInpageProvider
|
|
79
|
-
) {
|
|
80
|
-
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
81
|
-
const { walletAddress, message, signature } = await withMetamask(
|
|
82
|
-
tauntServiceEndpoint,
|
|
83
|
-
providerParam
|
|
84
|
-
)
|
|
85
|
-
return taunt.loginWithWeb3WalletSignature({
|
|
86
|
-
walletAddress,
|
|
87
|
-
message,
|
|
88
|
-
signature
|
|
89
|
-
})
|
|
90
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "@metamask/providers"
|
|
2
|
-
|
|
3
|
-
import { TauntApi } from "./taunt"
|
|
4
|
-
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
ethereum?: BaseProvider
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function withMetamask(
|
|
12
|
-
tauntServiceEndpoint: string,
|
|
13
|
-
provider?: BaseProvider
|
|
14
|
-
) {
|
|
15
|
-
provider = provider || window.ethereum
|
|
16
|
-
if (!provider) throw new Error("MetaMask provider not found")
|
|
17
|
-
|
|
18
|
-
const accounts = (await provider.request({
|
|
19
|
-
method: "eth_requestAccounts"
|
|
20
|
-
})) as string[]
|
|
21
|
-
if (!accounts.length) throw new Error("No accounts returned")
|
|
22
|
-
|
|
23
|
-
console.log("MetaMask", { accounts })
|
|
24
|
-
|
|
25
|
-
const walletAddress = accounts[0]
|
|
26
|
-
const baseUrl = `${tauntServiceEndpoint}/v1/auth`
|
|
27
|
-
const cryptoValuesArray = new Uint32Array(3)
|
|
28
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()
|
|
29
|
-
|
|
30
|
-
const serverNonce = await fetch(`${baseUrl}/nonce/login`, {
|
|
31
|
-
method: "POST",
|
|
32
|
-
body: JSON.stringify({ walletAddress, clientNonce }),
|
|
33
|
-
headers: { "Content-Type": "application/json" }
|
|
34
|
-
}).then((res) => res.text())
|
|
35
|
-
|
|
36
|
-
const payload = JSON.stringify({ clientNonce, serverNonce })
|
|
37
|
-
const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`
|
|
38
|
-
const params = [message, walletAddress]
|
|
39
|
-
|
|
40
|
-
const signature = await provider.request({ method: "personal_sign", params })
|
|
41
|
-
|
|
42
|
-
return { walletAddress, message, signature }
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function loginWithMetamask(
|
|
46
|
-
tauntServiceEndpoint: string,
|
|
47
|
-
providerParam?: MetaMaskInpageProvider
|
|
48
|
-
) {
|
|
49
|
-
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
50
|
-
const { walletAddress, message, signature } = await withMetamask(
|
|
51
|
-
tauntServiceEndpoint,
|
|
52
|
-
providerParam
|
|
53
|
-
)
|
|
54
|
-
return taunt.loginWithWeb3WalletSignature({
|
|
55
|
-
walletAddress,
|
|
56
|
-
message,
|
|
57
|
-
signature
|
|
58
|
-
})
|
|
59
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "@metamask/providers"
|
|
2
|
-
|
|
3
|
-
import { TauntApi } from "./taunt"
|
|
4
|
-
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
ethereum?: BaseProvider
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function withMetamask(
|
|
12
|
-
tauntServiceEndpoint: string,
|
|
13
|
-
provider?: BaseProvider
|
|
14
|
-
) {
|
|
15
|
-
provider = provider || window.ethereum
|
|
16
|
-
if (!provider) throw new Error("MetaMask provider not found")
|
|
17
|
-
|
|
18
|
-
const accounts = (await provider.request({
|
|
19
|
-
method: "eth_requestAccounts"
|
|
20
|
-
})) as string[]
|
|
21
|
-
if (!accounts.length) throw new Error("No accounts returned")
|
|
22
|
-
|
|
23
|
-
console.log("MetaMask", { accounts })
|
|
24
|
-
|
|
25
|
-
const walletAddress = accounts[0]
|
|
26
|
-
const baseUrl = `${tauntServiceEndpoint}/v1/auth`
|
|
27
|
-
const cryptoValuesArray = new Uint32Array(3)
|
|
28
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()
|
|
29
|
-
|
|
30
|
-
const serverNonce = await fetch(`${baseUrl}/nonce/login`, {
|
|
31
|
-
method: "POST",
|
|
32
|
-
body: JSON.stringify({ walletAddress, clientNonce }),
|
|
33
|
-
headers: { "Content-Type": "application/json" }
|
|
34
|
-
}).then((res) => res.text())
|
|
35
|
-
|
|
36
|
-
const payload = JSON.stringify({ clientNonce, serverNonce })
|
|
37
|
-
const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`
|
|
38
|
-
|
|
39
|
-
const signature = await provider.request({
|
|
40
|
-
method: "personal_sign",
|
|
41
|
-
params: [message, walletAddress]
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
return { walletAddress, message, signature }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export async function loginWithMetamask(
|
|
48
|
-
tauntServiceEndpoint: string,
|
|
49
|
-
providerParam?: MetaMaskInpageProvider
|
|
50
|
-
) {
|
|
51
|
-
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
52
|
-
const { walletAddress, message, signature } = await withMetamask(
|
|
53
|
-
tauntServiceEndpoint,
|
|
54
|
-
providerParam
|
|
55
|
-
)
|
|
56
|
-
return taunt.loginWithWeb3WalletSignature({
|
|
57
|
-
walletAddress,
|
|
58
|
-
message,
|
|
59
|
-
signature
|
|
60
|
-
})
|
|
61
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "@metamask/providers"
|
|
2
|
-
|
|
3
|
-
import { TauntApi } from "./taunt"
|
|
4
|
-
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
ethereum?: BaseProvider
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function withMetamask(
|
|
12
|
-
tauntServiceEndpoint: string,
|
|
13
|
-
provider?: BaseProvider
|
|
14
|
-
) {
|
|
15
|
-
provider = provider || window.ethereum
|
|
16
|
-
if (!provider) throw new Error("MetaMask provider not found")
|
|
17
|
-
|
|
18
|
-
const accounts = (await provider.request({
|
|
19
|
-
method: "eth_requestAccounts"
|
|
20
|
-
})) as string[]
|
|
21
|
-
if (!accounts.length) throw new Error("No accounts returned")
|
|
22
|
-
|
|
23
|
-
console.log("MetaMask", { accounts })
|
|
24
|
-
|
|
25
|
-
const walletAddress = accounts[0]
|
|
26
|
-
const baseUrl = `${tauntServiceEndpoint}/v1/auth`
|
|
27
|
-
const cryptoValuesArray = new Uint32Array(3)
|
|
28
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()
|
|
29
|
-
|
|
30
|
-
const serverNonce = await fetch(`${baseUrl}/nonce/login`, {
|
|
31
|
-
method: "POST",
|
|
32
|
-
body: JSON.stringify({ walletAddress, clientNonce }),
|
|
33
|
-
headers: { "Content-Type": "application/json" }
|
|
34
|
-
}).then((res) => res.text())
|
|
35
|
-
|
|
36
|
-
const payload = JSON.stringify({ clientNonce, serverNonce })
|
|
37
|
-
const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`
|
|
38
|
-
let signature
|
|
39
|
-
try {
|
|
40
|
-
signature = await provider.request<string>({
|
|
41
|
-
method: "personal_sign",
|
|
42
|
-
params: [message, walletAddress]
|
|
43
|
-
})
|
|
44
|
-
} catch (err) {
|
|
45
|
-
throw new Error("User denied message signature")
|
|
46
|
-
}
|
|
47
|
-
if (!signature) throw new Error("No signature returned")
|
|
48
|
-
|
|
49
|
-
return { walletAddress, message, signature }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function loginWithMetamask(
|
|
53
|
-
tauntServiceEndpoint: string,
|
|
54
|
-
providerParam?: BaseProvider
|
|
55
|
-
) {
|
|
56
|
-
const taunt = new TauntApi(tauntServiceEndpoint)
|
|
57
|
-
const { walletAddress, message, signature } = await withMetamask(
|
|
58
|
-
tauntServiceEndpoint,
|
|
59
|
-
providerParam
|
|
60
|
-
)
|
|
61
|
-
return taunt.loginWithWeb3WalletSignature({
|
|
62
|
-
walletAddress,
|
|
63
|
-
message,
|
|
64
|
-
signature
|
|
65
|
-
})
|
|
66
|
-
}
|