Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: add signaling server schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Sep 8, 2023
1 parent 1eb665a commit c55f135
Showing 1 changed file with 125 additions and 1 deletion.
126 changes: 125 additions & 1 deletion src/radix-connect-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { ResultAsync } from 'neverthrow'
import type { z } from 'zod'
import type { z, ZodError } from 'zod'
import { array, boolean, literal, number, object, string, union } from 'zod'

/**
* Wallet schemas
*/

export type Account = z.infer<typeof Account>
export const Account = object({
address: string(),
Expand Down Expand Up @@ -379,3 +383,123 @@ export type CallbackFns = {
getRequest: () => WalletInteraction
}) => void
}

/**
* Signaling server schemas
*/

const Offer = literal('offer')
const Answer = literal('answer')
const IceCandidate = literal('iceCandidate')
const IceCandidates = literal('iceCandidates')

const Types = union([Offer, Answer, IceCandidate, IceCandidates])

export const Sources = union([literal('wallet'), literal('extension')])

export const SignalingServerMessage = object({
requestId: string(),
targetClientId: string(),
encryptedPayload: string(),
source: Sources.optional(), // redundant, to be removed
connectionId: string().optional(), // redundant, to be removed
})

export const AnswerIO = SignalingServerMessage.extend({
method: Answer,
payload: object({
sdp: string(),
}),
})

export const OfferIO = SignalingServerMessage.extend({
method: Offer,
payload: object({
sdp: string(),
}),
})

const IceCandidatePayloadIO = object({
candidate: string(),
sdpMid: string(),
sdpMLineIndex: number(),
})

export const IceCandidateIO = SignalingServerMessage.extend({
method: IceCandidate,
payload: IceCandidatePayloadIO,
})

export const IceCandidatesIO = SignalingServerMessage.extend({
method: IceCandidates,
payload: array(IceCandidatePayloadIO),
})

export type Answer = z.infer<typeof AnswerIO>
export type Offer = z.infer<typeof OfferIO>
export type IceCandidate = z.infer<typeof IceCandidateIO>
export type IceCandidates = z.infer<typeof IceCandidatesIO>
export type MessagePayloadTypes = z.infer<typeof Types>
export type MessageSources = z.infer<typeof Sources>

export type DataTypes = Answer | IceCandidate | Offer | IceCandidates

export type Confirmation = {
info: 'confirmation'
requestId: DataTypes['requestId']
}

export type RemoteData<T extends DataTypes = DataTypes> = {
info: 'remoteData'
remoteClientId: string
requestId: T['requestId']
data: T
}

export type RemoteClientDisconnected = {
info: 'remoteClientDisconnected'
remoteClientId: string
}

export type RemoteClientJustConnected = {
info: 'remoteClientJustConnected'
remoteClientId: string
}

export type RemoteClientIsAlreadyConnected = {
info: 'remoteClientIsAlreadyConnected'
remoteClientId: string
}

export type MissingRemoteClientError = {
info: 'missingRemoteClientError'
requestId: DataTypes['requestId']
}

export type InvalidMessageError = {
info: 'invalidMessageError'
error: string
data: string
}

export type ValidationError = {
info: 'validationError'
requestId: DataTypes['requestId']
error: ZodError[]
}

export type SignalingServerResponse =
| Confirmation
| RemoteData
| RemoteClientJustConnected
| RemoteClientIsAlreadyConnected
| RemoteClientDisconnected
| MissingRemoteClientError
| InvalidMessageError
| ValidationError

export type SignalingServerErrorResponse =
| RemoteClientDisconnected
| MissingRemoteClientError
| InvalidMessageError
| ValidationError

0 comments on commit c55f135

Please sign in to comment.