Skip to content

Commit b6a5937

Browse files
committed
Refactor WebAuthnSigningKey helper
1 parent d16e10f commit b6a5937

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

test/helpers/signers.js

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
encodeBase64,
1111
toUtf8Bytes,
1212
} = require('ethers');
13-
const { secp256r1 } = require('@noble/curves/p256');
1413

1514
class ZKEmailSigningKey {
1615
#domainName;
@@ -79,46 +78,34 @@ class ZKEmailSigningKey {
7978
}
8079

8180
class WebAuthnSigningKey extends P256SigningKey {
82-
constructor(privateKey) {
83-
super(privateKey);
84-
}
85-
86-
static random() {
87-
return new this(secp256r1.utils.randomPrivateKey());
88-
}
89-
90-
get PREFIX() {
91-
return '{"type":"webauthn.get","challenge":"';
92-
}
93-
94-
get SUFFIX() {
95-
return '"}';
96-
}
97-
98-
base64toBase64Url = str => str.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
99-
10081
sign(digest /*: BytesLike*/) /*: { serialized: string } */ {
10182
assertArgument(dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
10283

103-
const clientDataJSON = this.PREFIX.concat(this.base64toBase64Url(encodeBase64(toBeHex(digest, 32)))).concat(
104-
this.SUFFIX,
105-
);
84+
const clientDataJSON = JSON.stringify({
85+
type: 'webauthn.get',
86+
challenge: encodeBase64(digest).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''),
87+
});
10688

107-
const authenticatorData = toBeHex('0', 37);
89+
const authenticatorData = toBeHex(0n, 37); // equivalent to `hexlify(new Uint8Array(37))`
10890

10991
// Regular P256 signature
110-
const sig = super.sign(sha256(concat([authenticatorData, sha256(toUtf8Bytes(clientDataJSON))])));
111-
112-
return {
113-
serialized: this.serialize(sig.r, sig.s, authenticatorData, clientDataJSON),
114-
};
115-
}
92+
const { r, s } = super.sign(sha256(concat([authenticatorData, sha256(toUtf8Bytes(clientDataJSON))])));
11693

117-
serialize(r, s, authenticatorData, clientDataJSON) {
118-
return AbiCoder.defaultAbiCoder().encode(
94+
const serialized = AbiCoder.defaultAbiCoder().encode(
11995
['tuple(bytes32,bytes32,uint256,uint256,bytes,string)'],
120-
[[r, s, 23, 1, authenticatorData, clientDataJSON]],
96+
[
97+
[
98+
r,
99+
s,
100+
clientDataJSON.indexOf('"challenge"'),
101+
clientDataJSON.indexOf('"type"'),
102+
authenticatorData,
103+
clientDataJSON,
104+
],
105+
],
121106
);
107+
108+
return { serialized };
122109
}
123110
}
124111

0 commit comments

Comments
 (0)