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

**URL:** <https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343>\
**Category:** Tech Support\
**Created:** [November 6, 2021, 4:32pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343 "2021-11-06T16:32:11Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 6, 2021, 4:32pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/1 "2021-11-06T16:32:11Z")

</div>

I have tried the following method:

```auto
GNU nano 5.4 recover.sh *                                         
./grin-wallet init -r <<EOD
    echo "my recovery phrase here"
    sleep 1
EOD

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![mcm-mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/mcm-mike/32/3492_2.png) [@mcm-mike](https://forum.grin.mw/u/mcm-mike)\
**Post date:** [November 6, 2021, 8:07pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/2 "2021-11-06T20:07:55Z")

</div>

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

> **[GitHub - Grinnode-live/grin-multi-wallet-scripts](https://github.com/Grinnode-live/grin-multi-wallet-scripts)**
>
> Contribute to Grinnode-live/grin-multi-wallet-scripts development by creating an account on GitHub.

**(outdated command syntax)** please adjust if needed

---

<div class="post-metadata">

**Author:** ![mcm-mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/mcm-mike/32/3492_2.png) [@mcm-mike](https://forum.grin.mw/u/mcm-mike)\
**Post date:** [November 6, 2021, 9:12pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/3 "2021-11-06T21:12:16Z")

</div>

one PoC, quick and dirty based on the [“grin-multi-wallet-scripts”](https://github.com/Grinnode-live/grin-multi-wallet-scripts)

“Fully” automated in one script:

## **Result:**

```auto
./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:

```auto
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](https://grinnode.live/) 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:**

- one you found a solution using `expect` please send a PR to [GitHub - Grinnode-live/grin-multi-wallet-scripts](https://github.com/Grinnode-live/grin-multi-wallet-scripts) as this is outdated.

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 7, 2021, 2:32pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/4 "2021-11-07T14:32:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mcm-mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/mcm-mike/32/3492_2.png) [@mcm-mike](https://forum.grin.mw/u/mcm-mike)\
**Post date:** [November 7, 2021, 2:55pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/5 "2021-11-07T14:55:58Z")

</div>

> [@harry.potter](#):
>
> When I run PHP exec (‘expect recover.sh’) I get: Wallet command failed: Parsing IO error: terminfo entry not found

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.

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 7, 2021, 3:36pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/6 "2021-11-07T15:36:36Z")

</div>

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)](https://github.com/mimblewimble/grin-wallet/blob/bdc5bd748a4e399e6febc5e3c4974e569ee39638/src/cmd/wallet_args.rs)

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 7, 2021, 5:55pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/7 "2021-11-07T17:55:27Z")

</div>

![bypasss](https://canada1.discourse-cdn.com/flex036/uploads/grin/original/2X/a/a65261a6cb06fde3dd4d6fb6d48a077e69b3abb1.png)  
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.

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 7, 2021, 6:02pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/8 "2021-11-07T18:02:05Z")

</div>

![error](https://canada1.discourse-cdn.com/flex036/uploads/grin/original/2X/b/ba9c2b95d3ffc56678b6b7104656e5ecac639b8c.png)

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [November 8, 2021, 1:59am UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/9 "2021-11-08T01:59:46Z")

</div>

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

```auto
grin-wallet owner_api

```

and make an API request to [create\_wallet method](https://docs.rs/grin_wallet_api/2.1.0/grin_wallet_api/struct.Owner.html#method.create_wallet). If you provide the mnemonic the new wallet will be restored wallet of one you owned before.

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 8, 2021, 10:30am UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/10 "2021-11-08T10:30:55Z")

</div>

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

```auto
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',
}));

```

```auto
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$

```

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 8, 2021, 1:09pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/11 "2021-11-08T13:09:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [November 8, 2021, 2:21pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/12 "2021-11-08T14:21:23Z")

</div>

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

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

```

while it should be

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

```

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

> [@harry.potter](#):
>
> is it possible for me to add a new parameter to my fork of grin-wallet somehow?

Pull requests always welcome bro! 🙏🏻

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?

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 8, 2021, 2:36pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/13 "2021-11-08T14:36:05Z")

</div>

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'))`

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 10, 2021, 8:58am UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/14 "2021-11-10T08:58:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [November 16, 2021, 1:56am UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/15 "2021-11-16T01:56:12Z")

</div>

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.

> [@harry.potter](#):
>
> The Grin wallet should implement program arguments as an alternative to starting the program and providing input.

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

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 16, 2021, 5:33pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/16 "2021-11-16T17:33:14Z")

</div>

> [@renzokuken](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [November 17, 2021, 2:06am UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/17 "2021-11-17T02:06:54Z")

</div>

> [@harry.potter](#):
>
> 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.

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.

> <https://github.com/mimblewimble/grin-pm/issues/385>
>
> \## About
> Non committing, list of ideas for things to work on in 2021.
> 
> \## Def…initions
> P1 - Critical / Must haves
> P2 - Important
> P3 - Fix if time
> 
> \## Wish list
> 
> \### P1
> PIBD\*
> multi-sig\*
> Wallet backend improvements (SQL)
> libsecp fork update
> preventing chain corruption\*
> safe cancel\*
> 
> \### P2
> tor support for nodes\*
> light node\*
> testnet exchange / making it easier for exchange integrations
> payjoins\*
> binaries for older systems, older GLIBC or build on low RAM machine \< 300MB
> invoice payment proofs\*
> hardware wallets\*
> 
> \### P3
> wallet view key\*
> one-time use slatepack addresses for wallet\*
> flyclient\*
> atomic swaps\*
> aggregators\*
> block archive node support\*
> improve compatibility with OSX
> anchors\*
> bulletproofs+\*
> NRD activation\*
> 
> \*) requires RFC to be accepted

---

<div class="post-metadata">

**Author:** ![harry.potter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/harry.potter/32/99_2.png) [@harry.potter](https://forum.grin.mw/u/harry.potter)\
**Post date:** [November 17, 2021, 1:33pm UTC](https://forum.grin.mw/t/help-how-do-i-recover-a-wallet-programmatically-5-reward/9343/18 "2021-11-17T13:33:06Z")

</div>

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.
