-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generateKeypair moved to package/utils (#157)
Signed-off-by: Amar Tumballi <[email protected]>
- Loading branch information
Showing
8 changed files
with
135 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as Cord from '@cord.network/sdk' | ||
|
||
const { | ||
KEY_SEED, | ||
SESSIONKEYS, | ||
} = process.env; | ||
|
||
async function main() { | ||
if (!KEY_SEED || !SESSIONKEYS) { | ||
console.log("Missing ENV variables (KEY_SEED || SESSIONKEYS)"); | ||
return; | ||
} | ||
const networkAddress = 'ws://127.0.0.1:9944' | ||
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK }) | ||
await Cord.connect(networkAddress) | ||
const api = Cord.ConfigService.get('api') | ||
|
||
try { | ||
const nodeKey = await Cord.Utils.Crypto.makeKeypairFromUri( | ||
KEY_SEED, | ||
'ed25519' | ||
) | ||
console.log (api.tx); | ||
|
||
const tx = api.tx.session.setKeys(SESSIONKEYS, "0x00") | ||
await Cord.Chain.signAndSubmitTx(tx, nodeKey) | ||
} catch (err) { | ||
console.log("Failed to execute setKeys method", err); | ||
} | ||
} | ||
|
||
main() | ||
.then(() => console.log('\nBye! 👋 👋 👋 ')) | ||
.finally(Cord.disconnect) | ||
|
||
process.on('SIGINT', async () => { | ||
console.log('\nBye! 👋 👋 👋 \n') | ||
Cord.disconnect() | ||
process.exit(0) | ||
}) |
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
import type { CordKeyringPair, CordEncryptionKeypair } from './Address' | ||
|
||
export interface ICordKeyPair { | ||
authentication: CordKeyringPair | ||
keyAgreement: CordEncryptionKeypair | ||
assertionMethod: CordKeyringPair | ||
capabilityDelegation: CordKeyringPair | ||
} | ||
|
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,75 @@ | ||
import { Keyring } from '@polkadot/keyring' | ||
import { | ||
blake2AsU8a, | ||
keyExtractPath, | ||
keyFromPath, | ||
mnemonicGenerate, | ||
mnemonicToMiniSecret, | ||
ed25519PairFromSeed, | ||
sr25519PairFromSeed, | ||
} from '@polkadot/util-crypto' | ||
|
||
import { makeEncryptionKeypairFromSeed } from './Crypto'; | ||
import { CordKeyringPair, ICordKeyPair } from '@cord.network/types'; | ||
|
||
/** | ||
* It takes a mnemonic and returns a keypair that can be used for encryption | ||
* @param {string} mnemonic - The mnemonic that was generated in the previous step. | ||
* @returns A keypair for encryption. | ||
*/ | ||
function generateKeyAgreement(mnemonic: string, type: string) { | ||
let secretKeyPair = ed25519PairFromSeed(mnemonicToMiniSecret(mnemonic)); | ||
if (type === 'sr25519') { | ||
secretKeyPair = sr25519PairFromSeed(mnemonicToMiniSecret(mnemonic)) | ||
} | ||
const { path } = keyExtractPath('//did//keyAgreement//0') | ||
const { secretKey } = keyFromPath(secretKeyPair, path, type === 'ed25519' ? 'ed25519' : (type === 'sr25519' ? 'sr25519' : 'ecdsa')) | ||
return makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey)) | ||
} | ||
|
||
|
||
/** | ||
* This function takes a mnemonic, creates an account from the mnemonic, and then derives four keypairs from | ||
* the account | ||
* @param mnemonic - A string of words that can be used to recover the keypairs. | ||
* @param keytype - type of key to generate, supports 'ed25519' (default) and 'sr25519' for now | ||
* @returns An object with 4 keyring pairs. | ||
*/ | ||
export function generateKeypairs(mnemonic: string, keytype: string): ICordKeyPair { | ||
if (!mnemonic) { | ||
mnemonic = mnemonicGenerate() | ||
} | ||
if (!keytype) { | ||
keytype = 'ed25519' | ||
} | ||
const keyring = new Keyring({ | ||
ss58Format: 29, | ||
type: keytype === 'ed25519' ? 'ed25519' : (keytype === 'sr25519' ? 'sr25519' : 'ecdsa'), | ||
}) | ||
|
||
const account = keyring.addFromMnemonic(mnemonic) as CordKeyringPair; | ||
|
||
const authentication = { | ||
...account.derive('//did//authentication//0'), | ||
type: keytype, | ||
} as CordKeyringPair | ||
|
||
const assertionMethod = { | ||
...account.derive('//did//assertion//0'), | ||
type: keytype, | ||
} as CordKeyringPair | ||
|
||
const capabilityDelegation = { | ||
...account.derive('//did//delegation//0'), | ||
type: keytype, | ||
} as CordKeyringPair | ||
|
||
const keyAgreement = generateKeyAgreement(mnemonic, keytype) | ||
|
||
return { | ||
authentication: authentication, | ||
keyAgreement: keyAgreement, | ||
assertionMethod: assertionMethod, | ||
capabilityDelegation: capabilityDelegation, | ||
} | ||
} |
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