Skip to content

Commit b861534

Browse files
authored
replace usage of Node's Buffer with browser-friendly code (#72)
* replace usage of Node's Buffer with browser-friendly code * test: add exception test to parseAuthenticatorFlags
1 parent 5c29bd2 commit b861534

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

packages/delegation-toolkit/src/webAuthn.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
encodePacked,
66
keccak256,
77
concat,
8+
hexToBytes,
89
} from 'viem';
910
import { parseSignature } from 'webauthn-p256';
1011

@@ -37,7 +38,7 @@ export const splitOnChallenge = (
3738
"challenge": "{userOpHash}",
3839
"origin": "{Domain}",
3940
"crossOrigin": boolean
40-
}
41+
}
4142
*/
4243
try {
4344
const { challenge } = JSON.parse(clientDataJson);
@@ -146,14 +147,12 @@ export type AuthenticatorFlags = {
146147
export function parseAuthenticatorFlags(
147148
authenticatorData: Hex,
148149
): AuthenticatorFlags {
149-
// eslint-disable-next-line no-restricted-globals
150-
const authenticatorDataBuffer = Buffer.from(
151-
authenticatorData.slice(2),
152-
'hex',
153-
);
154-
const flags = authenticatorDataBuffer.readUInt8(
155-
AUTHENTICATOR_DATA_FLAGS_OFFSET,
156-
);
150+
const authenticatorDataBuffer = hexToBytes(authenticatorData);
151+
const dataBufferUint8 = new Uint8Array(authenticatorDataBuffer);
152+
const flags = dataBufferUint8[AUTHENTICATOR_DATA_FLAGS_OFFSET];
153+
if (flags === undefined) {
154+
throw new Error('Authenticator flags not found in authenticator data');
155+
}
157156

158157
// Bit 0 is the least significant bit in the flags byte, so we left shift 0b1 by the bit index
159158
// eslint-disable-next-line no-bitwise

packages/delegation-toolkit/test/webAuthn.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,12 @@ describe('webAuthn', () => {
283283

284284
expect(userVerified).to.equal(false);
285285
});
286+
it('should throw an error if the authenticator flags are not found at the expected offset', () => {
287+
const flags = 0b11111011;
288+
289+
expect(() => parseAuthenticatorFlags(toHex(flags))).to.throw(
290+
'Authenticator flags not found in authenticator data',
291+
);
292+
});
286293
});
287294
});

0 commit comments

Comments
 (0)