Skip to content

Commit fdc1ca3

Browse files
committed
test: add constants for PSBT key types (BIP 174)
Also take use of the constants in the signet miner to get rid of magic numbers and increase readability and maintainability.
1 parent 1b035c0 commit fdc1ca3

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

contrib/signet/miner

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ sys.path.insert(0, PATH_BASE_TEST_FUNCTIONAL)
2020

2121
from test_framework.blocktools import get_witness_script, script_BIP34_coinbase_height # noqa: E402
2222
from test_framework.messages import CBlock, CBlockHeader, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, from_binary, from_hex, ser_string, ser_uint256, tx_from_hex # noqa: E402
23-
from test_framework.psbt import PSBT, PSBTMap # noqa: E402
23+
from test_framework.psbt import PSBT, PSBTMap, PSBT_GLOBAL_UNSIGNED_TX, PSBT_IN_FINAL_SCRIPTSIG, PSBT_IN_FINAL_SCRIPTWITNESS, PSBT_IN_NON_WITNESS_UTXO, PSBT_IN_SIGHASH_TYPE # noqa: E402
2424
from test_framework.script import CScriptOp # noqa: E402
2525

2626
logging.basicConfig(
@@ -74,11 +74,11 @@ def signet_txs(block, challenge):
7474

7575
def do_createpsbt(block, signme, spendme):
7676
psbt = PSBT()
77-
psbt.g = PSBTMap( {0: signme.serialize(),
77+
psbt.g = PSBTMap( {PSBT_GLOBAL_UNSIGNED_TX: signme.serialize(),
7878
PSBT_SIGNET_BLOCK: block.serialize()
7979
} )
80-
psbt.i = [ PSBTMap( {0: spendme.serialize(),
81-
3: bytes([1,0,0,0])})
80+
psbt.i = [ PSBTMap( {PSBT_IN_NON_WITNESS_UTXO: spendme.serialize(),
81+
PSBT_IN_SIGHASH_TYPE: bytes([1,0,0,0])})
8282
]
8383
psbt.o = [ PSBTMap() ]
8484
return psbt.to_base64()
@@ -90,8 +90,8 @@ def do_decode_psbt(b64psbt):
9090
assert len(psbt.tx.vout) == 1
9191
assert PSBT_SIGNET_BLOCK in psbt.g.map
9292

93-
scriptSig = psbt.i[0].map.get(7, b"")
94-
scriptWitness = psbt.i[0].map.get(8, b"\x00")
93+
scriptSig = psbt.i[0].map.get(PSBT_IN_FINAL_SCRIPTSIG, b"")
94+
scriptWitness = psbt.i[0].map.get(PSBT_IN_FINAL_SCRIPTWITNESS, b"\x00")
9595

9696
return from_binary(CBlock, psbt.g.map[PSBT_SIGNET_BLOCK]), ser_string(scriptSig) + scriptWitness
9797

test/functional/test_framework/psbt.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,57 @@
1313
)
1414

1515

16+
# global types
17+
PSBT_GLOBAL_UNSIGNED_TX = 0x00
18+
PSBT_GLOBAL_XPUB = 0x01
19+
PSBT_GLOBAL_TX_VERSION = 0x02
20+
PSBT_GLOBAL_FALLBACK_LOCKTIME = 0x03
21+
PSBT_GLOBAL_INPUT_COUNT = 0x04
22+
PSBT_GLOBAL_OUTPUT_COUNT = 0x05
23+
PSBT_GLOBAL_TX_MODIFIABLE = 0x06
24+
PSBT_GLOBAL_VERSION = 0xfb
25+
PSBT_GLOBAL_PROPRIETARY = 0xfc
26+
27+
# per-input types
28+
PSBT_IN_NON_WITNESS_UTXO = 0x00
29+
PSBT_IN_WITNESS_UTXO = 0x01
30+
PSBT_IN_PARTIAL_SIG = 0x02
31+
PSBT_IN_SIGHASH_TYPE = 0x03
32+
PSBT_IN_REDEEM_SCRIPT = 0x04
33+
PSBT_IN_WITNESS_SCRIPT = 0x05
34+
PSBT_IN_BIP32_DERIVATION = 0x06
35+
PSBT_IN_FINAL_SCRIPTSIG = 0x07
36+
PSBT_IN_FINAL_SCRIPTWITNESS = 0x08
37+
PSBT_IN_POR_COMMITMENT = 0x09
38+
PSBT_IN_RIPEMD160 = 0x0a
39+
PSBT_IN_SHA256 = 0x0b
40+
PSBT_IN_HASH160 = 0x0c
41+
PSBT_IN_HASH256 = 0x0d
42+
PSBT_IN_PREVIOUS_TXID = 0x0e
43+
PSBT_IN_OUTPUT_INDEX = 0x0f
44+
PSBT_IN_SEQUENCE = 0x10
45+
PSBT_IN_REQUIRED_TIME_LOCKTIME = 0x11
46+
PSBT_IN_REQUIRED_HEIGHT_LOCKTIME = 0x12
47+
PSBT_IN_TAP_KEY_SIG = 0x13
48+
PSBT_IN_TAP_SCRIPT_SIG = 0x14
49+
PSBT_IN_TAP_LEAF_SCRIPT = 0x15
50+
PSBT_IN_TAP_BIP32_DERIVATION = 0x16
51+
PSBT_IN_TAP_INTERNAL_KEY = 0x17
52+
PSBT_IN_TAP_MERKLE_ROOT = 0x18
53+
PSBT_IN_PROPRIETARY = 0xfc
54+
55+
# per-output types
56+
PSBT_OUT_REDEEM_SCRIPT = 0x00
57+
PSBT_OUT_WITNESS_SCRIPT = 0x01
58+
PSBT_OUT_BIP32_DERIVATION = 0x02
59+
PSBT_OUT_AMOUNT = 0x03
60+
PSBT_OUT_SCRIPT = 0x04
61+
PSBT_OUT_TAP_INTERNAL_KEY = 0x05
62+
PSBT_OUT_TAP_TREE = 0x06
63+
PSBT_OUT_TAP_BIP32_DERIVATION = 0x07
64+
PSBT_OUT_PROPRIETARY = 0xfc
65+
66+
1667
class PSBTMap:
1768
"""Class for serializing and deserializing PSBT maps"""
1869

0 commit comments

Comments
 (0)