Skip to content

Instantly share code, notes, and snippets.

@chafreaky
Created February 5, 2022 22:59
Show Gist options
  • Save chafreaky/84be80aa9091b5b015b14d75558d3182 to your computer and use it in GitHub Desktop.
Save chafreaky/84be80aa9091b5b015b14d75558d3182 to your computer and use it in GitHub Desktop.
Validate a JWT token in DeSo (BitClout) against a Public Key
const jsonwebtoken = require('jsonwebtoken');
const bs58check = require('bs58check');
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const KeyEncoder = require('key-encoder').default;
/**
* Thank you to https://github.com/mattetre/bitclout-jwt-validate/blob/main/index.js
* @param {String} publicKey
* @param {String} jwt
* @returns {Object}
*/
function verifyBitCloutJWT(publicKey, jwt) {
const bitCloutPublicKeyDecoded = bs58check.decode(publicKey);
// manipulate the decoded key to remove the prefix that gets added
// privateKeyToBitcloutPublicKey - https://github.com/bitclout/identity/blob/main/src/app/crypto.service.ts#L128
const bitCloutPublicKeyDecodedArray = [...bitCloutPublicKeyDecoded];
// PUBLIC_KEY_PREFIXES - https://github.com/bitclout/identity/blob/main/src/app/crypto.service.ts#L22
const rawPublicKeyArray = bitCloutPublicKeyDecodedArray.slice(3);
const rawPublicKeyHex = ec
.keyFromPublic(rawPublicKeyArray, 'hex')
.getPublic()
.encode('hex', true);
const keyEncoder = new KeyEncoder('secp256k1');
const rawPublicKeyEncoded = keyEncoder.encodePublic(
rawPublicKeyHex,
'raw',
'pem'
);
return jsonwebtoken.verify(jwt, rawPublicKeyEncoded, {
algorithms: ['ES256'],
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment