Last active
July 30, 2021 10:14
-
-
Save ioannist/e1888a320ee5bc69b673ca604e61a207 to your computer and use it in GitHub Desktop.
Cognito presignup lambda function for Polkadot authentication
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
const aws = require("aws-sdk"); | |
const Sentry = require("@sentry/node"); | |
const { signatureVerify } = require('@polkadot/util-crypto'); | |
const { decodeAddress, encodeAddress } = require('@polkadot/keyring'); | |
const { hexToU8a, isHex } = require('@polkadot/util'); | |
exports.handler = async (event) => { | |
event.response.autoConfirmUser = true; | |
try { | |
console.log('Verifying address format') | |
const isAddressValid = isValidAddressPolkadotAddress(event.userName) | |
if (!isAddressValid) { | |
const e = new Error("Invalid address") | |
handleError(e) | |
} | |
console.log('Verifying signed key') | |
const { isValid } = signatureVerify(key, signedKey, event.userName); | |
if (!isValid) { | |
const e = new Error("Invalid signature") | |
} | |
} catch (e) { | |
console.error(e) | |
throw e | |
} | |
return event; | |
}; | |
function isValidAddressPolkadotAddress(address) { | |
try { | |
encodeAddress( | |
isHex(address) | |
? hexToU8a(address) | |
: decodeAddress(address) | |
); | |
return true; | |
} catch (error) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment