-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement add custom sudo key for parachain (#204)
* fix: fix issue for robonomics mainnet relay not showing blocks * fix: fix issue litentry parachain testnet not showing blocks * fix: kilt issue and acala karura testnet issue * fix: acala, karura testnet issue * chore: change base for manta parachain * feat: implement add custom sudo key for parachain * refactor: change insert_keys function, from js script to curl command * chore: code clean in parachain.star * refactor: change insertkey function from curl to js script * chore: updated the config.json * refactor: refactor the insert_keys function parameter * refactor: refactor config.json, change sudo_key key position * feat: add functionality to add sepate key wallet for each node * refactor: refactor code in parachain.star, nodes can start with and without keys * refactor: update script to update parachain spec * fix: fix issue if sudo_key, key is not provided in config then it throw error * fix: issue with sudo key while without registratioon is true --------- Co-authored-by: Shanith K K <[email protected]>
- Loading branch information
Showing
6 changed files
with
205 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const fs = require("fs"); | ||
const { Keyring } = require('@polkadot/keyring'); | ||
const { encodeAddress, cryptoWaitReady } = require('@polkadot/util-crypto'); | ||
|
||
async function updateParachainSpec(paraSpecFile, paraId, newSudoKeyPhrase, initialCollatorsPhrase) { | ||
try { | ||
const rawdata = fs.readFileSync(paraSpecFile); | ||
const chainSpec = JSON.parse(rawdata); | ||
const collators = JSON.parse(initialCollatorsPhrase); | ||
|
||
chainSpec.para_id = paraId; | ||
chainSpec.genesis.runtime.parachainInfo.parachainId = paraId; | ||
|
||
|
||
await cryptoWaitReady(); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
|
||
let newSudoKey = ''; // Declaring newSudoKey as let instead of const | ||
|
||
if (newSudoKeyPhrase.length > 0) { | ||
const newSudoAccount = keyring.addFromUri(newSudoKeyPhrase); | ||
newSudoKey = newSudoAccount.address; | ||
chainSpec.genesis.runtime.sudo.key = newSudoKey; | ||
|
||
const SESSION_KEYS = [ | ||
newSudoKey, | ||
newSudoKey, | ||
{ aura: newSudoKey } | ||
]; | ||
chainSpec.genesis.runtime.session.keys.push(SESSION_KEYS); | ||
|
||
const BALANCE = [ | ||
newSudoKey, | ||
chainSpec.genesis.runtime.balances.balances[0][1] | ||
]; | ||
chainSpec.genesis.runtime.balances.balances.push(BALANCE); | ||
console.log("changed sudo key:", newSudoKey); | ||
} | ||
|
||
if (initialCollatorsPhrase.length > 0) { | ||
collators.forEach(collatorPhrase => { | ||
var collatorAccount = keyring.addFromUri(collatorPhrase); | ||
var collator = collatorAccount.address; | ||
//adding this condition to prevent adding duplicate keys | ||
if(collatorPhrase!=newSudoKeyPhrase){ | ||
console.log("updating collator:", collator); | ||
const sessionKey = [ | ||
collator, | ||
collator, | ||
{ aura: collator } | ||
]; | ||
chainSpec.genesis.runtime.session.keys.push(sessionKey); | ||
|
||
const balance = [ | ||
collator, | ||
chainSpec.genesis.runtime.balances.balances[0][1] | ||
]; | ||
chainSpec.genesis.runtime.balances.balances.push(balance); | ||
|
||
chainSpec.genesis.runtime.collatorSelection.invulnerables.push(collator); | ||
} else{ | ||
chainSpec.genesis.runtime.collatorSelection.invulnerables.push(collator); | ||
} | ||
}); | ||
} | ||
|
||
fs.writeFileSync(paraSpecFile, JSON.stringify(chainSpec, null, 2)); | ||
|
||
console.log("✓ Updated sudo key and session keys in parachain spec"); | ||
} catch (error) { | ||
console.error("Error updating parachain spec:", error.message); | ||
} | ||
} | ||
|
||
const paraSpecFile = process.argv[2]; | ||
const paraId = parseInt(process.argv[3], 10) || 2000; | ||
const newSudoKey = process.argv[4]; | ||
const initialCollators = process.argv[5]; | ||
updateParachainSpec(paraSpecFile, paraId, newSudoKey, initialCollators); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api'); | ||
const { encodeAddress } = require('@polkadot/util-crypto'); | ||
const { createType } = require('@polkadot/types'); | ||
const { u8aToHex } =require("@polkadot/util") | ||
const { decodeAddress } = require("@polkadot/util-crypto") | ||
|
||
|
||
|
||
async function insertKey(keyType, mnemonicOrPrivateKey, providerUrl) { | ||
try { | ||
const wsProvider = new WsProvider(providerUrl); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
const account = keyring.addFromMnemonic(mnemonicOrPrivateKey); | ||
const result = await api.rpc.author.insertKey(keyType, mnemonicOrPrivateKey, u8aToHex(decodeAddress(account.address))); | ||
console.log(`Account ${account.address} inserted successfully!`); | ||
return result; | ||
} catch (error) { | ||
console.error('Error inserting account:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
const keyType = process.argv[2]; | ||
const mnemonicOrPrivateKey = process.argv[3]; | ||
const providerUrl = process.argv[4]; | ||
|
||
(async () => { | ||
try { | ||
await insertKey(keyType, mnemonicOrPrivateKey, providerUrl); | ||
console.log('Script executed successfully!'); | ||
process.exit(0); | ||
} catch (error) { | ||
console.error('Error executing script:', error); | ||
process.exit(1); | ||
} | ||
})(); |