-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!-- | ||
Copyright 2023 Ocean Protocol Foundation | ||
SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
|
||
# Using hardware wallets with ocean.py | ||
|
||
This README describes how to setup ocean.py with hardware wallets. | ||
|
||
We assume you've already (a) [installed Ocean](install.md), configured any environment variables necessary and created the Ocean object as described in (b) done [local setup](setup-local.md) or [remote setup](setup-remote.md). | ||
These instructions are applicable to both local and remote setup. If you intend to use hardware wallets ONLY, then you can skip the wallet creation parts in the setup instructions. | ||
|
||
## 1. Setting up and running Clef | ||
ocean.py allows the use of hardware wallets via [Clef](https://geth.ethereum.org/docs/clef/tutorial), an account management tool included within [Geth](https://geth.ethereum.org/) | ||
|
||
To use a hardware wallet with ocean.py, start by [installing Geth](https://geth.ethereum.org/docs/install-and-build/installing-geth). | ||
Once finished, type the following command in a bash console and follow the on-screen prompts to set of Clef: | ||
|
||
```console | ||
clef init | ||
``` | ||
|
||
If you need to create a new account, you can use the command `clef newaccount`. For other usefull commands, please consult the [Clef documentation](https://geth.ethereum.org/docs/tools/clef/introduction). | ||
|
||
Once Clef is configured, run it in a bash console as needed, i.e. | ||
|
||
```console | ||
# you can use a different chain if needed | ||
clef --chainid 8996 | ||
``` | ||
|
||
You can also customise your run, e.g. `clef --chainid 8996 --advanced`. | ||
|
||
Keep the clef console open, you will be required to approve transactions and input your password when so requested. | ||
|
||
## 2. Connect ocean.py to Clef via Brownie | ||
|
||
In your Python console where you have setup the Ocean object: | ||
|
||
```python | ||
from ocean_lib.web3_internal.clef import get_clef_accounts | ||
clef_accounts = get_clef_accounts() | ||
``` | ||
|
||
Approve the connection from the Clef console. This will add your Clef account to the `accounts` array. | ||
You can now use the Clef account instead of any wallet argument, e.g. when publishing or consuming DDOs. | ||
|
||
|
||
```python | ||
# pick up the account for convenience | ||
clef_account = clef_accounts[index] | ||
|
||
# make sure account is funded. Let's transfer some ether and OCEAN from alice | ||
from ocean_lib.ocean.util import send_ether | ||
send_ether(config, alice, clef_account.address, to_wei(4)) | ||
OCEAN.transfer(clef_account, to_wei(4), {"from": alice}) | ||
|
||
# publish and download an asset | ||
name = "Branin dataset" | ||
url = "https://raw.githubusercontent.com/trentmc/branin/main/branin.arff" | ||
|
||
(data_nft, datatoken, ddo) = ocean.assets.create_url_asset(name, url, {"from": clef_account}) | ||
datatoken.mint(clef_account, to_wei(1), {"from": clef_account}) | ||
order_tx_id = ocean.assets.pay_for_access_service(ddo, {"from": clef_account}) | ||
ocean.assets.download_asset(ddo, clef_account, './', order_tx_id) | ||
|
||
``` | ||
|
||
Please note that you need to consult your clef console periodically to approve transactions and input your password if needed. | ||
You can use the ClefAccount object seamlessly, in any transaction, just like regular Accounts. Simply send your transaction with `{"from": clef_account}` where needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# Copyright 2023 Ocean Protocol Foundation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import sys | ||
from pathlib import Path | ||
|
||
from web3 import HTTPProvider, IPCProvider | ||
from web3.main import Web3 | ||
|
||
|
||
def get_clef_accounts(uri: str = None, timeout: int = 120) -> None: | ||
provider = None | ||
if uri is None: | ||
if sys.platform == "win32": | ||
uri = "http://localhost:8550/" | ||
else: | ||
uri = Path.home().joinpath(".clef/clef.ipc").as_posix() | ||
try: | ||
if Path(uri).exists(): | ||
provider = IPCProvider(uri, timeout=timeout) | ||
except OSError: | ||
if uri is not None and uri.startswith("http"): | ||
provider = HTTPProvider(uri, {"timeout": timeout}) | ||
if provider is None: | ||
raise ValueError( | ||
"Unknown URI, must be IPC socket path or URL starting with 'http'" | ||
) | ||
|
||
response = provider.make_request("account_list", []) | ||
if "error" in response: | ||
raise ValueError(response["error"]["message"]) | ||
|
||
clef_accounts = [ClefAccount(address, provider) for address in response["result"]] | ||
return clef_accounts | ||
|
||
|
||
class ClefAccount: | ||
def __init__(self, address: str, provider: [HTTPProvider, IPCProvider]) -> None: | ||
self.address = Web3.to_checksum_address(address) | ||
self.provider = provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## | ||
## Copyright 2023 Ocean Protocol Foundation | ||
## SPDX-License-Identifier: Apache-2.0 | ||
## | ||
export REMOTE_TEST_PRIVATE_KEY1=0x8c14238c0fef15881c5638c6e542b2891a9f1cc02d38c69d23724513a5369180 | ||
|
||
# address: ADDRESS2=0xD6764a9F3387B14692CC783BcF5604a23EBb779f | ||
export REMOTE_TEST_PRIVATE_KEY2=0xc217184f76bdcd66d0070bda50905082a96b714c872a5636982f2b676667f7dd | ||
|
||
export WEB3_INFURA_PROJECT_ID="9aa3d95b3bc440fa88ea12eaa4456161" | ||
export MUMBAI_RPC_URL="https://polygon-mumbai.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters