Skip to content

Commit dde33e7

Browse files
authored
fix: reuse code instead of duplicating it (#82)
## 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
1 parent 2ee5d7d commit dde33e7

13 files changed

Lines changed: 111 additions & 248 deletions

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ build
2929
dist
3030
.well-known/
3131

32-
# misc
33-
.DS_Store
32+
# environment constants
3433
.env
3534
.env.local
3635
.env.development.local
3736
.env.test.local
3837
.env.production.local
38+
*.env
39+
40+
# misc
41+
.DS_Store
3942
.vscode
4043
tmp/

scripts/genesisEnvironmentVariables.ts renamed to launchScripts/genesisEnvironmentVariables.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import dotenv from 'dotenv'
21
import * as Kilt from '@kiltprotocol/sdk-js'
32
import { mnemonicGenerate } from '@polkadot/util-crypto'
3+
import dotenv from 'dotenv'
44

5-
import { generateKeyPairs } from './launchUtils/generateKeyPairs'
6-
import { generateAccount } from './launchUtils/generateAccount'
5+
import {
6+
generateAccount,
7+
generateKeyPairs
8+
} from './launchUtils/recycledUtils/preEnvironment'
79
import { generateFullDid } from './launchUtils/generateFullDid'
810

911
// try to read the variables from the .env-file:
@@ -126,11 +128,11 @@ async function main() {
126128
case 9:
127129
imploreTrustedAttesters()
128130
break
129-
// ask you to choose a Ctype Hash
131+
// ask you to specify trusted credential issuers
130132
case 10:
131133
imploreRequestedProperties()
132134
break
133-
// ask you to choose a Ctype Hash
135+
// ask you to choose which credential-properties the user will have to disclose to login
134136
// if (step = -1):
135137
default:
136138
console.log(
@@ -208,14 +210,14 @@ async function spawnDid() {
208210
'This could take some minutes.\n',
209211
'Make sure you have enough funds! \n\n'
210212
)
211-
// Load attester account
212-
const attesterAccount = generateAccount(DAPP_ACCOUNT_MNEMONIC as string)
213+
// Load payer account
214+
const payerAccount = generateAccount(DAPP_ACCOUNT_MNEMONIC as string)
213215
// the DID can be generated by a different mnemonic than from the account. This is also the preferred option.
214216
// You could also pass a specific mnemonic, but here we generate a random mnemonic.
215217
// for custom, replace here with a string of 12 BIP-39 words
216218
const didMnemonic = mnemonicGenerate()
217219
// the next function requires connect()
218-
const fullDid = await generateFullDid(attesterAccount, didMnemonic)
220+
const fullDid = await generateFullDid(payerAccount, didMnemonic)
219221

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

264266
function imploreCtypeHash() {
265267
console.log(
266-
'Please provide the CType Hash(es) inside the .env file using this constant name:.\n',
267-
'Your dApp will only accept credentials of this given Claim Type(s).',
268-
`CTYPE_HASH={CType IDs your dApp consider valid}\n`,
269-
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
270-
`CTYPE_HASH=0x3291bb126e33b4862d421bfaa1d2f272e6cdfc4f96658988fbcffea8914bd9ac\n`
268+
`Please specify with types of credentials your dApp should consider valid.`,
269+
`For this, please, provide the CType Hash(es) inside the .env file using the constant name 'CTYPE_HASH'. \n`,
270+
'Your dApp will only accept credentials of the given Claim Type(s).\n\n',
271+
272+
`If you wish to accept Email Credentials, as the ones issued by SocialKYC.io, please add the following line to your .env file:\n`,
273+
`CTYPE_HASH=0x3291bb126e33b4862d421bfaa1d2f272e6cdfc4f96658988fbcffea8914bd9ac\n\n`,
274+
275+
`If you rather work with other CTypes, we recommend checking out the registry under https://ctypehub.galaniprojects.de/.`
271276
)
272277
}
273278
function imploreTrustedAttesters() {
274279
console.log(
275-
'Please provide a list for your Trusted Attesters inside the .env file using this constant name: \n',
276-
`TRUSTED_ATTESTERS={lists of trusted attesters}\n`,
277-
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
280+
`Please provide a list your dApp's Trusted Attesters (Credential Issuers) inside the .env file using the constant name 'TRUSTED_ATTESTERS'. \n`,
281+
'Only credentials attested on chain by one the specified DIDs will be accepted by your dApp.\n\n',
282+
`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`,
278283
`TRUSTED_ATTESTERS=did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY\n`
279284
)
280285
}
281286
function imploreRequestedProperties() {
282287
console.log(
283-
'Please provide a list of Required Properties inside the .env file using this constant name: \n',
284-
`REQUIRED_PROPERTIES={lists of Properties users should be Required to disclose}\n`,
288+
`Please provide a list of Required Properties inside the .env file using this constant name 'REQUIRED_PROPERTIES' \n`,
289+
`The users will only be Required to disclose the listed Properties during credential presentation (≙login).\n\n`,
285290
`If you wish to use the default Email Credential settings, please add the following line to your .env file:\n`,
286291
`REQUIRED_PROPERTIES=Email\n`
287292
)

scripts/genesisWellKnownDidConfig.ts renamed to launchScripts/genesisWellKnownDidConfig.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ import path from 'path'
55
import * as Kilt from '@kiltprotocol/sdk-js'
66
import dotenv from 'dotenv'
77

8-
import { validateOurKeys } from '../backend/src/config'
9-
import { fetchDidDocument } from '../backend/src/utils/fetchDidDocument'
8+
import {
9+
VerifiableDomainLinkagePresentation,
10+
generateAccount,
11+
generateKeyPairs
12+
} from './launchUtils/recycledUtils/preEnvironment'
1013

11-
import { generateAccount } from './launchUtils/generateAccount'
12-
import { generateKeyPairs } from './launchUtils/generateKeyPairs'
13-
import { VerifiableDomainLinkagePresentation } from './launchUtils/types'
14+
import {
15+
fetchDidDocument,
16+
validateOurKeys
17+
} from './launchUtils/recycledUtils/postEnvironment'
1418

1519
import {
1620
createCredential,
1721
createPresentation,
18-
selfAttestCredential,
1922
getDomainLinkagePresentation,
23+
selfAttestCredential,
2024
verifyDidConfigPresentation
2125
} from './wellKnownDIDConfiguration'
2226

scripts/launchUtils/generateFullDid.ts renamed to launchScripts/launchUtils/generateFullDid.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import * as Kilt from '@kiltprotocol/sdk-js'
22

3-
import { getApi } from './connection'
4-
5-
import { generateKeyPairs } from './generateKeyPairs'
3+
import { generateKeyPairs } from './recycledUtils/preEnvironment'
64

5+
/**
6+
* Generates a on-chain DID from a mnemonic.
7+
*
8+
* You need to be connected to a KILT Endpoint beforehand, to know where to register your DID at.
9+
* For that, use 'Kilt.connect(<Endpoint's Websocket Address>)'.
10+
*
11+
* You could use the BOTLabs Endpoints:
12+
* For the KILT Production Chain "Spiritnet": wss://spiritnet.kilt.io/
13+
* For the KILT Test Chain "Peregrine": wss://peregrine.kilt.io/
14+
*
15+
* @param mnemonic
16+
* @returns
17+
*/
718
export async function generateFullDid(
819
submitterAccount: Kilt.KiltKeyringPair,
920
mnemonic: string
1021
): Promise<Kilt.DidDocument> {
11-
await getApi()
12-
const didMnemonic = mnemonic
1322
const {
1423
authentication,
1524
keyAgreement,
1625
assertionMethod,
1726
capabilityDelegation
18-
} = generateKeyPairs(didMnemonic)
27+
} = generateKeyPairs(mnemonic)
1928

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

76-
Kilt.disconnect()
7785
return didDocument
7886
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Most of the utilities needed during the set-up that are also needed by the backend.
3+
* It is important that this utilities correlate so that the backend can use what was configured during set-up (launch).
4+
* To avoid duplicated code and discrepancies, it is better to import from the backend the utilities needed for the set-up.
5+
* That is what is done here on the 'recycledUtils' folder.
6+
*/
7+
8+
//
9+
// Import and Export Functions:
10+
//
11+
12+
// This two functions from the Backend use the backend-config directly or indirectly.
13+
// The config from the backend try to loads all the env-constants and throws if one is missing.
14+
// That's why they are imported in a separated file.
15+
// 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.
16+
17+
import { fetchDidDocument } from '../../../backend/src/utils/fetchDidDocument'
18+
import { validateOurKeys } from '../../../backend/src/config'
19+
20+
export { validateOurKeys, fetchDidDocument }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Most of the utilities needed during the set-up that are also needed by the backend.
3+
* It is important that this utilities correlate so that the backend can use what was configured during set-up (launch).
4+
* To avoid duplicated code and discrepancies, it is better to import from the backend the utilities needed for the set-up.
5+
* That is what is done here on the 'recycledUtils' folder.
6+
*/
7+
8+
//
9+
// Import and Export Types:
10+
//
11+
12+
import {
13+
CredentialSubject,
14+
DomainLinkageCredential,
15+
VerifiableDomainLinkagePresentation
16+
} from '../../../backend/src/utils/types'
17+
18+
export type This = typeof globalThis
19+
export {
20+
CredentialSubject,
21+
DomainLinkageCredential,
22+
VerifiableDomainLinkagePresentation
23+
}
24+
25+
//
26+
// Import and Export Functions:
27+
//
28+
29+
import { generateKeyPairs } from '../../../backend/src/utils/generateKeyPairs'
30+
import { generateAccount } from '../../../backend/src/utils/generateAccount'
31+
32+
export { generateAccount, generateKeyPairs }

scripts/wellKnownDIDConfiguration.ts renamed to launchScripts/wellKnownDIDConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
CredentialSubject,
1010
DomainLinkageCredential,
1111
VerifiableDomainLinkagePresentation
12-
} from './launchUtils/types'
12+
} from './launchUtils/recycledUtils/preEnvironment'
1313

1414
// This constants are needed to create a credential and/or presentation.
1515
// They are standard, an so it's better to fetch them from the @kiltprotocol/vc-export package, to keep them up to date.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
"node": ">= v18.17.0"
1616
},
1717
"scripts": {
18-
"environment": "ts-node ./scripts/genesisEnvironmentVariables.ts",
19-
"did-configuration": "ts-node ./scripts/genesisWellKnownDidConfig.ts",
18+
"environment": "ts-node ./launchScripts/genesisEnvironmentVariables.ts",
19+
"did-configuration": "ts-node ./launchScripts/genesisWellKnownDidConfig.ts",
2020
"build:frontend": "cd ./frontend && yarn build",
2121
"build:backend": "cd ./backend && yarn build",
2222
"build": "yarn build:frontend && yarn build:backend",
23-
"start:frontend": "ts-node ./scripts/updatePorts.ts && cd ./frontend && yarn start",
23+
"start:frontend": "ts-node ./launchScripts/updatePorts.ts && cd ./frontend && yarn start",
2424
"start:backend": "cd ./backend && yarn start",
2525
"start-prod:backend": "cd ./backend && yarn start-prod",
2626
"start": "concurrently 'yarn:start:*'",
2727
"lint": "concurrently 'yarn:lint:*'",
2828
"lint:frontend": "eslint --ext .js,.ts --config ./.eslintrc.json \"frontend/**/*.ts\"",
2929
"lint:backend": "eslint --ext .js,.ts --config ./.eslintrc.json \"backend/**/*.ts\"",
30-
"lint:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json \"scripts/**/*.ts\"",
30+
"lint:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json \"launchScripts/**/*.ts\"",
3131
"fix": "concurrently 'yarn:fix:*'",
3232
"fix:frontend": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"frontend/**/*.ts\"",
3333
"fix:backend": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"backend/**/*.ts\"",
34-
"fix:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"scripts/**/*.ts\""
34+
"fix:scripts": "eslint --ext .js,.ts --config ./.eslintrc.json --fix \"launchScripts/**/*.ts\""
3535
},
3636
"devDependencies": {
3737
"@types/node": "^20.9.1",

scripts/launchUtils/connection.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)