Skip to content

Commit

Permalink
fix(scrt::add_self): fetch signatures for pubkey and use correct enco…
Browse files Browse the repository at this point in the history
…ding format
  • Loading branch information
imsk17 committed Oct 28, 2024
1 parent e3ad005 commit 4dc2048
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/handler/secrets/utils/addSelfAsValidator.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -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(
Expand All @@ -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);
Expand Down

0 comments on commit 4dc2048

Please sign in to comment.