Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
beem
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
beem
Commits
03707966
Commit
03707966
authored
7 years ago
by
Fabian Schuh
Browse files
Options
Downloads
Patches
Plain Diff
[memo] simplify the use of bitshares.memo
parent
3137f8b1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bitshares/memo.py
+39
-15
39 additions, 15 deletions
bitshares/memo.py
bitshares/wallet.py
+9
-0
9 additions, 0 deletions
bitshares/wallet.py
docs/memo.rst
+20
-25
20 additions, 25 deletions
docs/memo.rst
with
68 additions
and
40 deletions
bitshares/memo.py
+
39
−
15
View file @
03707966
...
@@ -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
"
)
)
)
This diff is collapsed.
Click to expand it.
bitshares/wallet.py
+
9
−
0
View file @
03707966
...
@@ -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
))
...
...
This diff is collapsed.
Click to expand it.
docs/memo.rst
+
20
−
25
View file @
03707966
...
@@ -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:
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment