@Override public void sign(String secretPhrase) { checkForSignature(); signature = Crypto.signWithSecretPhrase(getBytes(), secretPhrase); }
@Override public TransactionImpl build() throws NxtException.NotValidException { return new TransactionImpl(this); }
@Override public String getFullHash() { if (fullHash == null) { getId(); } return fullHash; }
@Override public byte[] getUnsignedBytes() { return zeroSignature(getBytes()); }
@Override public long getId() { if (id == 0) { if (signature == null && type.isSigned()) { throw new IllegalStateException("Transaction is not signed yet"); } byte[] hash; if (useNQT()) { byte[] data = zeroSignature(getBytes()); byte[] signatureHash = Crypto.sha256().digest(signature != null ? signature : new byte[64]); MessageDigest digest = Crypto.sha256(); digest.update(data); hash = digest.digest(signatureHash); } else { hash = Crypto.sha256().digest(getBytes()); } BigInteger bigInteger = new BigInteger(1, new byte[] {hash[7], hash[6], hash[5], hash[4], hash[3], hash[2], hash[1], hash[0]}); id = bigInteger.longValue(); stringId = bigInteger.toString(); fullHash = Convert.toHexString(hash); } return id; }
@Override public byte[] getBytes() { try { ByteBuffer buffer = ByteBuffer.allocate(getSize()); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(type.getType()); buffer.putShort(deadline); buffer.put(senderPublicKey); buffer.putLong(type.hasRecipient() ? recipientId : getGenesisId()); if (useNQT()) { buffer.putLong(amountNQT); buffer.putLong(feeNQT); buffer.putInt(getFlags()); buffer.putInt(ecBlockHeight); buffer.putLong(ecBlockId);
@Test public void testNxtTransaction() throws WalletAccount.WalletAccountException, NxtException.ValidationException { NxtAddress destination = (NxtAddress) otherAccount.getReceiveAddress(); Value amount = NXT.value("1"); NxtSendRequest req = nxtAccount.sendCoinsOffline(destination, amount); nxtAccount.completeAndSignTx(req); Transaction nxtTx = req.nxtTxBuilder.build(); // nxtTx.sign(nxtSecret); byte[] txBytes = req.nxtTxBuilder.build().getBytes(); req.tx = new NxtTransaction(NXT, req.nxtTxBuilder.build()); Transaction parsedTx = TransactionImpl.parseTransaction(txBytes); assertEquals(Attachment.ORDINARY_PAYMENT, parsedTx.getAttachment()); assertEquals(NxtFamily.DEFAULT_DEADLINE, parsedTx.getDeadline()); assertEquals((req.tx.getRawTransaction()).getTimestamp(), parsedTx.getTimestamp()); assertEquals(nxtAccountId, parsedTx.getSenderId()); assertArrayEquals(nxtPublicKey, parsedTx.getSenderPublicKey()); assertEquals(amount.value, parsedTx.getAmountNQT()); assertEquals(req.fee.value, parsedTx.getFeeNQT()); assertEquals(destination.getAccountId(), parsedTx.getRecipientId()); // System.out.println(Convert.toHexString(nxtTx.getBytes())); // TODO check signature }
@Override public void onResponse(Response response) throws IOException { try { if (!response.isSuccessful()) { log.info("Unable to fetch txs."); log.info("[Error code] = " + response.code()); } JSONObject reply = parseReply(response); //String txId = reply.getString("transaction"); //Integer confirmations = reply.getInt("confirmations"); //getTransactionBytes(txId, listener, confirmations); NxtTransaction tx = new NxtTransaction(type, TransactionImpl.parseTransaction(reply)); //log.info("Transaction fetched"); listener.onTransactionUpdate(tx); } catch (IOException e) { log.info("IOException: " + e.getMessage()); } catch (JSONException e) { log.info("Could not parse JSON: " + e.getMessage()); }catch (NxtException.NotValidException e) { log.info("Not valid transaction: " + e.getMessage()); } } });
@Override public void onResponse(Response response) throws IOException { try { if (!response.isSuccessful()) { log.info("Unable to fetch txs."); log.info("[Error code] = " + response.code()); } JSONObject reply = parseReply(response); String txBytes = reply.getString("transactionBytes"); TransactionImpl rawTx = TransactionImpl.parseTransaction(Convert.parseHexString(txBytes)); rawTx.setConfirmations(confirmations); NxtTransaction tx = new NxtTransaction(type, rawTx); log.info("Fetching tx bytes"); listener.onTransactionUpdate(tx); } catch (IOException e) { log.info("IOException: " + e.getMessage()); } catch (JSONException e) { log.info("Could not parse JSON: " + e.getMessage()); } catch (NxtException.ValidationException e) { log.info("Transaction is invalid"); e.printStackTrace(); } } });
log.info("confirmations {}",confirmations); TransactionImpl tx = builder.build(); tx.setConfirmations(confirmations); return tx; } catch (JSONException e) {
if (recipientId != 0 || getAmountNQT() != 0) { throw new NxtException.NotValidException("Transactions of this type must have recipient == Genesis, amount == 0");
byte[] txBytes = transaction.getBytes(); Transaction parsedTx = TransactionImpl.parseTransaction(txBytes);
@Override public void sign(byte[] privateKey) { checkForSignature(); signature = Crypto.sign(getBytes(), privateKey); }
byte[] txBytes = transaction.getBytes(); Transaction parsedTx = TransactionImpl.parseTransaction(txBytes); assertEquals(Attachment.ORDINARY_PAYMENT, parsedTx.getAttachment()); assertEquals(deadline, parsedTx.getDeadline());
@Override public int compareTo(Transaction other) { return Long.compare(this.getId(), other.getId()); }
@Override public String getStringId() { if (stringId == null) { getId(); if (stringId == null) { stringId = Convert.toUnsignedLong(id); } } return stringId; }
@Override public boolean equals(Object o) { return o instanceof TransactionImpl && this.getId() == ((Transaction)o).getId(); }