Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs: Verifying in the Browser #1

Open
timkelty opened this issue Nov 21, 2023 · 1 comment
Open

Update docs: Verifying in the Browser #1

timkelty opened this issue Nov 21, 2023 · 1 comment

Comments

@timkelty
Copy link

I'm trying to use this lib to verify a signature.

Following the "Verifying in the Browser" docs, I've found some issues:

  • base64url dependency
    • The imported names don't match what base64url exports, at least with the current version
    • It appears this package comes with its own now, so it seems that should probably be used? import { base64 } from '@ltonetwork/http-message-signatures';
    • The signature for the verify function in the example is inaccurate (verifier should be 2nd argument)
    • The signature for the verifyHmac function in the example is inaccurate (signature: Uint8Array, not string)

I've tried to work through some of these things, and have TS happy, but still can't seem to get things verifying.

export async function verifyHmac(data: string, signature: Uint8Array, params: Parameters) {
	const keyData = new TextEncoder().encode('123456789');
	const algorithm = { name: 'HMAC', hash: 'SHA-256' };
	const key = await crypto.subtle.importKey('raw', keyData, algorithm, false, ['verify']);
	const encodedData = new TextEncoder().encode(data);
	const valid = await crypto.subtle.verify('HMAC', key, signature, encodedData);

	if (!valid) {
		throw new Error('Invalid signature');
	}

	return true;
}

export default {
	async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
                try {
	                verify(request, verifyHmac);
                } catch(e: any) {
	                return new Response(e.message, {
		                status: 403,
	                });
                }
        }
}
@timkelty timkelty changed the title Verifying in the Browser docs are innacurate Update docs: Verifying in the Browser Nov 21, 2023
jasny added a commit that referenced this issue Aug 26, 2024
[skip ci]

Based on #1. Need to test it in the browser before closing the issue.
@jasny
Copy link
Member

jasny commented Aug 26, 2024

Updated docs. All code examples need to be tested with the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants