Skip to content

Commit f9a7c3b

Browse files
refactor: rebase on sdk v1 (#31)
Co-authored-by: BlertaHamza <blertahamzallari@gmail.com>
1 parent 6024903 commit f9a7c3b

42 files changed

Lines changed: 9194 additions & 5927 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"sourceType": "module"
1111
},
1212
"plugins": ["@typescript-eslint", "license-header"],
13+
"ignorePatterns": ["license-header.js", "**/*.config.js", "**/*.config.cjs", "cjs/**", "esm/**"],
1314
"rules": {
1415
"linebreak-style": ["error", "unix"],
1516
"quotes": ["error", "single"],

.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ jobs:
3232
id: login-ecr
3333
uses: aws-actions/amazon-ecr-login@v1
3434

35-
- name: set image name
35+
- name: Set image name
3636
run: |
3737
echo "IMG_NAME=${{ steps.login-ecr.outputs.registry }}/kiltprotocol/prototype-chain:latest" >> "$GITHUB_ENV"
3838
39-
- name: pull image
39+
- name: Pull Docker image
4040
run: docker pull $IMG_NAME
4141

42-
- name: run tests
42+
- name: Run tests
4343
timeout-minutes: 5
4444
env:
4545
TESTCONTAINERS_NODE_IMG: ${{ env.IMG_NAME }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.yarn
12
/node_modules
23
yarn-error.log
34
/.vscode

.yarn/releases/yarn-4.9.1.cjs

Lines changed: 948 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
compressionLevel: 0
2+
3+
enableGlobalCache: false
4+
5+
nodeLinker: node-modules
6+
7+
yarnPath: .yarn/releases/yarn-4.9.1.cjs

jest.config.cjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ module.exports = {
99
rootDir: 'src',
1010
coverageDirectory: 'coverage',
1111
moduleDirectories: ['node_modules'],
12-
globals: {
13-
'ts-jest': {
14-
tsconfig: 'tsconfig.cjs.json',
15-
},
12+
transform: {
13+
'^.+\\.ts$': ['ts-jest', { tsconfig: 'tsconfig.cjs.json' }],
1614
},
1715
}

license-header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*
44
* This source code is licensed under the BSD 4-Clause "Original" license
55
* found in the LICENSE file in the root directory of this source tree.
6-
*/
6+
*/

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"main": "./index.js",
55
"types": "./index.d.ts",
66
"type": "commonjs",
7+
"engines": {
8+
"node": ">=20.0.0"
9+
},
710
"exports": {
811
".": {
912
"import": "./esm/index.js",
@@ -58,39 +61,37 @@
5861
"build": "yarn build:cjs && yarn build:esm",
5962
"build:cjs": "tsc -p tsconfig.cjs.json",
6063
"build:esm": "tsc -p tsconfig.esm.json && echo '{\"type\": \"module\"}' > esm/package.json",
61-
"clean": "exec git clean -xf -e 'node_modules' '*'",
64+
"clean": "yarn exec git clean -xf -e 'node_modules' -e '.yarn' '*'",
6265
"test": "jest test --no-cache --forceExit",
6366
"prepack": "yarn build"
6467
},
6568
"dependencies": {
66-
"@kiltprotocol/sdk-js": "^0.35.0",
67-
"@kiltprotocol/types": "^0.35.0",
68-
"@kiltprotocol/vc-export": "^0.35.0",
69-
"@polkadot/keyring": "^12.3.2",
70-
"@polkadot/util": "^12.3.2",
69+
"@kiltprotocol/augment-api": "^1.11405.0",
70+
"@kiltprotocol/legacy-credentials": "^0.100.0",
71+
"@kiltprotocol/sdk-js": "1.0.0",
7172
"yargs": "^17.7.2"
7273
},
7374
"devDependencies": {
74-
"@polkadot/util-crypto": "^12.3.2",
7575
"@types/jest": "^28.0.0",
7676
"@types/node": "^18.8.2",
7777
"@types/react": "^18.0.21",
7878
"@types/valid-url": "^1.0.3",
7979
"@typescript-eslint/eslint-plugin": "^5.33.0",
8080
"@typescript-eslint/parser": "^5.33.0",
81-
"eslint": ">=8.14.0",
81+
"eslint": "^8.57.0",
8282
"eslint-config-prettier": "^9.0.0",
8383
"eslint-plugin-license-header": "^0.6.0",
84-
"jest": "^28.0.0",
84+
"jest": "^29.7.0",
8585
"prettier": "^3.0.1",
8686
"react": "^18.2.0",
8787
"testcontainers": "^9.5.0",
88-
"ts-jest": "^28.0.8",
88+
"ts-jest": "^29.3.2",
8989
"ts-jest-resolver": "^2.0.1",
9090
"ts-node": "^10.9.1",
9191
"typescript": "^4.7.4"
9292
},
9393
"bin": {
9494
"createDidConfig": "./cli/createDidConfig.js"
95-
}
95+
},
96+
"packageManager": "yarn@4.9.1"
9697
}

src/cli/createDidConfig.ts

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
* found in the LICENSE file in the root directory of this source tree.
88
*/
99

10-
import { Credential, Did, connect, disconnect } from '@kiltprotocol/sdk-js'
11-
import { DidResourceUri, DidUri, ICredentialPresentation, SignCallback } from '@kiltprotocol/types'
10+
import { DataIntegrity } from '@kiltprotocol/credentials'
11+
import { multibaseKeyToDidKey, validateDid } from '@kiltprotocol/did'
12+
import { DidResolver, connect, disconnect } from '@kiltprotocol/sdk-js'
13+
import { Did, DidUrl } from '@kiltprotocol/types'
14+
import { Signers } from '@kiltprotocol/utils'
1215

1316
import { Keyring } from '@polkadot/keyring'
1417
import { u8aEq } from '@polkadot/util'
1518

1619
import { readFile, writeFile } from 'fs/promises'
1720
import yargs from 'yargs/yargs'
1821

19-
import { didConfigResourceFromCredential, createCredential } from '../wellKnownDidConfiguration/index.js'
20-
import type { DidConfigResource } from '../types/index.js'
22+
import { DidConfigResource, DomainLinkageCredential } from '../types/Credential.js'
23+
import {
24+
KILT_SELF_SIGNED_PROOF_TYPE,
25+
createCredential,
26+
didConfigResourceFromCredentials,
27+
} from '../wellKnownDidConfiguration/index.js'
2128

2229
type KeyType = 'sr25519' | 'ed25519' | 'ecdsa'
2330

@@ -41,22 +48,39 @@ const createCredentialOpts = {
4148
wsAddress: { alias: 'w', type: 'string', demandOption: true, default: 'wss://spiritnet.kilt.io' },
4249
} as const
4350

44-
async function issueCredential(did: DidUri, origin: string, seed: string, keyType: KeyType, nodeAddress: string) {
45-
await connect(nodeAddress)
46-
const didDocument = await Did.resolve(did)
47-
const assertionMethod = didDocument?.document?.assertionMethod?.[0]
51+
async function issueCredential({
52+
did,
53+
origin,
54+
seed,
55+
keyType,
56+
proofType,
57+
}: {
58+
did: Did
59+
origin: string
60+
seed: string
61+
keyType: KeyType
62+
proofType: string
63+
}) {
64+
const { didDocument } = await DidResolver.resolve(did, {})
65+
const assertionMethodId = didDocument?.assertionMethod?.[0]
66+
const assertionMethod = didDocument?.verificationMethod?.find(({ id }) => id === assertionMethodId)
4867
if (!assertionMethod) {
4968
throw new Error(
5069
`Could not resolve assertionMethod of ${did}. Make sure the DID is registered to this chain and has an assertionMethod key.`
5170
)
5271
}
72+
const keyUri: DidUrl = `${didDocument!.id}${assertionMethod.id}`
73+
5374
const keypair = new Keyring({ type: keyType }).addFromUri(seed)
54-
if (assertionMethod.type !== keypair.type || !u8aEq(assertionMethod.publicKey, keypair.publicKey)) {
75+
const signers = await Signers.getSignersForKeypair({ keypair, id: keyUri })
76+
77+
const { keyType: vmType, publicKey } = multibaseKeyToDidKey(assertionMethod.publicKeyMultibase)
78+
if (vmType !== keypair.type || !u8aEq(publicKey, keypair.publicKey)) {
5579
throw new Error('public key and/or key type of the DIDs assertionMethod does not match the supplied signing key')
5680
}
57-
const keyUri: DidResourceUri = `${didDocument!.document!.uri}${assertionMethod.id}`
58-
const signCallback: SignCallback = async ({ data }) => ({ signature: keypair.sign(data), keyUri, keyType })
59-
const credential = await createCredential(signCallback, origin, did)
81+
82+
const credential = await createCredential(signers, origin, didDocument!, { proofType } as any)
83+
6084
return credential
6185
}
6286

@@ -72,29 +96,30 @@ async function write(toWrite: unknown, outPath?: string) {
7296
async function run() {
7397
await yargs(process.argv.slice(2))
7498
.command(
75-
'fromCredential <pathToCredential>',
76-
'create a Did Configuration Resource from an existing Kilt Credential Presentation',
99+
'fromCredential [pathToCredential..]',
100+
'create a Did Configuration Resource from one or more existing Domain Linkage Credentials',
77101
(ygs) =>
78102
ygs.options(commonOpts).positional('pathToCredential', {
79-
describe: 'Path to a json file containing the credential presentation',
103+
describe: 'Path to a json file containing a Domain Linkage Credential',
80104
type: 'string',
81105
demandOption: true,
106+
array: true,
82107
}),
83108
async ({ pathToCredential, outFile }) => {
84-
let credential: ICredentialPresentation
85-
try {
86-
credential = JSON.parse(await readFile(pathToCredential, { encoding: 'utf-8' }))
87-
} catch (cause) {
88-
throw new Error(`Cannot parse file ${pathToCredential}`, { cause })
89-
}
90-
if (!Credential.isPresentation(credential)) {
91-
throw new Error(`Malformed Credential Presentation loaded from ${pathToCredential}`)
92-
}
109+
const credentials: DomainLinkageCredential[] = await Promise.all(
110+
pathToCredential.map(async (path) => {
111+
try {
112+
return JSON.parse(await readFile(path, { encoding: 'utf-8' }))
113+
} catch (cause) {
114+
throw new Error(`Cannot parse file ${pathToCredential}`, { cause })
115+
}
116+
})
117+
)
93118
let didResource: DidConfigResource
94119
try {
95-
didResource = await didConfigResourceFromCredential(credential)
120+
didResource = didConfigResourceFromCredentials(credentials)
96121
} catch (cause) {
97-
throw new Error('Credential Presentation is not suitable for use in a Did Configuration Resource', {
122+
throw new Error('Credential is not suitable for use in a Did Configuration Resource', {
98123
cause,
99124
})
100125
}
@@ -103,20 +128,38 @@ async function run() {
103128
)
104129
.command(
105130
'credentialOnly',
106-
'issue a new Kilt Credential Presentation for use in a Did Configuration Resource',
107-
{ ...createCredentialOpts, ...commonOpts },
108-
async ({ origin, seed, keyType, wsAddress, outFile, did }) => {
109-
const credential = await issueCredential(did as DidUri, origin, seed, keyType, wsAddress)
131+
'issue a new Domain Linkage Credential for use in a Did Configuration Resource',
132+
{
133+
...createCredentialOpts,
134+
...commonOpts,
135+
proofType: {
136+
alias: 'p',
137+
choices: [DataIntegrity.PROOF_TYPE, KILT_SELF_SIGNED_PROOF_TYPE] as const,
138+
default: KILT_SELF_SIGNED_PROOF_TYPE,
139+
describe:
140+
'Which proof type to use in the credential. DataIntegrity is the more modern proof type, but might not be accepted by all extensions yet. Did Configuration Resources can contain multiple credentials, though.',
141+
},
142+
},
143+
async ({ origin, seed, keyType, wsAddress, outFile, did, proofType }) => {
144+
await connect(wsAddress)
145+
validateDid(did)
146+
const credential = await issueCredential({ did: did as Did, origin, seed, keyType, proofType })
110147
await write(credential, outFile)
111148
}
112149
)
113150
.command(
114151
'$0',
115-
'create a Did Configuration Resource from a freshly issued Kilt Credential',
152+
'create a Did Configuration Resource containing newly issued Domain Linkage Credentials',
116153
{ ...createCredentialOpts, ...commonOpts },
117154
async ({ origin, seed, keyType, wsAddress, outFile, did }) => {
118-
const credential = await issueCredential(did as DidUri, origin, seed, keyType, wsAddress)
119-
const didResource = await didConfigResourceFromCredential(credential)
155+
await connect(wsAddress)
156+
validateDid(did)
157+
const credentials = await Promise.all(
158+
[DataIntegrity.PROOF_TYPE, KILT_SELF_SIGNED_PROOF_TYPE].map((proofType) =>
159+
issueCredential({ did: did as Did, origin, seed, keyType, proofType })
160+
)
161+
)
162+
const didResource = didConfigResourceFromCredentials(credentials)
120163
await write(didResource, outFile)
121164
}
122165
)

src/messaging/CredentialApiMessageType.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* found in the LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { Attestation, Claim, Credential, CType, Quote } from '@kiltprotocol/core'
8+
import { Attestation, Claim, Credential } from '@kiltprotocol/legacy-credentials'
9+
import { CType } from '@kiltprotocol/credentials'
910
import { DataUtils } from '@kiltprotocol/utils'
1011
import * as Did from '@kiltprotocol/did'
1112
import { isHex } from '@polkadot/util'
@@ -23,6 +24,8 @@ import {
2324
import * as MessageError from './Error.js'
2425
import type { IMessage, CredentialApiMessageBody } from '../types/index.js'
2526
import { verifyMessageEnvelope } from './MessageEnvelope.js'
27+
import { validateQuoteSchema } from '../quote/Quote.js'
28+
import { QuoteSchema } from '../quote/QuoteSchema.js'
2629

2730
/**
2831
* Checks if the message body is well-formed.
@@ -38,15 +41,15 @@ export function assertKnownMessageBody(message: IMessage): void {
3841
DataUtils.verifyIsHex(body.content.delegationId)
3942
}
4043
if (body.content.quote) {
41-
Quote.validateQuoteSchema(Quote.QuoteSchema, body.content.quote)
44+
validateQuoteSchema(QuoteSchema, body.content.quote)
4245
}
4346
if (body.content.cTypes) {
4447
body.content.cTypes.forEach((val) => CType.verifyDataStructure(val))
4548
}
4649
} else if (isRequestAttestation(message)) {
4750
Credential.verifyDataStructure(message.body.content.credential)
4851
if (message.body.content.quote) {
49-
Quote.validateQuoteSchema(Quote.QuoteSchema, message.body.content.quote)
52+
validateQuoteSchema(QuoteSchema, message.body.content.quote)
5053
}
5154
} else if (isSubmitAttestation(message)) {
5255
Attestation.verifyDataStructure(message.body.content.attestation)
@@ -55,9 +58,9 @@ export function assertKnownMessageBody(message: IMessage): void {
5558
throw new MessageError.HashMalformedError()
5659
}
5760
} else if (isIRequestCredential(message)) {
58-
message.body.content.cTypes.forEach(({ cTypeHash, trustedAttesters, requiredProperties }) => {
61+
message.body.content.cTypes.forEach(({ cTypeHash, trustedIssuers, requiredProperties }) => {
5962
DataUtils.verifyIsHex(cTypeHash)
60-
trustedAttesters?.forEach((did) => Did.validateUri(did, 'Did'))
63+
trustedIssuers?.forEach((did) => Did.validateDid(did, 'Did'))
6164
requiredProperties?.forEach((requiredProps) => {
6265
if (typeof requiredProps !== 'string') throw new TypeError('Required properties is expected to be a string')
6366
})
@@ -87,16 +90,16 @@ export function assertKnownMessageBody(message: IMessage): void {
8790
export function ensureOwnerIsSender(message: IMessage): void {
8891
if (isRequestAttestation(message)) {
8992
if (!Did.isSameSubject(message.body.content.credential.claim.owner, message.sender)) {
90-
throw new MessageError.IdentityMismatchError('Claim', 'Sender')
93+
throw new MessageError.IdentityMismatchError('Sender not matching claim owner')
9194
}
9295
} else if (isSubmitAttestation(message)) {
9396
if (!Did.isSameSubject(message.body.content.attestation.owner, message.sender)) {
94-
throw new MessageError.IdentityMismatchError('Attestation', 'Sender')
97+
throw new MessageError.IdentityMismatchError('Sender not matching attestation issuer')
9598
}
9699
} else if (isSubmitCredential(message)) {
97100
message.body.content.forEach((presentation) => {
98101
if (!Did.isSameSubject(presentation.claim.owner, message.sender)) {
99-
throw new MessageError.IdentityMismatchError('Claims', 'Sender')
102+
throw new MessageError.IdentityMismatchError('Sender not matching claim owner')
100103
}
101104
})
102105
}

0 commit comments

Comments
 (0)