-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,131 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Only way to silence PolkadotJS API warnings we don't want | ||
console.warn = () => {}; | ||
|
||
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api'; | ||
import { mnemonicGenerate } from '@polkadot/util-crypto'; | ||
import { u8aToHex, u8aWrapBytes } from '@polkadot/util'; | ||
|
||
export async function getApi() { | ||
const api = await ApiPromise.create({ | ||
// Using mainnet as: 1. Nothing here would use local only metadata, 2. We aren't actually submitting. | ||
provider: new WsProvider('wss://0.rpc.frequency.xyz'), | ||
}); | ||
await api.isReady; | ||
return api; | ||
} | ||
|
||
export function createKey() { | ||
const mnemonic = mnemonicGenerate(); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
const keypair = keyring.createFromUri(mnemonic); | ||
return keypair; | ||
} | ||
|
||
export function signPayloadSr25519(keys, data) { | ||
return { Sr25519: u8aToHex(keys.sign(u8aWrapBytes(data.toU8a()))) }; | ||
} |
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,69 @@ | ||
import fs from 'fs/promises'; | ||
import { Bytes } from '@polkadot/types'; | ||
import { getApi, createKey, signPayloadSr25519 } from './helpers/chain.mjs'; | ||
|
||
const GENERATE_COUNT = 10; | ||
|
||
const signup = (api) => { | ||
const keypair = createKey(); | ||
const expiration = 100; // Will this fail when running against longer chains? | ||
|
||
const addProviderData = api.registry.createType('PalletMsaAddProvider', { | ||
authorizedMsaId: 1, | ||
schemaIds: [1], | ||
expiration, | ||
}); | ||
const createTx = api.tx.msa.createSponsoredAccountWithDelegation( | ||
keypair.address, | ||
signPayloadSr25519(keypair, addProviderData), | ||
addProviderData, | ||
); | ||
const claimHandlePayload = api.registry.createType('CommonPrimitivesHandlesClaimHandlePayload', { | ||
baseHandle: new Bytes(api.registry, keypair.address.substring(0, 8)), | ||
expiration, | ||
}); | ||
const claimTx = api.tx.handles.claimHandle( | ||
keypair.address, | ||
signPayloadSr25519(keypair, claimHandlePayload), | ||
claimHandlePayload, | ||
); | ||
|
||
return { | ||
signUp: { | ||
extrinsics: [ | ||
{ | ||
pallet: 'msa', | ||
extrinsicName: 'createSponsoredAccountWithDelegation', | ||
encodedExtrinsic: createTx.toHex(), | ||
}, | ||
{ | ||
pallet: 'handles', | ||
extrinsicName: 'claimHandle', | ||
encodedExtrinsic: claimTx.toHex(), | ||
}, | ||
], | ||
}, | ||
}; | ||
}; | ||
|
||
const main = async () => { | ||
const api = await getApi(); | ||
const generated = []; | ||
for (let i = 0; i < GENERATE_COUNT; i++) { | ||
generated.push(signup(api)); | ||
} | ||
// Write the generated array to './signups.gen.js' | ||
const fileContent = ` | ||
// GENERATED FILE! Regenerate using npm run generate:signups | ||
export const signups = ${JSON.stringify(generated, null, 2)}; | ||
`; | ||
|
||
try { | ||
await fs.writeFile('./signups.gen.js', fileContent, 'utf8'); | ||
console.log('Successfully wrote to ./signups.gen.js'); | ||
} catch (err) { | ||
console.error('Error writing to file:', err); | ||
} | ||
}; | ||
|
||
main().catch(console.error).finally(process.exit); |
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
Oops, something went wrong.