import type { ReadStream , WriteStream } from "@iota/util.js" ; import { IReferenceUnlockBlock , REFERENCE_UNLOCK_BLOCK_TYPE } from "../models/IReferenceUnlockBlock" ; import { ISignatureUnlockBlock , SIGNATURE_UNLOCK_BLOCK_TYPE } from "../models/ISignatureUnlockBlock" ; import type { ITypeBase } from "../models/ITypeBase" ; import { SMALL_TYPE_LENGTH , UINT16_SIZE } from "./common" ; import { deserializeSignature , MIN_SIGNATURE_LENGTH , serializeSignature } from "./signature" ; /**
 * The minimum length of an unlock block binary representation.
 */ export const MIN_UNLOCK_BLOCK_LENGTH = SMALL_TYPE_LENGTH ; /**
 * The minimum length of a signature unlock block binary representation.
 */ export const MIN_SIGNATURE_UNLOCK_BLOCK_LENGTH = MIN_UNLOCK_BLOCK_LENGTH + MIN_SIGNATURE_LENGTH ; /**
 * The minimum length of a reference unlock block binary representation.
 */ export const MIN_REFERENCE_UNLOCK_BLOCK_LENGTH = MIN_UNLOCK_BLOCK_LENGTH + UINT16_SIZE ; param readStream returns export function deserializeUnlockBlocks ( readStream ) { const numUnlockBlocks = readStream . readUInt16 ( "transactionEssence.numUnlockBlocks" ) ; const unlockBlocks = [ ] ; for ( let i = 0 ; i < numUnlockBlocks ; i ++ ) { unlockBlocks . push ( deserializeUnlockBlock ( readStream ) ) ; } return unlockBlocks ; } param writeStream param objects export function serializeUnlockBlocks ( writeStream , objects ) { writeStream . writeUInt16 ( "transactionEssence.numUnlockBlocks" , objects . length ) ; for ( let i = 0 ; i < objects . length ; i ++ ) { serializeUnlockBlock ( writeStream , objects [ i ] ) ; } } param readStream returns export function deserializeUnlockBlock ( readStream ) { if ( ! readStream . hasRemaining ( MIN_UNLOCK_BLOCK_LENGTH ) ) { throw new Error ( `Unlock Block data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_UNLOCK_BLOCK_LENGTH }` ) ; } const type = readStream . readByte ( "unlockBlock.type" , false ) ; let unlockBlock ; if ( type === SIGNATURE_UNLOCK_BLOCK_TYPE ) { unlockBlock = deserializeSignatureUnlockBlock ( readStream ) ; } else if ( type === REFERENCE_UNLOCK_BLOCK_TYPE ) { unlockBlock = deserializeReferenceUnlockBlock ( readStream ) ; } else { throw new Error ( `Unrecognized unlock block type ${ type }` ) ; } return unlockBlock ; } param writeStream param object export function serializeUnlockBlock ( writeStream , object ) { if ( object . type === SIGNATURE_UNLOCK_BLOCK_TYPE ) { serializeSignatureUnlockBlock ( writeStream , object ) ; } else if ( object . type === REFERENCE_UNLOCK_BLOCK_TYPE ) { serializeReferenceUnlockBlock ( writeStream , object ) ; } else { throw new Error ( `Unrecognized unlock block type ${ ( object as ITypeBase < number > ) . type }` ) ; } } param readStream returns export function deserializeSignatureUnlockBlock ( readStream ) { if ( ! readStream . hasRemaining ( MIN_SIGNATURE_UNLOCK_BLOCK_LENGTH ) ) { throw new Error ( `Signature Unlock Block data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_SIGNATURE_UNLOCK_BLOCK_LENGTH }` ) ; } const type = readStream . readByte ( "signatureUnlockBlock.type" ) ; if ( type !== SIGNATURE_UNLOCK_BLOCK_TYPE ) { throw new Error ( `Type mismatch in signatureUnlockBlock ${ type }` ) ; } const signature = deserializeSignature ( readStream ) ; return { type : SIGNATURE_UNLOCK_BLOCK_TYPE , signature } ; } param writeStream param object export function serializeSignatureUnlockBlock ( writeStream , object ) { writeStream . writeByte ( "signatureUnlockBlock.type" , object . type ) ; serializeSignature ( writeStream , object . signature ) ; } param readStream returns export function deserializeReferenceUnlockBlock ( readStream ) { if ( ! readStream . hasRemaining ( MIN_REFERENCE_UNLOCK_BLOCK_LENGTH ) ) { throw new Error ( `Reference Unlock Block data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_REFERENCE_UNLOCK_BLOCK_LENGTH }` ) ; } const type = readStream . readByte ( "referenceUnlockBlock.type" ) ; if ( type !== REFERENCE_UNLOCK_BLOCK_TYPE ) { throw new Error ( `Type mismatch in referenceUnlockBlock ${ type }` ) ; } const reference = readStream . readUInt16 ( "referenceUnlockBlock.reference" ) ; return { type : REFERENCE_UNLOCK_BLOCK_TYPE , reference } ; } param writeStream param object export function serializeReferenceUnlockBlock ( writeStream , object ) { writeStream . writeByte ( "referenceUnlockBlock.type" , object . type ) ; writeStream . writeUInt16 ( "referenceUnlockBlock.reference" , object . reference ) ; }