From 1383d71a7204763129de9d83f02ca2f12d7f531f Mon Sep 17 00:00:00 2001 From: imsk17 Date: Wed, 30 Oct 2024 14:30:36 +0530 Subject: [PATCH] feat(aptos::add_validator): modify as per contract --- src/config.ts | 2 +- src/handler/aptos/abi/bridge.ts | 45 ++++++++++++++----- src/handler/aptos/utils/addSelfAsValidator.ts | 25 ++++++----- src/handler/aptos/utils/signData.ts | 11 +++-- 4 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/config.ts b/src/config.ts index 007af21..464d411 100644 --- a/src/config.ts +++ b/src/config.ts @@ -132,7 +132,7 @@ export const bridgeTestChains = [ chain: "APTOS", chainType: "aptos", contractAddress: - "0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9", + "0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a", decimals: 8, intialFund: "100000000", lastBlock: 0, diff --git a/src/handler/aptos/abi/bridge.ts b/src/handler/aptos/abi/bridge.ts index 3fd5d2e..305dde7 100644 --- a/src/handler/aptos/abi/bridge.ts +++ b/src/handler/aptos/abi/bridge.ts @@ -1,6 +1,6 @@ export const ABI = (address: string) => { return { - address: address, + address, name: "aptos_nft_bridge", friends: [], exposed_functions: [ @@ -10,7 +10,12 @@ export const ABI = (address: string) => { is_entry: true, is_view: false, generic_type_params: [], - params: ["vector", "vector>", "vector>"], + params: [ + "vector", + "address", + "vector>", + "vector>", + ], return: [], }, { @@ -83,7 +88,7 @@ export const ABI = (address: string) => { is_entry: true, is_view: false, generic_type_params: [], - params: ["signer", "address", "vector"], + params: ["signer", "vector"], return: [], }, { @@ -101,7 +106,13 @@ export const ABI = (address: string) => { is_entry: true, is_view: false, generic_type_params: [], - params: ["&signer", "vector>", "vector", "vector"], + params: [ + "&signer", + "vector>", + "vector
", + "vector", + "vector", + ], return: [], }, { @@ -191,25 +202,25 @@ export const ABI = (address: string) => { fields: [ { name: "validators", - type: "0x1::simple_map::SimpleMap, 0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::Validator>", + type: "0x1::simple_map::SimpleMap, 0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::Validator>", }, { name: "signer_cap", type: "0x1::account::SignerCapability" }, { name: "collection_objects", - type: "0x1::table::Table<0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::CollectionObject, u256>", + type: "0x1::table::Table<0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::CollectionObject, u256>", }, { name: "nfts_counter", type: "u64" }, { name: "original_to_duplicate_mapping", - type: "0x1::table::Table<0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::OriginalToDuplicateKey, 0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::OriginalToDuplicateInfo>", + type: "0x1::table::Table<0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::OriginalToDuplicateKey, 0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::OriginalToDuplicateInfo>", }, { name: "duplicate_to_original_mapping", - type: "0x1::table::Table<0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::DuplicateToOriginalKey, 0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::DuplicateToOriginalInfo>", + type: "0x1::table::Table<0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::DuplicateToOriginalKey, 0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::DuplicateToOriginalInfo>", }, { name: "nft_collection_tokens", - type: "0x1::simple_map::SimpleMap<0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::CollectionNftObject, address>", + type: "0x1::simple_map::SimpleMap<0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a::aptos_nft_bridge::CollectionNftObject, address>", }, { name: "nft_collections_counter", @@ -412,7 +423,21 @@ export const ABI = (address: string) => { is_event: false, abilities: ["copy", "drop", "store"], generic_type_params: [], - fields: [{ name: "pending_reward", type: "u64" }], + fields: [ + { name: "pending_reward", type: "u64" }, + { name: "addr", type: "address" }, + ], + }, + { + name: "ValidatorWithAddress", + is_native: false, + is_event: false, + abilities: ["copy", "drop"], + generic_type_params: [], + fields: [ + { name: "public_key", type: "vector" }, + { name: "addr", type: "address" }, + ], }, ], } as const; diff --git a/src/handler/aptos/utils/addSelfAsValidator.ts b/src/handler/aptos/utils/addSelfAsValidator.ts index b4e2165..e5d9fc0 100644 --- a/src/handler/aptos/utils/addSelfAsValidator.ts +++ b/src/handler/aptos/utils/addSelfAsValidator.ts @@ -18,11 +18,16 @@ export default async function addSelfAsValidator( validatorAddress: string, ): Promise<"success" | "failure"> { const stakedAmt = await staking.stakingBalances(validatorAddress); + const pubKeyHex = Buffer.from(identity.publicKey.toUint8Array()).toString( + "hex", + ); + const address = identity.accountAddress.toString(); + const vad = `${pubKeyHex}|${address}`; if (stakedAmt > 0n) { const add = await staking.addNewChains([ { chainType: "aptos", - validatorAddress: identity.publicKey.bcsToHex().toString(), + validatorAddress: vad, }, ]); const receipt = await add.wait(); @@ -30,7 +35,6 @@ export default async function addSelfAsValidator( `Added self as new chain at hash: ${receipt?.hash}. BN: ${receipt?.blockNumber}`, ); } - const publicKey = identity.publicKey.bcsToHex().toString(); try { async function getStakingSignatureCount() { return Number( @@ -44,9 +48,8 @@ export default async function addSelfAsValidator( ), ); } - const newV = publicKey; let validatorsCount = await getStakingSignatureCount(); - let signatureCount = Number(await storage.getStakingSignaturesCount(newV)); + let signatureCount = Number(await storage.getStakingSignaturesCount(vad)); while (signatureCount < confirmationCountNeeded(validatorsCount)) { await waitForMSWithMsg( @@ -55,10 +58,10 @@ export default async function addSelfAsValidator( validatorsCount, )}`, ); - signatureCount = Number(await storage.getStakingSignaturesCount(newV)); + signatureCount = Number(await storage.getStakingSignaturesCount(vad)); validatorsCount = await getStakingSignatureCount(); } - const signatures = [...(await storage.getStakingSignatures(newV))].map( + const signatures = [...(await storage.getStakingSignatures(vad))].map( (item) => { return { signerAddress: item.signerAddress, @@ -74,23 +77,25 @@ export default async function addSelfAsValidator( }; }), ); - await useMutexAndRelease( + const added = await useMutexAndRelease( fetchBridge, async (bridge) => await bridge.entry.add_validator({ account: identity, functionArguments: [ - identity.publicKey.bcsToHex().toString(), + identity.publicKey.toUint8Array(), + identity.accountAddress.toString(), signatures.map((e) => { - return Buffer.from(e.signature.slice(2)); + return Buffer.from(e.signature.slice(2), "hex"); }), signatures.map((e) => { - return Buffer.from(e.signerAddress); + return Buffer.from(e.signerAddress, "hex"); }), ], typeArguments: [], }), ); + logger.info(`Added self as validator at hash: ${added.hash}`); return "success"; } catch (e) { logger.error("Failed to add self as validator: ", e); diff --git a/src/handler/aptos/utils/signData.ts b/src/handler/aptos/utils/signData.ts index 77ea711..b8392eb 100644 --- a/src/handler/aptos/utils/signData.ts +++ b/src/handler/aptos/utils/signData.ts @@ -1,9 +1,14 @@ -import type { Ed25519Account } from "@aptos-labs/ts-sdk"; +import { type Ed25519Account, Serializer } from "@aptos-labs/ts-sdk"; import * as ed from "@noble/ed25519"; -import { sha256 } from "@noble/hashes/sha256"; +import { sha256 } from "@noble/hashes/sha2"; export default async function signData(buf: string, signer: Ed25519Account) { - const hashed = sha256(buf); + const [pubKeyHex, address] = buf.split("|"); + const serializer = new Serializer(); + + serializer.serializeBytes(Buffer.from(pubKeyHex, "hex")); + serializer.serializeFixedBytes(Buffer.from(address.slice(2), "hex")); + const hashed = sha256(serializer.toUint8Array()); const signed = await ed.sign(hashed, signer.privateKey.toUint8Array()); const signature = `0x${Buffer.from(signed).toString("hex")}`; return {