Skip to content

Commit

Permalink
Add beacon hash field
Browse files Browse the repository at this point in the history
  • Loading branch information
aion-kelvin authored and aionick committed Sep 20, 2019
1 parent 7ad146b commit 5114e24
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/main/SignedTransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class SignedTransactionBuilder {
private byte[] data = null;
private long energyPrice = -1;
private byte type = 0x1;
private byte[] beaconHash = null;

/**
* The private key used to sign the transaction with.
Expand Down Expand Up @@ -143,6 +144,11 @@ public SignedTransactionBuilder useAvmTransactionType() {
return this;
}

public SignedTransactionBuilder beaconHash(byte[] hash) {
this.beaconHash = hash;
return this;
}

/**
* Constructs a transaction whose fields correspond to the fields as they have been set by the
* provided builder methods, and signs this transaction with the provided private key.
Expand Down Expand Up @@ -183,7 +189,6 @@ public byte[] buildSignedTransaction() throws InvalidKeySpecException, InvalidKe

byte[] nonce = this.nonce.toByteArray();
byte[] timestamp = BigInteger.valueOf(System.currentTimeMillis() * 1000).toByteArray();

byte[] encodedNonce = RLP.encodeElement(nonce);
byte[] encodedTo = RLP.encodeElement(to);
byte[] encodedValue = RLP.encodeElement(value);
Expand All @@ -193,7 +198,22 @@ public byte[] buildSignedTransaction() throws InvalidKeySpecException, InvalidKe
byte[] encodedEnergyPrice = RLP.encodeLong((this.energyPrice == -1) ? 10_000_000_000L : this.energyPrice);
byte[] encodedType = RLP.encodeByte(this.type);

byte[] fullEncoding = RLP.encodeList(encodedNonce, encodedTo, encodedValue, encodedData, encodedTimestamp, encodedEnergy, encodedEnergyPrice, encodedType);
final byte[] fullEncoding;
final byte[] shapeIdEncoded;
final byte[] beaconHashEncoded;
if(beaconHash == null) {
beaconHashEncoded = null;
shapeIdEncoded = null;
fullEncoding = RLP.encodeList(encodedNonce, encodedTo, encodedValue, encodedData, encodedTimestamp,
encodedEnergy, encodedEnergyPrice, encodedType);
} else {
// For details, see https://aionnetwork.atlassian.net/wiki/spaces/TE/pages/292389035/Transaction+RLP+Encoding
beaconHashEncoded = RLP.encodeElement(beaconHash);
byte shapeId = (byte)1;
shapeIdEncoded = RLP.encodeByte(shapeId);
fullEncoding = RLP.encodeList(encodedNonce, encodedTo, encodedValue, encodedData, encodedTimestamp,
encodedEnergy, encodedEnergyPrice, encodedType, shapeIdEncoded, beaconHashEncoded);
}

byte[] rawHash = blake2b(fullEncoding);
byte[] signatureOnly = sign(privateKey, rawHash);
Expand All @@ -202,7 +222,16 @@ public byte[] buildSignedTransaction() throws InvalidKeySpecException, InvalidKe
System.arraycopy(signatureOnly, 0, preEncodeSignature, publicKey.length, signatureOnly.length);
byte[] signature = RLP.encodeElement(preEncodeSignature);

return RLP.encodeList(encodedNonce, encodedTo, encodedValue, encodedData, encodedTimestamp, encodedEnergy, encodedEnergyPrice, encodedType, signature);
if(beaconHash == null) {
return RLP.encodeList(encodedNonce, encodedTo, encodedValue,
encodedData, encodedTimestamp, encodedEnergy,
encodedEnergyPrice, encodedType, signature);
} else {
return RLP.encodeList(encodedNonce, encodedTo, encodedValue,
encodedData, encodedTimestamp, encodedEnergy,
encodedEnergyPrice, encodedType, signature,
shapeIdEncoded, beaconHashEncoded);
}
}

/**
Expand Down Expand Up @@ -246,6 +275,7 @@ public void reset() {
this.data = null;
this.energyPrice = -1;
this.type = 0x1;
this.beaconHash = null;
}

private static byte[] addSkPrefix(byte[] skString) {
Expand Down

0 comments on commit 5114e24

Please sign in to comment.