-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add graphene common, instance, and wallet
- Loading branch information
Showing
5 changed files
with
446 additions
and
31 deletions.
There are no files selected for viewing
Empty file.
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,60 @@ | ||
__all__ = [ | ||
"InvalidWifError", | ||
"KeyAlreadyInStoreException", | ||
"KeyNotFound", | ||
"NoWalletException", | ||
"OfflineHasNoRPCException", | ||
"WalletExists", | ||
"WalletLocked", | ||
] | ||
|
||
|
||
class WalletExists(Exception): | ||
""" A wallet has already been created and requires a password to be | ||
unlocked by means of :func:`bitshares.wallet.unlock`. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class WalletLocked(Exception): | ||
""" Wallet is locked | ||
""" | ||
|
||
pass | ||
|
||
|
||
class OfflineHasNoRPCException(Exception): | ||
""" When in offline mode, we don't have RPC | ||
""" | ||
|
||
pass | ||
|
||
|
||
class NoWalletException(Exception): | ||
""" No Wallet could be found, please use :func:`bitshares.wallet.create` to | ||
create a new wallet | ||
""" | ||
|
||
pass | ||
|
||
|
||
class KeyNotFound(Exception): | ||
""" Key not found | ||
""" | ||
|
||
pass | ||
|
||
|
||
class KeyAlreadyInStoreException(Exception): | ||
""" The key is already stored in the store | ||
""" | ||
|
||
pass | ||
|
||
|
||
class InvalidWifError(Exception): | ||
""" The provided private Key has an invalid format | ||
""" | ||
|
||
pass |
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,67 @@ | ||
class SharedInstance: | ||
""" This class merely offers a singelton for the Blockchain Instance | ||
""" | ||
|
||
instance = None | ||
config = {} | ||
|
||
|
||
class BlockchainInstance: | ||
""" This is a class that allows compatibility with previous | ||
naming conventions. It will extract 'blockchain_instance' | ||
from the key word arguments and ensure that self.blockchain | ||
contains an instance of the main chain instance | ||
""" | ||
|
||
def get_instance_class(self): | ||
""" Should return the Chain instance class, e.g. `bitshares.BitShares` | ||
""" | ||
raise NotImplementedError | ||
|
||
def clear_cache(self): | ||
raise NotImplementedError | ||
|
||
def __init__(self, *args, **kwargs): | ||
if "blockchain_instance" in kwargs and kwargs["blockchain_instance"]: | ||
SharedInstance.instance = kwargs["blockchain_instance"] | ||
|
||
@property | ||
def blockchain(self): | ||
return self.shared_blockchain_instance() | ||
|
||
@property | ||
def chain(self): | ||
""" Short form for blockchain (for the lazy) | ||
""" | ||
return self.blockchain | ||
|
||
def shared_blockchain_instance(self): | ||
""" This method will initialize ``SharedInstance.instance`` and return it. | ||
The purpose of this method is to have offer single default | ||
instance that can be reused by multiple classes. | ||
""" | ||
if not SharedInstance.instance: | ||
klass = self.get_instance_class() | ||
SharedInstance.instance = klass(**SharedInstance.config) | ||
return SharedInstance.instance | ||
|
||
@staticmethod | ||
def set_shared_blockchain_instance(instance): | ||
""" This method allows us to override default instance for all | ||
users of ``SharedInstance.instance``. | ||
:param chaininstance instance: Chain instance | ||
""" | ||
SharedInstance.instance = instance | ||
|
||
def set_shared_config(self, config): | ||
""" This allows to set a config that will be used when calling | ||
``shared_blockchain_instance`` and allows to define the configuration | ||
without requiring to actually create an instance | ||
""" | ||
assert isinstance(config, dict) | ||
SharedInstance.config.update(config) | ||
# if one is already set, delete | ||
if SharedInstance.instance: | ||
self.shared_blockchain_instance().clear_cache() | ||
SharedInstance.instance = None |
Oops, something went wrong.