@@ -20,26 +20,29 @@ export async function generateKeyFromSeed(seed: string): Promise<any> {
2020 return sr_keyring . createFromUri ( `//${ seed } ` ) ;
2121}
2222
23- export async function generateKeyForNode ( nodeName ?: string ) : Promise < any > {
23+ export async function generateKeyForNode (
24+ nodeName ?: string ,
25+ keyMap ?: KeyTypesMap ,
26+ ) : Promise < any > {
2427 await cryptoWaitReady ( ) ;
2528
2629 const mnemonic = mnemonicGenerate ( ) ;
2730 const seed = nodeName
2831 ? `//${ nameCase ( nodeName ) } `
2932 : u8aToHex ( mnemonicToMiniSecret ( mnemonic ) ) ;
3033
34+ // Create keyrings
3135 const sr_keyring = new Keyring ( { type : "sr25519" } ) ;
36+ const ed_keyring = new Keyring ( { type : "ed25519" } ) ;
37+ const ec_keyring = new Keyring ( { type : "ecdsa" } ) ;
38+
3239 const sr_account = sr_keyring . createFromUri ( `${ seed } ` ) ;
3340 const sr_stash = sr_keyring . createFromUri ( `${ seed } //stash` ) ;
34-
35- const ed_keyring = new Keyring ( { type : "ed25519" } ) ;
3641 const ed_account = ed_keyring . createFromUri ( `${ seed } ` ) ;
37-
38- const ec_keyring = new Keyring ( { type : "ecdsa" } ) ;
3942 const ec_account = ec_keyring . createFromUri ( `${ seed } ` ) ;
4043
41- // return the needed info
42- return {
44+ // create the base info
45+ const keysForNode : any = {
4346 seed,
4447 mnemonic,
4548 sr_account : {
@@ -58,72 +61,92 @@ export async function generateKeyForNode(nodeName?: string): Promise<any> {
5861 publicKey : u8aToHex ( ec_account . publicKey ) ,
5962 } ,
6063 } ;
64+
65+ // and customize
66+ if ( keyMap ) {
67+ for ( const [ key , schema ] of Object . entries ( keyMap ) ) {
68+ const key_seed = `${ seed } //${ key } ` ;
69+ const acc =
70+ schema == "ec"
71+ ? ec_keyring . createFromUri ( `${ key_seed } ` )
72+ : schema == "ed"
73+ ? ed_keyring . createFromUri ( `${ key_seed } ` )
74+ : sr_keyring . createFromUri ( `${ key_seed } ` ) ;
75+
76+ keysForNode [ key ] = {
77+ address : acc . address ,
78+ publicKey : u8aToHex ( acc . publicKey ) ,
79+ seed : key_seed ,
80+ schema,
81+ } ;
82+ }
83+ }
84+
85+ console . log ( "keysForNode" , keysForNode ) ;
86+ return keysForNode ;
6187}
6288
63- export async function generateKeystoreFiles (
64- node : Node ,
65- path : string ,
66- isAssetHubPolkadot = false ,
67- ) : Promise < string [ ] > {
68- const keystoreDir = `${ path } /keystore` ;
69- await makeDir ( keystoreDir ) ;
89+ export interface DefaultKeystoreKeyTypes {
90+ [ key : string ] : string ;
91+ }
7092
71- const paths : string [ ] = [ ] ;
93+ // map short name with key schema (e.g "aura" -> "ed")
94+ export interface KeyTypesMap {
95+ [ key : string ] : string ;
96+ }
7297
73- interface DefaultKeystoreKeyTypes {
74- [ key : string ] : string ;
75- }
76- let keystore_key_types : DefaultKeystoreKeyTypes = { } ;
77-
78- const default_keystore_key_types : DefaultKeystoreKeyTypes = {
79- aura : isAssetHubPolkadot
80- ? node . accounts . ed_account . publicKey
81- : node . accounts . sr_account . publicKey ,
82- babe : node . accounts . sr_account . publicKey ,
83- imon : node . accounts . sr_account . publicKey ,
84- gran : node . accounts . ed_account . publicKey ,
85- audi : node . accounts . sr_account . publicKey ,
86- asgn : node . accounts . sr_account . publicKey ,
87- para : node . accounts . sr_account . publicKey ,
88- beef : node . accounts . ec_account . publicKey ,
89- nmbs : node . accounts . sr_account . publicKey , // Nimbus
90- rand : node . accounts . sr_account . publicKey , // Randomness (Moonbeam)
91- rate : node . accounts . ed_account . publicKey , // Equilibrium rate module
92- mixn : node . accounts . sr_account . publicKey , // Mixnet
93- bcsv : node . accounts . sr_account . publicKey , // BlockchainSrvc (StorageHub)
94- ftsv : node . accounts . ed_account . publicKey , // FileTransferSrvc (StorageHub)
95- } ;
98+ export function generateKeyTypeMap (
99+ keystoreKeyTypes : string [ ] | undefined ,
100+ isAssetHubPolkadot = false ,
101+ ) : KeyTypesMap {
102+ const keyMap : KeyTypesMap = { } ;
96103
97104 // 2 ways keys can be defined:
98- node . keystoreKeyTypes ?. forEach ( ( key_spec ) => {
99- // short: by only 4 letter key type with defaulted scheme e.g. "audi", if default scheme doesn't exist it is "ed "
105+ keystoreKeyTypes ?. forEach ( ( key_spec ) => {
106+ // short: by only 4 letter key type with defaulted scheme e.g. "audi", default schema is "sr "
100107 if ( key_spec . length === 4 ) {
101- keystore_key_types [ key_spec ] =
102- default_keystore_key_types [ key_spec ] ||
103- node . accounts . sr_account . publicKey ;
108+ keyMap [ key_spec ] = "sr" ;
104109 }
105110
106111 // long: 4 letter key type with scheme separated by underscore e.g. "audi_sr"
107112 const [ key_type , key_scheme ] = key_spec . split ( "_" ) ;
108113 if ( key_type . length === 4 ) {
109114 if ( key_scheme === "ed" ) {
110- keystore_key_types [ key_type ] = node . accounts . ed_account . publicKey ;
115+ keyMap [ key_type ] = "ed" ;
111116 } else if ( key_scheme === "ec" ) {
112- keystore_key_types [ key_type ] = node . accounts . ec_account . publicKey ;
117+ keyMap [ key_type ] = "ec" ;
113118 } else if ( key_scheme === "sr" ) {
114- keystore_key_types [ key_type ] = node . accounts . sr_account . publicKey ;
119+ keyMap [ key_type ] = "sr" ;
115120 }
116121 }
117122 } ) ;
118123
119- if ( Object . keys ( keystore_key_types ) . length === 0 )
120- keystore_key_types = default_keystore_key_types ;
124+ // ensure aura has the correct key
125+ keyMap [ "aura" ] = isAssetHubPolkadot ? "ed" : "sr" ;
126+
127+ return keyMap ;
128+ }
129+ export async function generateKeystoreFiles (
130+ node : Node ,
131+ path : string ,
132+ keyMap : KeyTypesMap ,
133+ // isAssetHubPolkadot = false,
134+ ) : Promise < string [ ] > {
135+ const keystoreDir = `${ path } /keystore` ;
136+ await makeDir ( keystoreDir ) ;
137+
138+ const paths : string [ ] = [ ] ;
121139
122- for ( const [ k , v ] of Object . entries ( keystore_key_types ) ) {
123- const filename = Buffer . from ( k ) . toString ( "hex" ) + v . replace ( / ^ 0 x / , "" ) ;
140+ for ( const [ k , v ] of Object . entries ( keyMap ) ) {
141+ // check if we have the account to use by key or by schema
142+ const acc = node . accounts [ k ]
143+ ? node . accounts [ k ]
144+ : node . accounts [ `${ v } _account` ] ;
145+ const filename =
146+ Buffer . from ( k ) . toString ( "hex" ) + acc . publicKey . replace ( / ^ 0 x / , "" ) ;
124147 const keystoreFilePath = `${ keystoreDir } /${ filename } ` ;
125148 paths . push ( keystoreFilePath ) ;
126- await fs . promises . writeFile ( keystoreFilePath , `"${ node . accounts . seed } "` ) ;
149+ await fs . promises . writeFile ( keystoreFilePath , `"${ acc . seed } "` ) ;
127150 }
128151
129152 return paths ;
0 commit comments