Skip to content

Commit 9adf949

Browse files
bstinb0xxer
authored and
b0xxer
committed
contrib: rpcauth.py - Add new option (-j/--json) to output text in json format
-j/--json update and remove un-needed parens Update README to reflect new -j/--json options
1 parent baed5ed commit 9adf949

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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 bitcoin.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 bitcoin.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)