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
.commands
.exceptions
.CommandException
;
8 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
9 import org
.asamk
.signal
.manager
.Manager
;
10 import org
.asamk
.signal
.output
.OutputWriter
;
11 import org
.asamk
.signal
.util
.CommandUtil
;
12 import org
.asamk
.signal
.util
.Hex
;
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("recipient").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 recipentString
= ns
.getString("recipient");
41 var recipient
= CommandUtil
.getSingleRecipientIdentifier(recipentString
, m
.getSelfNumber());
42 if (Boolean
.TRUE
.equals(ns
.getBoolean("trust-all-known-keys"))) {
43 boolean res
= m
.trustIdentityAllKeys(recipient
);
45 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
48 var safetyNumber
= ns
.getString("verified-safety-number");
49 if (safetyNumber
== null) {
50 throw new UserErrorException(
51 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
54 safetyNumber
= safetyNumber
.replaceAll(" ", "");
55 if (safetyNumber
.length() == 66) {
56 byte[] fingerprintBytes
;
58 fingerprintBytes
= Hex
.toByteArray(safetyNumber
.toLowerCase(Locale
.ROOT
));
59 } catch (Exception e
) {
60 throw new UserErrorException(
61 "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
63 boolean res
= m
.trustIdentityVerified(recipient
, fingerprintBytes
);
65 throw new UserErrorException(
66 "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
68 } else if (safetyNumber
.length() == 60) {
69 boolean res
= m
.trustIdentityVerifiedSafetyNumber(recipient
, safetyNumber
);
71 throw new UserErrorException(
72 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
75 final byte[] scannableSafetyNumber
;
77 scannableSafetyNumber
= Base64
.getDecoder().decode(safetyNumber
);
78 } catch (IllegalArgumentException e
) {
79 throw new UserErrorException(
80 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
82 boolean res
= m
.trustIdentityVerifiedSafetyNumber(recipient
, scannableSafetyNumber
);
84 throw new UserErrorException(
85 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");