Skip to content

Commit

Permalink
refactor: java-crypto to follow builder/serializer improvements from …
Browse files Browse the repository at this point in the history
…latest arkecosystem/crypto changes (#112)

* refactor: Serialized class

* refactor: Deserialize class

* refactor: Added abstract getTransactionInstance method to AbstractTransactionBuilder class

* fix: vendorField deserialization

* refactor: Transfer transaction

* refactor: Abstract transaction

* refactor: vote transaction

* fix: TransferBuilder - added missing expiration function

* refactor: SecondSignatureRegistration

* refactor: ipfs transaction type

* refactor: MultiPayment test

* test: added IpfsBuilder test

* refactor: HtlcRefund Transaction

* refactor: HtlcLock Transaction

* refactor: HtlcClaim Transaction

* refactor: relegate resignation transaction

* test: add secondVerify check to MultiPaymentBuilderTest

* refactor: Delegate Registration transaction

* style: apply spotless

* refactor: deprecate MultiSignatureRegistration transaction

* refactor: remove Fee configuration class

* refactor: set default transaction amount to 0

* refactor: fees are now added in type specific builder constructors

* style: apply spotlles plugin

* refactor: added assetToHashMap method

* style: rename assetHaspMap method to assetToHashMap

* refactor: made asset classes static

* test: added HtlcLock serializer tests

* feat: added customAsset field

* fix: remove static in classes

* fix: static class

* fix: maximum payment

* style: run spotless apply

* fix: make MultiPayment class static
  • Loading branch information
KovacZan authored Mar 9, 2020
1 parent 7e3ad65 commit dbbdb67
Show file tree
Hide file tree
Showing 117 changed files with 2,101 additions and 2,769 deletions.
53 changes: 0 additions & 53 deletions src/main/java/org/arkecosystem/crypto/configuration/Fee.java

This file was deleted.

183 changes: 74 additions & 109 deletions src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,145 +2,110 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import org.arkecosystem.crypto.encoding.Hex;
import org.arkecosystem.crypto.enums.CoreTransactionTypes;
import org.arkecosystem.crypto.enums.TransactionTypeGroup;
import org.arkecosystem.crypto.identities.Address;
import org.arkecosystem.crypto.transactions.deserializers.*;
import org.arkecosystem.crypto.transactions.types.*;

public class Deserializer {

private String serialized;
private ByteBuffer buffer;
private Transaction transaction;

public Transaction deserialize(String serialized) {
this.serialized = serialized;
private Map<Integer, Transaction> transactionsClasses = new HashMap<>();

public Deserializer(String serialized) {
this.transactionsClasses.put(CoreTransactionTypes.TRANSFER.getValue(), new Transfer());
this.transactionsClasses.put(
CoreTransactionTypes.SECOND_SIGNATURE_REGISTRATION.getValue(),
new SecondSignatureRegistration());
this.transactionsClasses.put(
CoreTransactionTypes.DELEGATE_REGISTRATION.getValue(), new DelegateRegistration());
this.transactionsClasses.put(CoreTransactionTypes.VOTE.getValue(), new Vote());
this.transactionsClasses.put(
CoreTransactionTypes.MULTI_SIGNATURE_REGISTRATION.getValue(),
new MultiSignatureRegistration());
this.transactionsClasses.put(CoreTransactionTypes.IPFS.getValue(), new Ipfs());
this.transactionsClasses.put(
CoreTransactionTypes.MULTI_PAYMENT.getValue(), new MultiPayment());
this.transactionsClasses.put(
CoreTransactionTypes.DELEGATE_RESIGNATION.getValue(), new DelegateResignation());
this.transactionsClasses.put(CoreTransactionTypes.HTLC_LOCK.getValue(), new HtlcLock());
this.transactionsClasses.put(CoreTransactionTypes.HTLC_CLAIM.getValue(), new HtlcClaim());
this.transactionsClasses.put(CoreTransactionTypes.HTLC_REFUND.getValue(), new HtlcRefund());

this.buffer = ByteBuffer.wrap(Hex.decode(serialized)).slice();
this.buffer.order(ByteOrder.LITTLE_ENDIAN);
}

public Transaction deserialize() {
this.buffer.get();

this.transaction = new Transaction();
deserializeCommon();
deserializeVendorField();

int assetOffset = deserializeHeader();
deserializeTypeSpecific(assetOffset);
this.transaction.deserialize(this.buffer);

deserializeVersionOne();
deserializeSignature();

this.transaction.computeId();

return this.transaction;
}

private int deserializeHeader() {
transaction.version = this.buffer.get();
transaction.network = this.buffer.get();
if (transaction.version == 1) {
transaction.type = CoreTransactionTypes.values()[this.buffer.get()].getValue();
transaction.timestamp = this.buffer.getInt();
} else {
transaction.typeGroup = TransactionTypeGroup.values()[this.buffer.getInt()].getValue();
transaction.type = CoreTransactionTypes.values()[this.buffer.getShort()].getValue();
transaction.nonce = this.buffer.getLong();
}
private void deserializeCommon() {
int version = this.buffer.get();
int network = this.buffer.get();
int typeGroup = this.buffer.getInt();
int type = this.buffer.getShort();
long nonce = this.buffer.getLong();

this.transaction = this.transactionsClasses.get(type);
this.transaction.version = version;
this.transaction.network = network;
this.transaction.typeGroup = typeGroup;
this.transaction.type = type;
this.transaction.nonce = nonce;

byte[] senderPublicKey = new byte[33];
this.buffer.get(senderPublicKey);
transaction.senderPublicKey = Hex.encode(senderPublicKey);
this.transaction.senderPublicKey = Hex.encode(senderPublicKey);

transaction.fee = this.buffer.getLong();
this.transaction.fee = this.buffer.getLong();
}

private void deserializeVendorField() {
int vendorFieldLength = this.buffer.get();
if (vendorFieldLength > 0) {
byte[] vendorFieldHex = new byte[vendorFieldLength];
this.buffer.get(vendorFieldHex);
transaction.vendorFieldHex = Hex.encode(vendorFieldHex);
}

if (transaction.version == 1) {
return (41 + 8 + 1) * 2 + vendorFieldLength * 2;
} else {
return 59 * 2 + vendorFieldLength * 2;
byte[] vendorField = new byte[vendorFieldLength];
this.buffer.get(vendorField);
transaction.vendorField = new String(vendorField);
}
}

private void deserializeTypeSpecific(int assetOffset) {
CoreTransactionTypes transactionType = CoreTransactionTypes.values()[transaction.type];
switch (transactionType) {
case TRANSFER:
new Transfer(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case SECOND_SIGNATURE_REGISTRATION:
new SecondSignatureRegistration(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case DELEGATE_REGISTRATION:
new DelegateRegistration(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case VOTE:
new Vote(this.serialized, this.buffer, this.transaction).deserialize(assetOffset);
break;
case MULTI_SIGNATURE_REGISTRATION:
new MultiSignatureRegistration(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case IPFS:
new Ipfs(this.serialized, this.buffer, this.transaction).deserialize(assetOffset);
break;
case MULTI_PAYMENT:
new MultiPayment(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case DELEGATE_RESIGNATION:
new DelegateResignation(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case HTLC_LOCK:
new HtlcLock(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case HTLC_CLAIM:
new HtlcClaim(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
case HTLC_REFUND:
new HtlcRefund(this.serialized, this.buffer, this.transaction)
.deserialize(assetOffset);
break;
default:
throw new UnsupportedOperationException();
private void deserializeSignature() {
if (buffer.remaining() != 0) {
int signatureLength = currentSignatureLength();
byte[] signatureBuffer = new byte[signatureLength];
this.buffer.get(signatureBuffer);
this.transaction.signature = Hex.encode(signatureBuffer);
}
}

private void deserializeVersionOne() {
if (transaction.secondSignature != null) {
transaction.signSignature = transaction.secondSignature;
}

if (transaction.type == CoreTransactionTypes.VOTE.getValue()) {
transaction.recipientId =
Address.fromPublicKey(transaction.senderPublicKey, transaction.network);
}

if (transaction.type == CoreTransactionTypes.MULTI_SIGNATURE_REGISTRATION.getValue()) {
for (int i = 0; i < transaction.asset.multisignature.keysgroup.size(); i++) {
transaction.asset.multisignature.keysgroup.set(
i, "+" + transaction.asset.multisignature.keysgroup.get(i));
}
}

if (transaction.vendorFieldHex != null) {
transaction.vendorField = new String(Hex.decode(transaction.vendorFieldHex));
}

if (transaction.id == null) {
transaction.id = transaction.computeId();
if (buffer.remaining() != 0) {
int signatureLength = currentSignatureLength();
byte[] signatureBuffer = new byte[signatureLength];
this.buffer.get(signatureBuffer);
this.transaction.secondSignature = Hex.encode(signatureBuffer);
}
}

if (transaction.type == CoreTransactionTypes.SECOND_SIGNATURE_REGISTRATION.getValue()
|| transaction.type
== CoreTransactionTypes.MULTI_SIGNATURE_REGISTRATION.getValue()) {
transaction.recipientId =
Address.fromPublicKey(transaction.senderPublicKey, transaction.network);
}
private int currentSignatureLength() {
int mark = this.buffer.position();
this.buffer.position(mark + 1);
String length = String.valueOf(this.buffer.get());
int signatureLength = Integer.parseInt(length) + 2;
this.buffer.position(mark);
return signatureLength;
}
}
Loading

0 comments on commit dbbdb67

Please sign in to comment.