diff --git a/src/main/java/it/auties/whatsapp/crypto/SessionCipher.java b/src/main/java/it/auties/whatsapp/crypto/SessionCipher.java index 745eaaee..6f5be005 100644 --- a/src/main/java/it/auties/whatsapp/crypto/SessionCipher.java +++ b/src/main/java/it/auties/whatsapp/crypto/SessionCipher.java @@ -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(); } @@ -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(), diff --git a/src/main/java/it/auties/whatsapp/model/signal/message/SignalMessage.java b/src/main/java/it/auties/whatsapp/model/signal/message/SignalMessage.java index c48e1148..a23b3eb9 100644 --- a/src/main/java/it/auties/whatsapp/model/signal/message/SignalMessage.java +++ b/src/main/java/it/auties/whatsapp/model/signal/message/SignalMessage.java @@ -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; @@ -24,9 +25,18 @@ public final class SignalMessage extends SignalProtocolMessage { @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; diff --git a/src/main/java/it/auties/whatsapp/socket/SocketHandler.java b/src/main/java/it/auties/whatsapp/socket/SocketHandler.java index b9a31c39..fb4a30de 100644 --- a/src/main/java/it/auties/whatsapp/socket/SocketHandler.java +++ b/src/main/java/it/auties/whatsapp/socket/SocketHandler.java @@ -445,6 +445,7 @@ public CompletableFuture send(Node node, Function filter) { if (state() == SocketState.RESTORE) { return CompletableFuture.completedFuture(node); } + var request = node.toRequest(filter, true); var result = request.send(session, keys, store); onNodeSent(node); diff --git a/src/test/java/it/auties/whatsapp/local/WebTest.java b/src/test/java/it/auties/whatsapp/local/WebTest.java index 83bcc0fe..df5bd712 100644 --- a/src/test/java/it/auties/whatsapp/local/WebTest.java +++ b/src/test/java/it/auties/whatsapp/local/WebTest.java @@ -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; @@ -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))