import { hash } from '@stablelib/sha256' ; import * as u8a from 'uint8arrays' ; function writeUint32BE ( value , array = new Uint8Array ( 4 ) ) { const encoded = u8a . fromString ( value . toString ( ) , 'base10' ) ; array . set ( encoded , 4 - encoded . length ) ; return array ; } const lengthAndInput = ( input ) : Uint8Array => u8a . concat ( [ writeUint32BE ( input . length ) , input ] ) ; export function concatKDF ( secret , keyLen , alg ) { if ( keyLen !== 256 ) throw new Error ( `Unsupported key length: ${ keyLen }` ) ; const value = u8a . concat ( [ lengthAndInput ( u8a . fromString ( alg ) ) , lengthAndInput ( new Uint8Array ( 0 ) ) , lengthAndInput ( new Uint8Array ( 0 ) ) , writeUint32BE ( keyLen ) , ] ) ; const roundNumber = 1 ; return hash ( u8a . concat ( [ writeUint32BE ( roundNumber ) , secret , value ] ) ) ; } export const base64ToBytes = ( s ) : Uint8Array => u8a . fromString ( s , 'base64pad' ) ; export const bytesToBase64 = ( b ) : string => u8a . toString ( b , 'base64pad' ) ; export const stringToBytes = ( s ) : Uint8Array => u8a . fromString ( s ) ; export const bytesToString = ( s ) : string => u8a . toString ( s ) ; export const base58ToBytes = ( s ) : Uint8Array => u8a . fromString ( s , 'base58btc' ) ; export const bytesToBase58 = ( b ) : string => u8a . toString ( b , 'base58btc' ) ;