Skip to content

Commit

Permalink
Merge pull request #95 from jolocom/feat/interaction-listing
Browse files Browse the repository at this point in the history
Feat/interaction listing
  • Loading branch information
chunningham authored Jan 15, 2021
2 parents 1c973fa + 0ad96c0 commit 6de0ad8
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@babel/preset-typescript": "^7.10.4",
"@babel/runtime": "^7.11.2",
"@jolocom/local-resolver-registrar": "^1.0.1",
"@jolocom/sdk-storage-typeorm": "^4.0.0",
"@jolocom/sdk-storage-typeorm": "^4.1.0",
"@types/jest": "^26.0.10",
"@types/node": "^13.9.8",
"@types/node-fetch": "^2.5.5",
Expand Down Expand Up @@ -56,7 +56,7 @@
"ts-node": "^9.0.0",
"tslib": "^1.7.1",
"typedoc": "^0.19.2",
"typeorm": "^0.2.25",
"typeorm": "0.2.24",
"typescript": "^3.7.5",
"yarn": "^1.22.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/authenticationFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { isAuthenticationRequest } from './guards'

export class AuthenticationFlow extends Flow<Authentication> {
public state: AuthenticationFlowState = { description: '' }
public type = FlowType.Authentication
public static type = FlowType.Authentication
public static firstMessageType = InteractionType.Authentication

// TODO InteractionType.AuthenticaitonResponse should exist
public async handleInteractionToken(token: Authentication, interactionType: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/authorizationFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { isAuthorizationRequest, isAuthorizationResponse } from './guards'
export class AuthorizationFlow extends Flow<
AuthorizationResponse | AuthorizationRequest
> {
public type = FlowType.Authorization
public static type = FlowType.Authorization
public state: AuthorizationFlowState = {
description: '',
}
public static firstMessageType = AuthorizationType.AuthorizationRequest

public async handleInteractionToken(
token: AuthorizationRequest | AuthorizationResponse,
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/credentialOfferFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export class CredentialOfferFlow extends Flow<
credentialsValidity: [],
credentialsAllValid: true,
}
public type = FlowType.CredentialOffer
public static type = FlowType.CredentialOffer
public static firstMessageType = InteractionType.CredentialOfferRequest

public constructor(ctx: Interaction) {
super(ctx)
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/credentialRequestFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class CredentialRequestFlow extends Flow<
constraints: [],
providedCredentials: [],
}
public type = FlowType.CredentialShare
public static type = FlowType.CredentialShare
public static firstMessageType = InteractionType.CredentialRequest

constructor(ctx: Interaction) {
super(ctx)
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/decryptionFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface DecryptionFlowState extends FlowState {
export class DecryptionFlow extends Flow<
DecryptionRequest | DecryptionResponse
> {
public type = FlowType.Decrypt
public static type = FlowType.Decrypt
public state: DecryptionFlowState = {}
public static firstMessageType = DecryptionType.DecryptionRequest

public constructor(ctx: Interaction) {
super(ctx)
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/encryptionFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface EncryptionFlowState extends FlowState {
export class EncryptionFlow extends Flow<
EncryptionRequest | EncryptionResponse
> {
public type = FlowType.Encrypt
public static type = FlowType.Encrypt
public state: EncryptionFlowState = {}
public static firstMessageType = EncryptionType.EncryptionRequest

public constructor(ctx: Interaction) {
super(ctx)
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/establishChannelFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class EstablishChannelFlow extends Flow<
description: '',
established: false,
}
public type = FlowType.EstablishChannel
public static type = FlowType.EstablishChannel
public static firstMessageType = EstablishChannelType.EstablishChannelRequest

public constructor(ctx: Interaction) {
super(ctx)
Expand Down
9 changes: 8 additions & 1 deletion src/interactionManager/flow.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { Interaction } from './interaction'
import { FlowType } from './types'
import { InteractionType } from '@jolocom/protocol-ts/dist/lib/interactionTokens'

// FIXME why is this exported?
export interface FlowState { }

export abstract class Flow<T> {
protected ctx: Interaction

public abstract type: FlowType
public static type: FlowType
public abstract state: FlowState
public static firstMessageType: InteractionType | string

constructor(ctx: Interaction) {
this.ctx = ctx
}

get type() {
// @ts-ignore
return this.constructor.type
}

public getState() {
return this.state
}
Expand Down
41 changes: 23 additions & 18 deletions src/interactionManager/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CredentialOfferFlow } from './credentialOfferFlow'
import {
InteractionType,
CredentialOfferResponseSelection,
Expand All @@ -22,10 +21,8 @@ import {
SigningResponse,
SigningType,
} from './types'
import { CredentialRequestFlow } from './credentialRequestFlow'
import { Flow } from './flow'
import { CredentialOfferRequest } from 'jolocom-lib/js/interactionTokens/credentialOfferRequest'
import { AuthenticationFlow } from './authenticationFlow'
import { CredentialRequest } from 'jolocom-lib/js/interactionTokens/credentialRequest'
import { SDKError, ErrorCode } from '../errors'
import { Authentication } from 'jolocom-lib/js/interactionTokens/authentication'
Expand All @@ -36,8 +33,6 @@ import {
AuthorizationRequest,
AuthorizationFlowState,
} from './types'
import { AuthorizationFlow } from './authorizationFlow'
import { EstablishChannelFlow } from './establishChannelFlow'

import {
InteractionManager,
Expand All @@ -46,32 +41,42 @@ import {
import { SignedCredential } from 'jolocom-lib/js/credentials/signedCredential/signedCredential'
import { CredentialOfferResponse } from 'jolocom-lib/js/interactionTokens/credentialOfferResponse'

import { CredentialOfferFlow } from './credentialOfferFlow'
import { CredentialRequestFlow } from './credentialRequestFlow'
import { AuthenticationFlow } from './authenticationFlow'
import { AuthorizationFlow } from './authorizationFlow'
import { EstablishChannelFlow } from './establishChannelFlow'
import { EncryptionFlow } from './encryptionFlow'
import { DecryptionFlow } from './decryptionFlow'
import { SigningFlow } from './signingFlow'
import { generateIdentitySummary } from '../util'

import {
ResolutionType,
ResolutionFlow,
ResolutionFlowState,
ResolutionRequest,
} from './resolutionFlow'

import { generateIdentitySummary } from '../util'
import { last } from 'ramda'
import { TransportAPI, TransportDesc, InteractionTransportType } from '../types'
import { Transportable } from '../transports'

const interactionFlowForMessage = {
[InteractionType.CredentialOfferRequest]: CredentialOfferFlow,
[InteractionType.CredentialRequest]: CredentialRequestFlow,
[InteractionType.Authentication]: AuthenticationFlow,
[AuthorizationType.AuthorizationRequest]: AuthorizationFlow,
[EstablishChannelType.EstablishChannelRequest]: EstablishChannelFlow,
[EncryptionType.EncryptionRequest]: EncryptionFlow,
[DecryptionType.DecryptionRequest]: DecryptionFlow,
[ResolutionType.ResolutionRequest]: ResolutionFlow,
[SigningType.SigningRequest]: SigningFlow,
}
export const flows = [
AuthenticationFlow,
AuthorizationFlow,
CredentialOfferFlow,
CredentialRequestFlow,
EstablishChannelFlow,
SigningFlow,
EncryptionFlow,
DecryptionFlow,
ResolutionFlow
]

const interactionFlowForMessage = {}
flows.forEach(f => {
interactionFlowForMessage[f.firstMessageType] = f
})

/**
* This class is instantiated by the {@link InteractionManager} when it needs to
Expand Down
37 changes: 36 additions & 1 deletion src/interactionManager/interactionManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { JSONWebToken } from 'jolocom-lib/js/interactionTokens/JSONWebToken'
import { Interaction } from './interaction'
import { Interaction, flows } from './interaction'
import { Agent } from '../agent'
import { Flow } from './flow'
import { TransportAPI } from '../types'
import { SDKError, ErrorCode } from '../errors'
import { FlowType } from './types'

const firstMessageForFlowType = {}
flows.forEach(f => {
firstMessageForFlowType[f.type] = f.firstMessageType
})

/**
* The {@link InteractionManager} is an entry point to dealing with {@link
Expand Down Expand Up @@ -72,9 +78,38 @@ export class InteractionManager {
// interaction messages
const messages = await this.ctx.storage.get.interactionTokens({ nonce: id })
if (messages.length === 0) throw new SDKError(ErrorCode.NoSuchInteraction)

const interxn = await Interaction.fromMessages(messages, this, id, transportAPI)

this.interactions[id] = interxn

return interxn
}

/**
* Returns a list of {@link Interaction} instances given filtering and
* pagination criteria
*
* @param flows - a list of {@link FlowType}s or Flow classes
* @param take - number of results to return (pagination limit)
* @param skip - number of results to skip (pagination offset)
* @param reverse - if true, return the list in reverse storage order
*/
public async listInteractions<T>(opts?: {
flows?: Array<FlowType | { firstMessageType: string }>,
take?: number,
skip?: number,
reverse?: boolean
}): Promise<Interaction[]> {
let queryOpts = opts && {
take: opts.take,
skip: opts.skip,
...(opts.reverse && { order: { id: 'DESC' as 'DESC' } })
}
const attrs = opts && opts.flows && opts.flows.map(f => ({
type: typeof f === "string" ? firstMessageForFlowType[f] : f.firstMessageType
}))
const ids = await this.ctx.storage.get.interactionIds(attrs, queryOpts)
return Promise.all(ids.map((id: string) => this.getInteraction(id)))
}
}
3 changes: 2 additions & 1 deletion src/interactionManager/resolutionFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export const isResolutionResponse = (
): t is ResolutionResult => typ === ResolutionType.ResolutionResponse

export class ResolutionFlow extends Flow<ResolutionRequest | ResolutionResult> {
public type = FlowType.Resolution
public static type = FlowType.Resolution
public state: ResolutionFlowState = {}
public static firstMessageType = ResolutionType.ResolutionRequest

public async handleInteractionToken(
token: ResolutionRequest | ResolutionResult,
Expand Down
3 changes: 2 additions & 1 deletion src/interactionManager/signingFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export interface SigningFlowState extends FlowState {
export class SigningFlow extends Flow<SigningRequest | SigningResponse> {
public state: SigningFlowState = {}

public type = FlowType.Sign
public static type = FlowType.Sign
public static firstMessageType = SigningType.SigningRequest

public constructor(ctx: Interaction) {
super(ctx)
Expand Down
19 changes: 13 additions & 6 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export interface EncryptedWalletAttributes {

export interface FindOptions {
skip?: number
take: number
take?: number
order?: { [k: string]: 'ASC' | 'DESC' }
}

/**
Expand All @@ -40,6 +41,12 @@ export interface IStorageStore {
interactionToken(token: JSONWebToken<any>): Promise<void>
}

export interface InteractionTokenAttrs {
nonce?: string
type?: string
issuer?: string
}

export interface IStorageGet {
settingsObject(): Promise<{ [key: string]: any }>
setting(key: string): Promise<any>
Expand All @@ -54,13 +61,13 @@ export interface IStorageGet {
publicProfile(did: string): Promise<IdentitySummary>
identity(did: string): Promise<Identity | undefined>
interactionTokens(
attrs: {
nonce?: string
type?: string
issuer?: string
},
attrs?: InteractionTokenAttrs,
findOptions?: FindOptions,
): Promise<Array<JSONWebToken<any>>>
interactionIds(
attrs?: InteractionTokenAttrs | InteractionTokenAttrs[],
findOptions?: FindOptions,
): Promise<Array<string>>
}

export interface IStorageDelete {
Expand Down
Loading

0 comments on commit 6de0ad8

Please sign in to comment.