-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
44 lines (36 loc) · 1.33 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
import { APIGatewayProxyHandler } from 'aws-lambda';
import Logger from './lib/logger';
import RequestParser from './lib/request-parser';
import NodeClient from './lib/node-client';
import { httpOK, httpServerError } from './lib/http-request-codes';
import Signer from './lib/signer';
// URL of Tezos Node.
const tezosNodeURL = process.env.NODE_ADDR;
export const sign: APIGatewayProxyHandler = async (event, _context) => {
const logger = new Logger();
logger.log("Handling a new request.");
const nodeClient = new NodeClient(tezosNodeURL);
try {
// Load secret key from environment variables.
const secretKeyBase58 = process.env.SECRET_KEY_BASE_58;
if (secretKeyBase58 == undefined) {
throw new Error("Could not instantiate secret key");
}
const operationHex = RequestParser.parseOperationFromRequest(event);
logger.log("Parsed operation as: " + operationHex);
const signedTransaction = await Signer.signOperation(operationHex, secretKeyBase58);
const hash = await nodeClient.inject(signedTransaction);
logger.log("Injected with hash: " + hash);
return {
statusCode: httpOK,
body: hash,
};
} catch (exception) {
const failureMessage = "Failure: " + exception
logger.log(failureMessage);
return {
statusCode: httpServerError,
body: failureMessage
}
}
}