Skip to content

Instantly share code, notes, and snippets.

@yai333
Last active June 27, 2021 05:51
Show Gist options
  • Select an option

  • Save yai333/abc104d9bf4cb11b6d4ca0f0f281983d to your computer and use it in GitHub Desktop.

Select an option

Save yai333/abc104d9bf4cb11b6d4ca0f0f281983d to your computer and use it in GitHub Desktop.
'use strict';
const AWS = require('aws-sdk');
const dynamoClient = new AWS.DynamoDB.DocumentClient();
const crypto = require('crypto');
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
};
module.exports.handler = async (event) => {
const requestBody = JSON.parse(event.body);
const { address } = requestBody;
const response = await updateNonce(address);
return {
headers,
statusCode: 200,
body: JSON.stringify(response),
};
};
const updateNonce = (address) => {
const nonce = crypto.randomBytes(16).toString('hex');
const params = {
TableName: process.env.USERTABLE_NAME,
Key: {
address,
},
UpdateExpression: 'set nonce = :n',
ExpressionAttributeValues: {
':n': nonce,
},
ReturnValues: 'ALL_NEW',
};
return dynamoClient.update(params).promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment