Skip to content

Commit a08971a

Browse files
committed
wip: server
1 parent e80b7ae commit a08971a

14 files changed

+4707
-0
lines changed

server-example/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "server-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tsc -p tsconfig.json",
8+
"build:watch": "tsc -w -p tsconfig.json",
9+
"start": "nodemon -e ts src/server.ts"
10+
},
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"@tonapps/tonlogin-server": "../tonlogin-server",
15+
"cors": "^2.8.5",
16+
"create-hmac": "^1.1.7",
17+
"express": "^4.17.3",
18+
"express-handlebars": "^6.0.3",
19+
"tweetnacl": "^1.0.3",
20+
"tweetnacl-util": "^0.15.1",
21+
"typescript": "^4.6.2"
22+
},
23+
"devDependencies": {
24+
"@types/cors": "^2.8.12",
25+
"@types/create-hmac": "^1.1.0",
26+
"@types/express": "^4.17.13",
27+
"@types/express-handlebars": "^6.0.0",
28+
"nodemon": "^2.0.15",
29+
"ts-node": "^10.7.0"
30+
}
31+
}

server-example/src/server.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { engine } from 'express-handlebars';
2+
import express from 'express';
3+
import path from 'path';
4+
import cors from 'cors';
5+
import { TonLoginServer } from '@tonapps/tonlogin-server';
6+
import { getLocalIPAddress } from './utils';
7+
8+
// use generateServerSecret();
9+
const staticSecret = 'C0n0Tm4x4ACU9f0mQNEs0LPYMXIpwkKaRQYQsrc9Hx8=';
10+
const port = 8080;
11+
12+
function init() {
13+
const host = getLocalIPAddress();
14+
const hostname = `${host}:${port}`;
15+
const app = express();
16+
17+
app.use(cors());
18+
app.engine("handlebars", engine());
19+
app.set("view engine", "handlebars");
20+
app.set("views", path.resolve(__dirname, "./views"));
21+
22+
const tonlogin = new TonLoginServer({ staticSecret });
23+
24+
app.get('/authRequest', (req, res) => {
25+
const request = tonlogin.generateAuthRequest({
26+
image_url: 'https://ddejfvww7sqtk.cloudfront.net/images/landing/ton-nft-tegro-dog/avatar/image_d0315e1461.jpg',
27+
return_url: `${hostname}/tonlogin`,
28+
items: [{
29+
type: 'ton-address',
30+
require: true
31+
}],
32+
});
33+
34+
res.send(request);
35+
});
36+
37+
app.get('/tonlogin', (req, res) => {
38+
try {
39+
const encodedResponse = req.query.tonlogin as string;
40+
const decodedResponse = tonlogin.decodeAuthResponse(encodedResponse);
41+
42+
console.log(decodedResponse.client_id, decodedResponse.payload);
43+
44+
res.send(decodedResponse);
45+
} catch (err) {
46+
console.log(err);
47+
res.status(400).send({ error: true });
48+
}
49+
});
50+
51+
app.get('/', (req, res) => {
52+
res.render('home', {
53+
layout: false,
54+
requestEndpoint: `${hostname}/authRequest`
55+
});
56+
});
57+
58+
app.listen(port, host, () => {
59+
console.log(`Server running at http://${hostname}/`);
60+
});
61+
}
62+
63+
init();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os from 'os';
2+
3+
export function getLocalIPAddress() {
4+
const interfaces = os.networkInterfaces();
5+
for (let devName in interfaces) {
6+
const iface = interfaces[devName];
7+
8+
for (let i = 0; i < iface.length; i++) {
9+
const alias = iface[i];
10+
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
11+
return alias.address;
12+
}
13+
}
14+
}
15+
16+
return '0.0.0.0';
17+
}

server-example/src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './getLocalIPAddress';
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
.wrap {
6+
display: flex;
7+
align-items: stretch;
8+
border: 0 solid black;
9+
box-sizing: border-box;
10+
flex-basis: auto;
11+
flex-direction: column;
12+
flex-shrink: 0;
13+
margin-top: 40px;
14+
justify-content: center;
15+
align-items: center;
16+
}
17+
18+
#link {
19+
font-size: 18px;
20+
}
21+
</style>
22+
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
23+
<script type="text/javascript">
24+
function init() {
25+
const authRequestUrl = '{{requestEndpoint}}';
26+
const loginLink = 'https://app.tonkeeper.com/ton-login/' + authRequestUrl;
27+
28+
document.getElementById('link').href = loginLink;;
29+
new QRCode(document.getElementById('qrcode'), loginLink);
30+
}
31+
</script>
32+
</head>
33+
<body onload="init()">
34+
<div class="wrap">
35+
<div id="qrcode"></div>
36+
<br />
37+
<a id="link">Login by link</a>
38+
</div>
39+
</body>
40+
</html>

server-example/tsconfig.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"lib": ["es2017", "esnext.asynciterable"],
6+
"typeRoots": ["node_modules/@types"],
7+
"skipLibCheck": false,
8+
"allowSyntheticDefaultImports": true,
9+
"experimentalDecorators": true,
10+
"emitDecoratorMetadata": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"moduleResolution": "node",
13+
"module": "commonjs",
14+
"sourceMap": false,
15+
"declaration": false,
16+
"outDir": "./build",
17+
"allowJs": true,
18+
"esModuleInterop": true,
19+
"resolveJsonModule": true,
20+
"importHelpers": false,
21+
"strictNullChecks": false,
22+
"suppressImplicitAnyIndexErrors": true,
23+
"noUnusedLocals": false,
24+
},
25+
"include": ["src/**/*"],
26+
"exclude": ["node_modules"]
27+
}

0 commit comments

Comments
 (0)