-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathwebAuthn.ts
More file actions
200 lines (177 loc) · 6.41 KB
/
Copy pathwebAuthn.ts
File metadata and controls
200 lines (177 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {
parseAbiParameters,
encodeAbiParameters,
type Hex,
encodePacked,
keccak256,
concat,
hexToBytes,
} from 'viem';
import { parseSignature } from 'webauthn-p256';
export const FIELD_MODULUS =
115792089210356248762697446949407573529996955224135760342422259061068512044369n;
export const MALLEABILITY_THRESHOLD = FIELD_MODULUS / 2n;
export const SIGNATURE_ABI_PARAMS = parseAbiParameters(
'bytes32, uint256, uint256, bytes, bool, string, string, uint256',
);
/**
* This function is used to convert the client data returned from the
* credentials API into a format that can be consumed by the DeleGator
* contracts. We need the flattend JSON strings before and after the
* userOpHash/challenge. This function provides those two client data string
* slices.
* @param clientDataJson - The client data JSON string.
* @returns Returns [clientDataJSONPrefix and clientDataJSONSuffix]
* ClientDataJSONPrefix contains the client data till the challengeHash
* ClientDataJSONSuffix contains the client data after the challengeHash.
*/
export const splitOnChallenge = (
clientDataJson: string,
): [clientDataJSONPrefix: string, clientDataJSONSuffix: string] => {
/*
CientData looks like this:
{
"type": "webauthn.create" | "webauthn.get",
"challenge": "{userOpHash}",
"origin": "{Domain}",
"crossOrigin": boolean
}
*/
try {
const { challenge } = JSON.parse(clientDataJson);
if (challenge === undefined) {
throw new Error('No "challenge" found in the input string');
}
return clientDataJson.split(challenge) as [string, string];
} catch (error) {
throw new Error('No "challenge" found in the input string', {
cause: error,
});
}
};
/**
* Returns the index of '"type":' in the ClientData.
* @param clientDataJson - Stringified ClientDataJSON.
* @returns The index of '"type":' in the ClientData.
*/
export const getResponseTypeLocation = (clientDataJson: string): bigint => {
try {
// Find the index of the `"type":` key in the JSON string directly
const typeIndex = clientDataJson.indexOf('"type":');
if (typeIndex === -1) {
throw new Error('No "type" found in the input string');
}
// Return the index of the `"type":` key
return BigInt(typeIndex);
} catch (error) {
// Handle any errors that occur during the search
throw new Error('No "type" found in the input string', {
cause: error,
});
}
};
/**
* Encodes a signature to a hexadecimal signature that will be accepted
* by the DeleGator contracts.
* @param keyId - The key used for the signature, represented as a hexadecimal string.
* @param signature - The signature to convert, as Hex.
* @param clientDataJSON - The client data used in the creation of the signature.
* @param authenticatorData - The authenticator data used in the creation of the signature.
* @returns The signature as a valid DeleGator signature encoded as Hexadecimal string.
*/
export function encodeDeleGatorSignature(
keyId: string,
signature: Hex,
clientDataJSON: string,
authenticatorData: Hex,
): Hex {
const keyIdHash = keccak256(encodePacked(['string'], [keyId]));
const parsedSignature = parseSignature(signature);
let { s } = parsedSignature;
while (s > MALLEABILITY_THRESHOLD) {
s = FIELD_MODULUS - s;
}
const { r } = parsedSignature;
const [clientDataComponent1, clientDataComponent2] =
splitOnChallenge(clientDataJSON);
const { userVerified } = parseAuthenticatorFlags(authenticatorData);
const responseTypeLocation = getResponseTypeLocation(clientDataJSON);
const encodedSignature = encodeAbiParameters(SIGNATURE_ABI_PARAMS, [
keyIdHash,
r,
s,
authenticatorData,
userVerified,
clientDataComponent1,
clientDataComponent2,
responseTypeLocation,
]);
return encodedSignature;
}
const AUTHENTICATOR_DATA_FLAGS_OFFSET = 32;
// We have all of the flag bits defined here for completeness, even though we only extract the userVerified flag.
enum AuthenticatorDataFlagBitIndex {
UserPresence = 0,
UserVerified = 2,
BackupEligibility = 3,
BackupState = 4,
AttestedCredentialData = 6,
ExtensionData = 7,
}
export type AuthenticatorFlags = {
userVerified: boolean;
};
/**
* Parses the authenticator data and returns an authenticator flags object with the `userVerified` flag.
* See https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API/Authenticator_data.
* @param authenticatorData - The authenticator data to parse.
* @returns An object representing the parsed authenticator flags.
*/
export function parseAuthenticatorFlags(
authenticatorData: Hex,
): AuthenticatorFlags {
const authenticatorDataBuffer = hexToBytes(authenticatorData);
const dataBufferUint8 = new Uint8Array(authenticatorDataBuffer);
const flags = dataBufferUint8[AUTHENTICATOR_DATA_FLAGS_OFFSET];
if (flags === undefined) {
throw new Error('Authenticator flags not found in authenticator data');
}
// Bit 0 is the least significant bit in the flags byte, so we left shift 0b1 by the bit index
// eslint-disable-next-line no-bitwise
const bitMask = 0b1 << AuthenticatorDataFlagBitIndex.UserVerified;
return {
// eslint-disable-next-line no-bitwise
userVerified: (flags & bitMask) !== 0x0,
};
}
/**
* Creates a dummy signature.
* This must meet all early-failure conditions of the real signature, but does not need to be a valid signature.
* @param keyId - The key ID to use for the dummy signature.
* @returns The encoded signature.
*/
export const createDummyWebAuthnSignature = (keyId: Hex) => {
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API/Authenticator_data#data_structure
const rpIdHash = keccak256(encodePacked(['string'], ['AuthenticatorData']));
const flags = '0x05';
const signCount = '0x00000000';
const authenticatorData = concat([rpIdHash, flags, signCount]);
const keyIdHash = keccak256(encodePacked(['string'], [keyId]));
const rs =
57896044605178124381348723474703786764998477612067880171211129530534256022184n;
const userVerification = true;
const clientDataPrefix = '{"type":"webauthn.get","challenge":"';
const clientDataSuffix = '","origin":"passkey-domain","crossOrigin":false}';
const responseTypeLocation = 1n;
const encodedSignature = encodeAbiParameters(SIGNATURE_ABI_PARAMS, [
keyIdHash,
rs,
rs,
authenticatorData,
userVerification,
clientDataPrefix,
clientDataSuffix,
responseTypeLocation,
]);
return encodedSignature;
};