Skip to content

Commit

Permalink
Blah
Browse files Browse the repository at this point in the history
  • Loading branch information
Liquid369 committed Oct 25, 2024
1 parent 07c102f commit f81ad99
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/pivx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
# Distributed under the MIT software license, see the accompanying
# file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php.

from typing import Dict, Tuple
from typing import Dict, Tuple, TypedDict
from typing import Any
from misc import getCallerName, getFunctionName, printException
import utils
from pivx_hashlib import pubkeyhash_to_address

class VinType(TypedDict, total=False):
txid: str
vout: int
scriptSig: Dict[str, str]
sequence: int
coinbase: str

class HexParser:
def __init__(self, hex_str: str):
self.cursor = 0
self.hex_str = hex_str

def readInt(self, nbytes: int, byteorder: str = 'big', signed: bool = False) -> int:
def readInt(self, nbytes: int, byteorder: str = "big", signed: bool = False) -> int:
if self.cursor + nbytes * 2 > len(self.hex_str):
raise Exception("HexParser range error")
b = bytes.fromhex(self.hex_str[self.cursor:self.cursor + nbytes * 2])
Expand Down Expand Up @@ -44,12 +51,12 @@ def readString(self, nbytes: int, byteorder: str = "big") -> str:
return res


def IsCoinBase(vin: Dict[str, any]) -> bool:
def IsCoinBase(vin: Dict[str, Any]) -> bool:
return vin["txid"] == "0" * 64 and vin["vout"] == 4294967295 and vin["scriptSig"]["hex"][:2] != "c2"


def ParseTxInput(p: HexParser) -> Dict[str, any]:
vin = {
def ParseTxInput(p: HexParser) -> Dict[str, Any]:
vin = VinType {
"txid": p.readString(32, "little"),
"vout": p.readInt(4, "little"),
"scriptSig": {
Expand All @@ -66,7 +73,7 @@ def ParseTxInput(p: HexParser) -> Dict[str, any]:
return vin


def ParseTxOutput(p: HexParser, isTestnet: bool = False) -> Dict[str, any]:
def ParseTxOutput(p: HexParser, isTestnet: bool = False) -> Dict[str, Any]:
vout = {
"value": p.readInt(8, "little"),
"scriptPubKey": {
Expand All @@ -86,7 +93,7 @@ def ParseTxOutput(p: HexParser, isTestnet: bool = False) -> Dict[str, any]:
return vout


def ParseTx(hex_string: str, isTestnet: bool = False) -> Dict[str, any]:
def ParseTx(hex_string: str, isTestnet: bool = False) -> Dict[str, Any]:
p = HexParser(hex_string)
tx = {
"version": p.readInt(4, "little"),
Expand All @@ -103,7 +110,7 @@ def IsPayToColdStaking(rawtx: str, out_n: int) -> Tuple[bool, bool]:
return utils.IsPayToColdStaking(bytes.fromhex(script)), IsCoinStake(tx)


def IsCoinStake(json_tx: Dict[str, any]) -> bool:
def IsCoinStake(json_tx: Dict[str, Any]) -> bool:
return json_tx['vout'][0]["scriptPubKey"]["hex"] == ""


Expand Down

0 comments on commit f81ad99

Please sign in to comment.