Skip to content
Snippets Groups Projects
Commit 03707966 authored by Fabian Schuh's avatar Fabian Schuh
Browse files

[memo] simplify the use of bitshares.memo

parent 3137f8b1
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ import random ...@@ -3,7 +3,7 @@ import random
from bitsharesbase import memo as BtsMemo from bitsharesbase import memo as BtsMemo
from bitsharesbase.account import PrivateKey, PublicKey from bitsharesbase.account import PrivateKey, PublicKey
from .account import Account from .account import Account
from .exceptions import MissingKeyError from .exceptions import MissingKeyError, KeyNotFound
class Memo(object): class Memo(object):
...@@ -23,24 +23,38 @@ class Memo(object): ...@@ -23,24 +23,38 @@ class Memo(object):
from bitshares.memo import Memo from bitshares.memo import Memo
m = Memo("bitshareseu", "wallet.xeroc") m = Memo("bitshareseu", "wallet.xeroc")
m.bitshares.wallet.unlock("secret")
enc = (m.encrypt("foobar")) enc = (m.encrypt("foobar"))
print(enc) print(enc)
>> {'nonce': '17329630356955254641', 'message': '8563e2bb2976e0217806d642901a2855'} >> {'nonce': '17329630356955254641', 'message': '8563e2bb2976e0217806d642901a2855'}
print(m.decrypt(enc)) print(m.decrypt(enc))
>> foobar >> foobar
To decrypt a memo, simply use
.. code-block:: python
from bitshares.memo import Memo
m = Memo()
m.bitshares.wallet.unlock("secret")
print(memo.decrypt(op_data["memo"]))
if ``op_data`` being the payload of a transfer operation.
""" """
def __init__( def __init__(
self, self,
from_account, from_account=None,
to_account, to_account=None,
bitshares_instance=None bitshares_instance=None
): ):
self.bitshares = bitshares_instance or shared_bitshares_instance() self.bitshares = bitshares_instance or shared_bitshares_instance()
self.to_account = Account(to_account, bitshares_instance=self.bitshares) if to_account:
self.from_account = Account(from_account, bitshares_instance=self.bitshares) self.to_account = Account(to_account, bitshares_instance=self.bitshares)
if from_account:
self.from_account = Account(from_account, bitshares_instance=self.bitshares)
def encrypt(self, memo): def encrypt(self, memo):
""" Encrypt a memo """ Encrypt a memo
...@@ -86,19 +100,29 @@ class Memo(object): ...@@ -86,19 +100,29 @@ class Memo(object):
if not memo: if not memo:
return None return None
memo_wif = self.bitshares.wallet.getPrivateKeyForPublicKey( # We first try to decode assuming we received the memo
self.to_account["options"]["memo_key"] try:
) memo_wif = self.bitshares.wallet.getPrivateKeyForPublicKey(
if not memo_wif: memo["to"]
raise MissingKeyError("Memo key for %s missing!" % self.to_account["name"]) )
pubkey = memo["from"]
except KeyNotFound:
try:
# if that failed, we assume that we have sent the memo
memo_wif = self.bitshares.wallet.getPrivateKeyForPublicKey(
memo["from"]
)
pubkey = memo["to"]
except KeyNotFound:
# if all fails, raise exception
raise MissingKeyError(
"Non of the required memo keys are installed!"
"Need any of {}".format(
[memo["to"], memo["from"]]))
# TODO: Use pubkeys of the message, not pubkeys of account!
return BtsMemo.decode_memo( return BtsMemo.decode_memo(
PrivateKey(memo_wif), PrivateKey(memo_wif),
PublicKey( PublicKey(pubkey, prefix=self.bitshares.prefix),
self.from_account["options"]["memo_key"],
prefix=self.bitshares.rpc.chain_params["prefix"]
),
memo.get("nonce"), memo.get("nonce"),
memo.get("message") memo.get("message")
) )
...@@ -7,6 +7,7 @@ from .exceptions import ( ...@@ -7,6 +7,7 @@ from .exceptions import (
KeyNotFound, KeyNotFound,
InvalidWifError, InvalidWifError,
WalletExists, WalletExists,
WalletLocked,
WrongMasterPasswordException, WrongMasterPasswordException,
NoWalletException NoWalletException
) )
...@@ -127,6 +128,11 @@ class Wallet(): ...@@ -127,6 +128,11 @@ class Wallet():
""" """
self.masterpassword = None self.masterpassword = None
def unlocked(self):
""" Is the wallet database unlocked?
"""
return not self.locked()
def locked(self): def locked(self):
""" Is the wallet database locked? """ Is the wallet database locked?
""" """
...@@ -226,6 +232,9 @@ class Wallet(): ...@@ -226,6 +232,9 @@ class Wallet():
if not self.created(): if not self.created():
raise NoWalletException raise NoWalletException
if not self.unlocked():
raise WalletLocked
encwif = self.keyStorage.getPrivateKeyForPublicKey(pub) encwif = self.keyStorage.getPrivateKeyForPublicKey(pub)
if not encwif: if not encwif:
raise KeyNotFound("No private key for {} found".format(pub)) raise KeyNotFound("No private key for {} found".format(pub))
......
...@@ -50,8 +50,8 @@ of the message. ...@@ -50,8 +50,8 @@ of the message.
Example Example
####### #######
High Level Encrypting a memo
~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
The high level memo class makes use of the pybitshares wallet to obtain keys The high level memo class makes use of the pybitshares wallet to obtain keys
for the corresponding accounts. for the corresponding accounts.
...@@ -65,34 +65,29 @@ for the corresponding accounts. ...@@ -65,34 +65,29 @@ for the corresponding accounts.
from_account=Account(from_account), from_account=Account(from_account),
to_account=Account(to_account) to_account=Account(to_account)
) )
cipher = memoObj.encrypt(memo) encrypted_memo = memoObj.encrypt(memo)
plain = memoObj.decrypt(cipher)
Decoding of a received memo
Low Level ~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~
.. code-block:: python .. code-block:: python
from bitsharesbase.memo import memo from getpass import getpass
from bitsharesbase.account import PrivateKey, PublicKey from bitshares.block import Block
from bitshares.memo import Memo
wifkey = "5....<wif>" block = Block(23755086)
memo = { transaction = block["transactions"][3]
"from": "GPH5mgup8evDqMnT86L7scVebRYDC2fwAWmygPEUL43LjstQegYCC", op = transaction["operations"][0]
"to": "GPH5Ar4j53kFWuEZQ9XhxbAja4YXMPJ2EnUg5QcrdeMFYUNMMNJbe", op_id = op[0]
"nonce": "13043867485137706821", op_data = op[1]
"message": "d55524c37320920844ca83bb20c8d008"
} memo = Memo()
try : memo.bitshares.wallet.unlock(getpass())
privkey = PrivateKey(wifkey) print(memo.decrypt(op_data["memo"]))
pubkey = PublicKey(memo["from"], prefix=prefix)
memomsg = memo.decode_memo(privkey, pubkey, memo["nonce"], memo["message"]) API
except Exception as e: ###
memomsg = "--cannot decode-- %s" % str(e)
Definitions
###########
.. automodule:: bitsharesbase.memo .. automodule:: bitsharesbase.memo
:members: :members:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment