diff --git a/demo/src/func-test.ts b/demo/src/func-test.ts index c904e5c9..c58e8a73 100644 --- a/demo/src/func-test.ts +++ b/demo/src/func-test.ts @@ -1,6 +1,5 @@ import * as Cord from '@cord.network/sdk' // import { UUID, Crypto } from '@cord.network/utils' -import { generateKeypairs } from './utils/generateKeypairs' import { createDid } from './utils/generateDid' import { createDidName } from './utils/generateDidName' import { getDidDocFromName } from './utils/queryDidName' @@ -62,7 +61,7 @@ async function main() { const { mnemonic: verifierMnemonic, document: verifierDid } = await createDid( authorIdentity ) - const verifierKeys = generateKeypairs(verifierMnemonic) + const verifierKeys = Cord.Utils.Keys.generateKeypairs(verifierMnemonic) console.log( `🏢 Verifier (${verifierDid.assertionMethod![0].type}): ${verifierDid.uri}` ) @@ -70,7 +69,7 @@ async function main() { const { mnemonic: holderMnemonic, document: holderDid } = await createDid( authorIdentity ) - const holderKeys = generateKeypairs(holderMnemonic) + const holderKeys = Cord.Utils.Keys.generateKeypairs(holderMnemonic) console.log( `👩‍⚕️ Holder (${holderDid.assertionMethod![0].type}): ${holderDid.uri}` ) @@ -78,7 +77,7 @@ async function main() { const { mnemonic: issuerMnemonic, document: issuerDid } = await createDid( authorIdentity ) - const issuerKeys = generateKeypairs(issuerMnemonic) + const issuerKeys = Cord.Utils.Keys.generateKeypairs(issuerMnemonic) console.log( `🏛 Issuer (${issuerDid?.assertionMethod![0].type}): ${issuerDid.uri}` ) @@ -93,7 +92,7 @@ async function main() { // Create Delegate One DID const { mnemonic: delegateOneMnemonic, document: delegateOneDid } = await createDid(authorIdentity) - const delegateOneKeys = generateKeypairs(delegateOneMnemonic) + const delegateOneKeys = Cord.Utils.Keys.generateKeypairs(delegateOneMnemonic) console.log( `🏛 Delegate (${delegateOneDid?.assertionMethod![0].type}): ${ delegateOneDid.uri @@ -102,7 +101,7 @@ async function main() { // Create Delegate Two DID const { mnemonic: delegateTwoMnemonic, document: delegateTwoDid } = await createDid(authorIdentity) - const delegateTwoKeys = generateKeypairs(delegateTwoMnemonic) + const delegateTwoKeys = Cord.Utils.Keys.generateKeypairs(delegateTwoMnemonic) console.log( `🏛 Delegate (${delegateTwoDid?.assertionMethod![0].type}): ${ delegateTwoDid.uri @@ -111,7 +110,7 @@ async function main() { // Create Delegate 3 DID const { mnemonic: delegate3Mnemonic, document: delegate3Did } = await createDid(authorIdentity) - const delegate3Keys = generateKeypairs(delegate3Mnemonic) + const delegate3Keys = Cord.Utils.Keys.generateKeypairs(delegate3Mnemonic) console.log( `🏛 Delegate (${delegate3Did?.assertionMethod![0].type}): ${ delegate3Did.uri diff --git a/demo/src/network-score-test.ts b/demo/src/network-score-test.ts index 9c1ef751..1b433080 100644 --- a/demo/src/network-score-test.ts +++ b/demo/src/network-score-test.ts @@ -1,5 +1,4 @@ import * as Cord from '@cord.network/sdk' -import { generateKeypairs } from './utils/generateKeypairs' import { createDid } from './utils/generateDid' import { createDidName } from './utils/generateDidName' import { getDidDocFromName } from './utils/queryDidName' @@ -28,20 +27,20 @@ async function main() { const { mnemonic: chainSpaceAdminMnemonic, document: chainSpaceAdminDid } = await createDid(networkAuthorIdentity) - const chainSpaceAdminKeys = generateKeypairs(chainSpaceAdminMnemonic) + const chainSpaceAdminKeys = Cord.Utils.Keys.generateKeypairs(chainSpaceAdminMnemonic) console.log( `🔐 Network Score Admin (${chainSpaceAdminDid.authentication[0].type}): ${chainSpaceAdminDid.uri}` ) const { mnemonic: networkProviderMnemonic, document: networkProviderDid } = await createDid(networkAuthorIdentity) - const networkProviderKeys = generateKeypairs(networkProviderMnemonic) + const networkProviderKeys = Cord.Utils.Keys.generateKeypairs(networkProviderMnemonic) console.log( `🔐 Network Participant (Provider) (${networkProviderDid.authentication[0].type}): ${networkProviderDid.uri}` ) const { mnemonic: networkAuthorMnemonic, document: networkAuthorDid } = await createDid(networkAuthorIdentity) - const networkAuthorKeys = generateKeypairs(networkAuthorMnemonic) + const networkAuthorKeys = Cord.Utils.Keys.generateKeypairs(networkAuthorMnemonic) console.log( `🔐 Network Author (API -> Node) (${networkAuthorDid.authentication[0].type}): ${networkAuthorDid.uri}` ) diff --git a/demo/src/system-setkey.ts b/demo/src/system-setkey.ts new file mode 100644 index 00000000..8ad1697b --- /dev/null +++ b/demo/src/system-setkey.ts @@ -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) +}) diff --git a/demo/src/utils/generateKeypairs.ts b/demo/src/utils/generateKeypairs.ts deleted file mode 100644 index e4d9f4e8..00000000 --- a/demo/src/utils/generateKeypairs.ts +++ /dev/null @@ -1,56 +0,0 @@ -import * as Cord from '@cord.network/sdk' -import { - blake2AsU8a, - keyExtractPath, - keyFromPath, - mnemonicGenerate, - mnemonicToMiniSecret, - ed25519PairFromSeed, -} from '@polkadot/util-crypto' -import { createAccount } from './createAccount' - -/** - * 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) { - const secretKeyPair = ed25519PairFromSeed(mnemonicToMiniSecret(mnemonic)) - const { path } = keyExtractPath('//did//keyAgreement//0') - const { secretKey } = keyFromPath(secretKeyPair, path, 'ed25519') - return Cord.Utils.Crypto.makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey)) -} - -/** - * It generates 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. - * @returns An object with 4 keyring pairs. - */ -export function generateKeypairs(mnemonic = mnemonicGenerate()) { - const { account } = createAccount(mnemonic) - - const authentication = { - ...account.derive('//did//authentication//0'), - type: 'ed25519', - } as Cord.CordKeyringPair - - const assertionMethod = { - ...account.derive('//did//assertion//0'), - type: 'ed25519', - } as Cord.CordKeyringPair - - const capabilityDelegation = { - ...account.derive('//did//delegation//0'), - type: 'ed25519', - } as Cord.CordKeyringPair - - const keyAgreement = generateKeyAgreement(mnemonic) - - return { - authentication: authentication, - keyAgreement: keyAgreement, - assertionMethod: assertionMethod, - capabilityDelegation: capabilityDelegation, - } -} diff --git a/packages/types/src/Keys.ts b/packages/types/src/Keys.ts new file mode 100644 index 00000000..31972ab7 --- /dev/null +++ b/packages/types/src/Keys.ts @@ -0,0 +1,9 @@ +import type { CordKeyringPair, CordEncryptionKeypair } from './Address' + +export interface ICordKeyPair { + authentication: CordKeyringPair + keyAgreement: CordEncryptionKeypair + assertionMethod: CordKeyringPair + capabilityDelegation: CordKeyringPair +} + diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index b6010107..d7379920 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -20,3 +20,4 @@ export * from './ChainSpace.js' export * from './Statement.js' export * from './Weight.js' export * from './Imported.js' +export * from './Keys.js' diff --git a/packages/utils/src/Keys.ts b/packages/utils/src/Keys.ts new file mode 100644 index 00000000..fc6a7f9f --- /dev/null +++ b/packages/utils/src/Keys.ts @@ -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, + } +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index d7010153..300da419 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,6 +1,7 @@ export * as jsonabc from './jsonabc.js' export * as Crypto from './Crypto.js' +export * as Keys from './Keys.js' export * as Balance from './Balance.js' export * as UUID from './UUID.js' export * as DataUtils from './DataUtils.js'