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

refactor: messaging #18

Merged
merged 39 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
40301e9
copy all types
Ad96el Aug 4, 2023
e6d2f3d
integrated messaging
Ad96el Aug 4, 2023
f442317
with messaging
Ad96el Aug 4, 2023
0ab879c
with quote
Ad96el Aug 4, 2023
7008be2
fix: make exportable
Ad96el Aug 7, 2023
5bb1159
fix: test against latest not develop
Ad96el Aug 7, 2023
819d315
fix: test against latest not develop
Ad96el Aug 7, 2023
dcc28a7
refactor: reduce types
Ad96el Aug 8, 2023
0ce054a
refactor: remove jest setup
Ad96el Aug 8, 2023
844cdbf
refactor: restructure
Ad96el Aug 9, 2023
b5042c7
chore: copyright
Ad96el Aug 9, 2023
f66bca6
remove terms
Ad96el Aug 9, 2023
e026d9c
remove newline
Ad96el Aug 9, 2023
d4fb2a0
update imports
Ad96el Aug 9, 2023
3f3aa01
add comments
Ad96el Aug 9, 2023
92f433e
docs docs docs
Ad96el Aug 9, 2023
e7dc7cc
docs docs docs
Ad96el Aug 9, 2023
d1c69c7
feat: type guards
Ad96el Aug 9, 2023
a493e93
feat: generics for message
Ad96el Aug 10, 2023
13b7dcf
feat: type guards
Ad96el Aug 10, 2023
7cc83fa
fmt
Ad96el Aug 10, 2023
7670f94
feat: errors
Ad96el Aug 10, 2023
0c97d2b
feat: catch
Ad96el Aug 10, 2023
6bfd5d8
refactor: errors
Ad96el Aug 10, 2023
878fe28
feat: default type
Ad96el Aug 10, 2023
1aa812a
newline
Ad96el Aug 11, 2023
792dc5d
refactor: change return type
Ad96el Aug 11, 2023
1b23e4c
fmt
Ad96el Aug 14, 2023
b0bb518
fix tests
Ad96el Aug 14, 2023
c76cc9b
change response
Ad96el Aug 21, 2023
dfaafcd
fix tests
Ad96el Aug 22, 2023
22073b2
namespace
Ad96el Aug 22, 2023
e06c1ec
do not export verifyMessageEnvelope
Ad96el Aug 23, 2023
0a2ffb9
reduce interface
Ad96el Aug 23, 2023
3969f72
change body type
Ad96el Aug 23, 2023
b5316c5
remove comments
Ad96el Aug 23, 2023
64f5d52
message body
Ad96el Aug 23, 2023
7d348a0
from body generic
Ad96el Aug 24, 2023
a93d110
refactor types
Ad96el Aug 29, 2023
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
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
node-version: 16
cache: 'yarn'

- name: yarn install
- name: yarn install
run: yarn install --immutable

- name: Configure AWS credentials
Expand All @@ -34,7 +34,7 @@ jobs:

- name: set image name
run: |
echo "IMG_NAME=${{ steps.login-ecr.outputs.registry }}/kilt/prototype-chain:latest-develop" >> "$GITHUB_ENV"
echo "IMG_NAME=${{ steps.login-ecr.outputs.registry }}/kilt/prototype-chain:latest" >> "$GITHUB_ENV"

- name: pull image
run: docker pull $IMG_NAME
Expand All @@ -45,7 +45,7 @@ jobs:
TESTCONTAINERS_NODE_IMG: ${{ env.IMG_NAME }}
run: |
yarn test

- name: yarn build
run: yarn build

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: set image name
run: |
echo "IMG_NAME=${{ steps.login-ecr.outputs.registry }}/kilt/prototype-chain:latest-develop" >> "$GITHUB_ENV"
echo "IMG_NAME=${{ steps.login-ecr.outputs.registry }}/kilt/prototype-chain:latest" >> "$GITHUB_ENV"

- name: pull image
run: docker pull $IMG_NAME
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/node_modules
yarn.lock
yarn-error.log
/.vscode

*.js
*.d.ts
*.d.ts
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { getExtensions, watchExtensions, initializeKiltExtensionAPI } from './getExtension'
export * from './messaging'
export * from './quote'
Ad96el marked this conversation as resolved.
Show resolved Hide resolved
133 changes: 133 additions & 0 deletions src/messaging/CredentialApiMessageTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright (c) 2018-2023, BOTLabs GmbH.
*
* This source code is licensed under the BSD 4-Clause "Original" license
* found in the LICENSE file in the root directory of this source tree.
*/

import { Attestation, Claim, Credential, CType, Quote } from '@kiltprotocol/core'
import { DataUtils, SDKErrors } from '@kiltprotocol/utils'
import * as Did from '@kiltprotocol/did'
import { isHex } from '@polkadot/util'

import {verifyMessageEnvelope} from './MessageEnvelope'
import type {
IMessage,
MessageBody,
} from '../types'

/**
* Checks if the message body is well-formed.
*
* @param body The message body.
*/
export function verifyMessageBody(body: MessageBody): void {
rflechtner marked this conversation as resolved.
Show resolved Hide resolved
switch (body.type) {
case 'submit-terms': {
Claim.verifyDataStructure(body.content.claim)
body.content.legitimations.forEach((credential) => Credential.verifyDataStructure(credential))
if (body.content.delegationId) {
DataUtils.verifyIsHex(body.content.delegationId)
}
if (body.content.quote) {
Quote.validateQuoteSchema(Quote.QuoteSchema, body.content.quote)
}
if (body.content.cTypes) {
body.content.cTypes.forEach((val) => CType.verifyDataStructure(val))
}
break
}
case 'request-attestation': {
Credential.verifyDataStructure(body.content.credential)
if (body.content.quote) {
Quote.validateQuoteSchema(Quote.QuoteSchema, body.content.quote)
}
break
}
case 'submit-attestation': {
Attestation.verifyDataStructure(body.content.attestation)
break
}
case 'reject-attestation': {
if (!isHex(body.content)) {
throw new SDKErrors.HashMalformedError()
}
break
}
case 'request-credential': {
body.content.cTypes.forEach(({ cTypeHash, trustedAttesters, requiredProperties }): void => {
DataUtils.verifyIsHex(cTypeHash)
trustedAttesters?.forEach((did) => Did.validateUri(did, 'Did'))
requiredProperties?.forEach((requiredProps) => {
if (typeof requiredProps !== 'string') throw new TypeError('Required properties is expected to be a string')
})
})
break
}
case 'submit-credential': {
body.content.forEach((presentation) => {
Credential.verifyDataStructure(presentation)
if (!Did.isDidSignature(presentation.claimerSignature)) {
throw new SDKErrors.SignatureMalformedError()
}
})
break
}

default:
throw new SDKErrors.UnknownMessageBodyTypeError()
}
}

/**
* Verifies that the sender of a [[Message]] is also the owner of it, e.g the owner's and sender's DIDs refer to the same subject.
*
* @param message The [[Message]] object which needs to be decrypted.
* @param message.body The body of the [[Message]] which depends on the [[BodyType]].
* @param message.sender The sender's DID taken from the [[IMessage]].
*/
export function ensureOwnerIsSender({ body, sender }: IMessage): void {
switch (body.type) {
case 'request-attestation':
{
const requestAttestation = body
if (!Did.isSameSubject(requestAttestation.content.credential.claim.owner, sender)) {
throw new SDKErrors.IdentityMismatchError('Claim', 'Sender')
}
}
break
case 'submit-attestation':
{
const submitAttestation = body
if (!Did.isSameSubject(submitAttestation.content.attestation.owner, sender)) {
throw new SDKErrors.IdentityMismatchError('Attestation', 'Sender')
}
}
break
case 'submit-credential':
{
const submitClaimsForCtype = body
submitClaimsForCtype.content.forEach((presentation) => {
if (!Did.isSameSubject(presentation.claim.owner, sender)) {
throw new SDKErrors.IdentityMismatchError('Claims', 'Sender')
}
})
}
break
default:
}
}

/**
* Checks the message structure and body contents (e.g. Hashes match, ensures the owner is the sender).
* Throws, if a check fails.
*
* @param decryptedMessage The decrypted message to check.
*/
export function verify(decryptedMessage: IMessage): void {
rflechtner marked this conversation as resolved.
Show resolved Hide resolved
verifyMessageBody(decryptedMessage.body)
verifyMessageEnvelope(decryptedMessage)
ensureOwnerIsSender(decryptedMessage)
}


Loading