import base45 from 'base45' ; import pako from 'pako' ; import cbor from 'cbor-js' ; export function typedArrayToBufferSliced ( array ) { return array . buffer . slice ( array . byteOffset , array . byteLength + array . byteOffset ) ; } export function typedArrayToBuffer ( array ) { var buffer = new ArrayBuffer ( array . length ) ; array . map ( function ( value , i ) { return buffer [ i ] = value ; } ) ; return array . buffer ; } export function decodeData ( data ) { if ( data . startsWith ( 'https://' ) ) { var url = new URL ( data ) ; data = decodeURIComponent ( url . hash . substring ( 1 ) ) ; } if ( data . startsWith ( 'HC1' ) ) { data = data . substring ( 3 ) ; if ( data . startsWith ( ':' ) ) { data = data . substring ( 1 ) ; } } var arrayBuffer = base45 . decode ( data ) ; if ( arrayBuffer [ 0 ] == 0x78 ) { arrayBuffer = pako . inflate ( arrayBuffer ) ; } var payloadArray = cbor . decode ( typedArrayToBuffer ( arrayBuffer ) ) ; if ( ! Array . isArray ( payloadArray ) || payloadArray . length !== 4 ) { throw new Error ( 'decodingFailed' ) ; } var plaintext = payloadArray [ 2 ] ; var decoded = cbor . decode ( typedArrayToBufferSliced ( plaintext ) ) ; return decoded ; }