import { ThreadID } from '@textile/threads-id' ; import Convert from 'multiaddr/src/convert' ; import varint from 'varint' ; import { protocols } from './protocols' ; function thread2bytes ( str ) { const buf = ThreadID . fromString ( str ) . toBytes ( ) ; const size = varint . encode ( buf . length ) ; return Uint8Array . from ( [ ... size , ... buf ] ) ; } function bytes2thread ( buf ) { const size = varint . decode ( buf as any ) ; buf = buf . slice ( varint . decode . bytes ) ; if ( buf . length !== size ) { throw new Error ( 'inconsistent lengths' ) ; } return ThreadID . fromBytes ( buf ) . toString ( ) ; } export function toString ( prt , buf ) { const proto = ( protocols as any ) ( prt ) ; switch ( proto . code ) { case 406 : return bytes2thread ( buf ) ; default : return Convert . toString ( prt , buf ) ; } } export function toBytes ( prt , str ) { const proto = ( protocols as any ) ( prt ) ; switch ( proto . code ) { case 406 : return thread2bytes ( str ) ; default : return Convert . toBytes ( prt , str ) ; } }