Skip to content

Commit

Permalink
Added helper functions to check if a tx spents p2pkh input
Browse files Browse the repository at this point in the history
  • Loading branch information
kwuaint committed Jul 28, 2018
1 parent d6b1768 commit 1958d1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
12 changes: 6 additions & 6 deletions uscj-core/src/main/java/co/usc/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ public void registerUldTransaction(Transaction uscTx, byte[] uldTxSerialized, in
}

// Check there are at least N blocks on top of the supplied height
int headHeight = uldBlockChain.getBestChainHeight();
if ((headHeight - height + 1) < bridgeConstants.getUld2UscMinimumAcceptableConfirmations()) {
logger.warn("At least " + bridgeConstants.getUld2UscMinimumAcceptableConfirmations() + " confirmations are required, but there are only " + (headHeight - height) + " confirmations");
int confirmations = uldBlockChain.getBestChainHeight() - height + 1;
if (confirmations < bridgeConstants.getUld2UscMinimumAcceptableConfirmations()) {
logger.warn("At least " + bridgeConstants.getUld2UscMinimumAcceptableConfirmations() + " confirmations are required, but there are only " + confirmations + " confirmations");
return;
}

Expand All @@ -439,8 +439,8 @@ public void registerUldTransaction(Transaction uscTx, byte[] uldTxSerialized, in
// Specific code for lock/release/none txs
if (BridgeUtils.isLockTx(uldTx, getLiveFederations(), uldContext, bridgeConstants)) {
logger.debug("This is a lock tx {}", uldTx);
Script scriptSig = uldTx.getInput(0).getScriptSig();
if (scriptSig.getChunks().size() != 2) {
Optional<Script> scriptSig = BridgeUtils.getFirstInputScriptSig(uldTx);
if (!scriptSig.isPresent()) {
logger.warn("[uldlock:{}] First input does not spend a Pay-to-PubkeyHash " + uldTx.getInput(0), uldTx.getHash());
return;
}
Expand All @@ -457,7 +457,7 @@ public void registerUldTransaction(Transaction uscTx, byte[] uldTxSerialized, in
Coin totalAmount = amountToActive.add(amountToRetiring);

// Get the sender public key
byte[] data = scriptSig.getChunks().get(1).data;
byte[] data = scriptSig.get().getChunks().get(1).data;

// Tx is a lock tx, check whether the sender is whitelisted
UldECKey senderUldKey = UldECKey.fromPublicOnly(data);
Expand Down
36 changes: 33 additions & 3 deletions uscj-core/src/main/java/co/usc/peg/BridgeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import co.usc.ulordj.core.*;
import co.usc.ulordj.script.Script;
import co.usc.ulordj.store.BlockStoreException;
import co.usc.ulordj.store.UldBlockStore;
import co.usc.ulordj.wallet.Wallet;
import co.usc.config.BridgeConstants;
import co.usc.core.UscAddress;
Expand All @@ -33,11 +32,12 @@
import org.ethereum.vm.PrecompiledContracts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* @author Oscar Guindzberg
Expand Down Expand Up @@ -130,6 +130,36 @@ private static boolean scriptCorrectlySpendsTx(UldTransaction tx, int index, Scr
}
}

/**
* Indicates whether a tx is a valid lock tx or not, checking the first input's script sig
* @param tx
* @return
*/
public static boolean isValidLockTx(UldTransaction tx) {
if (tx.getInputs().size() == 0) {
return false;
}
// This indicates that the tx is a P2PKH transaction which is the only one we support for now
if (tx.getInput(0).getScriptSig().getChunks().size() != 2) {
logger.warn("[btctx:{}] is not a valid lock tx and won't be processed!", Hex.toHexString(tx.getHash().getBytes()));
return false;
}
return true;
}

/**
* Will return a valid scriptsig for the first input
* @param tx
* @return
*/
public static Optional<Script> getFirstInputScriptSig(UldTransaction tx) {
if (!isValidLockTx(tx)) {
return Optional.empty();
}
return Optional.of(tx.getInput(0).getScriptSig());
}


public static boolean isLockTx(UldTransaction tx, List<Federation> federations, Context uldContext, BridgeConstants bridgeConstants) {
// First, check tx is not a typical release tx (tx spending from the any of the federation addresses and
// optionally sending some change to any of the federation addresses)
Expand All @@ -145,7 +175,7 @@ public static boolean isLockTx(UldTransaction tx, List<Federation> federations,

int valueSentToMeSignum = valueSentToMe.signum();
if (valueSentToMe.isLessThan(bridgeConstants.getMinimumLockTxValue())) {
logger.warn("Someone sent to the federation less than {} satoshis", bridgeConstants.getMinimumLockTxValue());
logger.warn("[uldtx:{}]Someone sent to the federation less than {} satoshis", Hex.toHexString(tx.getHash().getBytes()), bridgeConstants.getMinimumLockTxValue());
}
return (valueSentToMeSignum > 0 && !valueSentToMe.isLessThan(bridgeConstants.getMinimumLockTxValue()));
}
Expand Down

0 comments on commit 1958d1a

Please sign in to comment.