import type { ReadStream , WriteStream } from "@iota/util.js" ; import bigInt from "big-integer" ; import { ISigLockedDustAllowanceOutput , SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE } from "../models/ISigLockedDustAllowanceOutput" ; import { ISigLockedSingleOutput , SIG_LOCKED_SINGLE_OUTPUT_TYPE } from "../models/ISigLockedSingleOutput" ; import { ITreasuryOutput , TREASURY_OUTPUT_TYPE } from "../models/ITreasuryOutput" ; import type { ITypeBase } from "../models/ITypeBase" ; import { deserializeAddress , MIN_ADDRESS_LENGTH , MIN_ED25519_ADDRESS_LENGTH , serializeAddress } from "./address" ; import { SMALL_TYPE_LENGTH , UINT64_SIZE } from "./common" ; /**
 * The minimum length of an output binary representation.
 */ export const MIN_OUTPUT_LENGTH = SMALL_TYPE_LENGTH ; /**
 * The minimum length of a sig locked single output binary representation.
 */ export const MIN_SIG_LOCKED_SINGLE_OUTPUT_LENGTH = MIN_OUTPUT_LENGTH + MIN_ADDRESS_LENGTH + MIN_ED25519_ADDRESS_LENGTH ; /**
 * The minimum length of a sig locked dust allowance output binary representation.
 */ export const MIN_SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_LENGTH = MIN_OUTPUT_LENGTH + MIN_ADDRESS_LENGTH + MIN_ED25519_ADDRESS_LENGTH ; /**
 * The minimum length of a treasury output binary representation.
 */ export const MIN_TREASURY_OUTPUT_LENGTH = MIN_OUTPUT_LENGTH + UINT64_SIZE ; /**
 * The minimum number of outputs.
 */ export const MIN_OUTPUT_COUNT = 1 ; /**
 * The maximum number of outputs.
 */ export const MAX_OUTPUT_COUNT = 127 ; param readStream returns export function deserializeOutputs ( readStream ) { const numOutputs = readStream . readUInt16 ( "outputs.numOutputs" ) ; const inputs = [ ] ; for ( let i = 0 ; i < numOutputs ; i ++ ) { inputs . push ( deserializeOutput ( readStream ) ) ; } return inputs ; } param writeStream param objects export function serializeOutputs ( writeStream , objects ) { if ( objects . length < MIN_OUTPUT_COUNT ) { throw new Error ( `The minimum number of outputs is ${ MIN_OUTPUT_COUNT }, you have provided ${ objects . length }` ) ; } if ( objects . length > MAX_OUTPUT_COUNT ) { throw new Error ( `The maximum number of outputs is ${ MAX_OUTPUT_COUNT }, you have provided ${ objects . length }` ) ; } writeStream . writeUInt16 ( "outputs.numOutputs" , objects . length ) ; for ( let i = 0 ; i < objects . length ; i ++ ) { serializeOutput ( writeStream , objects [ i ] ) ; } } param readStream returns export function deserializeOutput ( readStream ) { if ( ! readStream . hasRemaining ( MIN_OUTPUT_LENGTH ) ) { throw new Error ( `Output data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_OUTPUT_LENGTH }` ) ; } const type = readStream . readByte ( "output.type" , false ) ; let input ; if ( type === SIG_LOCKED_SINGLE_OUTPUT_TYPE ) { input = deserializeSigLockedSingleOutput ( readStream ) ; } else if ( type === SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE ) { input = deserializeSigLockedDustAllowanceOutput ( readStream ) ; } else if ( type === TREASURY_OUTPUT_TYPE ) { input = deserializeTreasuryOutput ( readStream ) ; } else { throw new Error ( `Unrecognized output type ${ type }` ) ; } return input ; } param writeStream param object export function serializeOutput ( writeStream , object ) { if ( object . type === SIG_LOCKED_SINGLE_OUTPUT_TYPE ) { serializeSigLockedSingleOutput ( writeStream , object as ISigLockedSingleOutput ) ; } else if ( object . type === SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE ) { serializeSigLockedDustAllowanceOutput ( writeStream , object as ISigLockedDustAllowanceOutput ) ; } else if ( object . type === TREASURY_OUTPUT_TYPE ) { serializeTreasuryOutput ( writeStream , object as ITreasuryOutput ) ; } else { throw new Error ( `Unrecognized output type ${ object . type }` ) ; } } param readStream returns export function deserializeSigLockedSingleOutput ( readStream ) { if ( ! readStream . hasRemaining ( MIN_SIG_LOCKED_SINGLE_OUTPUT_LENGTH ) ) { throw new Error ( `Signature Locked Single Output data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_SIG_LOCKED_SINGLE_OUTPUT_LENGTH }` ) ; } const type = readStream . readByte ( "sigLockedSingleOutput.type" ) ; if ( type !== SIG_LOCKED_SINGLE_OUTPUT_TYPE ) { throw new Error ( `Type mismatch in sigLockedSingleOutput ${ type }` ) ; } const address = deserializeAddress ( readStream ) ; const amount = readStream . readUInt64 ( "sigLockedSingleOutput.amount" ) ; return { type : SIG_LOCKED_SINGLE_OUTPUT_TYPE , address , amount : Number ( amount ) } ; } param writeStream param object export function serializeSigLockedSingleOutput ( writeStream , object ) { writeStream . writeByte ( "sigLockedSingleOutput.type" , object . type ) ; serializeAddress ( writeStream , object . address ) ; writeStream . writeUInt64 ( "sigLockedSingleOutput.amount" , bigInt ( object . amount ) ) ; } param readStream returns export function deserializeSigLockedDustAllowanceOutput ( readStream ) { if ( ! readStream . hasRemaining ( MIN_SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_LENGTH ) ) { throw new Error ( `Signature Locked Dust Allowance Output data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_LENGTH }` ) ; } const type = readStream . readByte ( "sigLockedDustAllowanceOutput.type" ) ; if ( type !== SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE ) { throw new Error ( `Type mismatch in sigLockedDustAllowanceOutput ${ type }` ) ; } const address = deserializeAddress ( readStream ) ; const amount = readStream . readUInt64 ( "sigLockedDustAllowanceOutput.amount" ) ; return { type : SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE , address , amount : Number ( amount ) } ; } param writeStream param object export function serializeSigLockedDustAllowanceOutput ( writeStream , object ) { writeStream . writeByte ( "sigLockedDustAllowanceOutput.type" , object . type ) ; serializeAddress ( writeStream , object . address ) ; writeStream . writeUInt64 ( "sigLockedDustAllowanceOutput.amount" , bigInt ( object . amount ) ) ; } param readStream returns export function deserializeTreasuryOutput ( readStream ) { if ( ! readStream . hasRemaining ( MIN_TREASURY_OUTPUT_LENGTH ) ) { throw new Error ( `Treasury Output data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_TREASURY_OUTPUT_LENGTH }` ) ; } const type = readStream . readByte ( "treasuryOutput.type" ) ; if ( type !== TREASURY_OUTPUT_TYPE ) { throw new Error ( `Type mismatch in treasuryOutput ${ type }` ) ; } const amount = readStream . readUInt64 ( "treasuryOutput.amount" ) ; return { type : TREASURY_OUTPUT_TYPE , amount : Number ( amount ) } ; } param writeStream param object export function serializeTreasuryOutput ( writeStream , object ) { writeStream . writeByte ( "treasuryOutput.type" , object . type ) ; writeStream . writeUInt64 ( "treasuryOutput.amount" , bigInt ( object . amount ) ) ; }