Exemple #1
0
  public static String generateHallmark(String secretPhrase, String host, int weight, int date) {

    if (host.length() == 0 || host.length() > 100) {
      throw new IllegalArgumentException("Hostname length should be between 1 and 100");
    }
    if (weight <= 0 || weight > Constants.MAX_BALANCE_NXT) {
      throw new IllegalArgumentException(
          "Weight should be between 1 and " + Constants.MAX_BALANCE_NXT);
    }

    byte[] publicKey = Crypto.getPublicKey(secretPhrase);
    byte[] hostBytes = Convert.toBytes(host);

    ByteBuffer buffer = ByteBuffer.allocate(32 + 2 + hostBytes.length + 4 + 4 + 1);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(publicKey);
    buffer.putShort((short) hostBytes.length);
    buffer.put(hostBytes);
    buffer.putInt(weight);
    buffer.putInt(date);

    byte[] data = buffer.array();
    data[data.length - 1] = (byte) ThreadLocalRandom.current().nextInt();
    byte[] signature = Crypto.sign(data, secretPhrase);

    return Convert.toHexString(data) + Convert.toHexString(signature);
  }
Exemple #2
0
 private Account(long id) {
   if (id != Crypto.rsDecode(Crypto.rsEncode(id))) {
     Logger.logMessage("CRITICAL ERROR: Reed-Solomon encoding fails for " + id);
   }
   this.id = id;
   this.dbKey = accountDbKeyFactory.newKey(this.id);
   this.creationHeight = Nxt.getBlockchain().getHeight();
   currentLeasingHeightFrom = Integer.MAX_VALUE;
 }
Exemple #3
0
  public static boolean verify(
      byte[] signature, byte[] message, byte[] publicKey, boolean enforceCanonical) {

    try {

      if (enforceCanonical && !Curve25519.isCanonicalSignature(signature)) {
        Logger.logDebugMessage("Rejecting non-canonical signature");
        return false;
      }

      if (enforceCanonical && !Curve25519.isCanonicalPublicKey(publicKey)) {
        Logger.logDebugMessage("Rejecting non-canonical public key");
        return false;
      }

      byte[] Y = new byte[32];
      byte[] v = new byte[32];
      System.arraycopy(signature, 0, v, 0, 32);
      byte[] h = new byte[32];
      System.arraycopy(signature, 32, h, 0, 32);
      Curve25519.verify(Y, v, h, publicKey);

      MessageDigest digest = Crypto.sha256();
      byte[] m = digest.digest(message);
      digest.update(m);
      byte[] h2 = digest.digest(Y);

      return Arrays.equals(h, h2);

    } catch (RuntimeException e) {
      Logger.logMessage("Error in Crypto verify", e);
      return false;
    }
  }
 private void loadPassPhrases() {
   try {
     List<String> passphrases =
         Files.readAllLines(Paths.get("passphrases.txt"), Charset.forName("US-ASCII"));
     for (String ps : passphrases) {
       if (!ps.isEmpty()) {
         byte[] publicKey = Crypto.getPublicKey(ps);
         byte[] publicKeyHash = Crypto.sha256().digest(publicKey);
         Long id = Convert.fullHashToId(publicKeyHash);
         loadedPassPhrases.put(id, ps);
         LOGGER.info("Added key: {" + ps + "} -> {" + Convert.toUnsignedLong(id) + "}");
       }
     }
   } catch (IOException e) {
     LOGGER.info("Warning: no passphrases.txt found");
   }
 }
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) throws NxtException.ValidationException {

    String transactionBytes = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
    String transactionJSON = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON"));
    if (transactionBytes == null && transactionJSON == null) {
      return MISSING_UNSIGNED_BYTES;
    }
    String secretPhrase = Convert.emptyToNull(req.getParameter("secretPhrase"));
    if (secretPhrase == null) {
      return MISSING_SECRET_PHRASE;
    }

    try {
      Transaction transaction;
      if (transactionBytes != null) {
        byte[] bytes = Convert.parseHexString(transactionBytes);
        transaction = Nxt.getTransactionProcessor().parseTransaction(bytes);
      } else {
        JSONObject json = (JSONObject) JSONValue.parse(transactionJSON);
        transaction = Nxt.getTransactionProcessor().parseTransaction(json);
      }
      transaction.validate();
      if (transaction.getSignature() != null) {
        JSONObject response = new JSONObject();
        response.put("errorCode", 4);
        response.put(
            "errorDescription",
            "Incorrect \"unsignedTransactionBytes\" - transaction is already signed");
        return response;
      }
      transaction.sign(secretPhrase);
      JSONObject response = new JSONObject();
      response.put("transaction", transaction.getStringId());
      response.put("fullHash", transaction.getFullHash());
      response.put("transactionBytes", Convert.toHexString(transaction.getBytes()));
      response.put(
          "signatureHash", Convert.toHexString(Crypto.sha256().digest(transaction.getSignature())));
      response.put("verify", transaction.verifySignature());
      return response;
    } catch (NxtException.ValidationException | RuntimeException e) {
      // Logger.logDebugMessage(e.getMessage(), e);
      return INCORRECT_UNSIGNED_BYTES;
    }
  }
Exemple #6
0
  public static byte[] getPublicKey(String secretPhrase) {

    try {

      byte[] publicKey = new byte[32];
      Curve25519.keygen(publicKey, null, Crypto.sha256().digest(secretPhrase.getBytes("UTF-8")));

      if (!Curve25519.isCanonicalPublicKey(publicKey)) {
        throw new RuntimeException("Public key not canonical");
      }

      return publicKey;

    } catch (RuntimeException | UnsupportedEncodingException e) {
      Logger.logMessage("Error getting public key", e);
      return null;
    }
  }
Exemple #7
0
  public static byte[] sign(byte[] message, String secretPhrase) {

    try {

      byte[] P = new byte[32];
      byte[] s = new byte[32];
      MessageDigest digest = Crypto.sha256();
      Curve25519.keygen(P, s, digest.digest(secretPhrase.getBytes("UTF-8")));

      byte[] m = digest.digest(message);

      digest.update(m);
      byte[] x = digest.digest(s);

      byte[] Y = new byte[32];
      Curve25519.keygen(Y, null, x);

      digest.update(m);
      byte[] h = digest.digest(Y);

      byte[] v = new byte[32];
      Curve25519.sign(v, h, x, s);

      byte[] signature = new byte[64];
      System.arraycopy(v, 0, signature, 0, 32);
      System.arraycopy(h, 0, signature, 32, 32);

      if (!Curve25519.isCanonicalSignature(signature)) {
        throw new RuntimeException("Signature not canonical");
      }

      return signature;

    } catch (RuntimeException | UnsupportedEncodingException e) {
      Logger.logMessage("Error in signing message", e);
      return null;
    }
  }
  public static Transaction create(
      IAccount sender,
      Long order,
      short deadline,
      int fee,
      Long referencedTransaction,
      NXTService nxt)
      throws TransactionException, ValidationException {

    String secretPhrase = sender.getPrivateKey();
    byte[] publicKey = Crypto.getPublicKey(secretPhrase);

    if ((fee <= 0) || (fee >= 1000000000L))
      throw new TransactionException(TransactionException.INCORRECT_FEE);

    if ((deadline < 1) || (deadline > 1440))
      throw new TransactionException(TransactionException.INCORRECT_DEADLINE);

    Account account = Account.getAccount(publicKey);
    if (account == null) throw new TransactionException(TransactionException.INTERNAL_ERROR);

    Order.Ask orderData = Order.Ask.getAskOrder(order);
    if (orderData == null || !orderData.getAccount().getId().equals(account.getId()))
      throw new TransactionException(TransactionException.UNKNOWN_ORDER);

    Attachment attachment = new Attachment.ColoredCoinsAskOrderCancellation(order);

    Transaction transaction =
        Nxt.getTransactionProcessor()
            .newTransaction(
                deadline, publicKey, Genesis.CREATOR_ID, 0, fee, referencedTransaction, attachment);
    transaction.sign(secretPhrase);

    nxt.broacastTransaction(transaction);

    return transaction;
  }
Exemple #9
0
  public static Hallmark parseHallmark(String hallmarkString) {

    byte[] hallmarkBytes = Convert.parseHexString(hallmarkString);

    ByteBuffer buffer = ByteBuffer.wrap(hallmarkBytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    byte[] publicKey = new byte[32];
    buffer.get(publicKey);
    int hostLength = buffer.getShort();
    if (hostLength > 300) {
      throw new IllegalArgumentException("Invalid host length");
    }
    byte[] hostBytes = new byte[hostLength];
    buffer.get(hostBytes);
    String host = Convert.toString(hostBytes);
    int weight = buffer.getInt();
    int date = buffer.getInt();
    buffer.get();
    byte[] signature = new byte[64];
    buffer.get(signature);

    byte[] data = new byte[hallmarkBytes.length - 64];
    System.arraycopy(hallmarkBytes, 0, data, 0, data.length);

    boolean isValid =
        host.length() < 100
            && weight > 0
            && weight <= Constants.MAX_BALANCE_NXT
            && Crypto.verify(signature, data, publicKey, true);
    try {
      return new Hallmark(hallmarkString, publicKey, signature, host, weight, date, isValid);
    } catch (URISyntaxException e) {
      throw new RuntimeException(e.toString(), e);
    }
  }
Exemple #10
0
 public byte[] decryptFrom(EncryptedData encryptedData, String recipientSecretPhrase) {
   if (getPublicKey() == null) {
     throw new IllegalArgumentException("Sender account doesn't have a public key set");
   }
   return encryptedData.decrypt(Crypto.getPrivateKey(recipientSecretPhrase), publicKey);
 }
Exemple #11
0
 public EncryptedData encryptTo(byte[] data, String senderSecretPhrase) {
   if (getPublicKey() == null) {
     throw new IllegalArgumentException("Recipient account doesn't have a public key set");
   }
   return EncryptedData.encrypt(data, Crypto.getPrivateKey(senderSecretPhrase), publicKey);
 }
Exemple #12
0
 public static long getId(byte[] publicKey) {
   byte[] publicKeyHash = Crypto.sha256().digest(publicKey);
   return Convert.fullHashToId(publicKeyHash);
 }