import type { ReadStream , WriteStream } from "@iota/util.js" ; import { Ed25519Address } from "../addressTypes/ed25519Address" ; import { ED25519_ADDRESS_TYPE , IEd25519Address } from "../models/IEd25519Address" ; import { SMALL_TYPE_LENGTH } from "./common" ; /**
 * The minimum length of an address binary representation.
 */ export const MIN_ADDRESS_LENGTH = SMALL_TYPE_LENGTH ; /**
 * The minimum length of an ed25519 address binary representation.
 */ export const MIN_ED25519_ADDRESS_LENGTH = MIN_ADDRESS_LENGTH + Ed25519Address . ADDRESS_LENGTH ; param readStream returns export function deserializeAddress ( readStream ) { if ( ! readStream . hasRemaining ( MIN_ADDRESS_LENGTH ) ) { throw new Error ( `Address data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_ADDRESS_LENGTH }` ) ; } const type = readStream . readByte ( "address.type" , false ) ; let address ; if ( type === ED25519_ADDRESS_TYPE ) { address = deserializeEd25519Address ( readStream ) ; } else { throw new Error ( `Unrecognized address type ${ type }` ) ; } return address ; } param writeStream param object export function serializeAddress ( writeStream , object ) { if ( object . type === ED25519_ADDRESS_TYPE ) { serializeEd25519Address ( writeStream , object ) ; } else { throw new Error ( `Unrecognized address type ${ object . type }` ) ; } } param readStream returns export function deserializeEd25519Address ( readStream ) { if ( ! readStream . hasRemaining ( MIN_ED25519_ADDRESS_LENGTH ) ) { throw new Error ( `Ed25519 address data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_ED25519_ADDRESS_LENGTH }` ) ; } const type = readStream . readByte ( "ed25519Address.type" ) ; if ( type !== ED25519_ADDRESS_TYPE ) { throw new Error ( `Type mismatch in ed25519Address ${ type }` ) ; } const address = readStream . readFixedHex ( "ed25519Address.address" , Ed25519Address . ADDRESS_LENGTH ) ; return { type : ED25519_ADDRESS_TYPE , address } ; } param writeStream param object export function serializeEd25519Address ( writeStream , object ) { writeStream . writeByte ( "ed25519Address.type" , object . type ) ; writeStream . writeFixedHex ( "ed25519Address.address" , Ed25519Address . ADDRESS_LENGTH , object . address ) ; }