setup.py.jinja 1012 B

12345678910111213141516171819202122232425262728293031323334353637
  1. {% macro setup_script(values) -%}
  2. import os
  3. import hmac
  4. import secrets
  5. print('Updating bitcoin.conf...')
  6. username = '{{ values.bitcoin.rpc_user }}'
  7. password = '{{ values.bitcoin.rpc_password }}'
  8. salt = secrets.token_hex(16)
  9. password_hmac = hmac.new(salt.encode('utf-8'), password.encode('utf-8'), 'SHA256').hexdigest()
  10. rpc_auth_line = f'rpcauth={username}:{salt}${password_hmac}'
  11. conf_path = '{{ values.consts.bitcoin_dir }}/bitcoin.conf'
  12. os.makedirs(os.path.dirname(conf_path), exist_ok=True)
  13. try:
  14. with open(conf_path, 'r') as f:
  15. content = f.read()
  16. except FileNotFoundError:
  17. content = ''
  18. with open(conf_path, 'w') as f:
  19. if 'rpcauth=' not in content:
  20. print('Adding rpcauth to config...')
  21. f.write(rpc_auth_line + '\n' + content)
  22. else:
  23. lines = content.split('\n')
  24. for line in lines:
  25. if line.startswith('rpcauth='):
  26. line = rpc_auth_line
  27. f.write(line + '\n')
  28. print('Config updated!')
  29. exit(0)
  30. {%- endmacro -%}