import { Ed25519 } from "@iota/crypto.js" ; import type { ReadStream , WriteStream } from "@iota/util.js" ; import { ED25519_SIGNATURE_TYPE , IEd25519Signature } from "../models/IEd25519Signature" ; import { SMALL_TYPE_LENGTH } from "./common" ; /**
 * The minimum length of a signature binary representation.
 */ export const MIN_SIGNATURE_LENGTH = SMALL_TYPE_LENGTH ; /**
 * The minimum length of an ed25519 signature binary representation.
 */ export const MIN_ED25519_SIGNATURE_LENGTH = MIN_SIGNATURE_LENGTH + Ed25519 . SIGNATURE_SIZE + Ed25519 . PUBLIC_KEY_SIZE ; param readStream returns export function deserializeSignature ( readStream ) { if ( ! readStream . hasRemaining ( MIN_SIGNATURE_LENGTH ) ) { throw new Error ( `Signature data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_SIGNATURE_LENGTH }` ) ; } const type = readStream . readByte ( "signature.type" , false ) ; let input ; if ( type === ED25519_SIGNATURE_TYPE ) { input = deserializeEd25519Signature ( readStream ) ; } else { throw new Error ( `Unrecognized signature type ${ type }` ) ; } return input ; } param writeStream param object export function serializeSignature ( writeStream , object ) { if ( object . type === ED25519_SIGNATURE_TYPE ) { serializeEd25519Signature ( writeStream , object ) ; } else { throw new Error ( `Unrecognized signature type ${ object . type }` ) ; } } param readStream returns export function deserializeEd25519Signature ( readStream ) { if ( ! readStream . hasRemaining ( MIN_ED25519_SIGNATURE_LENGTH ) ) { throw new Error ( `Ed25519 signature data is ${ readStream . length ( ) } in length which is less than the minimimum size required of ${ MIN_ED25519_SIGNATURE_LENGTH }` ) ; } const type = readStream . readByte ( "ed25519Signature.type" ) ; if ( type !== ED25519_SIGNATURE_TYPE ) { throw new Error ( `Type mismatch in ed25519Signature ${ type }` ) ; } const publicKey = readStream . readFixedHex ( "ed25519Signature.publicKey" , Ed25519 . PUBLIC_KEY_SIZE ) ; const signature = readStream . readFixedHex ( "ed25519Signature.signature" , Ed25519 . SIGNATURE_SIZE ) ; return { type : ED25519_SIGNATURE_TYPE , publicKey , signature } ; } param writeStream param object export function serializeEd25519Signature ( writeStream , object ) { writeStream . writeByte ( "ed25519Signature.type" , object . type ) ; writeStream . writeFixedHex ( "ed25519Signature.publicKey" , Ed25519 . PUBLIC_KEY_SIZE , object . publicKey ) ; writeStream . writeFixedHex ( "ed25519Signature.signature" , Ed25519 . SIGNATURE_SIZE , object . signature ) ; }