Skip to content

Commit

Permalink
contrib: rpcauth.py - Add new option (-j/--json) to output text in js…
Browse files Browse the repository at this point in the history
…on format

-j/--json update and remove un-needed parens

Update README to reflect new -j/--json options
  • Loading branch information
bstin authored and b0xxer committed Apr 25, 2024
1 parent baed5ed commit 9adf949
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions share/rpcauth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
-j, --json output data in json format
```
12 changes: 9 additions & 3 deletions share/rpcauth/rpcauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from getpass import getpass
from secrets import token_hex, token_urlsafe
import hmac
import json

def generate_salt(size):
"""Create size byte hex salt"""
Expand All @@ -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:
Expand All @@ -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()

0 comments on commit 9adf949

Please sign in to comment.