1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.OutputWriter
;
8 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
10 import org
.asamk
.signal
.manager
.Manager
;
11 import org
.asamk
.signal
.util
.Hex
;
12 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
14 import java
.util
.Base64
;
15 import java
.util
.Locale
;
17 public class TrustCommand
implements JsonRpcLocalCommand
{
20 public String
getName() {
25 public void attachToSubparser(final Subparser subparser
) {
26 subparser
.help("Set the trust level of a given number.");
27 subparser
.addArgument("number").help("Specify the phone number, for which to set the trust.").required(true);
28 var mutTrust
= subparser
.addMutuallyExclusiveGroup();
29 mutTrust
.addArgument("-a", "--trust-all-known-keys")
30 .help("Trust all known keys of this user, only use this for testing.")
31 .action(Arguments
.storeTrue());
32 mutTrust
.addArgument("-v", "--verified-safety-number", "--verified-fingerprint")
33 .help("Specify the safety number of the key, only use this option if you have verified the safety number.");
37 public void handleCommand(
38 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
39 ) throws CommandException
{
40 var number
= ns
.getString("number");
41 if (ns
.getBoolean("trust-all-known-keys")) {
44 res
= m
.trustIdentityAllKeys(number
);
45 } catch (InvalidNumberException e
) {
46 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
49 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
52 var safetyNumber
= ns
.getString("verified-safety-number");
53 if (safetyNumber
== null) {
54 throw new UserErrorException(
55 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
58 safetyNumber
= safetyNumber
.replaceAll(" ", "");
59 if (safetyNumber
.length() == 66) {
60 byte[] fingerprintBytes
;
62 fingerprintBytes
= Hex
.toByteArray(safetyNumber
.toLowerCase(Locale
.ROOT
));
63 } catch (Exception e
) {
64 throw new UserErrorException(
65 "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
69 res
= m
.trustIdentityVerified(number
, fingerprintBytes
);
70 } catch (InvalidNumberException e
) {
71 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
74 throw new UserErrorException(
75 "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
77 } else if (safetyNumber
.length() == 60) {
80 res
= m
.trustIdentityVerifiedSafetyNumber(number
, safetyNumber
);
81 } catch (InvalidNumberException e
) {
82 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
85 throw new UserErrorException(
86 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
89 final byte[] scannableSafetyNumber
;
91 scannableSafetyNumber
= Base64
.getDecoder().decode(safetyNumber
);
92 } catch (IllegalArgumentException e
) {
93 throw new UserErrorException(
94 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
98 res
= m
.trustIdentityVerifiedSafetyNumber(number
, scannableSafetyNumber
);
99 } catch (InvalidNumberException e
) {
100 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
103 throw new UserErrorException(
104 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");