Skip to content

Commit

Permalink
SendTransactionParms To RawTransaction converter created (Consensys#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain-on authored Apr 4, 2019
1 parent f583eca commit a48ad4c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.mockserver.model.JsonBody.json;
import static org.web3j.utils.Async.defaultExecutorService;

import tech.pegasys.ethfirewall.RawTransactionConverter;
import tech.pegasys.ethfirewall.Runner;
import tech.pegasys.ethfirewall.jsonrpcproxy.model.request.EthFirewallRequest;
import tech.pegasys.ethfirewall.jsonrpcproxy.model.request.EthNodeRequest;
Expand Down Expand Up @@ -180,7 +181,7 @@ private static TransactionSigner transactionSigner(final ChainIdProvider chain)
final File keyFile = createKeyFile();
final Credentials credentials = WalletUtils.loadCredentials("password", keyFile);

return new TransactionSigner(chain, credentials);
return new TransactionSigner(chain, credentials, new RawTransactionConverter());
}

@SuppressWarnings("UnstableApiUsage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private TransactionSigner transactionSigner(
throws IOException, CipherException {
final Credentials credentials = WalletUtils.loadCredentials(password, keyFile);

return new TransactionSigner(chain, credentials);
return new TransactionSigner(chain, credentials, new RawTransactionConverter());
}

private Optional<String> readPasswordFromFile() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.ethfirewall;

import tech.pegasys.ethfirewall.jsonrpc.SendTransactionJsonParameters;

import java.math.BigInteger;
import java.util.Optional;

import org.web3j.crypto.RawTransaction;

public class RawTransactionConverter {

public RawTransaction from(final SendTransactionJsonParameters input) {
return RawTransaction.createTransaction(
valueOrDefault(input.nonce(), null),
valueOrDefault(input.gasPrice(), BigInteger.ZERO),
valueOrDefault(input.gas(), BigInteger.valueOf(90000)),
valueOrDefault(input.receiver(), ""),
valueOrDefault(input.value(), BigInteger.ZERO),
input.data());
}

private <T> T valueOrDefault(Optional<T> value, final T defaultValue) {
if (value.isPresent()) {
return value.get();
}

if (defaultValue != null) {
return defaultValue;
} else {
throw new RuntimeException("Unable to have a default value of null.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
*/
package tech.pegasys.ethfirewall.signing;

import tech.pegasys.ethfirewall.RawTransactionConverter;
import tech.pegasys.ethfirewall.jsonrpc.SendTransactionJsonParameters;
import tech.pegasys.ethfirewall.signing.web3j.TransactionEncoder;

import java.math.BigInteger;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.utils.Numeric;
Expand All @@ -25,18 +24,23 @@ public class TransactionSigner {

private final Credentials credentials;
private final ChainIdProvider chain;
private final RawTransactionConverter converter;

public TransactionSigner(final ChainIdProvider chain, final Credentials credentials) {
public TransactionSigner(
final ChainIdProvider chain,
final Credentials credentials,
final RawTransactionConverter converter) {
this.chain = chain;
this.credentials = credentials;
this.converter = converter;
}

public String signTransaction(final SendTransactionJsonParameters params) {
if (senderNotUnlockedAccount(params)) {
throw new IllegalArgumentException("From address does not match unlocked account");
}

final RawTransaction rawTransaction = rawTransaction(params);
final RawTransaction rawTransaction = converter.from(params);

// Sign the transaction using the post Spurious Dragon technique
final byte[] signedMessage =
Expand All @@ -47,38 +51,4 @@ public String signTransaction(final SendTransactionJsonParameters params) {
private boolean senderNotUnlockedAccount(final SendTransactionJsonParameters params) {
return !params.sender().equalsIgnoreCase(credentials.getAddress());
}

private RawTransaction rawTransaction(final SendTransactionJsonParameters params) {

return RawTransaction.createTransaction(
nonce(params),
params.gasPrice().orElse(BigInteger.ZERO),
gas(params),
receiver(params),
params.value().orElse(BigInteger.ZERO),
params.data());
}

private String receiver(final SendTransactionJsonParameters params) {
return params.receiver().orElse(null);
}

private BigInteger nonce(final SendTransactionJsonParameters params) {
if (params.nonce().isPresent()) {
return params.nonce().get();
} else {
// TODO when missing nonce - sensible retrieval (PIE-1468)
throw new RuntimeException("Missing nonce");
}
}

private BigInteger gas(final SendTransactionJsonParameters params) {

if (params.gas().isPresent()) {
return params.gas().get();
}

// TODO This should be configurable, but currently matches Geth.
return new BigInteger("90000");
}
}

0 comments on commit a48ad4c

Please sign in to comment.