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); }
@Test public void scrypt() { byte[] hash = HashFunction.SCRYPT.hash(new byte[] {(byte) 0x41, (byte) 0xFB}); Assert.assertEquals( "da3f4f010d772567a8896465d11df28693b244c91b8ba4bea5a30f6be572b667".toLowerCase(), Convert.toHexString(hash)); hash = HashFunction.SCRYPT.hash(new byte[] {}); Assert.assertEquals( "0cf2967ca5c120e80b37f8f75c971842e05da107278c1058e6ffbc68911c11f1", Convert.toHexString(hash)); }
@Test public void sha3() { byte[] hash = HashFunction.SHA3.hash(new byte[] {(byte) 0x41, (byte) 0xFB}); Assert.assertEquals( "A8EACEDA4D47B3281A795AD9E1EA2122B407BAF9AABCB9E18B5717B7873537D2".toLowerCase(), Convert.toHexString(hash)); hash = HashFunction.SHA3.hash(new byte[] {}); Assert.assertEquals( "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", Convert.toHexString(hash)); }
@Test public void sha256() { byte[] hash = HashFunction.SHA256.hash(new byte[] {0x61, 0x62, 0x63}); Assert.assertEquals( "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", Convert.toHexString(hash)); hash = HashFunction.SHA256.hash(new byte[] {}); Assert.assertEquals( "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Convert.toHexString(hash)); }
public static Account getAccount(byte[] publicKey) { Account account = accountTable.get(accountDbKeyFactory.newKey(getId(publicKey))); if (account == null) { return null; } if (account.getPublicKey() == null || Arrays.equals(account.getPublicKey(), publicKey)) { return account; } throw new RuntimeException( "DUPLICATE KEY for account " + Convert.toUnsignedLong(account.getId()) + " existing key " + Convert.toHexString(account.getPublicKey()) + " new key " + Convert.toHexString(publicKey)); }
@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; } }
@Override public Transaction parseTransaction(byte[] bytes) throws NxtException.ValidationException { try { boolean useNQT = Nxt.getBlockchain().getLastBlock().getHeight() >= Constants.NQT_BLOCK; ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(ByteOrder.LITTLE_ENDIAN); byte type = buffer.get(); byte subtype = buffer.get(); int timestamp = buffer.getInt(); short deadline = buffer.getShort(); byte[] senderPublicKey = new byte[32]; buffer.get(senderPublicKey); Long recipientId = buffer.getLong(); long amountNQT; long feeNQT; String referencedTransactionFullHash = null; Long referencedTransactionId = null; if (!useNQT) { amountNQT = ((long) buffer.getInt()) * Constants.ONE_NXT; feeNQT = ((long) buffer.getInt()) * Constants.ONE_NXT; referencedTransactionId = Convert.zeroToNull(buffer.getLong()); } else { amountNQT = buffer.getLong(); feeNQT = buffer.getLong(); byte[] referencedTransactionFullHashBytes = new byte[32]; buffer.get(referencedTransactionFullHashBytes); if (Convert.emptyToNull(referencedTransactionFullHashBytes) != null) { referencedTransactionFullHash = Convert.toHexString(referencedTransactionFullHashBytes); referencedTransactionId = Convert.fullHashToId(referencedTransactionFullHash); } } byte[] signature = new byte[64]; buffer.get(signature); signature = Convert.emptyToNull(signature); TransactionType transactionType = TransactionType.findTransactionType(type, subtype); TransactionImpl transaction; if (!useNQT) { transaction = new TransactionImpl( transactionType, timestamp, deadline, senderPublicKey, recipientId, amountNQT, feeNQT, referencedTransactionId, signature); } else { transaction = new TransactionImpl( transactionType, timestamp, deadline, senderPublicKey, recipientId, amountNQT, feeNQT, referencedTransactionFullHash, signature); } transactionType.loadAttachment(transaction, buffer); return transaction; } catch (RuntimeException e) { throw new NxtException.ValidationException(e.toString(), e); } }