Last active
September 8, 2021 21:16
-
-
Save sassmilic/5d963786bfcb2ecfc03a1fc85c237850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import codecs | |
import json | |
import random | |
from Crypto.Hash import keccak | |
# NOTE: `AIRNODE_ID` temporary -- update once Airnode deployed | |
AIRNODE_ID = '0' * 64 | |
COMMIT_RAW_PATH = "/commit" | |
REVEAL_RAW_PATH = "/reveal" | |
s3 = boto3.resource("s3") | |
bucket = s3.Bucket("commit-reveal-hashes") | |
def lambda_handler(event, context): | |
if event["rawPath"] == COMMIT_RAW_PATH: | |
# generate random number | |
xx = int(random.uniform(1, 1_000_000)) | |
# convert to hex string | |
x = hex(xx)[2:] | |
# left pad number with 0s in order to make 32 bytes | |
padded = "0" * (64 - len(x)) + x | |
# concatenate number and airnode ID | |
concat = padded + AIRNODE_ID | |
# generate hash | |
keccak_hash = keccak.new(digest_bits=256) | |
keccak_hash.update(bytearray.fromhex(concat)) | |
hsh = keccak_hash.hexdigest() | |
# save to S3 | |
s3.Object("commit-reveal-hashes", hsh).put(Body=padded) | |
return hsh | |
elif event["rawPath"] == REVEAL_RAW_PATH: | |
# get hash from request and load corresponding x value | |
hsh = event["queryStringParameters"]["hash"] | |
obj = s3.Object("commit-reveal-hashes", hsh).get() | |
result = obj["Body"].read().decode("utf-8") | |
# delete (hash, x) pair from storage | |
s3.Object("commit-reveal-hashes", hsh).delete() | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment