|
5 | 5 | """A limited-functionality wallet, which may replace a real wallet in tests"""
|
6 | 6 |
|
7 | 7 | from decimal import Decimal
|
| 8 | +from enum import Enum |
8 | 9 | from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
|
9 | 10 | from test_framework.key import ECKey
|
10 | 11 | from test_framework.messages import (
|
|
30 | 31 | )
|
31 | 32 |
|
32 | 33 |
|
| 34 | +class MiniWalletMode(Enum): |
| 35 | + """Determines the transaction type the MiniWallet is creating and spending. |
| 36 | +
|
| 37 | + For most purposes, the default mode ADDRESS_OP_TRUE should be sufficient; |
| 38 | + it simply uses a fixed bech32 P2WSH address whose coins are spent with a |
| 39 | + witness stack of OP_TRUE, i.e. following an anyone-can-spend policy. |
| 40 | + However, if the transactions need to be modified by the user (e.g. prepending |
| 41 | + scriptSig for testing opcodes that are activated by a soft-fork), or the txs |
| 42 | + should contain an actual signature, the raw modes RAW_OP_TRUE and RAW_P2PK |
| 43 | + can be useful. Summary of modes: |
| 44 | +
|
| 45 | + | output | | tx is | can modify | needs |
| 46 | + mode | description | address | standard | scriptSig | signing |
| 47 | + ----------------+-------------------+-----------+----------+------------+---------- |
| 48 | + ADDRESS_OP_TRUE | anyone-can-spend | bech32 | yes | no | no |
| 49 | + RAW_OP_TRUE | anyone-can-spend | - (raw) | no | yes | no |
| 50 | + RAW_P2PK | pay-to-public-key | - (raw) | yes | yes | yes |
| 51 | + """ |
| 52 | + ADDRESS_OP_TRUE = 1 |
| 53 | + RAW_OP_TRUE = 2 |
| 54 | + RAW_P2PK = 3 |
| 55 | + |
| 56 | + |
33 | 57 | class MiniWallet:
|
34 |
| - def __init__(self, test_node, *, raw_script=False, use_p2pk=False): |
| 58 | + def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE): |
35 | 59 | self._test_node = test_node
|
36 | 60 | self._utxos = []
|
37 | 61 | self._priv_key = None
|
38 | 62 | self._address = None
|
39 | 63 |
|
40 |
| - if raw_script: |
| 64 | + assert isinstance(mode, MiniWalletMode) |
| 65 | + if mode == MiniWalletMode.RAW_OP_TRUE: |
41 | 66 | self._scriptPubKey = bytes(CScript([OP_TRUE]))
|
42 |
| - elif use_p2pk: |
| 67 | + elif mode == MiniWalletMode.RAW_P2PK: |
43 | 68 | # use simple deterministic private key (k=1)
|
44 | 69 | self._priv_key = ECKey()
|
45 | 70 | self._priv_key.set((1).to_bytes(32, 'big'), True)
|
46 | 71 | pub_key = self._priv_key.get_pubkey()
|
47 | 72 | self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
|
48 |
| - else: |
| 73 | + elif mode == MiniWalletMode.ADDRESS_OP_TRUE: |
49 | 74 | self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
|
50 | 75 | self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
|
51 | 76 |
|
|
0 commit comments