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

Commit

Permalink
[#176] Added SecurityLevel filter macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Jun 8, 2018
1 parent 7733db9 commit edbebf4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
6 changes: 4 additions & 2 deletions iota/commands/extended/get_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from typing import Optional

import filters as f

from iota import BadApiResponse
from iota.commands import FilterCommand, RequestFilter
from iota.commands.core.get_balances import GetBalancesCommand
from iota.commands.extended.utils import iter_used_addresses
from iota.crypto.addresses import AddressGenerator
from iota.crypto.types import Seed
from iota.exceptions import with_context
from iota.filters import Trytes
from iota.filters import SecurityLevel, Trytes

__all__ = [
'GetInputsCommand',
Expand Down Expand Up @@ -112,7 +113,8 @@ def __init__(self):
'stop': f.Type(int) | f.Min(0),
'start': f.Type(int) | f.Min(0) | f.Optional(0),
'threshold': f.Type(int) | f.Min(0),
'securityLevel': f.Type(int) | f.Min(1) | f.Max(3) | f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL),

'securityLevel': SecurityLevel,

# These arguments are required.
'seed': f.Required | Trytes(result_type=Seed),
Expand Down
12 changes: 4 additions & 8 deletions iota/commands/extended/get_new_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import filters as f

from iota import Address, AddressChecksum
from iota import Address
from iota.commands import FilterCommand, RequestFilter
from iota.commands.core.find_transactions import FindTransactionsCommand
from iota.crypto.addresses import AddressGenerator
from iota.crypto.types import Seed
from iota.filters import Trytes
from iota.filters import SecurityLevel, Trytes

__all__ = [
'GetNewAddressesCommand',
Expand Down Expand Up @@ -40,7 +40,7 @@ def _execute(self, request):
seed = request['seed'] # type: Seed

return {
'addresses':
'addresses':
self._find_addresses(seed, index, count, security_level, checksum),
}

Expand Down Expand Up @@ -85,11 +85,7 @@ def __init__(self):
'count': f.Type(int) | f.Min(1),
'index': f.Type(int) | f.Min(0) | f.Optional(default=0),

'securityLevel':
f.Type(int)
| f.Min(1)
| f.Max(self.MAX_SECURITY_LEVEL)
| f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL),
'securityLevel': SecurityLevel,

'seed': f.Required | Trytes(result_type=Seed),
},
Expand Down
6 changes: 3 additions & 3 deletions iota/commands/extended/prepare_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from typing import List, Optional

import filters as f

from iota import Address, BadApiResponse, ProposedBundle, \
ProposedTransaction
from iota.commands import FilterCommand, RequestFilter
from iota.commands.core.get_balances import GetBalancesCommand
from iota.commands.extended.get_inputs import GetInputsCommand
from iota.commands.extended.get_new_addresses import GetNewAddressesCommand
from iota.crypto.addresses import AddressGenerator
from iota.crypto.signing import KeyGenerator
from iota.crypto.types import Seed
from iota.exceptions import with_context
from iota.filters import GeneratedAddress, Trytes
from iota.filters import GeneratedAddress, SecurityLevel, Trytes

__all__ = [
'PrepareTransferCommand',
Expand Down Expand Up @@ -133,7 +133,7 @@ def __init__(self):

# Optional parameters.
'changeAddress': Trytes(result_type=Address),
'securityLevel': f.Type(int) | f.Min(1) | f.Max(3) | f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL),
'securityLevel': SecurityLevel,

# Note that ``inputs`` is allowed to be an empty array.
'inputs':
Expand Down
6 changes: 3 additions & 3 deletions iota/commands/extended/send_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from typing import List, Optional

import filters as f

from iota import Address, Bundle, ProposedTransaction, TransactionHash
from iota.commands import FilterCommand, RequestFilter
from iota.commands.extended.prepare_transfer import PrepareTransferCommand
from iota.commands.extended.send_trytes import SendTrytesCommand
from iota.crypto.types import Seed
from iota.crypto.addresses import AddressGenerator
from iota.filters import Trytes
from iota.filters import SecurityLevel, Trytes

__all__ = [
'SendTransferCommand',
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self):

# Optional parameters.
'changeAddress': Trytes(result_type=Address),
'securityLevel': f.Choice([1, 2, 3]) | f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL),
'securityLevel': SecurityLevel,

# Note that ``inputs`` is allowed to be an empty array.
'inputs':
Expand Down
15 changes: 15 additions & 0 deletions iota/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from typing import Text

import filters as f
from filters.macros import filter_macro
from six import binary_type, moves as compat, text_type

from iota import Address, TryteString, TrytesCompatible
from iota.crypto.addresses import AddressGenerator


class GeneratedAddress(f.BaseFilter):
Expand Down Expand Up @@ -70,6 +72,19 @@ def _apply(self, value):
return value


@filter_macro
def SecurityLevel():
"""
Generates a filter chain for validating a security level.
"""
return (
f.Type(int) |
f.Min(1) |
f.Max(3) |
f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL)
)


class Trytes(f.BaseFilter):
"""
Validates a sequence as a sequence of trytes.
Expand Down
12 changes: 6 additions & 6 deletions test/commands/extended/send_transfer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import filters as f
from filters.test import BaseFilterTestCase
from six import binary_type, text_type
from six import binary_type

from iota import Address, Bundle, Iota, ProposedTransaction, \
TransactionTrytes, TryteString, TransactionHash
from iota import Address, Bundle, Iota, ProposedTransaction, TransactionHash, \
TransactionTrytes, TryteString
from iota.adapter import MockAdapter
from iota.commands.extended.send_transfer import SendTransferCommand
from iota.crypto.addresses import AddressGenerator
Expand Down Expand Up @@ -175,13 +175,13 @@ def test_pass_optional_parameters_omitted(self):

'depth': 100,
'minWeightMagnitude': 13,
'securityLevel': AddressGenerator.DEFAULT_SECURITY_LEVEL,
'seed': Seed(self.trytes2),

'transfers': [
self.transfer1,
self.transfer2
],
'securityLevel': AddressGenerator.DEFAULT_SECURITY_LEVEL,
}
)

Expand Down Expand Up @@ -635,7 +635,7 @@ def test_fail_wrong_security_level(self):
},

{
'securityLevel': [f.Choice.CODE_INVALID],
'securityLevel': [f.Min.CODE_TOO_SMALL],
},
)

Expand All @@ -656,7 +656,7 @@ def test_fail_wrong_security_level_type(self):
},

{
'securityLevel': [f.Choice.CODE_INVALID],
'securityLevel': [f.Type.CODE_WRONG_TYPE],
},
)

Expand Down

0 comments on commit edbebf4

Please sign in to comment.