Skip to content

Commit

Permalink
Merge branch 'main' into interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Ad96el committed Oct 30, 2023
2 parents 614406b + d6e3cdf commit 9ff643e
Show file tree
Hide file tree
Showing 29 changed files with 1,957 additions and 1,670 deletions.
22 changes: 18 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "never"]

"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
yarn.lock
yarn-error.log
/.vscode

/dist
*.js
*.d.ts
8 changes: 4 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"tabWidth": 2
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"tabWidth": 2
}
8 changes: 7 additions & 1 deletion jest.config.js → jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ module.exports = {
collectCoverageFrom: ['**/*/src/**/*.ts'],
rootDir: 'src',
coverageDirectory: 'coverage',
moduleDirectories: ['node_modules'],
moduleDirectories: [ 'dist', 'node_modules'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.cjs.json'
}
},
resolver: 'ts-jest-resolver',
}
25 changes: 10 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
{
"name": "kilt-extension-api",
"version": "0.1.0",
"main": "./index.js",
"types": "./index.d.ts",
"type": "commonjs",
"main": "./dist/cjs/index.js",
"types": "./dist/cjs/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./wellKnownDidConfiguration": {
"types": "./wellKnownDidConfiguration/index.d.ts",
"default": "./wellKnownDidConfiguration/index.js"
},
"./types": {
"types": "./types/index.d.ts"
}
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"default": "./dist/cjs/index.js"
},
"author": "KILT <[email protected]>",
"license": "BSD-4-Clause",
"scripts": {
"lint": "eslint --ext .ts .",
"build": "tsc -p tsconfig.build.json",
"build": "yarn build:cjs && yarn build:esm",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.json",
"clean": "exec git clean -xf -e 'node_modules' '*'",
"test": "jest test"
},
Expand All @@ -35,6 +29,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"ts-jest-resolver": "^2.0.1",
"@polkadot/util-crypto": "^12.3.2",
"@types/jest": "^28.0.0",
"@types/node": "^18.8.2",
Expand Down
6 changes: 3 additions & 3 deletions src/cli/createDidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { u8aEq } from '@polkadot/util'
import { readFile, writeFile } from 'fs/promises'
import yargs from 'yargs/yargs'

import { didConfigResourceFromCredential, createCredential } from '../wellKnownDidConfiguration'
import { DidConfigResource } from '../types'
import { didConfigResourceFromCredential, createCredential } from '../wellKnownDidConfiguration/index.js'
import { DidConfigResource } from '../types/index.js'

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

Expand All @@ -37,7 +37,7 @@ const createCredentialOpts = {
description: 'Mnemonic or seed for the assertionMethod key to be used for issuing a new credential.',
demandOption: true,
},
keyType: { alias: 't', choices: ['sr25519', 'ed25519', 'ecdsa'] as const, default: 'sr25519' },
keyType: { alias: 't', choices: ['sr25519', 'ed25519', 'ecdsa'] as const, default: 'ed25519' },
wsAddress: { alias: 'w', type: 'string', demandOption: true, default: 'wss://spiritnet.kilt.io' },
} as const

Expand Down
9 changes: 1 addition & 8 deletions src/getExtension/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/**
* 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 { ApiWindow, InjectedWindowProvider, PubSubSessionV1, PubSubSessionV2 } from '../types'
import { ApiWindow, InjectedWindowProvider, PubSubSessionV1, PubSubSessionV2 } from '../types/index.js'

// cross-environment reference to global object (aka 'window' in browser environments)
const apiWindow = globalThis as Window & ApiWindow
Expand Down
12 changes: 3 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/**
* 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.
*/
export * as Message from './messaging/index.js'
export * as Quote from './quote/index.js'

export { getExtensions, watchExtensions, initializeKiltExtensionAPI } from './getExtension'
export * as Message from './messaging'
export * as Quote from './quote'
export { getExtensions, watchExtensions, initializeKiltExtensionAPI } from './getExtension/index.js'
30 changes: 4 additions & 26 deletions src/messaging/CredentialApiMessageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
isIConfirmPayment,
} from '../utils'
import * as MessageError from './Error'
import type { IMessage, MessageBody } from '../types'
import type { IMessage, CredentialApiMessageBody } from '../types'
import { verifyMessageEnvelope } from './MessageEnvelope'

/**
* Checks if the message body is well-formed.
Expand Down Expand Up @@ -101,38 +102,15 @@ export function ensureOwnerIsSender(message: IMessage): void {
}
}

/**
* Checks if the message object is well-formed.
*
* @param message The message object.
*/
export function verifyMessageEnvelope(message: IMessage): void {
const { messageId, createdAt, receiver, sender, receivedAt, inReplyTo } = message
if (messageId !== undefined && typeof messageId !== 'string') {
throw new TypeError('Message id is expected to be a string')
}
if (createdAt !== undefined && typeof createdAt !== 'number') {
throw new TypeError('Created at is expected to be a number')
}
if (receivedAt !== undefined && typeof receivedAt !== 'number') {
throw new TypeError('Received at is expected to be a number')
}
Did.validateUri(sender, 'Did')
Did.validateUri(receiver, 'Did')
if (inReplyTo && typeof inReplyTo !== 'string') {
throw new TypeError('In reply to is expected to be a string')
}
}

/**
* 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 assertKnownMessage(decryptedMessage: IMessage): MessageBody {
export function assertKnownMessage(decryptedMessage: IMessage): CredentialApiMessageBody {
assertKnownMessageBody(decryptedMessage)
verifyMessageEnvelope(decryptedMessage)
ensureOwnerIsSender(decryptedMessage)
return decryptedMessage.body as MessageBody
return decryptedMessage.body as CredentialApiMessageBody
}
14 changes: 7 additions & 7 deletions src/messaging/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export declare class MessageError extends Error {}
export declare class MessageError extends Error { }
export declare class HashMalformedError extends MessageError {
constructor(hash?: string, type?: string)
}

export declare class SignatureMalformedError extends MessageError {}
export declare class UnknownMessageBodyTypeError extends MessageError {}
export declare class DecodingMessageError extends MessageError {}
export declare class CTypeUnknownPropertiesError extends MessageError {}
export declare class InvalidDidFormatError extends MessageError {}
export declare class KeyError extends MessageError {}
export declare class SignatureMalformedError extends MessageError { }
export declare class UnknownMessageBodyTypeError extends MessageError { }
export declare class DecodingMessageError extends MessageError { }
export declare class CTypeUnknownPropertiesError extends MessageError { }
export declare class InvalidDidFormatError extends MessageError { }
export declare class KeyError extends MessageError { }
export declare class DidError extends MessageError {
constructor(context?: string, type?: string)
}
Expand Down
15 changes: 4 additions & 11 deletions src/messaging/Message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ import {
makeSigningKeyTool,
} from '../tests'
import { fromBody, verifyRequiredCTypeProperties } from './utils'

import {
ensureOwnerIsSender,
assertKnownMessage,
verifyMessageEnvelope,
assertKnownMessageBody,
} from './CredentialApiMessageType'
import { decrypt, encrypt } from './MessageEnvelope.'

import { decrypt, encrypt, verifyMessageEnvelope } from './MessageEnvelope'
import { ensureOwnerIsSender, assertKnownMessage, assertKnownMessageBody } from './CredentialApiMessageType'
import type {
IEncryptedMessage,
IMessage,
Expand Down Expand Up @@ -565,8 +558,8 @@ describe('Error checking / Verification', () => {

// Claim
claim = Claim.fromCTypeAndClaimContents(testCType, claimContents, identityAlice.uri)
// Legitimation
;[legitimation] = await buildCredential(identityAlice.uri, identityBob.uri, {}, [])
// Legitimation
;[legitimation] = await buildCredential(identityAlice.uri, identityBob.uri, {}, [])
// Quote Data
quoteData = {
attesterDid: identityAlice.uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,29 @@ import * as MessageError from './Error'
import { hexToU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util'

import type { IEncryptedMessage, IEncryptedMessageContents, IMessage } from '../types'
import { verifyMessageEnvelope } from './CredentialApiMessageType'

/**
* Checks if the message object is well-formed.
*
* @param message The message object.
*/
export function verifyMessageEnvelope(message: IMessage): void {
const { messageId, createdAt, receiver, sender, receivedAt, inReplyTo } = message
if (messageId !== undefined && typeof messageId !== 'string') {
throw new TypeError('Message id is expected to be a string')
}
if (createdAt !== undefined && typeof createdAt !== 'number') {
throw new TypeError('Created at is expected to be a number')
}
if (receivedAt !== undefined && typeof receivedAt !== 'number') {
throw new TypeError('Received at is expected to be a number')
}
Did.validateUri(sender, 'Did')
Did.validateUri(receiver, 'Did')
if (inReplyTo && typeof inReplyTo !== 'string') {
throw new TypeError('In reply to is expected to be a string')
}
}

/**
* Symmetrically decrypts the result of [[encrypt]].
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
*/

export { fromBody } from './utils'
export { encrypt, decrypt } from './MessageEnvelope.'
export { encrypt, decrypt } from './MessageEnvelope'
export { assertKnownMessage } from './CredentialApiMessageType'
export * from './Error'
8 changes: 4 additions & 4 deletions src/messaging/interface/attestation/Attestation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
isSubmitAttestation,
isSubmitTerms,
} from '../../../utils'
import { decrypt } from '../../MessageEnvelope.'
import { decrypt } from '../../MessageEnvelope'
import { verifyAttesterSignedQuote, verifyQuoteAgreement } from '../../../quote'

describe('Attestation', () => {
Expand Down Expand Up @@ -97,19 +97,19 @@ describe('Attestation', () => {
//give alice 10 KILT
await fundAccount(aliceAccount.address, new BN('10000000000000000'))
aliceFullDid = await generateDid(aliceAccount, aliceMnemonic)
const keyPairsAlice = await keypairs(aliceAccount, aliceMnemonic)
const keyPairsAlice = await keypairs(aliceMnemonic)
aliceEncryptCallback = makeEncryptCallback(keyPairsAlice.keyAgreement)(aliceFullDid)
aliceDecryptCallback = makeDecryptCallback(keyPairsAlice.keyAgreement)
aliceSign = makeSignCallback(keyPairsAlice.authentication)
aliceSignAssertion = makeSignCallback(keyPairsAlice.assertion)
aliceSignAssertion = makeSignCallback(keyPairsAlice.assertionMethod)

//setup bob
const bobMnemonic = mnemonicGenerate()
bobAccount = new Keyring({}).addFromMnemonic(bobMnemonic) as KiltKeyringPair
//give bob 10 KILT
await fundAccount(bobAccount.address, new BN('10000000000000000'))
bobFullDid = await generateDid(bobAccount, bobMnemonic)
const keyPairsBob = await keypairs(bobAccount, bobMnemonic)
const keyPairsBob = await keypairs(bobMnemonic)
bobEncryptCallback = makeEncryptCallback(keyPairsBob.keyAgreement)(bobFullDid)
bobDecryptCallback = makeDecryptCallback(keyPairsBob.keyAgreement)
bobSign = makeSignCallback(keyPairsBob.authentication)
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/interface/attestation/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '../../../types'
import { isIConfirmPayment, isIRequestPayment, isRequestAttestation } from '../../../utils'
import { fromBody } from '../../utils'
import { decrypt, encrypt } from '../../MessageEnvelope.'
import { decrypt, encrypt } from '../../MessageEnvelope.js'
import { assertKnownMessage } from '../../CredentialApiMessageType'
import { verifyQuoteAgreement } from '../../../quote'
import { isNumber } from '@polkadot/util'
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/interface/attestation/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ISession,
ISubmitAttestation,
} from '../../../types'
import { decrypt, encrypt } from '../../MessageEnvelope.'
import { decrypt, encrypt } from '../../MessageEnvelope'
import { isIRequestPayment, isRequestAttestation, isSubmitAttestation, isSubmitTerms } from '../../../utils'
import { fromBody } from '../../utils'
import { createQuoteAgreement, verifyAttesterSignedQuote } from '../../../quote'
Expand Down
8 changes: 4 additions & 4 deletions src/messaging/interface/verifier/Verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { receiveSessionRequest, requestSession, verifySession } from '../session
import { IRequestCredential, ISession, ISessionRequest, ISubmitCredential } from '../../../types'
import { requestCredential, submitCredential, verifySubmittedCredentialMessage } from '.'
import { isIRequestCredential, isSubmitCredential } from '../../../utils'
import { decrypt } from '../../MessageEnvelope.'
import { decrypt } from '../../MessageEnvelope'

describe('Verifier', () => {
//Alice
Expand Down Expand Up @@ -76,18 +76,18 @@ describe('Verifier', () => {
aliceAccount = new Keyring({}).addFromMnemonic(aliceMnemonic) as KiltKeyringPair
await fundAccount(aliceAccount.address, new BN('10000000000000000'))
aliceFullDid = await generateDid(aliceAccount, aliceMnemonic)
const keyPairsAlice = await keypairs(aliceAccount, aliceMnemonic)
const keyPairsAlice = await keypairs(aliceMnemonic)
aliceEncryptCallback = makeEncryptCallback(keyPairsAlice.keyAgreement)(aliceFullDid)
aliceDecryptCallback = makeDecryptCallback(keyPairsAlice.keyAgreement)
aliceSign = makeSignCallback(keyPairsAlice.authentication)
aliceSignAssertion = makeSignCallback(keyPairsAlice.assertion)
aliceSignAssertion = makeSignCallback(keyPairsAlice.assertionMethod)

// Setup Bob
const bobMnemonic = mnemonicGenerate()
const bobAccount = new Keyring({}).addFromMnemonic(bobMnemonic) as KiltKeyringPair
await fundAccount(bobAccount.address, new BN('10000000000000000'))
bobFullDid = await generateDid(bobAccount, bobMnemonic)
const keyPairsBob = await keypairs(bobAccount, bobMnemonic)
const keyPairsBob = await keypairs(bobMnemonic)
bobEncryptCallback = makeEncryptCallback(keyPairsBob.keyAgreement)(bobFullDid)
bobDecryptCallback = makeDecryptCallback(keyPairsBob.keyAgreement)
bobSign = makeSignCallback(keyPairsBob.authentication)
Expand Down
Loading

0 comments on commit 9ff643e

Please sign in to comment.