-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
96 lines (88 loc) · 2.39 KB
/
handler.js
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
import { createCaptcha } from './lib/captcha'
import { sendFunds } from './lib/faucet'
import { createChallenge } from './lib/challenge'
import { isAddress } from './lib/utils'
const fail = (msg) => {
return {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(msg)
};
}
export const status = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: `Alive!`,
}),
};
};
export const hello = async (event, context) => {
const { captcha, text } = await createCaptcha()
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
'Content-Type': 'text/html',
},
body: `<img src="data:image/png;base64,${captcha.toString('base64')}"/>`
}
};
export const captchaTest = async (event, context) => {
const { columns, rows } = event.pathParameters
const { captcha, text } = await createCaptcha({ format: 'ansi', columns, rows })
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
'Content-Type': 'text/plain',
},
body: JSON.stringify({
captcha,
text
})
}
};
export const captcha = async (event, context) => {
const { columns, rows } = event.pathParameters
const { captcha, text } = await createCaptcha({ format: 'ansi', columns, rows })
const challenge = await createChallenge(text)
console.log('created new captcha', text, challenge)
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
'Content-Type': 'text/plain',
},
body: JSON.stringify({
captcha,
challenge
})
}
};
export const fundAccount = async (event, context) => {
const { network: networkId, address } = event.pathParameters
console.log('fund account', networkId, address)
const to = isAddress(address)
if (!to) {
return fail('Not a valid address:'+address)
}
const amount = '1.0'
const network = 'goerli'
const result = await sendFunds(to, amount, network)
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
'Content-Type': 'text/plain',
},
body: result
}
};