Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Update mainchain registration example #9113

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions examples/interop/common/mainchain_registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
registrationSignatureMessageSchema,
mainchainRegParams as mainchainRegParamsSchema,
MESSAGE_TAG_CHAIN_REG,
MODULE_NAME_INTEROPERABILITY,
} from 'lisk-framework';
import { COMMAND_NAME_MAINCHAIN_REG } from 'lisk-framework/dist-node/modules/interoperability/constants';
} from 'lisk-sdk';

export const registerMainchain = async (
num: string,
Expand All @@ -17,6 +15,7 @@ export const registerMainchain = async (
const mainchainClient = await apiClient.createIPCClient(`~/.lisk/mainchain-node-${num}`);
const sidechainClient = await apiClient.createIPCClient(`~/.lisk/pos-sidechain-example-${num}`);

// Get node info from sidechain and mainchain
const mainchainNodeInfo = await mainchainClient.invoke('system_getNodeInfo');
const sidechainNodeInfo = await sidechainClient.invoke('system_getNodeInfo');

Expand Down Expand Up @@ -55,25 +54,29 @@ export const registerMainchain = async (
{ height: sidechainNodeInfo.height },
);

const activeValidatorsWithPrivateKey: { blsPublicKey: Buffer; blsPrivateKey: Buffer }[] = [];
// Add validator private keys to the sidechain validator list
const activeValidatorsBLSKeys: { blsPublicKey: Buffer; blsPrivateKey: Buffer }[] = [];
for (const v of sidechainActiveValidators as { blsKey: string; bftWeight: string }[]) {
const validatorInfo = sidechainValidatorsKeys.find(
configValidator => configValidator.plain.blsKey === v.blsKey,
);
if (validatorInfo) {
activeValidatorsWithPrivateKey.push({
activeValidatorsBLSKeys.push({
blsPublicKey: Buffer.from(v.blsKey, 'hex'),
blsPrivateKey: Buffer.from(validatorInfo.plain.blsPrivateKey, 'hex'),
});
}
}
console.log('Total activeValidatorsWithPrivateKey:', activeValidatorsWithPrivateKey.length);
// Sort active validators from sidechainChain
activeValidatorsWithPrivateKey.sort((a, b) => a.blsPublicKey.compare(b.blsPublicKey));

console.log('Total activeValidatorsBLSKeys:', activeValidatorsBLSKeys.length);

// Sort active validators from sidechain lexicographically after their BLS public key
activeValidatorsBLSKeys.sort((a, b) => a.blsPublicKey.compare(b.blsPublicKey));

const sidechainValidatorsSignatures: { publicKey: Buffer; signature: Buffer }[] = [];
// Sign with each active validator
for (const validator of activeValidatorsWithPrivateKey) {

// Sign parameters with each active sidechain validator
for (const validator of activeValidatorsBLSKeys) {
const signature = bls.signData(
MESSAGE_TAG_CHAIN_REG,
params.ownChainID,
Expand All @@ -83,7 +86,7 @@ export const registerMainchain = async (
sidechainValidatorsSignatures.push({ publicKey: validator.blsPublicKey, signature });
}

const publicKeysList = activeValidatorsWithPrivateKey.map(v => v.blsPublicKey);
const publicKeysList = activeValidatorsBLSKeys.map(v => v.blsPublicKey);
console.log('Total active sidechain validators:', sidechainValidatorsSignatures.length);

// Create an aggregated signature
Expand All @@ -102,8 +105,8 @@ export const registerMainchain = async (
aggregationBits: aggregationBits.toString('hex'),
};
const tx = new Transaction({
module: MODULE_NAME_INTEROPERABILITY,
command: COMMAND_NAME_MAINCHAIN_REG,
module: 'interoperability',
command: 'registerMainchain',
fee: BigInt(2000000000),
params: codec.encodeJSON(mainchainRegParamsSchema, mainchainRegParams),
nonce: BigInt(nonce),
Expand Down