Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Update storage to use api key #138

Merged
merged 9 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ pip install thirdweb-sdk

## Getting Started

To start using this SDK, you just need to pass in a provider configuration.
To start using this SDK, you just need to pass in a provider configuration. It's also strongly recommended that you use your thirdweb API keys with the SDK in order to get the best infrastructure performance (across RPCs, IPFS, etc.) - you can learn more about creating and using API keys [here](https://portal.thirdweb.com/api-keys).

### Instantiating the SDK

Once you have all the necessary dependencies, you can follow the following setup steps to get started with the SDK read-only functions:

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# You can create a new instance of the SDK to use by just passing in a network name
sdk = ThirdwebSDK("mumbai")
# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."

# You can create a new instance of the SDK to use by passing in a network name and your api key
sdk = ThirdwebSDK("mumbai", options=SDKOptions(secret_key=SECRET_KEY))
```

The SDK supports the `mainnet`, `rinkeby`, `goerli`, `polygon`, `mumbai`, `fantom`, and `avalanche` networks.
Expand Down Expand Up @@ -73,12 +77,16 @@ Meanwhile, if you want to use write functions as well and connect a signer, you
```python
from thirdweb import ThirdwebSDK
from thirdweb.types.nft import NFTMetadataInput
import os

# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."

# Learn more about securely accessing your private key: https://portal.thirdweb.com/web3-sdk/set-up-the-sdk/securing-your-private-key
PRIVATE_KEY = "<your-private-key-here>",
# This PRIVATE KEY is coming from your environment variables. Make sure to never put it in a tracked file or share it with anyone.
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")

# Now you can create a new instance of the SDK with your private key
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai")
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai", options=SDKOptions(secret_key=SECRET_KEY))

# Instantiate a new NFT Collection contract as described above.
NFT_COLLECTION_ADDRESS = "0x.."
Expand Down
15 changes: 11 additions & 4 deletions docs/common/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ pip install thirdweb-sdk

## Getting Started

To start using this SDK, you just need to pass in a provider configuration.
To start using this SDK, you just need to pass in a provider configuration. It's also strongly recommended that you use your thirdweb API keys with the SDK in order to get the best infrastructure performance (across RPCs, IPFS, etc.) - you can learn more about creating and using API keys [here](https://portal.thirdweb.com/api-keys).

### Instantiating the SDK

Once you have all the necessary dependencies, you can follow the following setup steps to get started with the SDK read-only functions:

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."

# You can create a new instance of the SDK to use by just passing in a network name
sdk = ThirdwebSDK("mumbai")
# You can create a new instance of the SDK to use by passing in a network name and your api key
sdk = ThirdwebSDK("mumbai", options=SDKOptions(secret_key=SECRET_KEY))
```

The SDK supports the `mainnet`, `rinkeby`, `goerli`, `polygon`, `mumbai`, `fantom`, and `avalanche` networks.
Expand Down Expand Up @@ -74,12 +79,14 @@ from thirdweb import ThirdwebSDK
from thirdweb.types.nft import NFTMetadataInput
import os

# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."

# This PRIVATE KEY is coming from your environment variables. Make sure to never put it in a tracked file or share it with anyone.
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")

# Now you can create a new instance of the SDK with your private key
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai")
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai", options=SDKOptions(secret_key=SECRET_KEY))

# Instantiate a new NFT Collection contract as described above.
NFT_COLLECTION_ADDRESS = "0x.."
Expand Down
9 changes: 8 additions & 1 deletion tests/fixtures/sdk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from thirdweb.core.classes.ipfs_storage import IpfsStorage
from thirdweb.core.classes.registry import ContractRegistry
from thirdweb.core.classes.factory import ContractFactory
from thirdweb.types.sdk import SDKOptions
from web3.middleware import geth_poa_middleware
from thirdweb.core.sdk import ThirdwebSDK
from eth_account import Account
Expand All @@ -16,5 +17,11 @@
@pytest.fixture(scope="session")
def sdk(primary_account):
signer = primary_account
sdk = ThirdwebSDK("http://localhost:8545", signer)
sdk = ThirdwebSDK(
"http://localhost:8545",
signer,
SDKOptions(
secret_key=os.getenv("THIRDWEB_SECRET_KEY")
)
)
return sdk
4 changes: 2 additions & 2 deletions tests/test_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_replace_gateway_urls(storage: IpfsStorage):
}
)

other_storage = IpfsStorage(gateway_url="https://ipfs.thirdweb.com/ipfs/")
other_storage = IpfsStorage(secret_key=None, gateway_url="https://ipfs.io/ipfs/")
downloaded = other_storage.get(upload)

assert downloaded["svg"].startswith("https://ipfs.thirdweb.com/ipfs/")
assert downloaded["svg"].startswith("https://ipfs.io/ipfs/")
5 changes: 5 additions & 0 deletions thirdweb/common/keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import hashlib

def derive_client_id_from_secret_key(secret_key: str) -> str:
hashed = hashlib.sha256(secret_key.encode()).hexdigest()
return hashed[:32]
32 changes: 17 additions & 15 deletions thirdweb/constants/urls.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
from typing import Optional
from web3 import Web3


DEFAULT_IPFS_GATEWAY = "https://gateway.ipfscdn.io/ipfs/"
DEFAULT_IPFS_GATEWAY = "https://ipfs.io/ipfs/"

TW_IPFS_SERVER_URL = "https://upload.nftlabs.co"

PINATA_IPFS_URL = "https://api.pinata.cloud/pinning/pinFileToIPFS"
TW_STORAGE_SERVER_URL = "https://storage.thirdweb.com"

DEFAULT_API_KEY = "39a3c037d7a88e6692c6681bccfd1f1cf36370324c4051a83acd0edcffb20708"

def get_rpc_url(network: str) -> str:
return f"https://{network}.rpc.thirdweb.com/{DEFAULT_API_KEY}"
def get_rpc_url(network: str, client_id: Optional[str]) -> str:
api_key = client_id if client_id is not None else DEFAULT_API_KEY
return f"https://{network}.rpc.thirdweb.com/{api_key}"

def get_provider_for_network(network: str) -> Web3:
def get_provider_for_network(network: str, client_id: str) -> Web3:
"""
Returns a web3 provider instance for the given network.
"""

rpc_url = ""
if network == "mainnet" or network == "ethereum":
rpc_url = get_rpc_url("ethereum")
rpc_url = get_rpc_url("ethereum", client_id)
elif network == "goerli":
rpc_url = get_rpc_url("goerli")
rpc_url = get_rpc_url("goerli", client_id)
elif network == "polygon":
rpc_url = get_rpc_url("polygon")
rpc_url = get_rpc_url("polygon", client_id)
elif network == "mumbai":
rpc_url = get_rpc_url("mumbai")
rpc_url = get_rpc_url("mumbai", client_id)
elif network == "optimism":
rpc_url = getRpcUrl("optimism");
rpc_url = get_rpc_url("optimism", client_id)
elif network == "optimism-goerli":
rpc_url = getRpcUrl("optimism-goerli");
rpc_url = get_rpc_url("optimism-goerli", client_id)
elif network == "arbitrum":
rpc_url = getRpcUrl("arbitrum");
rpc_url = get_rpc_url("arbitrum", client_id)
elif network == "arbitrum-goerli":
rpc_url = getRpcUrl("arbitrum-goerli");
rpc_url = get_rpc_url("arbitrum-goerli", client_id)
elif network == "fantom":
rpc_url = get_rpc_url("fantom")
rpc_url = get_rpc_url("fantom", client_id)
elif network == "avalanche":
rpc_url = get_rpc_url("avalanche")
rpc_url = get_rpc_url("avalanche", client_id)
else:
if network.startswith("http"):
rpc_url = network
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ class CustomContract(BaseContract[Any]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_contract("{{contract_address}}")

Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ class Edition(ERC1155Standard[TokenERC1155]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_edition("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/edition_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class EditionDrop(ERC1155Standard[DropERC1155]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_edition_drop("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/marketplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ class Marketplace(BaseContract[MarketplaceABI]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_marketplace("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/multiwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ class Multiwrap(ERC721Standard[MultiwrapABI]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_multiwrap("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/nft_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ class NFTCollection(ERC721Standard[TokenERC721]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_nft_collection("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/nft_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ class NFTDrop(ERC721Standard[DropERC721]):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_nft_drop("{{contract_address}}")
```
Expand Down
8 changes: 6 additions & 2 deletions thirdweb/contracts/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ class Token(ERC20Standard):

```python
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions

# Get your secret key from the thirdweb api keys dashboard
secret_key = "..."

# You can customize this to a supported network or your own RPC URL
network = "mumbai"

# Now we can create a new instance of the SDK
sdk = ThirdwebSDK(network)
sdk = ThirdwebSDK(network, options=SDKOptions(secret_key=secret_key))

# If you want to send transactions, you can instantiate the SDK with a private key instead:
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network)
# sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, network, options=SDKOptions(secret_key=secret_key))

contract = sdk.get_token("{{contract_address}}")
```
Expand Down
6 changes: 3 additions & 3 deletions thirdweb/core/classes/contract_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class ContractDeployer(ProviderHandler):
def __init__(
self,
provider: Web3,
signer: Optional[LocalAccount] = None,
options: SDKOptions = SDKOptions(),
storage: IpfsStorage = IpfsStorage(),
signer: Optional[LocalAccount],
options: SDKOptions,
storage: IpfsStorage,
):
super().__init__(provider, signer, options)
self._storage = storage
Expand Down
10 changes: 4 additions & 6 deletions thirdweb/core/classes/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def __init__(
self,
factory_address: str,
provider: Web3,
signer: Optional[LocalAccount] = None,
options: SDKOptions = SDKOptions(),
storage: IpfsStorage = IpfsStorage(),
signer: Optional[LocalAccount],
options: SDKOptions,
storage: IpfsStorage,
):
abi = TWFactory(provider, factory_address)
super().__init__(abi, provider, signer, options)
Expand All @@ -57,9 +57,7 @@ def deploy(
) -> str:
# First, we upload the contract metadata to IPFS
contract_uri = self._storage.upload_metadata(
contract_metadata,
self._contract_abi.contract_address,
self.get_signer_address(),
contract_metadata
)

# Then, we get setup the contract interface for the contract we want to deploy
Expand Down
Loading
Loading