Repository navigation
Expand file tree
/
Copy pathexpressBackend.js
More file actions
143 lines (130 loc) · 3.87 KB
/
Copy pathexpressBackend.js
File metadata and controls
143 lines (130 loc) · 3.87 KB
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
142
143
//#!/usr/bin/env node
//Install on project:
// $ npm install -s @gamechanger-finance/gc
// or
//Install globally:
// $ npm install -g @gamechanger-finance/gc
//Run this file
// $ node <FILENAME>.js
//Import if testing the library from this repository:
import gc from './res/nodejs.js'
// or
//Import normally:
//import gc from '@gamechanger-finance/gc'
import express from 'express';
const gcscript={
"type": "script",
"title": "🚀 Connect with dapp?",
"description": "About to share to the dapp your public wallet information and a CIP-8 signature to verify ownership",
"exportAs": "connect",
"return": {
"mode": "last"
},
"run": {
"data": {
"type": "script",
"run": {
"name": {
"type": "getName"
},
"address": {
"type": "getCurrentAddress"
},
"addressInfo": {
"type": "macro",
"run": "{getAddressInfo(get('cache.data.address'))}"
},
"agreement": {
"type": "macro",
"run": "{replaceAll('Myself, the user of wallet ADDRESS accepts to share all this information in order to connect with the dapp','ADDRESS',get('cache.data.address'))}"
},
"salt": {
"type": "macro",
"run": "{uuid()}"
}
}
},
"hash": {
"type": "macro",
"run": "{sha512(objToJson(get('cache.data')))}"
},
"sign": {
"type": "signDataWithAddress",
"address": "{get('cache.data.address')}",
"dataHex": "{get('cache.hash')}"
},
"finally": {
"type": "macro",
"run": {
"name": "{get('cache.data.name')}",
"address": "{get('cache.data.address')}",
"addressInfo": "{get('cache.data.addressInfo')}",
"signature": "{get('cache.sign')}",
"hash": "{get('cache.hash')}"
}
}
}
};
export const serve = function (options) {
const indexHtml = options.indexHtml;
const url = options.url;
const host = options.host || 'localhost';
const port = options.port || 3000;
const libPath = options.libPath || 'dist';
const app = express();
const routeDescriptions = {
'/dist': 'Gamechanger library files',
'/returnURL': 'Endpoint to receive exported data back from the wallet'
};
app.use('/dist', express.static(libPath));
app.get('/returnURL', async function (req, res) {
const resultRaw = req.query.result;
const resultObj = await gc.encodings.msg.decoder(resultRaw);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(resultObj, null, 2));
});
if (url) {
app.get('/url', function (req, res) {
res.status(301).redirect(url);
});
routeDescriptions['/url'] = 'redirects user to dapp connection URL';
}
if (indexHtml) {
app.get('/', function (req, res) {
res.send(indexHtml);
});
routeDescriptions['/'] = 'Minimal home';
}
app.listen(port, function () {
console.info('\n\n🚀 Express NodeJs Backend serving output URL with the hosted Gamechanger library on http://' + host + ':' + port + '/\n');
console.info('Routes:');
Object.entries(routeDescriptions).forEach(function (entry) {
console.info('\t' + entry[0] + ': \t' + entry[1]);
});
console.info('\n\n');
});
};
export const main = async function () {
const host = 'localhost';
const port = 3000;
const patchedGCScript = Object.assign({}, gcscript, {
returnURLPattern: 'http://' + host + ':' + port + '/returnURL/'
});
const url = await gc.encode.url({
input: JSON.stringify(patchedGCScript, null, 2),
apiVersion: "2",
network: "mainnet",
encoding: "gzip",
refAddress: undefined,
disableNetworkRouter: false,
urlPattern: undefined
});
const indexHtml = '<html><a href="/url">Click to get redirected to connect with GameChanger Wallet</a></html>';
serve({
host: host,
port: port,
url: url,
indexHtml: indexHtml
});
};
main();