Skip to content

Instantly share code, notes, and snippets.

@ioannist
Last active July 30, 2021 10:14
Show Gist options
  • Save ioannist/e1888a320ee5bc69b673ca604e61a207 to your computer and use it in GitHub Desktop.
Save ioannist/e1888a320ee5bc69b673ca604e61a207 to your computer and use it in GitHub Desktop.
Cognito presignup lambda function for Polkadot authentication
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