generated from AnomalyInnovations/serverless-typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
82 lines (73 loc) · 1.98 KB
/
handler.ts
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
import { APIGatewayProxyHandler } from "aws-lambda";
import axios from "axios";
import mongoose from "mongoose";
import Wallet from "walletmodel";
export const hello: APIGatewayProxyHandler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Go Serverless v2.0! Your function executed successfully!",
context,
event,
}),
};
};
export const addressProvider: APIGatewayProxyHandler = async (
event,
context
) => {
try {
const db = process.env.MONGO_URL;
console.log(db);
await mongoose.connect(db).then(() => console.log("Connected!"));
const { email, password } = JSON.parse(event.body);
// retrieve address
const addressData = await generateBlockAddress();
const doc = await Wallet.create({ password, email, wallet: addressData });
return {
statusCode: 200,
body: JSON.stringify({
message: "Your address was generated successfully",
data: {
address: addressData,
email,
},
}),
};
} catch (error) {
console.log("error", error);
}
};
export const retrieveWallet: APIGatewayProxyHandler = async (
event,
context
) => {
try {
const db = process.env.MONGO_URL;
await mongoose.connect(db).then(() => console.log("Connected!"));
const { email } = event.pathParameters;
const doc = await Wallet.findOne({ email });
// console.log("doc", doc);
const { public: publicKey, address } = doc.wallet;
return {
statusCode: 200,
body: JSON.stringify({
message: "Your address was generated successfully",
data: {
publicKey,
address,
},
}),
};
} catch (error) {
console.log("error", error);
}
};
// How do we transfer out the money ?
async function generateBlockAddress() {
const token = process.env.TOKEN;
const addressCall = await axios.post(
`https://api.blockcypher.com/v1/eth/main/addrs?token=${token}`
);
return addressCall.data;
}