zwalletcli icon indicating copy to clipboard operation
zwalletcli copied to clipboard

feat: create-wallet cli with offline support + some cleanup

Open bisakhmondal opened this issue 2 years ago • 9 comments

A brief description of the changes in this PR:

Migration from PR #182 (tests were failing due to missing keys)

closes #150

Tasks to complete before merging PR:

  • [x] Ensure system tests are passing. If not Run them manually to check for any regressions :clipboard:
  • [ ] Do any new system tests need added to test this change? do any existing system tests need updated? If so create a PR at 0chain/system_test
  • [ ] Merge your system tests PR to master AFTER merging this PR

Associated PRs (Link as appropriate):

  • blobber:
  • gosdk: https://github.com/0chain/gosdk/pull/625
  • system_test:
  • zboxcli:
  • 0chain:
  • Other: ...

bisakhmondal avatar Oct 28 '22 20:10 bisakhmondal

FYI:

The new command works like this:

Offline

./zwallet create-wallet

0chain-core-sdk [INFO]   2022/10/28 21:23:43.616817 wallet_base.go:261: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (SetLogFile)
0chain-core-sdk [INFO]   2022/10/28 21:23:43.915630 transaction_base.go:138: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (InitZCNSDK)

        ======WALLET_START======

{"client_id":"562e084757c4b1139ae07276bbeec87b972e66191543d2d626d4d2a081d62f37","client_key":"8bbb5bc5ab6b9268eab333faca316cf7a0432d796bff014e892e98de2605e5097bb4806142679acea62bd409c7fd0dceae49a94995dca51ddfef9ff02a6e6e0d","keys":[{"public_key":"8bbb5bc5ab6b9268eab333faca316cf7a0432d796bff014e892e98de2605e5097bb4806142679acea62bd409c7fd0dceae49a94995dca51ddfef9ff02a6e6e0d","private_key":"da8d94c194e983efc3083c731df418fe65784fef7a4cfafa19c5f4a664f57a0c"}],"mnemonics":"measure glue vibrant agent mass service bread ripple annual elevator way milk paddle leisure spell used equal feed believe labor bus current regular ticket","version":"1.0","date_created":"2022-10-28T21:23:43+01:00","nonce":0}

        ======WALLET_END======

Online

./zwallet create-wallet --register

0chain-core-sdk [INFO]   2022/10/28 21:23:47.772737 wallet_base.go:261: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (SetLogFile)
0chain-core-sdk [INFO]   2022/10/28 21:23:48.064770 transaction_base.go:138: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (InitZCNSDK)
0chain-core-sdk [INFO]   2022/10/28 21:23:48.069311 wallet_base.go:359: https://demo.0chain.net/miner01/v1/client/put
0chain-core-sdk [INFO]   2022/10/28 21:23:48.069367 wallet_base.go:359: https://demo.0chain.net/miner03/v1/client/put
0chain-core-sdk [INFO]   2022/10/28 21:23:48.069388 wallet_base.go:359: https://demo.0chain.net/miner02/v1/client/put
0chain-core-sdk [DEBUG]  2022/10/28 21:23:48.112506 wallet_base.go:382: https://demo.0chain.net/miner01/v1/client/putStatus: 200 OK
0chain-core-sdk [DEBUG]  2022/10/28 21:23:48.113186 wallet_base.go:382: https://demo.0chain.net/miner03/v1/client/putStatus: 200 OK
0chain-core-sdk [DEBUG]  2022/10/28 21:23:48.113309 wallet_base.go:382: https://demo.0chain.net/miner02/v1/client/putStatus: 200 OK
ZCN wallet created!!
Creating related read pool for storage smart-contract...
0chain-core-sdk [INFO]   2022/10/28 21:23:48.113922 transaction.go:241: New transaction interface
0chain-core-sdk [INFO]   2022/10/28 21:23:48.158887 wallet_base.go:221: Minimum miners used for submit :2
0chain-core-sdk [INFO]   2022/10/28 21:23:48.159898 transaction_base.go:341: Submitting smart contract transaction to https://demo.0chain.net/miner03 with JSON {
  "hash": "00b0daf44dc42d7c532d530d50679c321e3f953efd6e7f0fca1ad8f51c9ec423",
  "version": "1.0",
  "client_id": "c835a5bb69432330c3fd3834051ee4fd36f5fa068b9fc5556b12d904560e28a6",
  "public_key": "224a135f422b3c668a404d1adb9c9d83a2039f9817962bfc3a26ba7021120f0cf004e8688ff90fcb2451a80f36bddb8aa9791b571d8f1f2bd8e4efe83d0ac600",
  "to_client_id": "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7",
  "transaction_data": "{\"name\":\"new_read_pool\",\"input\":null}",
  "transaction_value": 0,
  "signature": "f47f467bfb711e1fb23a78fec976f85d322a7af07f37eb80ba5f2f0cd3a52893",
  "creation_date": 1666988628,
  "transaction_type": 1000,
  "transaction_fee": 0,
  "transaction_nonce": 1,
  "txn_output_hash": "",
  "transaction_status": 0
}
0chain-core-sdk [INFO]   2022/10/28 21:23:48.161563 transaction_base.go:341: Submitting smart contract transaction to https://demo.0chain.net/miner02 with JSON {
  "hash": "00b0daf44dc42d7c532d530d50679c321e3f953efd6e7f0fca1ad8f51c9ec423",
  "version": "1.0",
  "client_id": "c835a5bb69432330c3fd3834051ee4fd36f5fa068b9fc5556b12d904560e28a6",
  "public_key": "224a135f422b3c668a404d1adb9c9d83a2039f9817962bfc3a26ba7021120f0cf004e8688ff90fcb2451a80f36bddb8aa9791b571d8f1f2bd8e4efe83d0ac600",
  "to_client_id": "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7",
  "transaction_data": "{\"name\":\"new_read_pool\",\"input\":null}",
  "transaction_value": 0,
  "signature": "f47f467bfb711e1fb23a78fec976f85d322a7af07f37eb80ba5f2f0cd3a52893",
  "creation_date": 1666988628,
  "transaction_type": 1000,
  "transaction_fee": 0,
  "transaction_nonce": 1,
  "txn_output_hash": "",
  "transaction_status": 0
}
0chain-core-sdk [DEBUG]  2022/10/28 21:23:48.205305 transaction_base.go:357: https://demo.0chain.net/miner03/v1/transaction/putStatus: 200 OK
0chain-core-sdk [DEBUG]  2022/10/28 21:23:48.208350 transaction_base.go:357: https://demo.0chain.net/miner02/v1/transaction/putStatus: 200 OK
0chain-core-sdk [INFO]   2022/10/28 21:23:51.208991 transaction_query.go:122: zcn: check health https://demo.0chain.net/sharder01/v1/healthcheck
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.344707 transaction_query.go:303: GEThttps://demo.0chain.net/sharder01/v1/transaction/get/confirmation?hash=00b0daf44dc42d7c532d530d50679c321e3f953efd6e7f0fca1ad8f51c9ec423&content=lfb
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.384409 transaction_query.go:313: {"confirmation":{"version":"1.0","hash":"00b0daf44dc42d7c532d530d50679c321e3f953efd6e7f0fca1ad8f51c9ec423","block_hash":"d8b1cf870a141f638ac93128d51f5785d965d6dd8b969a48c07639aed81f6a9e","previous_block_hash":"c3d7b888227bc4c8d3f833453ef9993af7b58544bba172fed4b04f5a56aa0c00","txn":{"hash":"00b0daf44dc42d7c532d530d50679c321e3f953efd6e7f0fca1ad8f51c9ec423","version":"1.0","client_id":"c835a5bb69432330c3fd3834051ee4fd36f5fa068b9fc5556b12d904560e28a6","public_key":"224a135f422b3c668a404d1adb9c9d83a2039f9817962bfc3a26ba7021120f0cf004e8688ff90fcb2451a80f36bddb8aa9791b571d8f1f2bd8e4efe83d0ac600","to_client_id":"6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7","chain_id":"0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe","transaction_data":"{\"name\":\"new_read_pool\",\"input\":null}","transaction_value":0,"signature":"f47f467bfb711e1fb23a78fec976f85d322a7af07f37eb80ba5f2f0cd3a52893","creation_date":1666988628,"transaction_fee":0,"transaction_nonce":1,"transaction_type":1000,"transaction_output":"{\"balance\":0}","txn_output_hash":"df3143e64cf71a91ea7c064573457b70facf038bc2326c0f9f3095547c93d551","transaction_status":1},"creation_date":1666988628,"miner_id":"6ff7c4d7af36a6417f70fefe88caf94fd61e64245884edb59c9a78bf2c41d4cc","round":1544542,"transaction_status":1,"round_random_seed":-4576331873102760780,"state_changes_count":84,"merkle_tree_root":"e0c36febe05e6f19fd1a24fb4c484579bfb8c07456c9c73fab2948d5a041dac2","merkle_tree_path":{"nodes":["b3db1cf695a28c95ba11818bd72515862243584c88e4c261db4c9b873c5e2b86","6c5d8da100296b2222bdb82e9d1a0c525c25be76647e96d41662eb71a4bd3e75"],"leaf_index":1},"receipt_merkle_tree_root":"3211459da8de3042ac0af33449fd796a8edee3061553cdf23d828205641f3b21","receipt_merkle_tree_path":{"nodes":["80babbf947fcb8c2e107de71519522b4c0ec7b43771cda55793bcb7dbdad9738","958beb669158ace6e88ceb39363f8b32fcb011c8f2a84a0889f1382d33673347"],"leaf_index":1}},"latest_finalized_block":{"version":"1.0","creation_date":1666988630,"hash":"2e3ab3e7a4067be52e0ea8f7129b1e7037076bf949b4e8b06f3f8adf0ed09aae","miner_id":"6ff7c4d7af36a6417f70fefe88caf94fd61e64245884edb59c9a78bf2c41d4cc","round":1544550,"round_random_seed":-332100047794820905,"state_changes_count":58,"merkle_tree_root":"f04b443b8d9601203569d234d55d6087c86d3483286b89ab75b70b8eb2bc1263","state_hash":"4D0jom4KwR3Y04DX1Us8rrG7/6NfZj4OLJGeo4p0rY4=","receipt_merkle_tree_root":"319a1dab4f196d160de779ca24cb044c6c6705a686def4c125112d62acffa364","num_txns":3}}
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.384682 transaction_base.go:668: Confirmation round: 1544542
0chain-core-sdk [INFO]   2022/10/28 21:23:51.384726 transaction_base.go:502: Query from https://demo.0chain.net/sharder02/v1/block/get?round=1544543&content=header
0chain-core-sdk [INFO]   2022/10/28 21:23:51.384734 transaction_base.go:502: Query from https://demo.0chain.net/sharder01/v1/block/get?round=1544543&content=header
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.426187 transaction_base.go:619: https://demo.0chain.net/sharder01/v1/block/get?round=1544543&content=header200 OK
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.426355 transaction_base.go:619: https://demo.0chain.net/sharder02/v1/block/get?round=1544543&content=header200 OK
0chain-core-sdk [INFO]   2022/10/28 21:23:51.426445 transaction_base.go:502: Query from https://demo.0chain.net/sharder01/v1/block/get?round=1544544&content=header
0chain-core-sdk [INFO]   2022/10/28 21:23:51.426450 transaction_base.go:502: Query from https://demo.0chain.net/sharder02/v1/block/get?round=1544544&content=header
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.468363 transaction_base.go:619: https://demo.0chain.net/sharder01/v1/block/get?round=1544544&content=header200 OK
0chain-core-sdk [DEBUG]  2022/10/28 21:23:51.469687 transaction_base.go:619: https://demo.0chain.net/sharder02/v1/block/get?round=1544544&content=header200 OK
Read pool created successfully

        ======WALLET_START======

{"client_id":"c835a5bb69432330c3fd3834051ee4fd36f5fa068b9fc5556b12d904560e28a6","client_key":"224a135f422b3c668a404d1adb9c9d83a2039f9817962bfc3a26ba7021120f0cf004e8688ff90fcb2451a80f36bddb8aa9791b571d8f1f2bd8e4efe83d0ac600","keys":[{"public_key":"224a135f422b3c668a404d1adb9c9d83a2039f9817962bfc3a26ba7021120f0cf004e8688ff90fcb2451a80f36bddb8aa9791b571d8f1f2bd8e4efe83d0ac600","private_key":"0173a054f0676de321bd0d04e5dbb4a221c0f5fd94e66fc9787040f66f7f5d15"}],"mnemonics":"ancient tattoo empty spot zero cash angle avoid verify stairs hunt daring pass vibrant off slush fly quiz record large art margin few bleak","version":"1.0","date_created":"1666988628","nonce":0}

        ======WALLET_END======

bisakhmondal avatar Oct 28 '22 20:10 bisakhmondal

I like the actual core functionality and optional --register parameter, but the outputting of resultant wallet to stdout is different to the way the rest of the CLI command tools behave. By default, wallets are output to ~/.zcn/wallet.json or wallet name specified by --wallet, This should remain as standard and if the user wants the result to display then a --display or similar parameter should be added rather than change base functionality. Also be aware that the --json parameter is frequently used to indicate pure, parseable JSON output. That being the case, the wallet start and end sections should be omitted, resulting in parseable JSON output only (when stderr / debug output omitted).

sculptex avatar Oct 28 '22 22:10 sculptex

I like the actual core functionality and optional --register parameter, but the outputting of resultant wallet to stdout is different to the way the rest of the CLI command tools behave. By default, wallets are output to ~/.zcn/wallet.json or wallet name specified by --wallet, This should remain as standard and if the user wants the result to display then a --display or similar parameter should be added rather than change base functionality. Also be aware that the --json parameter is frequently used to indicate pure, parseable JSON output. That being the case, the wallet start and end sections should be omitted, resulting in parseable JSON output only (when stderr / debug output omitted).

Hi @sculptex, thanks for sharing the valuable feedback. Indeed I agree with you RE: outputting the wallet in stdout - but we can't dump it in ~/.zcn/wallet.json as it will overwrite the user's existing wallet.

Quoting @peterlimg comment from issue #150

We need to have the command to generate new wallets, we don't need to save the wallet to disk to avoid corrupting the existing wallet file by accident, which is a disaster if the user did not backup their wallet seed. So print the wallet info to console and users could copy that info and save to any places they want.

If you suggest, one thing I can do is generate some random wallet file names inside ~/.zcn root. For example wallet14953.json or something like that.

That being the case, the wallet start and end sections should be omitted, resulting in parseable JSON output only (when stderr / debug output omitted)

Ack - totally makes sense. Removing.

bisakhmondal avatar Oct 29 '22 16:10 bisakhmondal

Let's also save the generated wallet to ~/.zcn/ dir with date-random-hex-num. For example, 2022_11_1_ef23a.json. We might need to refactor to store all wallet files in a ~/.zcn/wallets/ dir later in another PR.

On it.

bisakhmondal avatar Nov 01 '22 09:11 bisakhmondal

Here you go @peterlimg https://github.com/0chain/zwalletcli/pull/190/commits/c6fefeaaf5f05bf3819d6c1381beacf27e499aea

File dump: wallet saved in /home/bisakh/.zcn/2022_11_01_wallet_6360ee9b.json

bisakhmondal avatar Nov 01 '22 10:11 bisakhmondal

Please make sure the switches actually work. --wallet atm does not work for me. Also, I would expect --silent to not output the wallet to stdout.

$ ./zwalletcli create-wallet --wallet ~/.zcn/test.json 0chain-core-sdk [INFO] 2022/11/03 15:53:27.206270 wallet_base.go:262: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (SetLogFile) 0chain-core-sdk [INFO] 2022/11/03 15:53:27.360121 transaction_base.go:138: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (InitZCNSDK) wallet saved in /home/user/.zcn/2022_11_03_wallet_6363d5e7.json {"client_id":"21174e1255797d8c928bb62a7351caf82502a4b07362272b1e421b3c5ec2dfd1","client_key":"1861016e14c1cff1d93d4d00956b19788b882e6b95a3c4e41bb667774ed731126f85e7ae5179063d33041ce3dd4c36d38be245a33522910c7d99c44408f4601e","keys":[{"public_key":"1861016e14c1cff1d93d4d00956b19788b882e6b95a3c4e41bb667774ed731126f85e7ae5179063d33041ce3dd4c36d38be245a33522910c7d99c44408f4601e","private_key":"747d8ad99fb2db83c34a613a3f8d323fe7dd66812648a7d417e0e4b6e04f110b"}],"mnemonics":"duck heart jewel blade nurse left thunder weapon buzz involve husband trouble subway comic curve danger cruise erode please bulk margin artist sport tuna","version":"1.0","date_created":"2022-11-03T15:53:27+01:00","nonce":0}

zeroservices avatar Nov 03 '22 14:11 zeroservices

Please make sure the switches actually work. --wallet atm does not work for me. Also, I would expect --silent to not output the wallet to stdout.

$ ./zwalletcli create-wallet --wallet ~/.zcn/test.json 0chain-core-sdk [INFO] 2022/11/03 15:53:27.206270 wallet_base.go:262: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (SetLogFile) 0chain-core-sdk [INFO] 2022/11/03 15:53:27.360121 transaction_base.go:138: ******* Wallet SDK Version:v1.8.8-8-g3c6d3d6c ******* (InitZCNSDK) wallet saved in /home/user/.zcn/2022_11_03_wallet_6363d5e7.json {"client_id":"21174e1255797d8c928bb62a7351caf82502a4b07362272b1e421b3c5ec2dfd1","client_key":"1861016e14c1cff1d93d4d00956b19788b882e6b95a3c4e41bb667774ed731126f85e7ae5179063d33041ce3dd4c36d38be245a33522910c7d99c44408f4601e","keys":[{"public_key":"1861016e14c1cff1d93d4d00956b19788b882e6b95a3c4e41bb667774ed731126f85e7ae5179063d33041ce3dd4c36d38be245a33522910c7d99c44408f4601e","private_key":"747d8ad99fb2db83c34a613a3f8d323fe7dd66812648a7d417e0e4b6e04f110b"}],"mnemonics":"duck heart jewel blade nurse left thunder weapon buzz involve husband trouble subway comic curve danger cruise erode please bulk margin artist sport tuna","version":"1.0","date_created":"2022-11-03T15:53:27+01:00","nonce":0}

This command does not support --wallet actually so it would not work. We may support if needed later but currently we would just keep it simple.

peterlimg avatar Nov 04 '22 10:11 peterlimg

That's good advice indeed. Even I think we can support it in this PR too including the --silent, @sculptex was also suggesting the same earlier.

bisakhmondal avatar Nov 04 '22 10:11 bisakhmondal

I'll just add a note, since we have hdwallet Seed Phrase compatibility, there is also option for users to use Seed Phrase generated by other existing ETH compatible wallet software.

They can just use the recoverwallet option which already has an --offline mode in order to obtain the ZCN address and keys from existing seed phrase.

sculptex avatar Nov 04 '22 22:11 sculptex

@dabasov this PR https://github.com/0chain/zwalletcli/pull/201 has addressed this task, can this PR be closed

boddumanohar avatar Feb 26 '23 21:02 boddumanohar

Closed via #201

peterlimg avatar Mar 03 '23 00:03 peterlimg