Skip to content

Commit

Permalink
Supporting optional transaction parameters (Consensys#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
CjHare authored Mar 20, 2019
1 parent 6c47feb commit 5e7ba11
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public TransactionBodyProvider(final TransactionSigner transactionSigner) {

@Override
public Buffer getBody(RoutingContext context) {

// TODO validate the JSON provided is a correct JSON-RPC formatted transaction payloead
final String signedTransactionHexString =
transactionSigner.signTransaction(context.getBodyAsJson());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

public class TransactionSigner {

private static final int HEXADECIMAL = 16;
private static final int HEXADECIMAL_PREFIX_LENGTH = 2;

private final Credentials credentials;
private final ChainIdProvider chain;

Expand All @@ -40,24 +43,55 @@ public String signTransaction(final JsonObject transaction) {
}

private RawTransaction fromTransactionJson(final JsonObject transaction) {
final JsonObject txnParams = transaction.getJsonArray("params").getJsonObject(0);
String dataString = "";
if (txnParams.getString("data") != null) {
dataString = txnParams.getString("data");
final JsonObject params = transaction.getJsonArray("params").getJsonObject(0);

// TODO validate the nonce is present, ie. not null
return RawTransaction.createTransaction(
nonce(transaction, params),
optionalHex("gasPrice", params),
gas(params),
params.getString("to"),
optionalHex("value", params),
data(params));
}

private BigInteger nonce(final JsonObject transaction, final JsonObject params) {

if (params.containsKey("nonce")) {
return hex("nonce", params);
}

throw new IllegalArgumentException("Missing the nonce%n" + transaction.encodePrettily());
}

private String data(final JsonObject params) {
return params.getString("data");
}

private BigInteger gas(final JsonObject params) {
// TODO(tmm): This should be configurable, but currently matches Geth.
String gasValue = "90000";
if (txnParams.getString("gas") != null) {
gasValue = txnParams.getString("gas").substring(2);

if (params.getString("gas") != null) {
gasValue = params.getString("gas").substring(HEXADECIMAL_PREFIX_LENGTH);
}

return RawTransaction.createTransaction(
new BigInteger(txnParams.getString("nonce").substring(2), 16),
new BigInteger(txnParams.getString("gasPrice").substring(2), 16),
new BigInteger(gasValue, 16),
txnParams.getString("to"),
new BigInteger(txnParams.getString("value").substring(2), 16),
dataString);
return hex(gasValue);
}

// TODO validate hex

private BigInteger hex(final String value) {
return new BigInteger(value, HEXADECIMAL);
}

private BigInteger hex(final String key, final JsonObject params) {
return new BigInteger(params.getString(key).substring(HEXADECIMAL_PREFIX_LENGTH), HEXADECIMAL);
}

private BigInteger optionalHex(final String key, final JsonObject params) {
return params.containsKey(key)
? new BigInteger(params.getString(key).substring(HEXADECIMAL_PREFIX_LENGTH), HEXADECIMAL)
: null;
}
}

0 comments on commit 5e7ba11

Please sign in to comment.