[HELP!] How do I recover a wallet programmatically [5ツREWARD]

I have tried the following method:

GNU nano 5.4                                 recover.sh *                                         
./grin-wallet init -r <<EOD
    echo "my recovery phrase here"
    sleep 1
EOD
sh recover.sh
Please enter your recovery phrase:
phrase> Wallet command failed: Parsing IO error: Inappropriate ioctl for device (os error 25)

I have also tried using expect.

I will have a closer look later, but perhaps this might help you in the mean time:

(outdated command syntax) please adjust if needed

one PoC, quick and dirty based on the “grin-multi-wallet-scripts”

“Fully” automated in one script:

Result:

./recover.sh 
spawn grin-wallet --api_server_address 'https://grinnode.live' -t recover init -r

Please enter your recovery phrase:
phrase> essence bring faith welcome bubble vault bar begin twenty okay child bundle remind people wheel reopen board oak grow alpha crumble steak more scrap
Please provide a new password for the recovered wallet
Password: 
Confirm Password: 
20211106 22:07:26.567 WARN grin_wallet_impls::lifecycle::seed - Generating wallet seed file at: recover/wallet_data/wallet.seed
Your recovery phrase is:

essence bring faith welcome bubble vault bar begin twenty okay child bundle remind people wheel reopen board oak grow alpha crumble steak more scrap

Please back-up these words in a non-digital format.
Command 'init' completed successfully
ok

Script:

cat recover.sh 
#!/usr/bin/expect

set seed "essence bring faith welcome bubble vault bar begin twenty okay child bundle remind people wheel reopen board oak grow alpha crumble steak more scrap"
set password ""

	spawn grin-wallet --api_server_address 'https://grinnode.live:3413' -t recover init -r 
	expect "Please enter your recovery phrase:"
	send "$seed\r"
	expect "Password:"
	send "$password\r"
	expect "Confirm Password:"
	send "$password\r"
	expect "Command 'init' completed successfully"
	puts "ok\n"

Info:

  • It used the Grinnode.live API for recovering, but you need to use your local node to be on the safe side.
  • also recovery phrases are hard coded into the script, which is not good. This is just a PoC.

Help needed:

When I run PHP exec (‘expect recover.sh’) I get: Wallet command failed: Parsing IO error: terminfo entry not found

When I run PHP exec(‘sh recover.sh’) I get a 127 return code and no output.

When I run it in the terminal manually it works.

@mcm-mike I’m pretty sure expect requires a terminal on some screen, meaning there is no programmatical solution, just some partially working hack with expect

I really think that the devs should just add a --phrase option so that it can be used normally

Or is there some conspiracy to not allow automated systems with grin

I am not an PHP expert, but if you want to run bash “code” in PHP you need do use something,which is really insecure and should not be used shell_exec.

@mcm-mike I’m pretty sure expect requires a terminal on some screen, meaning there is no programmatical solution, just some partially working hack with expect

yes my script requires an SSH access to get a bash or sh shell to run it and it needs to have expect also installed (should be usually). This is just a PoC not a working prototype please keep this in mind.

I really think that the devs should just add a --phrase option so that it can be used normally

I think there are some public solutions on how to do it, but we need to wait for other users to reply here.

I tried SSH’ing from PHP and also shell_exec, I still get Parsing IO error: terminfo entry not found.

Can we add an arg here? grin-wallet/wallet_args.rs at bdc5bd748a4e399e6febc5e3c4974e569ee39638 · mimblewimble/grin-wallet (github.com)


I havent coded in rust before, is there a way that I can hack this and get it to detect the parameter?
I tried here but I don’t think &args.phrase is correct.

@harry.potter I recommend you run your wallet in the owner API listener mode

grin-wallet owner_api

and make an API request to create_wallet method. If you provide the mnemonic the new wallet will be restored wallet of one you owned before.

1 Like

I have cloned
grinfans/grinmw.py: python for grin (github.com)

rom wallet_v3 import WalletV3

import pprint, os

pp = pprint.PrettyPrinter(indent=4)
api_url = 'http://localhost:3413/v3/owner'

# change to your grin owner_api sercret file
api_sercet_file = '/home/grin/.grin/main/.api_secret'
api_user = 'grin'
api_password = open(api_sercet_file).read().strip()
wallet = WalletV3(api_url, api_user, api_password)
#wallet = WalletV3(api_url, "", "")
wallet.init_secure_api()

# change to you wallet password
wallet_password = 'test'

wallet.open_wallet(None, wallet_password)
pp.pprint(wallet.node_height())
pp.pprint(wallet.get_slatepack_address())

print(wallet.create_wallet({'name':'testing123',
'mnemonic':'depth exotic project..',
'mnemonic_length':32,
'password':'test',
}));

grin@debian:~/grin/grinmw.py/grinmw$ python3.9 test.py
Traceback (most recent call last):
  File "/home/grin/grin/grinmw.py/grinmw/test.py", line 14, in <module>
    wallet.init_secure_api()
  File "/home/grin/grin/grinmw.py/grinmw/wallet_v3.py", line 105, in init_secure_api
    resp = self.post('init_secure_api', {'ecdh_pubkey': pubkey})
  File "/home/grin/grin/grinmw.py/grinmw/wallet_v3.py", line 65, in post
    raise WalletError(method, params, response.status_code, response.reason)
wallet_v3.WalletError: Callng init_secure_api with params {'ecdh_pubkey': '02e66f5ccab6b1eb1734c2ef956aeba3eed8f50db4cdf5adbf205d40556dd5025c'} failed with error code 404 because: Not Found
grin@debian:~/grin/grinmw.py/grinmw$

is it possible for me to add a new parameter to my fork of grin-wallet somehow?

That looks like it cannot reach the API. I see you have

api_url = 'http://localhost:3413/v3/owner'

while it should be

api_url = 'http://localhost:3413/v2/owner'

I don’t think we have v3 yet. (someone correct me if I’m wrong)

Pull requests always welcome bro! :pray:t2:

Edit: Sorry, I confused with the node API, for wallet yes, there is v3. @harry.potter it looks strangely similar to your bug with the local node. Are you able to run local listeners for other server-like apps? Could there be some problem with your /etc/hosts? Maybe you can try to replace localhost with 127.0.0.1 in your url?

ok i ran “./grin server --port 3413 run” and tried v2 and get

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

1 Like

The Grin wallet should implement program arguments as an alternative to starting the program and providing input. For all parameters that require manual input. This will end the era of everybody who isnt using the api having to depend on expect and pexpect to communicate with the wallet and will aid all developers who have taken any interest in grin.

1 Like

It would be helpful to know the http response status code. For instance, if it is 500 - internal server error then you should check wallet / node logs.

Could you try to provide more details? Some sort of verbose log output.

You mean, something like providing arguments by command line pipe?

Yes, exactly. You should be able to provide a command-line argument as a recovery phrase, when it comes to slatepacks maybe you could pipe in the file or pass a parameter with the file path.

using ‘expect’ is not the best solution, because what happens when you want to execute in a way that has no interface to a graphical terminal with human input, you can’t interact with the rust client with ‘expect’.

It’s important that the main implementation is usable.

I tried my hand at rust, but I really need to watch a course on it or something because its syntax or object types is alien to me.

‘expect’ and ‘pexpect’ is a bit of a hack let’s admit it. forcing everyone down that road is wrong.

1 Like

I see value in being able to provide seed phrase using the arguments as that would allow to script the wallet workflow, however not sure if providing seed phrase directly is a good idea as by default the commands get logged and first thing one does after breaking into someones server is checking the command line log.

Maybe better to put seed phrase in a file and provide path to the file as an argument?

There is a feature wishlist thread on GitHub, feel free to add your ideas and members of the core dev team will review them and consider.

Thanks. I’m glad that there is somewhere to append this. I have added a comment to the GitHub post above, hopefully, it gets worked on.

2 Likes