12345678910111213141516171819202122232425262728293031323334353637 |
- {% macro setup_script(values) -%}
- import os
- import hmac
- import secrets
- print('Updating bitcoin.conf...')
- username = '{{ values.bitcoin.rpc_user }}'
- password = '{{ values.bitcoin.rpc_password }}'
- salt = secrets.token_hex(16)
- password_hmac = hmac.new(salt.encode('utf-8'), password.encode('utf-8'), 'SHA256').hexdigest()
- rpc_auth_line = f'rpcauth={username}:{salt}${password_hmac}'
- conf_path = '{{ values.consts.bitcoin_dir }}/bitcoin.conf'
- os.makedirs(os.path.dirname(conf_path), exist_ok=True)
- try:
- with open(conf_path, 'r') as f:
- content = f.read()
- except FileNotFoundError:
- content = ''
- with open(conf_path, 'w') as f:
- if 'rpcauth=' not in content:
- print('Adding rpcauth to config...')
- f.write(rpc_auth_line + '\n' + content)
- else:
- lines = content.split('\n')
- for line in lines:
- if line.startswith('rpcauth='):
- line = rpc_auth_line
- f.write(line + '\n')
- print('Config updated!')
- exit(0)
- {%- endmacro -%}
|