import { BufferWriter } from "@node-lightning/bufio" ; /**
 * Converts an IPv6 address in string notation to the
 * byte representation.
 */ export function ipv6StringToBuffer ( host ) { if ( host . startsWith ( "::" ) ) host = host . substr ( 1 ) ; else if ( host . endsWith ( "::" ) ) host = host . substr ( 0 , host . length - 1 ) ; const parts = host . split ( ":" ) ; const writer = new BufferWriter ( Buffer . alloc ( 16 ) ) ; const expandBy = 8 - parts . length ; let needsExpansion = expandBy > 0 ; for ( const part of parts ) { if ( needsExpansion && part === "" ) { const b = Buffer . alloc ( ( expandBy + 1 ) * 2 ) ; writer . writeBytes ( b ) ; needsExpansion = false ; } else { const b = Buffer . from ( expandZeros ( part ) , "hex" ) ; writer . writeBytes ( b ) ; } } return writer . toBuffer ( ) ; } function expandZeros ( part ) { return part . padStart ( 4 , "0" ) ; }