Skip to content

Commit 752fa84

Browse files
achow101vijaydasmp
authored andcommitted
Merge bitcoin#29433: contrib: rpcauth.py - Add new option (-json) to output text in json format
9adf949 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 9adf949 achow101: ACK 9adf949 willcl-ark: tACK 9adf949 tdb3: ACK for 9adf949 Tree-SHA512: 2cdc3b2071fbe4fb32a84ce42ee8ad216cff96ed82aaef58daeb3991953ac137ae42d6898a7fdb6cbd1800e1f61ff8d292f0b150eaebdd2a3fd9d37ed7450787
1 parent a6a5bfd commit 752fa84

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

share/rpcauth/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ positional arguments:
1515
1616
optional arguments:
1717
-h, --help show this help message and exit
18+
-j, --json output data in json format
1819
```

share/rpcauth/rpcauth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from getpass import getpass
88
from secrets import token_hex, token_urlsafe
99
import hmac
10+
import json
1011

1112
def generate_salt(size):
1213
"""Create size byte hex salt"""
@@ -24,6 +25,7 @@ def main():
2425
parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')
2526
parser.add_argument('username', help='the username for authentication')
2627
parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')
28+
parser.add_argument("-j", "--json", help="output to json instead of plain-text", action='store_true')
2729
args = parser.parse_args()
2830

2931
if not args.password:
@@ -35,9 +37,13 @@ def main():
3537
salt = generate_salt(16)
3638
password_hmac = password_to_hmac(salt, args.password)
3739

38-
print('String to be appended to dash.conf:')
39-
print(f'rpcauth={args.username}:{salt}${password_hmac}')
40-
print(f'Your password:\n{args.password}')
40+
if args.json:
41+
odict={'username':args.username, 'password':args.password, 'rpcauth':f'{args.username}:{salt}${password_hmac}'}
42+
print(json.dumps(odict))
43+
else:
44+
print('String to be appended to dash.conf:')
45+
print(f'rpcauth={args.username}:{salt}${password_hmac}')
46+
print(f'Your password:\n{args.password}')
4147

4248
if __name__ == '__main__':
4349
main()

0 commit comments

Comments
 (0)