From 3ca39449026fbf1737f7e8732e1d5c3246574066 Mon Sep 17 00:00:00 2001
From: Berend Sliedrecht <sliedrecht@berend.io>
Date: Tue, 26 Nov 2024 22:22:47 +0100
Subject: [PATCH] fix: patch askar to return public bytes

Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>
---
 .../src/crypto/WalletServiceProviderClient.ts | 46 ++++++++++---------
 .../features/onboarding/onboardingContext.tsx |  4 +-
 ...__askar@0.6.0-pr-2102-20241122194225.patch |  3 +-
 3 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/apps/easypid/src/crypto/WalletServiceProviderClient.ts b/apps/easypid/src/crypto/WalletServiceProviderClient.ts
index 78fd435d..ad20de6c 100644
--- a/apps/easypid/src/crypto/WalletServiceProviderClient.ts
+++ b/apps/easypid/src/crypto/WalletServiceProviderClient.ts
@@ -20,32 +20,34 @@ import { deriveKeypairFromPin } from './pin'
 
 // TODO: should auto reset after X seconds
 let __pin: Array<number> | undefined
-export const setWalletServiceProviderPin = async (pin: Array<number>) => {
+export const setWalletServiceProviderPin = async (pin: Array<number>, validatePin = true) => {
   const pinString = pin.join('')
-  const walletKeyVersion = secureWalletKey.getWalletKeyVersion()
-  const walletKey = await secureWalletKey.getWalletKeyUsingPin(pinString, walletKeyVersion)
-  const walletId = `easypid-wallet-${walletKeyVersion}`
-  const agent = new Agent({
-    config: {
-      label: 'pin_test_agent',
-      walletConfig: { id: walletId, key: walletKey, keyDerivationMethod: KeyDerivationMethod.Raw },
-    },
-    modules: {
-      askar: new AskarModule({ ariesAskar }),
-    },
-    dependencies: agentDependencies,
-  })
+  if (validatePin) {
+    const walletKeyVersion = secureWalletKey.getWalletKeyVersion()
+    const walletKey = await secureWalletKey.getWalletKeyUsingPin(pinString, walletKeyVersion)
+    const walletId = `easypid-wallet-${walletKeyVersion}`
+    const agent = new Agent({
+      config: {
+        label: 'pin_test_agent',
+        walletConfig: { id: walletId, key: walletKey, keyDerivationMethod: KeyDerivationMethod.Raw },
+      },
+      modules: {
+        askar: new AskarModule({ ariesAskar }),
+      },
+      dependencies: agentDependencies,
+    })
 
-  try {
-    await agent.initialize()
-  } catch (e) {
-    if (e instanceof WalletInvalidKeyError) {
-      throw new InvalidPinError()
+    try {
+      await agent.initialize()
+    } catch (e) {
+      if (e instanceof WalletInvalidKeyError) {
+        throw new InvalidPinError()
+      }
+      throw e
     }
-    throw e
-  }
 
-  await agent.shutdown()
+    await agent.shutdown()
+  }
   __pin = pin
 }
 
diff --git a/apps/easypid/src/features/onboarding/onboardingContext.tsx b/apps/easypid/src/features/onboarding/onboardingContext.tsx
index f91ac050..b902258f 100644
--- a/apps/easypid/src/features/onboarding/onboardingContext.tsx
+++ b/apps/easypid/src/features/onboarding/onboardingContext.tsx
@@ -247,8 +247,8 @@ export function OnboardingContextProvider({
 
     return secureUnlock
       .setup(walletPin as string)
-      .then(({ walletKey }) => {
-        setWalletServiceProviderPin((walletPin as string).split('').map(Number))
+      .then(async ({ walletKey }) => {
+        await setWalletServiceProviderPin((walletPin as string).split('').map(Number), false)
         return initializeAgent(walletKey)
       })
       .then(goToNextStep)
diff --git a/patches/@credo-ts__askar@0.6.0-pr-2102-20241122194225.patch b/patches/@credo-ts__askar@0.6.0-pr-2102-20241122194225.patch
index 03e2f40b..83a8e4a8 100644
--- a/patches/@credo-ts__askar@0.6.0-pr-2102-20241122194225.patch
+++ b/patches/@credo-ts__askar@0.6.0-pr-2102-20241122194225.patch
@@ -8,8 +8,7 @@ index f4352d639a511347d9a087f4ba5c5afddec477ce..b67794d922ba8112dcca27b8166c2877
                  // Generate a hardware-backed P-256 keypair
 -                secureEnvironment.generateKeypair(kid);
 -                const publicKeyBytes = secureEnvironment.getPublicBytesForKeyId(kid);
-+                await secureEnvironment.generateKeypair(kid);
-+                const publicKeyBytes = await secureEnvironment.getPublicBytesForKeyId(kid);
++                const publicKeyBytes = await secureEnvironment.generateKeypair(kid);
                  const publicKeyBase58 = core_1.TypedArrayEncoder.toBase58(publicKeyBytes);
                  await this.storeSecureEnvironmentKeyById({
                      keyType,