-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: add cookbook recipe how to generate keys #223
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
890c5bd
feat: add cookbook recipe how to generate keys (taken from the worksh…
trusch 89dba2e
fix broken test
trusch 042a8d7
run prettier
trusch 491529a
try to fix test
trusch 6f19170
prettier
trusch 73a4fbb
try to fix test
trusch 56eaa58
prettier
trusch 18f3b1d
Merge branch 'master' into feat/key-generation-recipe
trusch 1f3e5d9
add more explanations about derivation pathes
trusch ba5e741
feat: updating to match sporran and updating naming throughout to match
Dudleyneedham a3f88a9
lint: updating the linting
Dudleyneedham 45dd6d0
feat: removal of the mention of light did
Dudleyneedham a9895e8
feat: adding remarks and suggestions for better text
Dudleyneedham a17686a
Update docs/develop/01_sdk/02_cookbook/01_dids/00_generate_keys.md
Dudleyneedham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
59 changes: 59 additions & 0 deletions
59
code_examples/sdk_examples/src/core_features/did/00_generate_did_keys.ts
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,59 @@ | ||
import * as Kilt from '@kiltprotocol/sdk-js' | ||
|
||
import { | ||
blake2AsU8a, | ||
keyExtractPath, | ||
keyFromPath, | ||
mnemonicGenerate, | ||
mnemonicToMiniSecret, | ||
sr25519PairFromSeed | ||
} from '@polkadot/util-crypto' | ||
|
||
// Because there is no first-class support for this class of keys, | ||
// we need to use a workaround to generate a key we can use for encryption/decryption. | ||
function generateKeyAgreement(mnemonic: string): Kilt.KiltEncryptionKeypair { | ||
const secretKeyPair = sr25519PairFromSeed(mnemonicToMiniSecret(mnemonic)) | ||
const { path } = keyExtractPath('//did//keyAgreement//0') | ||
const { secretKey } = keyFromPath(secretKeyPair, path, 'sr25519') | ||
return Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey)) | ||
} | ||
|
||
export function generateKeypairs(mnemonic = mnemonicGenerate()): { | ||
authentication: Kilt.KiltKeyringPair & { | ||
type: 'sr25519' | ||
} | ||
keyAgreement: Kilt.KiltEncryptionKeypair | ||
assertionMethod: Kilt.KiltKeyringPair | ||
capabilityDelegation: Kilt.KiltKeyringPair | ||
} { | ||
const account = Kilt.Utils.Crypto.makeKeypairFromSeed( | ||
mnemonicToMiniSecret(mnemonic), | ||
'sr25519' | ||
) | ||
|
||
const authentication = { | ||
...account.derive('//did//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair & { | ||
type: 'sr25519' | ||
} | ||
|
||
const assertionMethod = { | ||
...account.derive('//did//assertion//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair | ||
|
||
const capabilityDelegation = { | ||
...account.derive('//did//delegation//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair | ||
|
||
const keyAgreement = generateKeyAgreement(mnemonic) | ||
|
||
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
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
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
75 changes: 50 additions & 25 deletions
75
code_examples/sdk_examples/src/core_features/utils/generateKeypairs.ts
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 |
---|---|---|
@@ -1,35 +1,60 @@ | ||
import { mnemonicGenerate, mnemonicToMiniSecret } from '@polkadot/util-crypto' | ||
|
||
import * as Kilt from '@kiltprotocol/sdk-js' | ||
|
||
import { | ||
blake2AsU8a, | ||
keyExtractPath, | ||
keyFromPath, | ||
mnemonicGenerate, | ||
mnemonicToMiniSecret, | ||
sr25519PairFromSeed | ||
} from '@polkadot/util-crypto' | ||
|
||
// Because there is no first-class support for this class of keys, | ||
// we need to use a workaround to generate a key we can use for encryption/decryption. | ||
function generateKeyAgreement(mnemonic: string): Kilt.KiltEncryptionKeypair { | ||
const secretKeyPair = sr25519PairFromSeed(mnemonicToMiniSecret(mnemonic)) | ||
const { path } = keyExtractPath('//did//keyAgreement//0') | ||
const { secretKey } = keyFromPath(secretKeyPair, path, 'sr25519') | ||
return Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey)) | ||
} | ||
|
||
export function generateKeypairs(mnemonic = mnemonicGenerate()): { | ||
authentication: Kilt.KiltKeyringPair & { | ||
type: 'ed25519' | ||
type: 'sr25519' | ||
} | ||
encryption: Kilt.KiltEncryptionKeypair | ||
attestation: Kilt.KiltKeyringPair | ||
delegation: Kilt.KiltKeyringPair | ||
keyAgreement: Kilt.KiltEncryptionKeypair | ||
assertionMethod: Kilt.KiltKeyringPair | ||
capabilityDelegation: Kilt.KiltKeyringPair | ||
} { | ||
const authentication = Kilt.Utils.Crypto.makeKeypairFromSeed( | ||
mnemonicToMiniSecret(mnemonic) | ||
) | ||
const encryption = Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed( | ||
mnemonicToMiniSecret(mnemonic) | ||
) | ||
const attestation = authentication.derive( | ||
'//attestation' | ||
) as Kilt.KiltKeyringPair | ||
const delegation = authentication.derive( | ||
'//delegation' | ||
) as Kilt.KiltKeyringPair | ||
const keyring = new Kilt.Utils.Keyring({ | ||
ss58Format: 38, | ||
type: 'sr25519' | ||
}) | ||
const account = keyring.addFromMnemonic(mnemonic) | ||
|
||
const authentication = { | ||
...account.derive('//did//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair & { | ||
type: 'sr25519' | ||
} | ||
|
||
const assertionMethod = { | ||
...account.derive('//did//assertion//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair | ||
|
||
const capabilityDelegation = { | ||
...account.derive('//did//delegation//0'), | ||
type: 'sr25519' | ||
} as Kilt.KiltKeyringPair | ||
|
||
const keyAgreement = generateKeyAgreement(mnemonic) | ||
|
||
return { | ||
authentication, | ||
encryption, | ||
attestation, | ||
delegation | ||
authentication: authentication, | ||
keyAgreement: keyAgreement, | ||
assertionMethod: assertionMethod, | ||
capabilityDelegation: capabilityDelegation | ||
} | ||
} | ||
|
||
// Required for the raw-loader to successfully import this code snippet as text. | ||
export default generateKeypairs |
22 changes: 22 additions & 0 deletions
22
docs/develop/01_sdk/02_cookbook/01_dids/00_generate_keys.md
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,22 @@ | ||
--- | ||
id: key-generation | ||
title: Generate DID keys | ||
--- | ||
|
||
import TsJsBlock from '@site/src/components/TsJsBlock'; | ||
|
||
import GenerateKeys from '!!raw-loader!@site/code_examples/sdk_examples/src/core_features/did/00_generate_did_keys.ts'; | ||
|
||
Creating a DID (like in [Light DID Creation](./01_light_did_creation.md) or [Full DID Creation](./02_full_did_creation.md)) requires the generation of some keying material for keys that are to be used for authentication and encryption. | ||
|
||
The following is an example of how to create a a set of keypairs suitable for generating a KILT DID from it. | ||
|
||
<TsJsBlock> | ||
{GenerateKeys} | ||
</TsJsBlock> | ||
|
||
:::info | ||
This example doesn't show how to store the keys. | ||
It is recommended to store the keys in a secure manner, e.g. only storing the private keys encrypted on disk. | ||
The mnemonic seed phrase can be used to regenerate the keys, so it is recommended to also store the mnemonic in a secure manner and create a backup of it. | ||
::: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this:
Also please explain the reason for the derivation paths
Maybe like that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
soooo much 🤖