]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/util/PaymentUtils.java
Decrypt and verify the profile payment address
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / PaymentUtils.java
1 package org.asamk.signal.manager.util;
2
3 import com.google.protobuf.ByteString;
4
5 import org.signal.libsignal.protocol.IdentityKey;
6 import org.signal.libsignal.protocol.IdentityKeyPair;
7 import org.signal.libsignal.protocol.ecc.ECPrivateKey;
8 import org.signal.libsignal.protocol.ecc.ECPublicKey;
9 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
10
11 public class PaymentUtils {
12
13 private PaymentUtils() {
14 }
15
16 /**
17 * Signs the supplied address bytes with the {@link IdentityKeyPair}'s private key and returns a proto that includes it, and it's signature.
18 */
19 public static SignalServiceProtos.PaymentAddress signPaymentsAddress(
20 byte[] publicAddressBytes, ECPrivateKey privateKey
21 ) {
22 byte[] signature = privateKey.calculateSignature(publicAddressBytes);
23
24 return SignalServiceProtos.PaymentAddress.newBuilder()
25 .setMobileCoinAddress(SignalServiceProtos.PaymentAddress.MobileCoinAddress.newBuilder()
26 .setAddress(ByteString.copyFrom(publicAddressBytes))
27 .setSignature(ByteString.copyFrom(signature)))
28 .build();
29 }
30
31 /**
32 * Verifies that the payments address is signed with the supplied {@link IdentityKey}.
33 * <p>
34 * Returns the validated bytes if so, otherwise returns null.
35 */
36 public static byte[] verifyPaymentsAddress(
37 SignalServiceProtos.PaymentAddress paymentAddress, ECPublicKey publicKey
38 ) {
39 if (!paymentAddress.hasMobileCoinAddress()) {
40 return null;
41 }
42
43 byte[] bytes = paymentAddress.getMobileCoinAddress().getAddress().toByteArray();
44 byte[] signature = paymentAddress.getMobileCoinAddress().getSignature().toByteArray();
45
46 if (signature.length != 64 || !publicKey.verifySignature(bytes, signature)) {
47 return null;
48 }
49
50 return bytes;
51 }
52 }