import crc from 'crc' ; import * as types from '../config/types' ; import * as params from '../config/params' ; const base32 = require ( 'base32.js' ) ; function calculateChecksum ( payload ) { const checksum = Buffer . allocUnsafe ( 2 ) ; checksum . writeUInt16LE ( crc . crc16xmodem ( payload ) , 0 ) ; return checksum ; } export function encodeCheck ( versionByteName , data ) { const versionByte = params . VERSION_BYTES [ versionByteName ] ; const versionBuffer = Buffer . from ( [ versionByte ] ) ; const payload = Buffer . concat ( [ versionBuffer , data ] ) ; const checksum = calculateChecksum ( payload ) ; const unencoded = Buffer . concat ( [ payload , checksum ] ) ; return base32 . encode ( unencoded ) ; } export function encodeEd25519PublicKey ( data ) { return encodeCheck ( 'ed25519PublicKey' , data ) ; } export function pubKeyToAddress ( publicKey ) { const pubKey = publicKey . length === 66 ? publicKey . slice ( 2 ) : publicKey ; const pubKeyBuf = Buffer . from ( pubKey , 'hex' ) ; return encodeEd25519PublicKey ( pubKeyBuf ) ; }