Skip to content

Commit

Permalink
fix: reuse code instead of duplicating it (#82)
Browse files Browse the repository at this point in the history
## fixes KILTprotocol/ticket#3094

- Stop having duplicates. 
- Imports the types and functions necessary for set-up from the backend.
- Corrects some comments.

## How to test:

1. Delete all your all environment variables and domain linkage
credential
2. run `yarn environment` and follow the instructions until you have all
constants defined.
3. run `yarn did-configuration` to make a new domain linkage credential
4. run `yarn start`
5. login on the browser.

## Checklist:

- [x] I have verified that the code works
- [x] I have verified that the code is easy to understand
  - [ ] If not, I have left a well-balanced amount of inline comments
- [x] I have [left the code in a better
state](https://deviq.com/principles/boy-scout-rule)
- [x] I have documented the changes (where applicable)
* Either PR or Ticket to update [the
Docs](https://github.com/KILTprotocol/docs)
    * Link the PR/Ticket here
  • Loading branch information
rompemoldes authored May 22, 2024
1 parent 2ee5d7d commit dde33e7
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 248 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ build
dist
.well-known/

# misc
.DS_Store
# environment constants
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
*.env

# misc
.DS_Store
.vscode
tmp/
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import dotenv from 'dotenv'
import * as Kilt from '@kiltprotocol/sdk-js'
import { mnemonicGenerate } from '@polkadot/util-crypto'
import dotenv from 'dotenv'

import { generateKeyPairs } from './launchUtils/generateKeyPairs'
import { generateAccount } from './launchUtils/generateAccount'
import {
generateAccount,
generateKeyPairs
} from './launchUtils/recycledUtils/preEnvironment'
import { generateFullDid } from './launchUtils/generateFullDid'

// try to read the variables from the .env-file:
Expand Down Expand Up @@ -126,11 +128,11 @@ async function main() {
case 9:
imploreTrustedAttesters()
break
// ask you to choose a Ctype Hash
// ask you to specify trusted credential issuers
case 10:
imploreRequestedProperties()
break
// ask you to choose a Ctype Hash
// ask you to choose which credential-properties the user will have to disclose to login
// if (step = -1):
default:
console.log(
Expand Down Expand Up @@ -208,14 +210,14 @@ async function spawnDid() {
'This could take some minutes.\n',
'Make sure you have enough funds! \n\n'
)
// Load attester account
const attesterAccount = generateAccount(DAPP_ACCOUNT_MNEMONIC as string)
// Load payer account
const payerAccount = generateAccount(DAPP_ACCOUNT_MNEMONIC as string)
// the DID can be generated by a different mnemonic than from the account. This is also the preferred option.
// You could also pass a specific mnemonic, but here we generate a random mnemonic.
// for custom, replace here with a string of 12 BIP-39 words
const didMnemonic = mnemonicGenerate()
// the next function requires connect()
const fullDid = await generateFullDid(attesterAccount, didMnemonic)
const fullDid = await generateFullDid(payerAccount, didMnemonic)

console.log(
"\n Please, save the mnemonic and URI of your dApp's DID to the .env-file to continue!\n",
Expand Down Expand Up @@ -263,25 +265,28 @@ function imploreJwtSecretKey() {

function imploreCtypeHash() {
console.log(
'Please provide the CType Hash(es) inside the .env file using this constant name:.\n',
'Your dApp will only accept credentials of this given Claim Type(s).',
`CTYPE_HASH={CType IDs your dApp consider valid}\n`,
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
`CTYPE_HASH=0x3291bb126e33b4862d421bfaa1d2f272e6cdfc4f96658988fbcffea8914bd9ac\n`
`Please specify with types of credentials your dApp should consider valid.`,
`For this, please, provide the CType Hash(es) inside the .env file using the constant name 'CTYPE_HASH'. \n`,
'Your dApp will only accept credentials of the given Claim Type(s).\n\n',

`If you wish to accept Email Credentials, as the ones issued by SocialKYC.io, please add the following line to your .env file:\n`,
`CTYPE_HASH=0x3291bb126e33b4862d421bfaa1d2f272e6cdfc4f96658988fbcffea8914bd9ac\n\n`,

`If you rather work with other CTypes, we recommend checking out the registry under https://ctypehub.galaniprojects.de/.`
)
}
function imploreTrustedAttesters() {
console.log(
'Please provide a list for your Trusted Attesters inside the .env file using this constant name: \n',
`TRUSTED_ATTESTERS={lists of trusted attesters}\n`,
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
`Please provide a list your dApp's Trusted Attesters (Credential Issuers) inside the .env file using the constant name 'TRUSTED_ATTESTERS'. \n`,
'Only credentials attested on chain by one the specified DIDs will be accepted by your dApp.\n\n',
`If you wish to accept Credentials issued by the peregrine (test) version of SocialKYC.io, please add the following line to your .env file:\n`,
`TRUSTED_ATTESTERS=did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY\n`
)
}
function imploreRequestedProperties() {
console.log(
'Please provide a list of Required Properties inside the .env file using this constant name: \n',
`REQUIRED_PROPERTIES={lists of Properties users should be Required to disclose}\n`,
`Please provide a list of Required Properties inside the .env file using this constant name 'REQUIRED_PROPERTIES' \n`,
`The users will only be Required to disclose the listed Properties during credential presentation (≙login).\n\n`,
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
`REQUIRED_PROPERTIES=Email\n`
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import path from 'path'
import * as Kilt from '@kiltprotocol/sdk-js'
import dotenv from 'dotenv'

import { validateOurKeys } from '../backend/src/config'
import { fetchDidDocument } from '../backend/src/utils/fetchDidDocument'
import {
VerifiableDomainLinkagePresentation,
generateAccount,
generateKeyPairs
} from './launchUtils/recycledUtils/preEnvironment'

import { generateAccount } from './launchUtils/generateAccount'
import { generateKeyPairs } from './launchUtils/generateKeyPairs'
import { VerifiableDomainLinkagePresentation } from './launchUtils/types'
import {
fetchDidDocument,
validateOurKeys
} from './launchUtils/recycledUtils/postEnvironment'

import {
createCredential,
createPresentation,
selfAttestCredential,
getDomainLinkagePresentation,
selfAttestCredential,
verifyDidConfigPresentation
} from './wellKnownDIDConfiguration'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import * as Kilt from '@kiltprotocol/sdk-js'

import { getApi } from './connection'

import { generateKeyPairs } from './generateKeyPairs'
import { generateKeyPairs } from './recycledUtils/preEnvironment'

/**
* Generates a on-chain DID from a mnemonic.
*
* You need to be connected to a KILT Endpoint beforehand, to know where to register your DID at.
* For that, use 'Kilt.connect(<Endpoint's Websocket Address>)'.
*
* You could use the BOTLabs Endpoints:
* For the KILT Production Chain "Spiritnet": wss://spiritnet.kilt.io/
* For the KILT Test Chain "Peregrine": wss://peregrine.kilt.io/
*
* @param mnemonic
* @returns
*/
export async function generateFullDid(
submitterAccount: Kilt.KiltKeyringPair,
mnemonic: string
): Promise<Kilt.DidDocument> {
await getApi()
const didMnemonic = mnemonic
const {
authentication,
keyAgreement,
assertionMethod,
capabilityDelegation
} = generateKeyPairs(didMnemonic)
} = generateKeyPairs(mnemonic)

// Before submitting the transaction, it is worth it to assure that the DID does not already exist.
// If the DID already exist, the transaction will fail, but it will still costs the fee. Better to avoid this.
Expand Down Expand Up @@ -73,6 +82,5 @@ export async function generateFullDid(
throw new Error('Full DID was not successfully fetched.')
}

Kilt.disconnect()
return didDocument
}
20 changes: 20 additions & 0 deletions launchScripts/launchUtils/recycledUtils/postEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Most of the utilities needed during the set-up that are also needed by the backend.
* It is important that this utilities correlate so that the backend can use what was configured during set-up (launch).
* To avoid duplicated code and discrepancies, it is better to import from the backend the utilities needed for the set-up.
* That is what is done here on the 'recycledUtils' folder.
*/

//
// Import and Export Functions:
//

// This two functions from the Backend use the backend-config directly or indirectly.
// The config from the backend try to loads all the env-constants and throws if one is missing.
// That's why they are imported in a separated file.
// This file should only be loaded after all environment constants are defined; e.g. while generating the Domain-Linkage-Credential, but not the Environment Variables.

import { fetchDidDocument } from '../../../backend/src/utils/fetchDidDocument'
import { validateOurKeys } from '../../../backend/src/config'

export { validateOurKeys, fetchDidDocument }
32 changes: 32 additions & 0 deletions launchScripts/launchUtils/recycledUtils/preEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Most of the utilities needed during the set-up that are also needed by the backend.
* It is important that this utilities correlate so that the backend can use what was configured during set-up (launch).
* To avoid duplicated code and discrepancies, it is better to import from the backend the utilities needed for the set-up.
* That is what is done here on the 'recycledUtils' folder.
*/

//
// Import and Export Types:
//

import {
CredentialSubject,
DomainLinkageCredential,
VerifiableDomainLinkagePresentation
} from '../../../backend/src/utils/types'

export type This = typeof globalThis
export {
CredentialSubject,
DomainLinkageCredential,
VerifiableDomainLinkagePresentation
}

//
// Import and Export Functions:
//

import { generateKeyPairs } from '../../../backend/src/utils/generateKeyPairs'
import { generateAccount } from '../../../backend/src/utils/generateAccount'

export { generateAccount, generateKeyPairs }
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CredentialSubject,
DomainLinkageCredential,
VerifiableDomainLinkagePresentation
} from './launchUtils/types'
} from './launchUtils/recycledUtils/preEnvironment'

// This constants are needed to create a credential and/or presentation.
// They are standard, an so it's better to fetch them from the @kiltprotocol/vc-export package, to keep them up to date.
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
"node": ">= v18.17.0"
},
"scripts": {
"environment": "ts-node ./scripts/genesisEnvironmentVariables.ts",
"did-configuration": "ts-node ./scripts/genesisWellKnownDidConfig.ts",
"environment": "ts-node ./launchScripts/genesisEnvironmentVariables.ts",
"did-configuration": "ts-node ./launchScripts/genesisWellKnownDidConfig.ts",
"build:frontend": "cd ./frontend && yarn build",
"build:backend": "cd ./backend && yarn build",
"build": "yarn build:frontend && yarn build:backend",
"start:frontend": "ts-node ./scripts/updatePorts.ts && cd ./frontend && yarn start",
"start:frontend": "ts-node ./launchScripts/updatePorts.ts && cd ./frontend && yarn start",
"start:backend": "cd ./backend && yarn start",
"start-prod:backend": "cd ./backend && yarn start-prod",
"start": "concurrently 'yarn:start:*'",
"lint": "concurrently 'yarn:lint:*'",
"lint:frontend": "eslint --ext .js,.ts --config ./.eslintrc.json \"frontend/**/*.ts\"",
"lint:backend": "eslint --ext .js,.ts --config ./.eslintrc.json \"backend/**/*.ts\"",
"lint:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json \"scripts/**/*.ts\"",
"lint:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json \"launchScripts/**/*.ts\"",
"fix": "concurrently 'yarn:fix:*'",
"fix:frontend": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"frontend/**/*.ts\"",
"fix:backend": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"backend/**/*.ts\"",
"fix:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"scripts/**/*.ts\""
"fix:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"launchScripts/**/*.ts\""
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
24 changes: 0 additions & 24 deletions scripts/launchUtils/connection.ts

This file was deleted.

21 changes: 0 additions & 21 deletions scripts/launchUtils/generateAccount.ts

This file was deleted.

33 changes: 0 additions & 33 deletions scripts/launchUtils/generateKeyPairs.ts

This file was deleted.

Loading

0 comments on commit dde33e7

Please sign in to comment.