Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederik Rothenberger committed Mar 19, 2024
1 parent f6fdd19 commit ed507a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/flows/recovery/setupRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
IC_DERIVATION_PATH,
} from "$src/utils/iiConnection";
import { unreachable, unreachableLax } from "$src/utils/utils";
import { DerEncodedPublicKey, SignIdentity } from "@dfinity/agent";
import { WebAuthnIdentity } from "$src/utils/WebAuthnIdentityCopy";
import { DerEncodedPublicKey, SignIdentity } from "@dfinity/agent";
import { confirmSeedPhrase } from "./confirmSeedPhrase";
import { displaySeedPhrase } from "./displaySeedPhrase";

Expand Down
73 changes: 42 additions & 31 deletions src/frontend/src/utils/WebAuthnIdentityCopy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
DER_COSE_OID,
DerEncodedPublicKey,
PublicKey,
Signature,
SignIdentity,
wrapDER,
DER_COSE_OID,
Signature,
fromHex,
toHex,
} from '@dfinity/agent';
import borc from 'borc';
import { randomBytes } from '@noble/hashes/utils';
import { bufFromBufLike } from '@dfinity/candid';
wrapDER,
} from "@dfinity/agent";
import { bufFromBufLike } from "@dfinity/candid";
import { randomBytes } from "@noble/hashes/utils";
import borc from "borc";

function _coseToDerEncodedBlob(cose: ArrayBuffer): DerEncodedPublicKey {
return wrapDER(cose, DER_COSE_OID).buffer as DerEncodedPublicKey;
Expand Down Expand Up @@ -68,9 +68,11 @@ export class CosePublicKey implements PublicKey {
* @param challenge The challenge to transform into a byte array. By default a hard
* coded string.
*/
function _createChallengeBuffer(challenge: string | Uint8Array = '<ic0.app>'): Uint8Array {
if (typeof challenge === 'string') {
return Uint8Array.from(challenge, c => c.charCodeAt(0));
function _createChallengeBuffer(
challenge: string | Uint8Array = "<ic0.app>"
): Uint8Array {
if (typeof challenge === "string") {
return Uint8Array.from(challenge, (c) => c.charCodeAt(0));
} else {
return challenge;
}
Expand All @@ -84,27 +86,29 @@ function _createChallengeBuffer(challenge: string | Uint8Array = '<ic0.app>'): U
* @param credentialCreationOptions an optional CredentialCreationOptions object
*/
async function _createCredential(
credentialCreationOptions?: CredentialCreationOptions,
credentialCreationOptions?: CredentialCreationOptions
): Promise<PublicKeyCredentialWithAttachment | null> {
const creds = (await navigator.credentials.create(
credentialCreationOptions ?? {
publicKey: {
authenticatorSelection: {
userVerification: 'preferred',
userVerification: "preferred",
},
attestation: 'direct',
attestation: "direct",
challenge: _createChallengeBuffer(),
pubKeyCredParams: [{ type: 'public-key', alg: PubKeyCoseAlgo.ECDSA_WITH_SHA256 }],
pubKeyCredParams: [
{ type: "public-key", alg: PubKeyCoseAlgo.ECDSA_WITH_SHA256 },
],
rp: {
name: 'Internet Identity Service',
name: "Internet Identity Service",
},
user: {
id: randomBytes(16),
name: 'Internet Identity',
displayName: 'Internet Identity',
name: "Internet Identity",
displayName: "Internet Identity",
},
},
},
}
)) as PublicKeyCredentialWithAttachment | null;

if (creds === null) {
Expand Down Expand Up @@ -136,8 +140,8 @@ export class WebAuthnIdentity extends SignIdentity {
public static fromJSON(json: string): WebAuthnIdentity {
const { publicKey, rawId } = JSON.parse(json);

if (typeof publicKey !== 'string' || typeof rawId !== 'string') {
throw new Error('Invalid JSON string.');
if (typeof publicKey !== "string" || typeof rawId !== "string") {
throw new Error("Invalid JSON string.");
}

return new this(fromHex(rawId), fromHex(publicKey), undefined);
Expand All @@ -148,26 +152,33 @@ export class WebAuthnIdentity extends SignIdentity {
* @param credentialCreationOptions an optional CredentialCreationOptions Challenge
*/
public static async create(
credentialCreationOptions?: CredentialCreationOptions,
credentialCreationOptions?: CredentialCreationOptions
): Promise<WebAuthnIdentity> {
const creds = await _createCredential(credentialCreationOptions);

if (!creds || creds.type !== 'public-key') {
throw new Error('Could not create credentials. Error: ' + creds + ' creds JSON: ' + JSON.stringify(creds));
if (!creds || creds.type !== "public-key") {
throw new Error(
"Could not create credentials. Error: " +
creds +
" creds JSON: " +
JSON.stringify(creds)
);
}

const response = creds.response as AuthenticatorAttestationResponse;
if (response.attestationObject === undefined) {
throw new Error('Was expecting an attestation response.');
throw new Error("Was expecting an attestation response.");
}

// Parse the attestationObject as CBOR.
const attObject = borc.decodeFirst(new Uint8Array(response.attestationObject));
const attObject = borc.decodeFirst(
new Uint8Array(response.attestationObject)
);

return new this(
creds.rawId,
_authDataToCose(attObject.authData),
creds.authenticatorAttachment ?? undefined,
creds.authenticatorAttachment ?? undefined
);
}

Expand All @@ -176,7 +187,7 @@ export class WebAuthnIdentity extends SignIdentity {
public constructor(
public readonly rawId: ArrayBuffer,
cose: ArrayBuffer,
protected authenticatorAttachment: AuthenticatorAttachment | undefined,
protected authenticatorAttachment: AuthenticatorAttachment | undefined
) {
super();
this._publicKey = new CosePublicKey(cose);
Expand All @@ -203,12 +214,12 @@ export class WebAuthnIdentity extends SignIdentity {
publicKey: {
allowCredentials: [
{
type: 'public-key',
type: "public-key",
id: this.rawId,
},
],
challenge: blob,
userVerification: 'preferred',
userVerification: "preferred",
},
})) as PublicKeyCredentialWithAttachment;

Expand All @@ -223,10 +234,10 @@ export class WebAuthnIdentity extends SignIdentity {
authenticator_data: new Uint8Array(response.authenticatorData),
client_data_json: new TextDecoder().decode(response.clientDataJSON),
signature: new Uint8Array(response.signature),
}),
})
);
if (!cbor) {

Check failure on line 239 in src/frontend/src/utils/WebAuthnIdentityCopy.ts

View workflow job for this annotation

GitHub Actions / frontend-checks

Unexpected any value in conditional. An explicit comparison or type cast is required
throw new Error('failed to encode cbor');
throw new Error("failed to encode cbor");
}
return cbor.buffer as Signature;
}
Expand Down

0 comments on commit ed507a0

Please sign in to comment.