Skip to content

Instantly share code, notes, and snippets.

View yai333's full-sized avatar

Yi Ai yai333

  • Melbourne
View GitHub Profile
const { data } = await axios.post(
"/login",
{
address,
signature,
},
{
headers: {
'Content-Type': 'application/json',
},
'use strict';
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
};
module.exports.handler = async (event) => {
const {
queryStringParameters: { name },
} = event;
Hello:
handler: src/hello/index.handler
events:
- http:
path: hello
method: get
cors: true
authorizer: aws_iam
'use strict';
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB();
const dynamoClient = new AWS.DynamoDB.DocumentClient();
const cognitoidentity = new AWS.CognitoIdentity();
const crypto = require('crypto');
const Web3 = require('web3');
const headers = {
functions:
Login:
handler: src/login/index.handler
events:
- http:
path: login
method: post
cors: true
const getCredentials = (identityId, cognitoOpenIdToken) => {
const params = {
IdentityId: identityId,
Logins: {},
};
params.Logins['cognito-identity.amazonaws.com'] = cognitoOpenIdToken;
return cognitoidentity.getCredentialsForIdentity(params).promise();
};
const Web3 = require('web3');
const getIdToken = (address) => {
const param = {
IdentityPoolId: process.env.IDENTITY_POOL_ID,
Logins: {},
};
param.Logins[process.env.DEVELOPER_PROVIDER_NAME] = address;
return cognitoidentity.getOpenIdTokenForDeveloperIdentity(param).promise();
};
CognitoIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
AllowUnauthenticatedIdentities: false
IdentityPoolName: "${self:service.name}IdentityPool"
DeveloperProviderName: "${self:custom.customProviderName}"
CognitoAuthorizedRole:
Type: "AWS::IAM::Role"
Properties:
const validateSig = async (address, signature, nonce) => {
const message = `Welcome message, nonce: ${nonce}`;
const hash = web3.utils.sha3(message);
const signing_address = await web3.eth.accounts.recover(hash, signature);
return signing_address.toLowerCase() === address.toLowerCase();
};
iamRoleStatements:
- Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:UpdateItem"
- "dynamodb:ExecuteStatement"
- "dynamodb:PartiQLSelect"
Resource:
- "Fn::GetAtt": [UserTable, Arn]