From 520999496cbd7027f4fff977e0172842c10881b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Landabaso=20D=C3=ADaz?= Date: Thu, 20 Jul 2023 08:07:55 +0200 Subject: [PATCH] Fix issue with key replacement in address derivation Revised the key replacement logic in the `validateAddress` function to prevent misinterpretation of key indices. The loop now iterates in reverse order to avoid scenarios where, for example, @10 is mistakenly replaced as @1, leaving an extra 0. This change ensures that the correct keys are replaced when deriving the wallet address. --- bitcoin_client_js/src/lib/appClient.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bitcoin_client_js/src/lib/appClient.ts b/bitcoin_client_js/src/lib/appClient.ts index 763d9bbef..e4d52ce23 100644 --- a/bitcoin_client_js/src/lib/appClient.ts +++ b/bitcoin_client_js/src/lib/appClient.ts @@ -441,8 +441,9 @@ export class AppClient { } // Replace index: expression = expression.replace(/\/\*/g, `/${addressIndex}`); - // Replace origin: - for (let i = 0; i < walletPolicy.keys.length; i++) + // Replace origin in reverse order to prevent + // misreplacements, e.g., @10 being mistaken for @1 and leaving a 0. + for (let i = walletPolicy.keys.length - 1; i >= 0; i--) expression = expression.replace( new RegExp(`@${i}`, 'g'), walletPolicy.keys[i]