Skip to content

Commit

Permalink
feat(aptos::add_validator): modify as per contract
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Oct 30, 2024
1 parent 65f3b82 commit 1383d71
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const bridgeTestChains = [
chain: "APTOS",
chainType: "aptos",
contractAddress:
"0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9",
"0x68c508b1b20701c8ddecbbc8e603e788be96c1dcb6ccdcf43ac8594f6f49077a",
decimals: 8,
intialFund: "100000000",
lastBlock: 0,
Expand Down
45 changes: 35 additions & 10 deletions src/handler/aptos/abi/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const ABI = (address: string) => {
return {
address: address,
address,
name: "aptos_nft_bridge",
friends: [],
exposed_functions: [
Expand All @@ -10,7 +10,12 @@ export const ABI = (address: string) => {
is_entry: true,
is_view: false,
generic_type_params: [],
params: ["vector<u8>", "vector<vector<u8>>", "vector<vector<u8>>"],
params: [
"vector<u8>",
"address",
"vector<vector<u8>>",
"vector<vector<u8>>",
],
return: [],
},
{
Expand Down Expand Up @@ -83,7 +88,7 @@ export const ABI = (address: string) => {
is_entry: true,
is_view: false,
generic_type_params: [],
params: ["signer", "address", "vector<u8>"],
params: ["signer", "vector<u8>"],
return: [],
},
{
Expand All @@ -101,7 +106,13 @@ export const ABI = (address: string) => {
is_entry: true,
is_view: false,
generic_type_params: [],
params: ["&signer", "vector<vector<u8>>", "vector<u8>", "vector<u8>"],
params: [
"&signer",
"vector<vector<u8>>",
"vector<address>",
"vector<u8>",
"vector<u8>",
],
return: [],
},
{
Expand Down Expand Up @@ -191,25 +202,25 @@ export const ABI = (address: string) => {
fields: [
{
name: "validators",
type: "0x1::simple_map::SimpleMap<vector<u8>, 0x13f8d626e383e8621a89caeb05c56a95fda38aa2dddfa8c2b1ed063f0edb23c9::aptos_nft_bridge::Validator>",
type: "0x1::simple_map::SimpleMap<vector<u8>, 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",
Expand Down Expand Up @@ -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<u8>" },
{ name: "addr", type: "address" },
],
},
],
} as const;
Expand Down
25 changes: 15 additions & 10 deletions src/handler/aptos/utils/addSelfAsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ 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();
logger.info(
`Added self as new chain at hash: ${receipt?.hash}. BN: ${receipt?.blockNumber}`,
);
}
const publicKey = identity.publicKey.bcsToHex().toString();
try {
async function getStakingSignatureCount() {
return Number(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions src/handler/aptos/utils/signData.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 1383d71

Please sign in to comment.