diff --git a/packages/protocol/packages/boundwitness/packages/wrapper/src/BoundWitnessWrapper.ts b/packages/protocol/packages/boundwitness/packages/wrapper/src/BoundWitnessWrapper.ts index b3a4ae7ee75..6136f94c563 100644 --- a/packages/protocol/packages/boundwitness/packages/wrapper/src/BoundWitnessWrapper.ts +++ b/packages/protocol/packages/boundwitness/packages/wrapper/src/BoundWitnessWrapper.ts @@ -5,7 +5,14 @@ import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator' import { PayloadHasher } from '@xyo-network/hash' import { PayloadBuilder } from '@xyo-network/payload-builder' import { Payload, WithMeta } from '@xyo-network/payload-model' -import { PayloadWrapper, PayloadWrapperBase } from '@xyo-network/payload-wrapper' +import { isPayloadWrapperBase, PayloadWrapper, PayloadWrapperBase } from '@xyo-network/payload-wrapper' + +export const isBoundWitnessWrapper = (value?: unknown): value is PayloadWrapper => { + if (isPayloadWrapperBase(value)) { + return typeof (value as BoundWitnessWrapper).payloadsDataHashMap === 'function' + } + return false +} export class BoundWitnessWrapper< TBoundWitness extends BoundWitness<{ schema: string }> = BoundWitness, diff --git a/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapper.ts b/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapper.ts index bdf1872aeb2..5b8d2c8d402 100644 --- a/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapper.ts +++ b/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapper.ts @@ -3,7 +3,11 @@ import { PayloadBuilder } from '@xyo-network/payload-builder' import { Payload } from '@xyo-network/payload-model' import { PayloadValidator } from '@xyo-network/payload-validator' -import { PayloadLoaderFactory, PayloadWrapperBase } from './PayloadWrapperBase' +import { isPayloadWrapperBase, PayloadLoaderFactory, PayloadWrapperBase } from './PayloadWrapperBase' + +export const isPayloadWrapper = (value?: unknown): value is PayloadWrapper => { + return isPayloadWrapperBase(value) +} export class PayloadWrapper extends PayloadWrapperBase { private static loaderFactory: PayloadLoaderFactory | null = null diff --git a/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapperBase.ts b/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapperBase.ts index 41756847e28..2d3194f75d0 100644 --- a/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapperBase.ts +++ b/packages/protocol/packages/payload/packages/wrapper/src/PayloadWrapperBase.ts @@ -2,11 +2,18 @@ import { assertEx } from '@xylabs/assert' import { Promisable } from '@xylabs/promise' import { PayloadHasher } from '@xyo-network/hash' import { PayloadBuilder } from '@xyo-network/payload-builder' -import { Payload, WithMeta } from '@xyo-network/payload-model' +import { isAnyPayload, isPayload, Payload, WithMeta } from '@xyo-network/payload-model' export type PayloadLoader = (address: string) => Promise export type PayloadLoaderFactory = () => PayloadLoader +export const isPayloadWrapperBase = (value?: unknown): value is PayloadWrapperBase => { + if (typeof value === 'object' && !Array.isArray(value)) { + return typeof (value as PayloadWrapperBase).jsonPayload === 'function' + } + return false +} + export class PayloadWrapperBase extends PayloadHasher { private _errors?: Error[] @@ -41,10 +48,22 @@ export class PayloadWrapperBase extends Payl } if (!(typeof payload === 'object')) { - throw 'Can not unwrap class that is not extended from object' + throw new TypeError('Can not unwrap value that is not extended from object') + } + + if (Array.isArray(payload)) { + throw new TypeError('Can not unwrap value that is an array') + } + + if (isPayloadWrapperBase(payload)) { + return await PayloadBuilder.build(payload.jsonPayload()) + } + + if (isAnyPayload(payload)) { + return await PayloadBuilder.build(payload) } - return await PayloadBuilder.build(payload instanceof PayloadWrapperBase ? payload.jsonPayload() : payload) + throw new TypeError('Can not unwrap an object that is not a PayloadWrapper or Payload') } /** @deprecated use jsonPayload instead */