ox 0.12.4 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/_cjs/core/P256.js +1 -1
- package/_cjs/core/P256.js.map +1 -1
- package/_cjs/core/WebAuthnP256.js +15 -256
- package/_cjs/core/WebAuthnP256.js.map +1 -1
- package/_cjs/core/WebCryptoP256.js +3 -1
- package/_cjs/core/WebCryptoP256.js.map +1 -1
- package/_cjs/core/internal/webauthn.js +5 -13
- package/_cjs/core/internal/webauthn.js.map +1 -1
- package/_cjs/index.docs.js +1 -0
- package/_cjs/index.docs.js.map +1 -1
- package/_cjs/tempo/KeyAuthorization.js +18 -3
- package/_cjs/tempo/KeyAuthorization.js.map +1 -1
- package/_cjs/tempo/SignatureEnvelope.js +26 -0
- package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
- package/_cjs/tempo/TxEnvelopeTempo.js +5 -10
- package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_cjs/webauthn/Authentication.js +246 -0
- package/_cjs/webauthn/Authentication.js.map +1 -0
- package/_cjs/webauthn/Authenticator.js +55 -0
- package/_cjs/webauthn/Authenticator.js.map +1 -0
- package/_cjs/webauthn/Credential.js +53 -0
- package/_cjs/webauthn/Credential.js.map +1 -0
- package/_cjs/webauthn/Registration.js +349 -0
- package/_cjs/webauthn/Registration.js.map +1 -0
- package/_cjs/webauthn/Types.js +3 -0
- package/_cjs/webauthn/Types.js.map +1 -0
- package/_cjs/webauthn/index.js +9 -0
- package/_cjs/webauthn/index.js.map +1 -0
- package/_cjs/webauthn/internal/utils.js +53 -0
- package/_cjs/webauthn/internal/utils.js.map +1 -0
- package/_esm/core/P256.js +1 -1
- package/_esm/core/P256.js.map +1 -1
- package/_esm/core/WebAuthnP256.js +13 -261
- package/_esm/core/WebAuthnP256.js.map +1 -1
- package/_esm/core/WebCryptoP256.js +4 -1
- package/_esm/core/WebCryptoP256.js.map +1 -1
- package/_esm/core/internal/webauthn.js +5 -13
- package/_esm/core/internal/webauthn.js.map +1 -1
- package/_esm/erc8021/index.js +2 -2
- package/_esm/index.docs.js +1 -0
- package/_esm/index.docs.js.map +1 -1
- package/_esm/tempo/KeyAuthorization.js +66 -3
- package/_esm/tempo/KeyAuthorization.js.map +1 -1
- package/_esm/tempo/SignatureEnvelope.js +74 -0
- package/_esm/tempo/SignatureEnvelope.js.map +1 -1
- package/_esm/tempo/TransactionReceipt.js +1 -1
- package/_esm/tempo/TransactionRequest.js +1 -1
- package/_esm/tempo/TxEnvelopeTempo.js +5 -10
- package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_esm/webauthn/Authentication.js +453 -0
- package/_esm/webauthn/Authentication.js.map +1 -0
- package/_esm/webauthn/Authenticator.js +176 -0
- package/_esm/webauthn/Authenticator.js.map +1 -0
- package/_esm/webauthn/Credential.js +95 -0
- package/_esm/webauthn/Credential.js.map +1 -0
- package/_esm/webauthn/Registration.js +512 -0
- package/_esm/webauthn/Registration.js.map +1 -0
- package/_esm/webauthn/Types.js +2 -0
- package/_esm/webauthn/Types.js.map +1 -0
- package/_esm/webauthn/index.js +31 -0
- package/_esm/webauthn/index.js.map +1 -0
- package/_esm/webauthn/internal/utils.js +52 -0
- package/_esm/webauthn/internal/utils.js.map +1 -0
- package/_types/core/WebAuthnP256.d.ts +33 -208
- package/_types/core/WebAuthnP256.d.ts.map +1 -1
- package/_types/core/WebCryptoP256.d.ts +2 -0
- package/_types/core/WebCryptoP256.d.ts.map +1 -1
- package/_types/core/internal/webauthn.d.ts +2 -110
- package/_types/core/internal/webauthn.d.ts.map +1 -1
- package/_types/erc8021/index.d.ts +2 -2
- package/_types/index.docs.d.ts +1 -0
- package/_types/index.docs.d.ts.map +1 -1
- package/_types/tempo/KeyAuthorization.d.ts +57 -0
- package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
- package/_types/tempo/SignatureEnvelope.d.ts +75 -0
- package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
- package/_types/tempo/Transaction.d.ts +2 -2
- package/_types/tempo/TransactionReceipt.d.ts +2 -2
- package/_types/tempo/TransactionRequest.d.ts +2 -2
- package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/_types/webauthn/Authentication.d.ts +324 -0
- package/_types/webauthn/Authentication.d.ts.map +1 -0
- package/_types/webauthn/Authenticator.d.ts +182 -0
- package/_types/webauthn/Authenticator.d.ts.map +1 -0
- package/_types/webauthn/Credential.d.ts +77 -0
- package/_types/webauthn/Credential.d.ts.map +1 -0
- package/_types/webauthn/Registration.d.ts +308 -0
- package/_types/webauthn/Registration.d.ts.map +1 -0
- package/_types/webauthn/Types.d.ts +106 -0
- package/_types/webauthn/Types.d.ts.map +1 -0
- package/_types/webauthn/index.d.ts +33 -0
- package/_types/webauthn/index.d.ts.map +1 -0
- package/_types/webauthn/internal/utils.d.ts +17 -0
- package/_types/webauthn/internal/utils.d.ts.map +1 -0
- package/core/P256.ts +1 -1
- package/core/WebAuthnP256.ts +37 -582
- package/core/WebCryptoP256.ts +6 -1
- package/core/internal/webauthn.ts +6 -165
- package/erc8021/index.ts +2 -2
- package/index.docs.ts +1 -0
- package/package.json +31 -1
- package/tempo/KeyAuthorization.test.ts +139 -0
- package/tempo/KeyAuthorization.ts +82 -3
- package/tempo/SignatureEnvelope.test.ts +147 -0
- package/tempo/SignatureEnvelope.ts +113 -0
- package/tempo/Transaction.ts +2 -2
- package/tempo/TransactionReceipt.ts +2 -2
- package/tempo/TransactionRequest.ts +2 -2
- package/tempo/TxEnvelopeTempo.ts +5 -12
- package/tempo/e2e.test.ts +265 -0
- package/version.ts +1 -1
- package/webauthn/Authentication/package.json +6 -0
- package/webauthn/Authentication.ts +673 -0
- package/webauthn/Authenticator/package.json +6 -0
- package/webauthn/Authenticator.ts +259 -0
- package/webauthn/Credential/package.json +6 -0
- package/webauthn/Credential.ts +146 -0
- package/webauthn/Registration/package.json +6 -0
- package/webauthn/Registration.ts +805 -0
- package/webauthn/Types/package.json +6 -0
- package/webauthn/Types.ts +158 -0
- package/webauthn/index.ts +38 -0
- package/webauthn/internal/utils.ts +63 -0
- package/webauthn/package.json +6 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import * as Base64 from '../core/Base64.js'
|
|
2
|
+
import * as Bytes from '../core/Bytes.js'
|
|
3
|
+
import * as Cbor from '../core/Cbor.js'
|
|
4
|
+
import * as CoseKey from '../core/CoseKey.js'
|
|
5
|
+
import type * as Errors from '../core/Errors.js'
|
|
6
|
+
import * as Hash from '../core/Hash.js'
|
|
7
|
+
import * as Hex from '../core/Hex.js'
|
|
8
|
+
import type * as PublicKey from '../core/PublicKey.js'
|
|
9
|
+
import type * as Types from './Types.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Gets the authenticator data which contains information about the
|
|
13
|
+
* processing of an authenticator request (ie. from `Authentication.sign`).
|
|
14
|
+
*
|
|
15
|
+
* :::warning
|
|
16
|
+
*
|
|
17
|
+
* This function is mainly for testing purposes or for manually constructing
|
|
18
|
+
* autenticator data. In most cases you will not need this function.
|
|
19
|
+
* `authenticatorData` is typically returned as part of the
|
|
20
|
+
* authenticator response.
|
|
21
|
+
*
|
|
22
|
+
* :::
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts twoslash
|
|
26
|
+
* import { Authenticator } from 'ox/webauthn'
|
|
27
|
+
*
|
|
28
|
+
* const authenticatorData = Authenticator.getAuthenticatorData({
|
|
29
|
+
* rpId: 'example.com',
|
|
30
|
+
* signCount: 420,
|
|
31
|
+
* })
|
|
32
|
+
* // @log: "0xa379a6f6eeafb9a55e378c118034e2751e682fab9f2d30ab13d2125586ce194705000001a4"
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ### With Attested Credential Data
|
|
37
|
+
*
|
|
38
|
+
* Include a credential ID and public key in the authenticator data (for registration responses):
|
|
39
|
+
*
|
|
40
|
+
* ```ts twoslash
|
|
41
|
+
* import { P256 } from 'ox'
|
|
42
|
+
* import { Authenticator } from 'ox/webauthn'
|
|
43
|
+
*
|
|
44
|
+
* const { publicKey } = P256.createKeyPair()
|
|
45
|
+
*
|
|
46
|
+
* const authenticatorData = Authenticator.getAuthenticatorData({
|
|
47
|
+
* rpId: 'example.com',
|
|
48
|
+
* flag: 0x41, // UP + AT
|
|
49
|
+
* credential: {
|
|
50
|
+
* id: new Uint8Array(32),
|
|
51
|
+
* publicKey,
|
|
52
|
+
* },
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @param options - Options to construct the authenticator data.
|
|
57
|
+
* @returns The authenticator data.
|
|
58
|
+
*/
|
|
59
|
+
export function getAuthenticatorData(
|
|
60
|
+
options: getAuthenticatorData.Options = {},
|
|
61
|
+
): Hex.Hex {
|
|
62
|
+
const {
|
|
63
|
+
credential,
|
|
64
|
+
flag = 5,
|
|
65
|
+
rpId = window.location.hostname,
|
|
66
|
+
signCount = 0,
|
|
67
|
+
} = options
|
|
68
|
+
const rpIdHash = Hash.sha256(Hex.fromString(rpId))
|
|
69
|
+
const flag_bytes = Hex.fromNumber(flag, { size: 1 })
|
|
70
|
+
const signCount_bytes = Hex.fromNumber(signCount, { size: 4 })
|
|
71
|
+
const base = Hex.concat(rpIdHash, flag_bytes, signCount_bytes)
|
|
72
|
+
|
|
73
|
+
if (!credential) return base
|
|
74
|
+
|
|
75
|
+
// AAGUID (16 bytes of zeros)
|
|
76
|
+
const aaguid = Hex.fromBytes(new Uint8Array(16))
|
|
77
|
+
|
|
78
|
+
// Credential ID
|
|
79
|
+
const credentialId = Hex.fromBytes(credential.id)
|
|
80
|
+
const credIdLen = Hex.fromNumber(credential.id.length, { size: 2 })
|
|
81
|
+
|
|
82
|
+
// COSE public key
|
|
83
|
+
const coseKey = CoseKey.fromPublicKey(credential.publicKey)
|
|
84
|
+
|
|
85
|
+
return Hex.concat(base, aaguid, credIdLen, credentialId, coseKey)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export declare namespace getAuthenticatorData {
|
|
89
|
+
type Options = {
|
|
90
|
+
/** Attested credential data to include (credential ID + public key). When set, the AT flag (0x40) should also be set. */
|
|
91
|
+
credential?:
|
|
92
|
+
| {
|
|
93
|
+
/** The credential ID as raw bytes. */
|
|
94
|
+
id: Uint8Array
|
|
95
|
+
/** The P256 public key associated with the credential. */
|
|
96
|
+
publicKey: PublicKey.PublicKey
|
|
97
|
+
}
|
|
98
|
+
| undefined
|
|
99
|
+
/** A bitfield that indicates various attributes that were asserted by the authenticator. [Read more](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API/Authenticator_data#flags) */
|
|
100
|
+
flag?: number | undefined
|
|
101
|
+
/** The [Relying Party ID](https://w3c.github.io/webauthn/#relying-party-identifier) that the credential is scoped to. */
|
|
102
|
+
rpId?: Types.PublicKeyCredentialRequestOptions['rpId'] | undefined
|
|
103
|
+
/** A signature counter, if supported by the authenticator (set to 0 otherwise). */
|
|
104
|
+
signCount?: number | undefined
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type ErrorType = Errors.GlobalErrorType
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Extracts the signature counter from the authenticator data.
|
|
112
|
+
* The counter is a 4-byte big-endian unsigned integer at bytes 33–36.
|
|
113
|
+
*
|
|
114
|
+
* Useful for detecting cloned authenticators: if the counter is non-zero and
|
|
115
|
+
* does not monotonically increase between assertions, it may indicate a cloned key.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts twoslash
|
|
119
|
+
* import { Authenticator } from 'ox/webauthn'
|
|
120
|
+
*
|
|
121
|
+
* const signCount = Authenticator.getSignCount(
|
|
122
|
+
* '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000001',
|
|
123
|
+
* )
|
|
124
|
+
* // @log: 1
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @param authenticatorData - The authenticator data hex string.
|
|
128
|
+
* @returns The signature counter.
|
|
129
|
+
*/
|
|
130
|
+
export function getSignCount(authenticatorData: Hex.Hex): number {
|
|
131
|
+
const bytes = Bytes.fromHex(authenticatorData)
|
|
132
|
+
if (bytes.length < 37) return 0
|
|
133
|
+
return (
|
|
134
|
+
((bytes[33]! << 24) |
|
|
135
|
+
(bytes[34]! << 16) |
|
|
136
|
+
(bytes[35]! << 8) |
|
|
137
|
+
bytes[36]!) >>>
|
|
138
|
+
0
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export declare namespace getSignCount {
|
|
143
|
+
type ErrorType = Bytes.fromHex.ErrorType | Errors.GlobalErrorType
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Constructs the Client Data in stringified JSON format which represents client data that
|
|
148
|
+
* was passed to `credentials.get()` or `credentials.create()`.
|
|
149
|
+
*
|
|
150
|
+
* :::warning
|
|
151
|
+
*
|
|
152
|
+
* This function is mainly for testing purposes or for manually constructing
|
|
153
|
+
* client data. In most cases you will not need this function.
|
|
154
|
+
* `clientDataJSON` is typically returned as part of the authenticator response.
|
|
155
|
+
*
|
|
156
|
+
* :::
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts twoslash
|
|
160
|
+
* import { Authenticator } from 'ox/webauthn'
|
|
161
|
+
*
|
|
162
|
+
* const clientDataJSON = Authenticator.getClientDataJSON({
|
|
163
|
+
* challenge: '0xdeadbeef',
|
|
164
|
+
* origin: 'https://example.com',
|
|
165
|
+
* })
|
|
166
|
+
* // @log: "{"type":"webauthn.get","challenge":"3q2-7w","origin":"https://example.com","crossOrigin":false}"
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @param options - Options to construct the client data.
|
|
170
|
+
* @returns The client data.
|
|
171
|
+
*/
|
|
172
|
+
export function getClientDataJSON(options: getClientDataJSON.Options): string {
|
|
173
|
+
const {
|
|
174
|
+
challenge,
|
|
175
|
+
crossOrigin = false,
|
|
176
|
+
extraClientData,
|
|
177
|
+
origin = window.location.origin,
|
|
178
|
+
type = 'webauthn.get',
|
|
179
|
+
} = options
|
|
180
|
+
|
|
181
|
+
return JSON.stringify({
|
|
182
|
+
type,
|
|
183
|
+
challenge: Base64.fromHex(challenge, { url: true, pad: false }),
|
|
184
|
+
origin,
|
|
185
|
+
crossOrigin,
|
|
186
|
+
...extraClientData,
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export declare namespace getClientDataJSON {
|
|
191
|
+
type Options = {
|
|
192
|
+
/** The challenge to sign. */
|
|
193
|
+
challenge: Hex.Hex
|
|
194
|
+
/** If set to `true`, it means that the calling context is an `<iframe>` that is not same origin with its ancestor frames. */
|
|
195
|
+
crossOrigin?: boolean | undefined
|
|
196
|
+
/** Additional client data to include in the client data JSON. */
|
|
197
|
+
extraClientData?: Record<string, unknown> | undefined
|
|
198
|
+
/** The fully qualified origin of the relying party which has been given by the client/browser to the authenticator. */
|
|
199
|
+
origin?: string | undefined
|
|
200
|
+
/** The WebAuthn ceremony type. @default 'webauthn.get' */
|
|
201
|
+
type?: 'webauthn.create' | 'webauthn.get' | undefined
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
type ErrorType = Errors.GlobalErrorType
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Constructs a CBOR-encoded attestation object for testing WebAuthn registration
|
|
209
|
+
* verification. Combines the authenticator data with an attestation statement.
|
|
210
|
+
*
|
|
211
|
+
* :::warning
|
|
212
|
+
*
|
|
213
|
+
* This function is mainly for testing purposes. In production, the attestation
|
|
214
|
+
* object is returned by the authenticator during `navigator.credentials.create()`.
|
|
215
|
+
*
|
|
216
|
+
* :::
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts twoslash
|
|
220
|
+
* import { P256 } from 'ox'
|
|
221
|
+
* import { Authenticator } from 'ox/webauthn'
|
|
222
|
+
*
|
|
223
|
+
* const { publicKey } = P256.createKeyPair()
|
|
224
|
+
*
|
|
225
|
+
* const attestationObject = Authenticator.getAttestationObject({
|
|
226
|
+
* authData: Authenticator.getAuthenticatorData({
|
|
227
|
+
* rpId: 'example.com',
|
|
228
|
+
* flag: 0x41,
|
|
229
|
+
* credential: { id: new Uint8Array(32), publicKey },
|
|
230
|
+
* }),
|
|
231
|
+
* })
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @param options - Options to construct the attestation object.
|
|
235
|
+
* @returns The CBOR-encoded attestation object as a Hex string.
|
|
236
|
+
*/
|
|
237
|
+
export function getAttestationObject(
|
|
238
|
+
options: getAttestationObject.Options,
|
|
239
|
+
): Hex.Hex {
|
|
240
|
+
const { attStmt = {}, authData, fmt = 'none' } = options
|
|
241
|
+
return Cbor.encode({
|
|
242
|
+
fmt,
|
|
243
|
+
attStmt,
|
|
244
|
+
authData: Hex.toBytes(authData),
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export declare namespace getAttestationObject {
|
|
249
|
+
type Options = {
|
|
250
|
+
/** Attestation statement. */
|
|
251
|
+
attStmt?: Record<string, unknown> | undefined
|
|
252
|
+
/** Authenticator data as a Hex string (from `Authenticator.getAuthenticatorData`). */
|
|
253
|
+
authData: Hex.Hex
|
|
254
|
+
/** Attestation format. @default 'none' */
|
|
255
|
+
fmt?: string | undefined
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
type ErrorType = Cbor.encode.ErrorType | Errors.GlobalErrorType
|
|
259
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import * as Base64 from '../core/Base64.js'
|
|
2
|
+
import type * as Errors from '../core/Errors.js'
|
|
3
|
+
import type * as Hex from '../core/Hex.js'
|
|
4
|
+
import type { Compute } from '../core/internal/types.js'
|
|
5
|
+
import * as PublicKey from '../core/PublicKey.js'
|
|
6
|
+
import {
|
|
7
|
+
base64UrlOptions,
|
|
8
|
+
bufferSourceToBytes,
|
|
9
|
+
bytesToArrayBuffer,
|
|
10
|
+
responseKeys,
|
|
11
|
+
} from './internal/utils.js'
|
|
12
|
+
import type * as Types from './Types.js'
|
|
13
|
+
|
|
14
|
+
/** A WebAuthn-flavored P256 credential. */
|
|
15
|
+
export type Credential<serialized extends boolean = false> = {
|
|
16
|
+
attestationObject: serialized extends true ? string : ArrayBuffer
|
|
17
|
+
clientDataJSON: serialized extends true ? string : ArrayBuffer
|
|
18
|
+
id: string
|
|
19
|
+
publicKey: serialized extends true ? Hex.Hex : PublicKey.PublicKey
|
|
20
|
+
raw: Types.PublicKeyCredential<serialized>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Metadata for a WebAuthn P256 signature. */
|
|
24
|
+
export type SignMetadata = Compute<{
|
|
25
|
+
authenticatorData: Hex.Hex
|
|
26
|
+
challengeIndex?: number | undefined
|
|
27
|
+
clientDataJSON: string
|
|
28
|
+
typeIndex?: number | undefined
|
|
29
|
+
userVerificationRequired?: boolean | undefined
|
|
30
|
+
}>
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Serializes a credential into a JSON-serializable
|
|
34
|
+
* format.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts twoslash
|
|
38
|
+
* import { Registration, Credential } from 'ox/webauthn'
|
|
39
|
+
*
|
|
40
|
+
* const credential = await Registration.create({ name: 'Example' })
|
|
41
|
+
*
|
|
42
|
+
* const serialized = Credential.serialize(credential) // [!code focus]
|
|
43
|
+
*
|
|
44
|
+
* // `serialized` is JSON-serializable — send it to a server, store it, etc.
|
|
45
|
+
* const json = JSON.stringify(serialized)
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @param credential - The credential to serialize.
|
|
49
|
+
* @returns The serialized credential.
|
|
50
|
+
*/
|
|
51
|
+
export function serialize(credential: Credential): Credential<true> {
|
|
52
|
+
const { attestationObject, clientDataJSON, id, publicKey, raw } = credential
|
|
53
|
+
|
|
54
|
+
const response = {} as Record<string, string>
|
|
55
|
+
for (const key of responseKeys) {
|
|
56
|
+
const value = (raw.response as unknown as Record<string, unknown>)[key]
|
|
57
|
+
if (value instanceof ArrayBuffer)
|
|
58
|
+
response[key] = Base64.fromBytes(new Uint8Array(value), base64UrlOptions)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
attestationObject: Base64.fromBytes(
|
|
63
|
+
new Uint8Array(attestationObject),
|
|
64
|
+
base64UrlOptions,
|
|
65
|
+
),
|
|
66
|
+
clientDataJSON: Base64.fromBytes(
|
|
67
|
+
new Uint8Array(clientDataJSON),
|
|
68
|
+
base64UrlOptions,
|
|
69
|
+
),
|
|
70
|
+
id,
|
|
71
|
+
publicKey: PublicKey.toHex(publicKey),
|
|
72
|
+
raw: {
|
|
73
|
+
id: raw.id,
|
|
74
|
+
type: raw.type,
|
|
75
|
+
authenticatorAttachment: raw.authenticatorAttachment,
|
|
76
|
+
rawId: Base64.fromBytes(bufferSourceToBytes(raw.rawId), base64UrlOptions),
|
|
77
|
+
response: response as unknown as Types.AuthenticatorResponse<true>,
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export declare namespace serialize {
|
|
83
|
+
type ErrorType =
|
|
84
|
+
| Base64.fromBytes.ErrorType
|
|
85
|
+
| PublicKey.toHex.ErrorType
|
|
86
|
+
| Errors.GlobalErrorType
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Deserializes a serialized credential.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts twoslash
|
|
94
|
+
* import { Credential } from 'ox/webauthn'
|
|
95
|
+
*
|
|
96
|
+
* const credential = Credential.deserialize({ // [!code focus]
|
|
97
|
+
* attestationObject: 'o2NmbXRkbm9uZQ...', // [!code focus]
|
|
98
|
+
* clientDataJSON: 'eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIn0', // [!code focus]
|
|
99
|
+
* id: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus]
|
|
100
|
+
* publicKey: '0x04ab891400140fc4f8e941ce0ff90e419de9470acaca613bbd717a4775435031a7d884318e919fd3b3e5a631d866d8a380b44063e70f0c381ee16e0652f7f97554', // [!code focus]
|
|
101
|
+
* raw: { // [!code focus]
|
|
102
|
+
* id: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus]
|
|
103
|
+
* type: 'public-key', // [!code focus]
|
|
104
|
+
* authenticatorAttachment: 'platform', // [!code focus]
|
|
105
|
+
* rawId: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus]
|
|
106
|
+
* response: { // [!code focus]
|
|
107
|
+
* clientDataJSON: 'eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIn0', // [!code focus]
|
|
108
|
+
* }, // [!code focus]
|
|
109
|
+
* }, // [!code focus]
|
|
110
|
+
* }) // [!code focus]
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @param credential - The serialized credential.
|
|
114
|
+
* @returns The deserialized credential.
|
|
115
|
+
*/
|
|
116
|
+
export function deserialize(credential: Credential<true>): Credential {
|
|
117
|
+
const { attestationObject, clientDataJSON, id, publicKey, raw } = credential
|
|
118
|
+
|
|
119
|
+
const response = Object.create(null) as Record<string, ArrayBuffer>
|
|
120
|
+
for (const key of responseKeys) {
|
|
121
|
+
const value = (raw.response as unknown as Record<string, string>)[key]
|
|
122
|
+
if (value) response[key] = bytesToArrayBuffer(Base64.toBytes(value))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
attestationObject: bytesToArrayBuffer(Base64.toBytes(attestationObject)),
|
|
127
|
+
clientDataJSON: bytesToArrayBuffer(Base64.toBytes(clientDataJSON)),
|
|
128
|
+
id,
|
|
129
|
+
publicKey: PublicKey.from(publicKey),
|
|
130
|
+
raw: {
|
|
131
|
+
id: raw.id,
|
|
132
|
+
type: raw.type,
|
|
133
|
+
authenticatorAttachment: raw.authenticatorAttachment,
|
|
134
|
+
rawId: bytesToArrayBuffer(Base64.toBytes(raw.rawId)),
|
|
135
|
+
response: response as unknown as Types.AuthenticatorResponse,
|
|
136
|
+
getClientExtensionResults: () => ({}),
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export declare namespace deserialize {
|
|
142
|
+
type ErrorType =
|
|
143
|
+
| Base64.toBytes.ErrorType
|
|
144
|
+
| PublicKey.from.ErrorType
|
|
145
|
+
| Errors.GlobalErrorType
|
|
146
|
+
}
|