-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
accecd4
commit 9ee416f
Showing
20 changed files
with
1,131 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,6 @@ yarn-error.log* | |
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
# msw | ||
public/mockServiceWorker.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
Account, | ||
ConnectAdditionalRequest, | ||
TonProofItemReplySuccess, | ||
} from '@tonconnect/ui-react'; | ||
import '../../../lib/localStorageForGithubPages'; | ||
|
||
class TonProofDemoApiService { | ||
private localStorageKey = 'demo-api-access-token'; | ||
|
||
private host = window.location.origin; | ||
|
||
public accessToken: string | null = null; | ||
|
||
public readonly refreshIntervalMs = 9 * 60 * 1000; | ||
|
||
constructor() { | ||
this.accessToken = localStorage.getItem(this.localStorageKey); | ||
|
||
if (!this.accessToken) { | ||
this.generatePayload(); | ||
} | ||
} | ||
|
||
async generatePayload(): Promise<ConnectAdditionalRequest | null> { | ||
try { | ||
const response = await ( | ||
await fetch(`${this.host}/api/generate_payload`, { | ||
method: 'POST', | ||
}) | ||
).json(); | ||
return { tonProof: response.payload as string }; | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
async checkProof(proof: TonProofItemReplySuccess['proof'], account: Account): Promise<boolean> { | ||
try { | ||
const reqBody = { | ||
address: account.address, | ||
network: account.chain, | ||
public_key: account.publicKey, | ||
proof: { | ||
...proof, | ||
state_init: account.walletStateInit, | ||
}, | ||
}; | ||
|
||
const response = await ( | ||
await fetch(`${this.host}/api/check_proof`, { | ||
method: 'POST', | ||
body: JSON.stringify(reqBody), | ||
}) | ||
).json(); | ||
|
||
if (response?.token) { | ||
localStorage.setItem(this.localStorageKey, response.token); | ||
this.accessToken = response.token; | ||
return true; | ||
} | ||
return false; | ||
} catch (e) { | ||
console.log('checkProof error:', e); | ||
throw e; | ||
} | ||
} | ||
|
||
async getAccountInfo(account: Account) { | ||
const response = await ( | ||
await fetch(`${this.host}/api/get_account_info`, { | ||
headers: { | ||
Authorization: `Bearer ${this.accessToken}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
).json(); | ||
|
||
return response as {}; | ||
} | ||
|
||
reset() { | ||
this.accessToken = null; | ||
localStorage.removeItem(this.localStorageKey); | ||
this.generatePayload(); | ||
} | ||
} | ||
|
||
export const TonProofDemoApi = new TonProofDemoApiService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
const separator = `${window.location.pathname.replace(/\/+$/, '')}:`; | ||
|
||
const setItem = localStorage.setItem; | ||
localStorage.constructor.prototype.setItem = (key: string, value: string) => | ||
setItem.apply(localStorage, [separator + key, value]); | ||
localStorage.setItem = (key: string, value: string) => | ||
setItem.apply(localStorage, [separator + key, value]); | ||
|
||
const getItem = localStorage.getItem; | ||
localStorage.constructor.prototype.getItem = (key: string) => | ||
getItem.apply(localStorage, [separator + key]); | ||
localStorage.getItem = (key: string) => getItem.apply(localStorage, [separator + key]); | ||
|
||
const removeItem = localStorage.removeItem; | ||
localStorage.constructor.prototype.removeItem = (key: string) => | ||
removeItem.apply(localStorage, [separator + key]); | ||
localStorage.removeItem = (key: string) => removeItem.apply(localStorage, [separator + key]); | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client'; | ||
|
||
import { handlers } from './worker'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default function MswProvider({ children }: { children: React.ReactNode }) { | ||
const [mocksReady, setMocksReady] = useState(false); | ||
|
||
useEffect(() => { | ||
if (typeof window !== 'undefined') { | ||
void import('msw/browser').then((a) => { | ||
void a | ||
.setupWorker(...handlers(window.location.origin)) | ||
.start() | ||
.then(() => setMocksReady(true)); | ||
}); | ||
} | ||
}, []); | ||
|
||
return mocksReady && children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { HttpResponseResolver } from 'msw'; | ||
import { CheckProofRequest } from '../dto/checkProofRequestDto'; | ||
import { TonApiService } from '../services/tonApiService'; | ||
import { TonProofService } from '../services/tonProofService'; | ||
import { badRequest, ok } from '../utils/httpUtils'; | ||
import { createAuthToken, verifyToken } from '../utils/jwt'; | ||
|
||
/** | ||
* Checks the proof and returns an access token. | ||
* | ||
* POST /api/check_proof | ||
*/ | ||
export const checkProof: HttpResponseResolver = async ({ request }) => { | ||
try { | ||
const body = CheckProofRequest.parse(await request.json()); | ||
|
||
const client = TonApiService.create(body.network); | ||
const service = new TonProofService(); | ||
|
||
const isValid = await service.checkProof(body, (address) => client.getWalletPublicKey(address)); | ||
if (!isValid) { | ||
return badRequest({ error: 'Invalid proof' }); | ||
} | ||
|
||
const payloadToken = body.proof.payload; | ||
if (!(await verifyToken(payloadToken))) { | ||
return badRequest({ error: 'Invalid token' }); | ||
} | ||
|
||
const token = await createAuthToken({ address: body.address, network: body.network }); | ||
|
||
return ok({ token: token }); | ||
} catch (e) { | ||
return badRequest({ error: 'Invalid request', trace: e }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HttpResponseResolver } from 'msw'; | ||
import { TonProofService } from '../services/tonProofService'; | ||
import { badRequest, ok } from '../utils/httpUtils'; | ||
import { createPayloadToken } from '../utils/jwt'; | ||
|
||
/** | ||
* Generates a payload for ton proof. | ||
* | ||
* POST /api/generate_payload | ||
*/ | ||
export const generatePayload: HttpResponseResolver = async () => { | ||
try { | ||
const service = new TonProofService(); | ||
|
||
const payload = service.generatePayload(); | ||
const payloadToken = await createPayloadToken({ payload: payload }); | ||
|
||
return ok({ payload: payloadToken }); | ||
} catch (e) { | ||
return badRequest({ error: 'Invalid request', trace: e }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { HttpResponseResolver } from 'msw'; | ||
import { TonApiService } from '../services/tonApiService'; | ||
import { badRequest, ok, unauthorized } from '../utils/httpUtils'; | ||
import { decodeAuthToken, verifyToken } from '../utils/jwt'; | ||
|
||
/** | ||
* Returns account info. | ||
* | ||
* GET /api/get_account_info | ||
*/ | ||
export const getAccountInfo: HttpResponseResolver = async ({ request }) => { | ||
try { | ||
const token = request.headers.get('Authorization')?.replace('Bearer ', ''); | ||
|
||
if (!token || !(await verifyToken(token))) { | ||
return unauthorized({ error: 'Unauthorized' }); | ||
} | ||
|
||
const payload = decodeAuthToken(token); | ||
if (!payload?.address || !payload?.network) { | ||
return unauthorized({ error: 'Invalid token' }); | ||
} | ||
|
||
const client = TonApiService.create(payload.network); | ||
|
||
return ok(await client.getAccountInfo(payload.address)); | ||
} catch (e) { | ||
return badRequest({ error: 'Invalid request', trace: e }); | ||
} | ||
}; |
Oops, something went wrong.