-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2381 from rsksmart/hsm-segwit-compatibility-integ…
…ration New pegout creation event including UTXO outpoint values
- Loading branch information
Showing
20 changed files
with
1,201 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
rskj-core/src/main/java/co/rsk/peg/bitcoin/InvalidOutpointValueException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package co.rsk.peg.bitcoin; | ||
|
||
public class InvalidOutpointValueException extends RuntimeException { | ||
|
||
public InvalidOutpointValueException(String message) { | ||
super(message); | ||
} | ||
|
||
public InvalidOutpointValueException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
rskj-core/src/main/java/co/rsk/peg/bitcoin/UtxoUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package co.rsk.peg.bitcoin; | ||
|
||
import co.rsk.bitcoinj.core.BtcTransaction; | ||
import co.rsk.bitcoinj.core.Coin; | ||
import co.rsk.bitcoinj.core.TransactionInput; | ||
import co.rsk.bitcoinj.core.VarInt; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.spongycastle.util.encoders.Hex; | ||
|
||
public final class UtxoUtils { | ||
|
||
private UtxoUtils() { | ||
} | ||
|
||
/** | ||
* Decode a {@code byte[]} of encoded outpoint values. | ||
* | ||
* @param encodedOutpointValues | ||
* @return {@code List<Coin>} the list of outpoint values decoded preserving | ||
* the order of the entries. Or an {@code Collections.EMPTY_LIST} when {@code encodedOutpointValues} is | ||
* {@code null} or {@code empty byte[]}. | ||
*/ | ||
public static List<Coin> decodeOutpointValues(byte[] encodedOutpointValues) { | ||
if (encodedOutpointValues == null || encodedOutpointValues.length == 0) { | ||
return Collections.emptyList(); | ||
} | ||
int offset = 0; | ||
List<Coin> outpointValues = new ArrayList<>(); | ||
|
||
while (encodedOutpointValues.length > offset) { | ||
VarInt valueAsVarInt; | ||
try { | ||
valueAsVarInt = new VarInt(encodedOutpointValues, offset); | ||
} catch (Exception ex) { | ||
throw new InvalidOutpointValueException( | ||
String.format("Invalid value with invalid VarInt format: %s", | ||
Hex.toHexString(encodedOutpointValues).toUpperCase() | ||
), | ||
ex | ||
); | ||
} | ||
|
||
offset += valueAsVarInt.getSizeInBytes(); | ||
Coin outpointValue = Coin.valueOf(valueAsVarInt.value); | ||
validateOutpointValue(outpointValue); | ||
|
||
outpointValues.add(outpointValue); | ||
} | ||
return outpointValues; | ||
|
||
} | ||
|
||
/** | ||
* Encode a {@code List<Coin} of outpoint values. | ||
* | ||
* @param outpointValues | ||
* @return {@code byte[]} the list of outpoint values encoded preserving the order of the | ||
* entries. Or an {@code empty byte[]} when {@code outpointValues} is {@code null} or | ||
* {@code empty}. | ||
*/ | ||
public static byte[] encodeOutpointValues(List<Coin> outpointValues) { | ||
if (outpointValues == null || outpointValues.isEmpty()) { | ||
return new byte[]{}; | ||
} | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
for (Coin outpointValue : outpointValues) { | ||
validateOutpointValue(outpointValue); | ||
VarInt varIntOutpointValue = new VarInt(outpointValue.getValue()); | ||
try { | ||
outputStream.write(varIntOutpointValue.encode()); | ||
} catch (IOException ex) { | ||
throw new InvalidOutpointValueException( | ||
String.format("I/O exception for value: %s", | ||
outpointValue | ||
), | ||
ex | ||
); | ||
} | ||
} | ||
return outputStream.toByteArray(); | ||
} | ||
|
||
private static void validateOutpointValue(Coin outpointValue) { | ||
if (outpointValue == null || outpointValue.isNegative()) { | ||
throw new InvalidOutpointValueException(String.format( | ||
"Invalid outpoint value: %s. Negative and null values are not allowed.", | ||
outpointValue)); | ||
} | ||
} | ||
|
||
public static List<Coin> extractOutpointValues(BtcTransaction generatedTransaction) { | ||
if (generatedTransaction == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return generatedTransaction.getInputs().stream().map(TransactionInput::getValue).collect( | ||
Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.