Skip to content

Commit

Permalink
Calculate TransactionOutput.script_public_key_type to avoid having to…
Browse files Browse the repository at this point in the history
… store it.
  • Loading branch information
supertypo committed May 3, 2024
1 parent 40fdccd commit 4c4ff51
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions helper/PublicKeyType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
SCRIPT_CLASS_PUB_KEY = 'pubkey'
SCRIPT_CLASS_PUB_KEY_ECDSA = 'pubkeyecdsa'
SCRIPT_CLASS_SCRIPT_HASH = 'scripthash'
SCRIPT_CLASS_NON_STANDARD = 'nonstandard'


# Get the public_key_type for a public key.
# The types are PubKey, PubKeyECDSA, ScriptHash and NonStandard
def get_public_key_type(public_key):
if public_key is None:
return None
public_key_bytes = bytes.fromhex(public_key)
if is_pay_to_pubkey(public_key_bytes):
return SCRIPT_CLASS_PUB_KEY
if is_pay_to_pubkey_ecdsa(public_key_bytes):
return SCRIPT_CLASS_PUB_KEY_ECDSA
if is_pay_to_script_hash(public_key_bytes):
return SCRIPT_CLASS_SCRIPT_HASH
return SCRIPT_CLASS_NON_STANDARD


def is_pay_to_pubkey(public_key_bytes):
if len(public_key_bytes) == 34 and \
public_key_bytes[0] == 0x20 and \
public_key_bytes[33] == 0xac:
return True
return False


def is_pay_to_pubkey_ecdsa(public_key_bytes):
if len(public_key_bytes) == 35 and \
public_key_bytes[0] == 0x21 and \
public_key_bytes[34] == 0xab:
return True
return False


def is_pay_to_script_hash(public_key_bytes):
if len(public_key_bytes) == 35 and \
public_key_bytes[0] == 0xaa and \
public_key_bytes[1] == 0x20 and \
public_key_bytes[34] == 0x87:
return True
return False

0 comments on commit 4c4ff51

Please sign in to comment.