Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateKeypair moved to package/utils #157

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions demo/src/func-test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -62,23 +61,23 @@ 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}`
)
// Create Holder DID
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}`
)
// Create issuer DID
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}`
)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions demo/src/network-score-test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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}`
)
Expand Down
40 changes: 40 additions & 0 deletions demo/src/system-setkey.ts
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)
})
56 changes: 0 additions & 56 deletions demo/src/utils/generateKeypairs.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/types/src/Keys.ts
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
}

1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from './ChainSpace.js'
export * from './Statement.js'
export * from './Weight.js'
export * from './Imported.js'
export * from './Keys.js'
75 changes: 75 additions & 0 deletions packages/utils/src/Keys.ts
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,
}
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Loading