-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet_helper.py
101 lines (87 loc) · 3.62 KB
/
wallet_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Development module to get wallet info with no running faucet."""
import argparse
import os
import sys
import rgb_lib
from rich import print as rp
from faucet_rgb import settings, utils
def _print_assets(asset_type, assets):
rp(f'\n{asset_type} assets:')
asset_dict = utils.get_asset_dict(assets)
rp(asset_dict)
def entrypoint():
"""Poetry script entrypoint."""
parser = argparse.ArgumentParser(description='Wallet info.')
parser.add_argument('--init',
action='store_true',
help='initialize a new wallet, print its data, exit')
parser.add_argument('--address',
action='store_true',
help='print an address from the Bitcoin wallet')
parser.add_argument('--assets',
action='store_true',
help='print current assets from RGB wallet')
parser.add_argument('--blind',
action='store_true',
help='generate and print a new blinded UTXO')
parser.add_argument('--unspents',
action='store_true',
help='print wallet unspents')
args = parser.parse_args()
app = settings.get_app(__name__)
(data_dir, network) = (app.config['DATA_DIR'], app.config['NETWORK'])
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# wallet initialization
if args.init:
if not hasattr(rgb_lib.BitcoinNetwork, network.upper()):
print(f'unsupported Bitcoin network "{network}"')
sys.exit(1)
bitcoin_network = getattr(rgb_lib.BitcoinNetwork, network.upper())
keys = rgb_lib.generate_keys(bitcoin_network)
print(f'new {network} wallet keys:')
print(' - mnemonic:', keys.mnemonic)
print(' - xpub:', keys.xpub)
sys.exit(0) #
# processing other argument
wallet_data = {
'xpub': app.config['XPUB'],
'mnemonic': app.config['MNEMONIC'],
'data_dir': data_dir,
'network': network,
'keychain': app.config['VANILLA_KEYCHAIN'],
}
online, wallet = utils.wallet.init_wallet(app.config['ELECTRUM_URL'],
wallet_data)
if args.address:
print(f'new {network} wallet address: {wallet.get_address()}')
if args.assets:
assets = wallet.list_assets([])
_print_assets('NIA', assets.nia)
_print_assets('CFA', assets.cfa)
if args.blind:
try:
count = wallet.create_utxos(online, True, 1,
app.config['UTXO_SIZE'],
app.config['FEE_RATE'])
if count > 0:
print(f'{count} new UTXOs created')
except rgb_lib.RgbLibError.AllocationsAlreadyAvailable:
pass
except rgb_lib.RgbLibError.InsufficientBitcoins as err:
print((f'Insufficient funds ({err.available} available sats).\n'
f'Funds can be sent to the following address'),
wallet.get_address())
sys.exit(1)
try:
blind_data = wallet.blind_receive(
None, None, None, ['rpc://localhost:3000/json-rpc'],
app.config['MIN_CONFIRMATIONS'])
print(f'blinded_utxo: {blind_data.recipient_id}')
except rgb_lib.RgbLibError as err: # pylint: disable=catching-non-exception
print(f'Error generating blind data: {err}')
sys.exit(1)
if args.unspents:
rp('\nUnspents:')
unspent_dict = utils.wallet.get_unspent_list(wallet, online)
rp(unspent_dict)