import { encode , types , decode } from "binary-data" ; import { HandshakeType } from "../../const" ; import { ProtocolVersion , ExtensionList } from "../../binary" ; import { DtlsRandom } from "../../random" ; import { Version , Random , Handshake , Extension } from "../../../typings/domain" ; import { FragmentedHandshake } from "../../../record/message/fragment" ; export class ServerHello implements Handshake { msgType = HandshakeType . server_hello ; messageSeq ? : number ; static readonly spec = { serverVersion : ProtocolVersion , random : DtlsRandom . spec , sessionId : types . buffer ( types . uint8 ) , cipherSuite : types . uint16be , compressionMethod : types . uint8 , } ; constructor ( public serverVersion , public random , public sessionId , public cipherSuite , public compressionMethod , public extensions ) { } static createEmpty ( ) { return new ServerHello ( undefined as any , undefined as any , undefined as any , undefined as any , undefined as any , undefined as any ) ; } static deSerialize ( buf ) { const res = decode ( buf , ServerHello . spec ) ; const cls = new ServerHello ( ... Object . values ( res ) ) ; const expect = cls . serialize ( ) ; if ( expect . length < buf . length ) { return new ServerHello ( ... Object . values ( decode ( buf , { ... ServerHello . spec , extensions : ExtensionList } ) ) ) ; } return cls ; } serialize ( ) { const res = this . extensions === undefined ? encode ( this , ServerHello . spec ) . slice ( ) : encode ( this , { ... ServerHello . spec , extensions : ExtensionList , } ) . slice ( ) ; return Buffer . from ( res ) ; } toFragment ( ) { const body = this . serialize ( ) ; return new FragmentedHandshake ( this . msgType , body . length , this . messageSeq ! , 0 , body . length , body ) ; } }