forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: HenrikJannsen <[email protected]>
- Loading branch information
1 parent
f81ab85
commit 93f156b
Showing
29 changed files
with
1,430 additions
and
5 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
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
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
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
128 changes: 128 additions & 0 deletions
128
restapi/src/main/java/bisq/restapi/BlockDataToJsonConverter.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,128 @@ | ||
package bisq.restapi; | ||
|
||
import bisq.core.dao.state.DaoStateService; | ||
import bisq.core.dao.state.model.blockchain.Block; | ||
import bisq.core.dao.state.model.blockchain.PubKeyScript; | ||
import bisq.core.dao.state.model.blockchain.Tx; | ||
import bisq.core.dao.state.model.blockchain.TxOutput; | ||
import bisq.core.dao.state.model.blockchain.TxType; | ||
|
||
import com.google.common.io.BaseEncoding; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
|
||
import bisq.restapi.dto.JsonBlock; | ||
import bisq.restapi.dto.JsonScriptPubKey; | ||
import bisq.restapi.dto.JsonSpentInfo; | ||
import bisq.restapi.dto.JsonTx; | ||
import bisq.restapi.dto.JsonTxInput; | ||
import bisq.restapi.dto.JsonTxOutput; | ||
import bisq.restapi.dto.JsonTxOutputType; | ||
import bisq.restapi.dto.JsonTxType; | ||
|
||
@Slf4j | ||
public class BlockDataToJsonConverter { | ||
public static JsonBlock getJsonBlock(DaoStateService daoStateService, Block block) { | ||
List<JsonTx> jsonTxs = block.getTxs().stream() | ||
.map(tx -> getJsonTx(daoStateService, tx)) | ||
.collect(Collectors.toList()); | ||
return new JsonBlock(block.getHeight(), | ||
block.getTime(), | ||
block.getHash(), | ||
block.getPreviousBlockHash(), | ||
jsonTxs); | ||
} | ||
|
||
public static JsonTx getJsonTx(DaoStateService daoStateService, Tx tx) { | ||
JsonTxType jsonTxType = getJsonTxType(tx); | ||
String jsonTxTypeDisplayString = getJsonTxTypeDisplayString(jsonTxType); | ||
return new JsonTx(tx.getId(), | ||
tx.getBlockHeight(), | ||
tx.getBlockHash(), | ||
tx.getTime(), | ||
getJsonTxInputs(daoStateService, tx), | ||
getJsonTxOutputs(daoStateService, tx), | ||
jsonTxType, | ||
jsonTxTypeDisplayString, | ||
tx.getBurntFee(), | ||
tx.getInvalidatedBsq(), | ||
tx.getUnlockBlockHeight()); | ||
} | ||
|
||
private static List<JsonTxInput> getJsonTxInputs(DaoStateService daoStateService, Tx tx) { | ||
return tx.getTxInputs().stream() | ||
.map(txInput -> { | ||
Optional<TxOutput> optionalTxOutput = daoStateService.getConnectedTxOutput(txInput); | ||
if (optionalTxOutput.isPresent()) { | ||
TxOutput connectedTxOutput = optionalTxOutput.get(); | ||
boolean isBsqTxOutputType = daoStateService.isBsqTxOutputType(connectedTxOutput); | ||
return new JsonTxInput(txInput.getConnectedTxOutputIndex(), | ||
txInput.getConnectedTxOutputTxId(), | ||
connectedTxOutput.getValue(), | ||
isBsqTxOutputType, | ||
connectedTxOutput.getAddress(), | ||
tx.getTime()); | ||
} else { | ||
return null; | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static List<JsonTxOutput> getJsonTxOutputs(DaoStateService daoStateService, Tx tx) { | ||
JsonTxType jsonTxType = getJsonTxType(tx); | ||
String jsonTxTypeDisplayString = getJsonTxTypeDisplayString(jsonTxType); | ||
return tx.getTxOutputs().stream() | ||
.map(txOutput -> { | ||
boolean isBsqTxOutputType = daoStateService.isBsqTxOutputType(txOutput); | ||
long bsqAmount = isBsqTxOutputType ? txOutput.getValue() : 0; | ||
long btcAmount = !isBsqTxOutputType ? txOutput.getValue() : 0; | ||
PubKeyScript pubKeyScript = txOutput.getPubKeyScript(); | ||
JsonScriptPubKey scriptPubKey = pubKeyScript != null ? new JsonScriptPubKey(pubKeyScript) : null; | ||
JsonSpentInfo spentInfo = daoStateService.getSpentInfo(txOutput).map(JsonSpentInfo::new).orElse(null); | ||
JsonTxOutputType txOutputType = JsonTxOutputType.valueOf(txOutput.getTxOutputType().name()); | ||
int lockTime = txOutput.getLockTime(); | ||
BaseEncoding HEX = BaseEncoding.base16().lowerCase(); | ||
String opReturn = txOutput.getOpReturnData() != null ? HEX.encode(txOutput.getOpReturnData()) : null; | ||
boolean isUnspent = daoStateService.isUnspent(txOutput.getKey()); | ||
return new JsonTxOutput(tx.getId(), | ||
txOutput.getIndex(), | ||
bsqAmount, | ||
btcAmount, | ||
tx.getBlockHeight(), | ||
isBsqTxOutputType, | ||
tx.getBurntFee(), | ||
tx.getInvalidatedBsq(), | ||
txOutput.getAddress(), | ||
scriptPubKey, | ||
spentInfo, | ||
tx.getTime(), | ||
jsonTxType, | ||
jsonTxTypeDisplayString, | ||
txOutputType, | ||
txOutputType.getDisplayString(), | ||
opReturn, | ||
lockTime, | ||
isUnspent | ||
); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static String getJsonTxTypeDisplayString(JsonTxType jsonTxType) { | ||
return jsonTxType != null ? jsonTxType.getDisplayString() : ""; | ||
} | ||
|
||
private static JsonTxType getJsonTxType(Tx tx) { | ||
TxType txType = tx.getTxType(); | ||
return txType != null ? JsonTxType.valueOf(txType.name()) : null; | ||
} | ||
} |
Oops, something went wrong.