Skip to content

Commit

Permalink
Fixed message sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Sep 24, 2023
1 parent 9c0cd3b commit 950698e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/main/java/it/auties/whatsapp/crypto/SessionCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private String getMessageType(SessionState currentState) {

private byte[] encrypt(SessionState state, SessionChain chain, byte[] key, byte[] encrypted) {
var message = new SignalMessage(state.ephemeralKeyPair().encodedPublicKey(), chain.counter().get(), state.previousCounter(), encrypted);
message.setSignature(createMessageSignature(state, key, SignalMessageSpec.encode(message)));
message.setSignature(createMessageSignature(state, key, message));
if (!state.hasPreKey()) {
return message.serialized();
}
Expand All @@ -68,7 +68,11 @@ private byte[] encrypt(SessionState state, SessionChain chain, byte[] key, byte[
return preKeyMessage.serialized();
}

private byte[] createMessageSignature(SessionState state, byte[] key, byte[] encodedMessage) {
private byte[] createMessageSignature(SessionState state, byte[] key, SignalMessage message) {
var encodedMessage = BytesHelper.concat(
message.serializedVersion(),
SignalMessageSpec.encode(message)
);
var macInput = BytesHelper.concat(
keys.identityKeyPair().encodedPublicKey(),
state.remoteIdentityKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.auties.protobuf.model.ProtobufType;
import it.auties.whatsapp.util.BytesHelper;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Arrays;
import java.util.Objects;
Expand All @@ -24,9 +25,18 @@ public final class SignalMessage extends SignalProtocolMessage<SignalMessage> {
@ProtobufProperty(index = 4, type = ProtobufType.BYTES)
private final byte @NonNull [] ciphertext;

private byte[] signature;
private byte @Nullable [] signature;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public SignalMessage(byte @NonNull [] ephemeralPublicKey, Integer counter, Integer previousCounter, byte @NonNull [] ciphertext, byte @Nullable [] signature) {
this.ephemeralPublicKey = ephemeralPublicKey;
this.counter = counter;
this.previousCounter = previousCounter;
this.ciphertext = ciphertext;
this.signature = signature;
}


public SignalMessage(byte @NonNull [] ephemeralPublicKey, Integer counter, Integer previousCounter, byte @NonNull [] ciphertext) {
this.ephemeralPublicKey = ephemeralPublicKey;
this.counter = counter;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/it/auties/whatsapp/socket/SocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ public CompletableFuture<Node> send(Node node, Function<Node, Boolean> filter) {
if (state() == SocketState.RESTORE) {
return CompletableFuture.completedFuture(node);
}

var request = node.toRequest(filter, true);
var result = request.send(session, keys, store);
onNodeSent(node);
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/it/auties/whatsapp/local/WebTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.auties.whatsapp.local;

import it.auties.whatsapp.api.QrHandler;
import it.auties.whatsapp.api.WebHistoryLength;
import it.auties.whatsapp.api.Whatsapp;
import org.junit.jupiter.api.Test;

Expand All @@ -11,10 +10,15 @@ public class WebTest {
public void run() {
var whatsapp = Whatsapp.webBuilder()
.newConnection()
.historyLength(WebHistoryLength.EXTENDED)
.unregistered(QrHandler.toTerminal())
.addLoggedInListener(api -> System.out.printf("Connected: %s%n", api.store().privacySettings()))
.addNewMessageListener((api, message, offline) -> System.out.println(message.toJson()))
.addNewMessageListener((api, message, offline) -> {
if(message.fromMe()) {
api.sendMessage(message.chatJid(), "Hello");
}

System.out.println(message.toJson());
})
.addContactsListener((api, contacts) -> System.out.printf("Contacts: %s%n", contacts.size()))
.addChatsListener(chats -> System.out.printf("Chats: %s%n", chats.size()))
.addNodeReceivedListener(incoming -> System.out.printf("Received node %s%n", incoming))
Expand Down

0 comments on commit 950698e

Please sign in to comment.