Merge bitcoin/bitcoin#29433: contrib: rpcauth.py - Add new option (-json) to output text in json format

9adf949d2aa6d199b85295b18c08967395b5570a contrib: rpcauth.py - Add new option (-j/--json) to output text in json format (bstin)

Pull request description:

  This is a simple change to rpcauth.py utility in order to output as json instead raw text.

  This is beneficial because integrating json output is simpler with multiple different forms of automation and tooling

ACKs for top commit:
  maflcko:
    ACK 9adf949d2aa6d199b85295b18c08967395b5570a
  achow101:
    ACK 9adf949d2aa6d199b85295b18c08967395b5570a
  willcl-ark:
    tACK 9adf949d2aa6d199b85295b18c08967395b5570a
  tdb3:
    ACK for 9adf949d2aa6d199b85295b18c08967395b5570a

Tree-SHA512: 2cdc3b2071fbe4fb32a84ce42ee8ad216cff96ed82aaef58daeb3991953ac137ae42d6898a7fdb6cbd1800e1f61ff8d292f0b150eaebdd2a3fd9d37ed7450787
This commit is contained in:
Ava Chow 2024-04-25 13:55:37 -04:00
commit 2066295172
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41
2 changed files with 10 additions and 3 deletions

View File

@ -15,4 +15,5 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
-j, --json output data in json format
```

View File

@ -7,6 +7,7 @@ from argparse import ArgumentParser
from getpass import getpass
from secrets import token_hex, token_urlsafe
import hmac
import json
def generate_salt(size):
"""Create size byte hex salt"""
@ -24,6 +25,7 @@ def main():
parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')
parser.add_argument('username', help='the username for authentication')
parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')
parser.add_argument("-j", "--json", help="output to json instead of plain-text", action='store_true')
args = parser.parse_args()
if not args.password:
@ -35,9 +37,13 @@ def main():
salt = generate_salt(16)
password_hmac = password_to_hmac(salt, args.password)
print('String to be appended to bitcoin.conf:')
print(f'rpcauth={args.username}:{salt}${password_hmac}')
print(f'Your password:\n{args.password}')
if args.json:
odict={'username':args.username, 'password':args.password, 'rpcauth':f'{args.username}:{salt}${password_hmac}'}
print(json.dumps(odict))
else:
print('String to be appended to bitcoin.conf:')
print(f'rpcauth={args.username}:{salt}${password_hmac}')
print(f'Your password:\n{args.password}')
if __name__ == '__main__':
main()