Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions backend/src/extensions/users-permissions/strapi-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,40 @@ module.exports = (plugin) => {
}

if (provider === 'local') {
const { identifier, signedData } = params;

// todo also add the message that was signed over by the wallet
// const { expectedIdentifier, signedMessage, expectedSignedMessage } = params;
const { identifier, signedMessage } = params;

let userInfo = ctx?.state?.user;

if (!identifier) {
throw new ValidationError('identifier was not provided');
}

if (!signedData) {
if (!signedMessage) {
throw new ValidationError('signData object was not provided');
}

const decoded = COSESign1.from_bytes(
Buffer.from(signedData.signature, 'hex')
const receivedCOSESig = COSESign1.from_bytes(
Buffer.from(signedMessage.signature, 'hex')
);
const key = COSEKey.from_bytes(Buffer.from(signedData.key, 'hex'));
const pubKeyBytes = key
const receivedCOSEKey = COSEKey.from_bytes(Buffer.from(signedMessage.key, 'hex'));
const receivedPublicKeyBytes = receivedCOSEKey
.header(Label.new_int(Int.new_negative(BigNum.from_str('2'))))
.as_bytes();
const publicKey = PublicKey.from_bytes(pubKeyBytes);
const signature = Ed25519Signature.from_bytes(decoded.signature());
const receivedData = decoded.signed_data().to_bytes();

// Remove network id from identifier
const rawKeyHash = userInfo ? identifier : identifier.slice(2);

const receivedPublicKey = PublicKey.from_bytes(receivedPublicKeyBytes);
const receivedSignature = Ed25519Signature.from_bytes(receivedCOSESig.signature());
const receivedMessageBytes = receivedCOSESig.signed_data().to_bytes();

// Remove network id from identifier, if included
const expectedKeyHash = userInfo ? identifier : identifier.slice(2);

// Check the received key hash matches the received signature
// and check that the received key hash matches the expected key hash
const isVerified =
publicKey.verify(receivedData, signature) &&
rawKeyHash === publicKey.hash().to_hex();
receivedPublicKey.verify(receivedMessageBytes, signature) &&
expectedKeyHash === receivedPublicKey.hash().to_hex();

if (!isVerified) {
throw new ApplicationError('Verification failed');
Expand Down