Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
[#145] Reformat multisig crypto for PEP-8.
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Jun 15, 2018
1 parent 02f4767 commit 3ce90c0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion iota/multisig/crypto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function, \
unicode_literals
unicode_literals
119 changes: 63 additions & 56 deletions iota/multisig/crypto/addresses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function, \
unicode_literals
unicode_literals

from typing import List, Optional

Expand All @@ -10,76 +10,83 @@
from iota.multisig.types import MultisigAddress

__all__ = [
'MultisigAddressBuilder',
'MultisigAddressBuilder',
]


class MultisigAddressBuilder(object):
"""
Creates multisig addresses.
Note that this class generates a single address from multiple inputs,
(digests) unlike :py:class:`iota.crypto.addresses.AddressGenerator`
which generates multiple addresses from a single input (seed).
"""
def __init__(self):
super(MultisigAddressBuilder, self).__init__()

self._digests = [] # type: List[Digest]
"""
Keeps track of digests that were added, so that we can attach them
to the final :py:class:`MultisigAddress` object.
"""
Creates multisig addresses.
self._address = None # type: Optional[MultisigAddress]
Note that this class generates a single address from multiple
inputs (digests), unlike
:py:class:`iota.crypto.addresses.AddressGenerator` which generates
multiple addresses from a single input (seed).
"""
Caches the generated address.

Generating the address modifies the internal state of the curl
sponge, so each :py:class:`MultisigAddressBuilder` instance can
only generate a single address.
"""
def __init__(self):
super(MultisigAddressBuilder, self).__init__()

self._sponge = Kerl()
self._digests = [] # type: List[Digest]
"""
Keeps track of digests that were added, so that we can attach
them to the final :py:class:`MultisigAddress` object.
"""

def add_digest(self, digest):
# type: (Digest) -> None
"""
Absorbs a digest into the sponge.
self._address = None # type: Optional[MultisigAddress]
"""
Caches the generated address.
IMPORTANT: Keep track of the order that digests are added!
To spend inputs from a multisig address, you must provide the
private keys in the same order!
Generating the address modifies the internal state of the curl
sponge, so each :py:class:`MultisigAddressBuilder` instance can
only generate a single address.
"""

References:
- https://github.com/iotaledger/wiki/blob/master/multisigs.md#spending-inputs
"""
if self._address:
raise ValueError('Cannot add digests once an address is extracted.')
self._sponge = Kerl()

self._sponge.absorb(digest.as_trits())
self._digests.append(digest)
def add_digest(self, digest):
# type: (Digest) -> None
"""
Absorbs a digest into the sponge.
def get_address(self):
# type: () -> MultisigAddress
"""
Returns the new multisig address.
.. important::
Keep track of the order that digests are added!
Note that you can continue to add digests after extracting an
address; the next address will use *all* of the digests that have
been added so far.
"""
if not self._digests:
raise ValueError(
'Must call ``add_digest`` at least once '
'before calling ``get_address``.',
)
To spend inputs from a multisig address, you must provide
the private keys in the same order!
References:
- https://github.com/iotaledger/wiki/blob/master/multisigs.md#spending-inputs
"""
if self._address:
raise ValueError('Cannot add digests once an address is extracted.')

self._sponge.absorb(digest.as_trits())
self._digests.append(digest)

def get_address(self):
# type: () -> MultisigAddress
"""
Returns the new multisig address.
Note that you can continue to add digests after extracting an
address; the next address will use *all* of the digests that
have been added so far.
"""
if not self._digests:
raise ValueError(
'Must call ``add_digest`` at least once '
'before calling ``get_address``.',
)

if not self._address:
address_trits = [0] * HASH_LENGTH
self._sponge.squeeze(address_trits)
if not self._address:
address_trits = [0] * HASH_LENGTH
self._sponge.squeeze(address_trits)

self._address =\
MultisigAddress.from_trits(address_trits, digests=self._digests[:])
self._address = MultisigAddress.from_trits(
address_trits,
digests=self._digests[:],
)

return self._address
return self._address

0 comments on commit 3ce90c0

Please sign in to comment.