From 4dc2048d1c6e69bf4b8f16d6cec4827737406ab1 Mon Sep 17 00:00:00 2001 From: imsk17 Date: Mon, 28 Oct 2024 12:13:44 +0530 Subject: [PATCH] fix(scrt::add_self): fetch signatures for pubkey and use correct encoding format --- .../secrets/utils/addSelfAsValidator.ts | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/handler/secrets/utils/addSelfAsValidator.ts b/src/handler/secrets/utils/addSelfAsValidator.ts index b13f23a..ffff321 100644 --- a/src/handler/secrets/utils/addSelfAsValidator.ts +++ b/src/handler/secrets/utils/addSelfAsValidator.ts @@ -1,5 +1,8 @@ import { type Wallet, pubkeyToAddress } from "secretjs"; -import { encodeSecp256k1Pubkey } from "secretjs/dist/wallet_amino"; +import { + encodeSecp256k1Pubkey, + encodeSecp256k1Signature, +} from "secretjs/dist/wallet_amino"; import type { BridgeStorage, ERC20Staking } from "../../../contractsTypes/evm"; import type { AddValidatorType } from "../../../contractsTypes/secret/secretBridge"; import type { LogInstance } from "../../types"; @@ -50,9 +53,10 @@ export default async function addSelfAsValidator( ); return res.validator_count_response.count; } - const newV = Buffer.from(publicKey).toString("base64"); let validatorsCount = await getStakingSignatureCount(); - let signatureCount = Number(await storage.getStakingSignaturesCount(newV)); + let signatureCount = Number( + await storage.getStakingSignaturesCount(publicKey), + ); while (signatureCount < confirmationCountNeeded(validatorsCount)) { await waitForMSWithMsg( @@ -61,10 +65,12 @@ export default async function addSelfAsValidator( validatorsCount, )}`, ); - signatureCount = Number(await storage.getStakingSignaturesCount(newV)); + signatureCount = Number( + await storage.getStakingSignaturesCount(publicKey), + ); validatorsCount = await getStakingSignatureCount(); } - const signatures = [...(await storage.getStakingSignatures(newV))].map( + const signatures = [...(await storage.getStakingSignatures(publicKey))].map( (item) => { return { signerAddress: item.signerAddress, @@ -73,7 +79,7 @@ export default async function addSelfAsValidator( }, ); - const validatorToAddPublicKeyUint8 = Buffer.from(wallet.address, "hex"); + const validatorToAddPublicKeyUint8 = Buffer.from(publicKey, "hex"); const msg: AddValidatorType = { add_validator: { data: { @@ -83,17 +89,19 @@ export default async function addSelfAsValidator( ], signatures: signatures.map((item) => { return { - signature: Buffer.from( - item.signature.replace("0x", ""), - "hex", - ).toString("base64"), - signer_address: item.signerAddress, + signature: encodeSecp256k1Signature( + Buffer.from(item.signerAddress, "hex"), + Buffer.from(item.signature.replace("0x", ""), "hex"), + ).signature, + signer_address: encodeSecp256k1Pubkey( + Buffer.from(item.signerAddress, "hex"), + ).value, }; }), }, }, }; - await useMutexAndRelease( + const add = await useMutexAndRelease( fetchProvider, async (client) => await client.tx.compute.executeContract( @@ -108,6 +116,10 @@ export default async function addSelfAsValidator( }, ), ); + logger.info( + `Added self as chain at hash: ${add.transactionHash}. TX:`, + add, + ); return "success"; } catch (e) { logger.error("Failed to add self as validator: ", e);