-
Notifications
You must be signed in to change notification settings - Fork 70
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 #92 from jmacxx/entitlement_checking_mempool
Check roles using mempool tx lookup.
- Loading branch information
Showing
11 changed files
with
367 additions
and
53 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
110 changes: 110 additions & 0 deletions
110
social/src/main/java/bisq/social/userprofile/BsqTxValidator.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,110 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.social.userprofile; | ||
|
||
import bisq.common.data.Pair; | ||
import bisq.common.encoding.Hex; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Optional; | ||
|
||
@Slf4j | ||
public class BsqTxValidator { | ||
|
||
public static boolean initialSanityChecks(String txId, String jsonTxt) { | ||
if (jsonTxt == null || jsonTxt.length() == 0) { | ||
return false; | ||
} | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
// there should always be "id" string element at the top level | ||
if (json.get("id") == null) { | ||
return false; | ||
} | ||
// txid should match what we requested | ||
if (!txId.equals(json.get("id").getAsString())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static boolean isBsqTx(String url, String txId, String jsonTxt) { | ||
return url.matches(".*bisq.*") && initialSanityChecks(txId, jsonTxt); | ||
} | ||
|
||
public static boolean isProofOfBurn(String jsonTxt) { | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
JsonElement txType = json.get("txType"); | ||
if (txType == null) { | ||
return false; | ||
} | ||
return txType.getAsString().equalsIgnoreCase("PROOF_OF_BURN"); | ||
} | ||
|
||
public static boolean isLockup(String jsonTxt) { | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
JsonElement txType = json.get("txType"); | ||
if (txType == null) { | ||
return false; | ||
} | ||
return txType.getAsString().equalsIgnoreCase("LOCKUP"); | ||
} | ||
|
||
public static long getBurntAmount(String jsonTxt) { | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
JsonElement burntFee = json.get("burntFee"); | ||
if (burntFee == null) { | ||
return 0; // no json element, assume zero burnt amount | ||
} | ||
return burntFee.getAsLong(); | ||
} | ||
|
||
public static Optional<String> getOpReturnData(String jsonTxt) { | ||
try { | ||
Pair<JsonArray, JsonArray> vinAndVout = getVinAndVout(jsonTxt); | ||
JsonArray voutArray = vinAndVout.second(); | ||
for (JsonElement x : voutArray) { | ||
JsonObject y = x.getAsJsonObject(); | ||
if (y.get("txOutputType").getAsString().matches(".*OP_RETURN.*")) { | ||
return Optional.of(y.get("opReturn").getAsString()); | ||
} | ||
} | ||
} catch (JsonSyntaxException e) { | ||
log.error("json error:", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static Pair<JsonArray, JsonArray> getVinAndVout(String jsonTxt) throws JsonSyntaxException { | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
if (json.get("inputs") == null || json.get("outputs") == null) { | ||
throw new JsonSyntaxException("missing vin/vout"); | ||
} | ||
JsonArray jsonVin = json.get("inputs").getAsJsonArray(); | ||
JsonArray jsonVout = json.get("outputs").getAsJsonArray(); | ||
if (jsonVin == null || jsonVout == null || jsonVin.size() < 1 || jsonVout.size() < 1) { | ||
throw new JsonSyntaxException("not enough vins/vouts"); | ||
} | ||
return new Pair<>(jsonVin, jsonVout); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
social/src/main/java/bisq/social/userprofile/BtcTxValidator.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,93 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.social.userprofile; | ||
|
||
import bisq.common.data.Pair; | ||
import bisq.common.encoding.Hex; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.security.PublicKey; | ||
|
||
@Slf4j | ||
public class BtcTxValidator { | ||
|
||
private static int PUBLIC_KEY_LENGTH = 33; | ||
|
||
public static boolean initialSanityChecks(String txId, String jsonTxt) { | ||
if (jsonTxt == null || jsonTxt.length() == 0) { | ||
return false; | ||
} | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
// there should always be "txid" string element at the top level | ||
if (json.get("txid") == null) { | ||
return false; | ||
} | ||
// txid should match what we requested | ||
if (!txId.equals(json.get("txid").getAsString())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static String getFirstInputPubKey(String jsonTxt) { | ||
try { | ||
Pair<JsonArray, JsonArray> vinAndVout = getVinAndVout(jsonTxt); | ||
JsonArray vinArray = vinAndVout.first(); | ||
for (JsonElement x : vinArray) { | ||
JsonObject vin = x.getAsJsonObject(); | ||
// pubKey in witness or scriptsig (legacy or segwit txs) | ||
JsonArray witnesses = vin.getAsJsonArray("witness"); | ||
if (witnesses != null) { | ||
String witnessPubKey = witnesses.get(1).getAsString(); | ||
if (witnessPubKey.length() >= PUBLIC_KEY_LENGTH * 2) { | ||
return witnessPubKey; | ||
} | ||
} | ||
JsonElement scriptsig = vin.get("scriptsig"); | ||
if (scriptsig != null) { | ||
String scriptsigAsHex = scriptsig.getAsString(); | ||
if (scriptsigAsHex.length() >= PUBLIC_KEY_LENGTH * 2) { | ||
return scriptsigAsHex.substring(scriptsigAsHex.length() - PUBLIC_KEY_LENGTH * 2); | ||
} | ||
} | ||
} | ||
} catch (JsonSyntaxException e) { | ||
log.error("json error:", e); | ||
} | ||
throw new JsonSyntaxException("could not find pubKey"); | ||
} | ||
|
||
private static Pair<JsonArray, JsonArray> getVinAndVout(String jsonTxt) throws JsonSyntaxException { | ||
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class); | ||
if (json.get("vin") == null || json.get("vout") == null) { | ||
throw new JsonSyntaxException("missing vin/vout"); | ||
} | ||
JsonArray jsonVin = json.get("vin").getAsJsonArray(); | ||
JsonArray jsonVout = json.get("vout").getAsJsonArray(); | ||
if (jsonVin == null || jsonVout == null || jsonVin.size() < 1 || jsonVout.size() < 1) { | ||
throw new JsonSyntaxException("not enough vins/vouts"); | ||
} | ||
return new Pair<>(jsonVin, jsonVout); | ||
} | ||
} |
Oops, something went wrong.