private Promise < PublicKey > fetchUserPreKey ( final int uid , final int keyGroupId ) { return pickUserGroup ( uid , keyGroupId ) . flatMap ( new Function < Tuple2 < UserKeysGroup , UserKeys > , Promise < PublicKey > > ( ) { @ Override public Promise < PublicKey > apply ( final Tuple2 < UserKeysGroup , UserKeys > keyGroups ) { return api ( new RequestLoadPrePublicKeys ( new ApiUserOutPeer ( uid , getUser ( uid ) . getAccessHash ( ) ) , keyGroupId ) ) . map ( new Function < ResponsePublicKeys , PublicKey > ( ) { @ Override public PublicKey apply ( ResponsePublicKeys response ) { if ( response . getPublicKey ( ) . size ( ) == 0 ) { throw new RuntimeException ( "User doesn't have pre keys" ) ; } ApiEncryptionKey key = response . getPublicKey ( ) . get ( 0 ) ; ApiEncryptionKeySignature sig = null ; for ( ApiEncryptionKeySignature s : response . getSignatures ( ) ) { if ( s . getKeyId ( ) == key . getKeyId ( ) && "Ed25519" . equals ( s . getSignatureAlg ( ) ) ) { sig = s ; break ; } } if ( sig == null ) { throw new RuntimeException ( "Unable to find public key on server" ) ; } byte [ ] keyHash = RatchetKeySignature . hashForSignature ( key . getKeyId ( ) , key . getKeyAlg ( ) , key . getKeyMaterial ( ) ) ; if ( ! Curve25519 . verifySignature ( keyGroups . getT1 ( ) . getIdentityKey ( ) . getPublicKey ( ) , keyHash , sig . getSignature ( ) ) ) { throw new RuntimeException ( "Key signature does not isMatch" ) ; } return new PublicKey ( key . getKeyId ( ) , key . getKeyAlg ( ) , key . getKeyMaterial ( ) ) ; } } ) ; } } ) ; }
Fetching user s random pre key