-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate TransactionOutput.script_public_key_type to avoid having to…
… 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.
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
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 |