Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transaction serialize method #185

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Transaction deserialize() {
deserializeCommon();
deserializeVendorField();

this.transaction.deserialize(this.buffer);
this.transaction.deserializeData(this.buffer);

deserializeSignatures();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public byte[] serialize(
byte[] common = serializeCommon();
byte[] vendorField = serializeVendorField();

byte[] typeBuffer = this.transaction.serialize();
byte[] typeBuffer = this.transaction.serializeData();

byte[] signatures =
serializeSignatures(skipSignature, skipSecondSignature, skipMultiSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer = ByteBuffer.allocate(2 + this.asset.multiPayment.payments.size() * 28);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort((short) this.asset.multiPayment.payments.size());
Expand All @@ -56,7 +56,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
int paymentLength = buffer.getShort() & 0xff;

for (int i = 0; i < paymentLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer =
ByteBuffer.allocate(2 + this.asset.multiSignature.publicKeys.size() * 33);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand All @@ -48,7 +48,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
this.asset.multiSignature.min = buffer.get();

int publicKeyLength = buffer.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer = ByteBuffer.allocate(33);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(Hex.decode(this.asset.signature.publicKey));
return buffer.array();
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
byte[] publicKeyBuffer = new byte[33];
buffer.get(publicKeyBuffer);
this.asset.signature.publicKey = Hex.encode(publicKeyBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public void computeId() {
}

public String getId() {
return Hex.encode(Sha256Hash.hash(Serializer.serialize(this)));
return Hex.encode(Sha256Hash.hash(this.serialize()));
}

public Transaction sign(String passphrase) {
ECKey privateKey = PrivateKey.fromPassphrase(passphrase);

this.senderPublicKey = privateKey.getPublicKeyAsHex();
Sha256Hash hash = Sha256Hash.of(Serializer.serialize(this, true, true, false));
Sha256Hash hash = Sha256Hash.of(this.serialize(true, true, false));

this.signature = Hex.encode(signer().sign(hash.getBytes(), privateKey));

Expand All @@ -58,7 +58,7 @@ public Transaction sign(String passphrase) {
public Transaction secondSign(String passphrase) {
ECKey privateKey = PrivateKey.fromPassphrase(passphrase);

Sha256Hash hash = Sha256Hash.of(Serializer.serialize(this, false, true, false));
Sha256Hash hash = Sha256Hash.of(this.serialize(false, true));

this.secondSignature = Hex.encode(signer().sign(hash.getBytes(), privateKey));

Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean verify() {
ECKey keys = ECKey.fromPublicOnly(Hex.decode(this.senderPublicKey));

byte[] signature = Hex.decode(this.signature);
byte[] hash = Sha256Hash.hash(Serializer.serialize(this, true, true, false));
byte[] hash = Sha256Hash.hash(this.serialize(true, true, false));

return verifier().verify(hash, keys, signature);
}
Expand All @@ -98,7 +98,7 @@ public boolean secondVerify(String secondPublicKey) {
ECKey keys = ECKey.fromPublicOnly(Hex.decode(secondPublicKey));

byte[] signature = Hex.decode(this.secondSignature);
byte[] hash = Sha256Hash.hash(Serializer.serialize(this, false, true, false));
byte[] hash = Sha256Hash.hash(this.serialize(false, true, false));

return verifier().verify(hash, keys, signature);
}
Expand Down Expand Up @@ -145,9 +145,26 @@ public HashMap toHashMap() {
return map;
}

public abstract byte[] serialize();
public byte[] serialize(
boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
return Serializer.serialize(this, skipSignature, skipSecondSignature, skipMultiSignature);
}

public byte[] serialize(boolean skipSignature, boolean skipSecondSignature) {
return serialize(skipSignature, skipSecondSignature, false);
}

public byte[] serialize(boolean skipSignature) {
return serialize(skipSignature, false, false);
}

public byte[] serialize() {
return serialize(false, false, false);
}

public abstract byte[] serializeData();

public abstract void deserialize(ByteBuffer buffer);
public abstract void deserializeData(ByteBuffer buffer);

public abstract int getTransactionType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(this.amount);
Expand All @@ -45,7 +45,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
buffer.order(ByteOrder.LITTLE_ENDIAN);

this.amount = buffer.getLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
byte[] username = this.asset.username.getBytes();

ByteBuffer buffer = ByteBuffer.allocate(username.length + 1);
Expand All @@ -41,7 +41,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
int usernameLength = buffer.get() & 0xff;

byte[] username = new byte[usernameLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
return new byte[0];
}

@Override
public void deserialize(ByteBuffer buffer) {}
public void deserializeData(ByteBuffer buffer) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer = ByteBuffer.allocate(48);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(Hex.decode(this.asset.validatorPublicKey));
Expand All @@ -37,7 +37,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
byte[] validatorPublicKey = new byte[48];
buffer.get(validatorPublicKey);
this.asset.validatorPublicKey = Hex.encode(validatorPublicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
return new byte[0];
}

@Override
public void deserialize(ByteBuffer buffer) {}
public void deserializeData(ByteBuffer buffer) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public HashMap<String, Object> assetToHashMap() {
}

@Override
public byte[] serialize() {
public byte[] serializeData() {
ByteBuffer buffer =
ByteBuffer.allocate(
(1 + this.asset.votes.size() * 33) + (1 + this.asset.unvotes.size() * 33));
Expand All @@ -49,7 +49,7 @@ public byte[] serialize() {
}

@Override
public void deserialize(ByteBuffer buffer) {
public void deserializeData(ByteBuffer buffer) {
int voteLength = buffer.get();

for (int i = 0; i < voteLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ void checkNewTransactionType() {
deserializer.setNewTransactionType(
new Transaction() {
@Override
public byte[] serialize() {
public byte[] serializeData() {
return new byte[0];
}

@Override
public void deserialize(ByteBuffer buffer) {}
public void deserializeData(ByteBuffer buffer) {}

@Override
public int getTransactionType() {
Expand Down Expand Up @@ -58,12 +58,12 @@ void checkNewTransactionToCoreGroup() {
deserializer.setNewTransactionType(
new Transaction() {
@Override
public byte[] serialize() {
public byte[] serializeData() {
return new byte[0];
}

@Override
public void deserialize(ByteBuffer buffer) {}
public void deserializeData(ByteBuffer buffer) {}

@Override
public int getTransactionType() {
Expand Down
Loading