import Multiaddr from 'multiaddr' ; import * as codec from './codec' ; export function bytesFromAddr ( addr ) { if ( addr == null ) { addr = '' ; } let bytes = new Uint8Array ( 0 ) ; if ( addr instanceof Uint8Array ) { bytes = codec . fromBytes ( addr ) ; } else if ( typeof addr === 'string' ) { if ( addr . length > 0 && addr . charAt ( 0 ) !== '/' ) { throw new Error ( `multiaddr "${ addr }" must start with a "/"` ) ; } bytes = codec . fromString ( addr ) ; } else if ( addr . bytes && addr . protos && addr . protoCodes ) { bytes = codec . fromBytes ( addr . bytes ) ; } else { throw new Error ( 'addr must be a string, Uint8Array, or another Multiaddr' ) ; } return bytes ; } export function stringFromBytes ( bytes ) { return codec . bytesToString ( bytes ) ; } export function bytesToTuples ( bytes ) { const t = codec . bytesToTuples ( bytes ) ; return codec . tuplesToStringTuples ( t ) ; } export function bytesToOptions ( bytes ) { const parsed = stringFromBytes ( bytes ) . split ( '/' ) ; const opts = { family : parsed [ 1 ] === 'ip4' ? 'ipv4' : 'ipv6' , host : parsed [ 2 ] , transport : parsed [ 3 ] , port : parseInt ( parsed [ 4 ] ) , } ; return opts ; }