Skip to content

Commit

Permalink
temporary example how to decode a signedTx and build the SpendTransac…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
marc0olo committed Aug 12, 2019
1 parent 509190a commit 4c28dd0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public void postSpendTxTest(TestContext context) {
Tx signedTx =
transactionServiceNative.signTransaction(unsignedTxNative, keyPair.getPrivateKey());

context.assertEquals(
spendTx, transactionServiceNative.decodeTransaction(signedTx.getTx()));

Single<PostTxResponse> txResponse = transactionServiceNative.postTransaction(signedTx);
txResponse.subscribe(
it -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ Single<DryRunResults> dryRunTransactions(
List<Map<AccountParameter, Object>> accounts,
BigInteger block,
List<UnsignedTx> unsignedTransactions);

AbstractTransaction<?> decodeTransaction(String encodedSignedTx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.kryptokrauts.aeternity.sdk.service.transaction.TransactionServiceConfiguration;
import com.kryptokrauts.aeternity.sdk.service.transaction.type.AbstractTransaction;
import com.kryptokrauts.aeternity.sdk.service.transaction.type.TransactionFactory;
import com.kryptokrauts.aeternity.sdk.service.transaction.type.impl.SpendTransaction;
import com.kryptokrauts.aeternity.sdk.util.ByteUtils;
import com.kryptokrauts.aeternity.sdk.util.EncodingUtils;
import com.kryptokrauts.aeternity.sdk.util.SigningUtil;
Expand Down Expand Up @@ -228,6 +229,76 @@ private String encodeSignedTransaction(byte[] sig, byte[] binaryTx) {
return EncodingUtils.encodeCheck(encodedRlp.toArray(), ApiIdentifiers.TRANSACTION);
}

@Override
public AbstractTransaction<?> decodeTransaction(String encodedSignedTx) {
_logger.debug("-------- decodeTransaction --------");
byte[] encodedRlpByteArray = EncodingUtils.decodeCheckWithIdentifier(encodedSignedTx);
Bytes encodedRlp = Bytes.wrap(encodedRlpByteArray);
byte[] binaryTx =
RLP.decodeList(
encodedRlp,
rlpReader -> {
_logger.debug(String.format("Serialization Object-Tag: %s", rlpReader.readInt()));
_logger.debug(String.format("VSN (RLP Version): %s", rlpReader.readInt()));
rlpReader.readList(
reader -> {
int count = 0;
while (!reader.isComplete()) {
reader.readByteArray();
count++;
_logger.debug(String.format("Signatures: %s", count));
}
return null;
});
return rlpReader.readByteArray();
});
return decodeTransactionType(binaryTx);
}

private AbstractTransaction<?> decodeTransactionType(byte[] binaryTx) {
Bytes encodedRlp = Bytes.wrap(binaryTx);
return RLP.decodeList(
encodedRlp,
rlpReader -> {
AbstractTransaction<?> tx = null;
int objectTag = rlpReader.readInt();
switch (objectTag) {
case SerializationTags.OBJECT_TAG_SPEND_TRANSACTION:
rlpReader.readInt(); // VSN not needed for creating the TxModel
byte[] senderWithTag = rlpReader.readByteArray();
byte[] recipientWithTag = rlpReader.readByteArray();
String sender =
EncodingUtils.encodeCheck(
Arrays.copyOfRange(senderWithTag, 1, senderWithTag.length),
ApiIdentifiers.ACCOUNT_PUBKEY);
String recipient =
EncodingUtils.encodeCheck(
Arrays.copyOfRange(recipientWithTag, 1, recipientWithTag.length),
ApiIdentifiers.ACCOUNT_PUBKEY);
BigInteger amount = rlpReader.readBigInteger(true);
BigInteger fee = rlpReader.readBigInteger(true);
BigInteger ttl = rlpReader.readBigInteger(true);
BigInteger nonce = rlpReader.readBigInteger(true);
String payload = new String(rlpReader.readByteArray());
tx =
SpendTransaction.builder()
.sender(sender)
.recipient(recipient)
.amount(amount)
.payload(payload)
.ttl(ttl)
.nonce(nonce)
.build();
tx.setFee(fee);
break;
default:
throw new IllegalArgumentException(
String.format("Serialization Object-Tag '%s' is not supported.", objectTag));
}
return tx;
});
}

private TransactionApi getTransactionApi() {
if (transactionApi == null) {
transactionApi = new TransactionApi(new TransactionApiImpl(config.getApiClient()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kryptokrauts.aeternity.sdk.util.EncodingUtils;
import io.reactivex.Single;
import java.math.BigInteger;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.rlp.RLPWriter;
Expand All @@ -23,7 +24,7 @@ public abstract class AbstractTransaction<TxModel> {

private static final Logger _logger = LoggerFactory.getLogger(AbstractTransaction.class);

protected BigInteger fee;
@EqualsAndHashCode.Include protected BigInteger fee;

/** fee calculation model for this transaction type, one of {@link FeeCalculationModel} */
protected FeeCalculationModel feeCalculationModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.kryptokrauts.aeternity.sdk.util.EncodingUtils;
import io.reactivex.Single;
import java.math.BigInteger;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;
Expand All @@ -16,15 +17,16 @@

@Getter
@SuperBuilder
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class SpendTransaction extends AbstractTransaction<SpendTx> {

@NonNull private String sender;
@NonNull private String recipient;
@NonNull private BigInteger amount;
@NonNull private String payload;
@NonNull private BigInteger ttl;
@NonNull private BigInteger nonce;
@NonNull private TransactionApi transactionApi;
@EqualsAndHashCode.Include @NonNull private String sender;
@EqualsAndHashCode.Include @NonNull private String recipient;
@EqualsAndHashCode.Include @NonNull private BigInteger amount;
@EqualsAndHashCode.Include @NonNull private String payload;
@EqualsAndHashCode.Include @NonNull private BigInteger ttl;
@EqualsAndHashCode.Include @NonNull private BigInteger nonce;
private TransactionApi transactionApi;

@Override
protected Single<UnsignedTx> createInternal() {
Expand Down

0 comments on commit 4c28dd0

Please sign in to comment.