Skip to content

Commit

Permalink
fix: Remove step from channel payload
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Mar 5, 2024
1 parent 591826a commit af22970
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 41 deletions.
6 changes: 1 addition & 5 deletions packages/maake-oob/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface Msg {
encryptedPayload: string
fulfillRequest: boolean
msgId: string
step: string
}

export type Channel = C.Channel<Codec, Service>
Expand Down Expand Up @@ -84,7 +83,6 @@ function isProperMessage(x: unknown): x is {
encryptedPayload: string
fulfillRequest: boolean
msgId: string
step: string
} {
return (
typeof x === 'object' &&
Expand All @@ -93,10 +91,8 @@ function isProperMessage(x: unknown): x is {
'encryptedPayload' in x &&
'fulfillRequest' in x &&
'msgId' in x &&
'step' in x &&
typeof x.did === 'string' &&
typeof x.fulfillRequest === 'boolean' &&
typeof x.msgId === 'string' &&
typeof x.step === 'string'
typeof x.msgId === 'string'
)
}
18 changes: 4 additions & 14 deletions packages/maake-oob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Provider<Payload> extends Emittery<ProviderEvents<Payload>> {
if (!result.admissible) return

// Handshake approved
if (msg.step === 'handshake') {
if (result.handshake) {
await this.emit('new-consumer', {
did: msg.did,
answer: answerFn(session),
Expand Down Expand Up @@ -224,7 +224,7 @@ export class Consumer<Payload> extends Emittery<ConsumerEvents<Payload>> {
})

// Initiate handshake
const response = await session.send('handshake', this.#did, {
const response = await session.send(this.#did, {
handshake: {
challenge: this.#outOfBandParameters.challenge,
},
Expand Down Expand Up @@ -265,12 +265,7 @@ function answerFn<Payload>(session: Session<Payload>) {
payload: Payload,
timeout?: number
): Promise<Result<Payload>> => {
const response = await session.answer(
'tunnel',
msgId,
{ tunnel: payload },
timeout
)
const response = await session.answer(msgId, { tunnel: payload }, timeout)

if (response.error === undefined) {
if (response.result.tunnel === undefined)
Expand All @@ -292,12 +287,7 @@ function sendFn<Payload>(session: Session<Payload>) {
payload: Payload,
timeout?: number
): Promise<Result<Payload>> => {
const response = await session.send(
'tunnel',
msgId,
{ tunnel: payload },
timeout
)
const response = await session.send(msgId, { tunnel: payload }, timeout)

if (response.error === undefined) {
if (response.result.tunnel === undefined)
Expand Down
29 changes: 7 additions & 22 deletions packages/maake-oob/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ export abstract class Session<Payload> {
fulfillRequest,
msgId,
payload,
step,
timeout,
}: {
fulfillRequest: boolean
msgId: string
payload: MaakePayload<Payload>
step: string
timeout?: number
}): Promise<Result<MaakePayload<Payload>>> {
const response = await this.config.channel.request(
Expand All @@ -128,7 +126,6 @@ export abstract class Session<Payload> {
encryptedPayload: this.#encodeAndEncrypt(payload),
fulfillRequest,
msgId,
step,
},
timeout
)
Expand All @@ -147,7 +144,6 @@ export abstract class Session<Payload> {
}

async answer(
step: string,
msgId: string,
payload: MaakePayload<Payload>,
timeout?: number
Expand All @@ -156,13 +152,11 @@ export abstract class Session<Payload> {
fulfillRequest: true,
msgId,
payload,
step,
timeout,
})
}

async send(
step: string,
msgId: string,
payload: MaakePayload<Payload>,
timeout?: number
Expand All @@ -171,7 +165,6 @@ export abstract class Session<Payload> {
fulfillRequest: false,
msgId,
payload,
step,
timeout,
})
}
Expand All @@ -182,33 +175,25 @@ export abstract class Session<Payload> {
msg: Channel.Msg
): Promise<
| { admissible: false; reason: string }
| { admissible: true; payload: MaakePayload<Payload> }
| { admissible: true; payload: MaakePayload<Payload>; handshake: boolean }
> {
if (msg.did !== this.config.remoteDID)
return {
admissible: false,
reason: 'DID did not match the remote peer DID.',
}

switch (msg.step) {
case 'handshake': {
const payload = this.#decryptAndDecode(msg.encryptedPayload)
await this.handshake(msg, payload)
this.#handshakeCompleted = true
return { admissible: true, payload }
}
}

if (this.#handshakeCompleted)
return {
admissible: true,
handshake: false,
payload: this.#decryptAndDecode(msg.encryptedPayload),
}

return {
admissible: false,
reason: 'Handshake not completed yet.',
}
const payload = this.#decryptAndDecode(msg.encryptedPayload)
await this.handshake(msg, payload)
this.#handshakeCompleted = true
return { admissible: true, handshake: true, payload }
}

abstract handshake(
Expand Down Expand Up @@ -247,7 +232,7 @@ export class ProviderSession<Payload> extends Session<Payload> {
throw new Error(`Challenge failed during handshake with ${msg.did}`)
}

this.answer('handshake', msg.did, {
this.answer(msg.did, {
handshake: { approved: true },
}).catch((error) => {
throw error
Expand Down

0 comments on commit af22970

Please sign in to comment.