Skip to content

Commit

Permalink
REM-1312: Get particular Atomic Swap information by its identifier (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiia-bilova authored May 6, 2019
1 parent 666eb09 commit f7852ff
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 3 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* [Requirements](#getting-started-requirements)
* [Installation](#installation)
* [Usage](#usage)
* [Nodes](#nodes)
* [Configuration file](#configuration-file)
* [Service](#service)
* [Account](#account)
* [Atomic Swap](#atomic-swap)
* [Node](#node)
* [Public key](#public-key)
* [Transaction](#transaction)
Expand Down Expand Up @@ -159,6 +159,36 @@ $ remme atomic-swap get-public-key --node-url=node-6-testnet.remme.io
}
```

Get information about atomic swap by its identifier — ``remme atomic-swap get-info``:

| Arguments | Type | Required | Description |
| :-------: | :----: | :------: | ------------------------------------------------- |
| id | String | Yes | Swap identifier to get information about swap by. |
| node-url | String | No | Node URL to apply a command to. |

```bash
$ remme atomic-swap get-info \
--id=033402fe1346742486b15a3a9966eb5249271025fc7fb0b37ed3fdb4bcce6808 \
--node-url=node-genesis-testnet.remme.io
{
"result": {
"information": {
"amount": "10.0000",
"created_at": 1556803765,
"email_address_encrypted_optional": "",
"is_initiator": false,
"receiver_address": "112007484def48e1c6b77cf784aeabcac51222e48ae14f3821697f4040247ba01558b1",
"secret_key": "",
"secret_lock": "0728356568862f9da0825aa45ae9d3642d64a6a732ad70b8857b2823dbf2a0b8",
"sender_address": "1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3cf",
"sender_address_non_local": "0xe6ca0e7c974f06471759e9a05d18b538c5ced11e",
"state": "OPENED",
"swap_id": "033402fe1346742486b15a3a9966eb5249271025fc7fb0b37ed3fdb4bcce6808"
}
}
}
```

### Node

Get node configurations — ``remme node get-configs``:
Expand Down
38 changes: 37 additions & 1 deletion cli/atomic_swap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import click
from remme import Remme

from cli.atomic_swap.forms import GetAtomicSwapPublicKeyForm
from cli.atomic_swap.forms import (
GetAtomicSwapInformationForm,
GetAtomicSwapPublicKeyForm,
)
from cli.atomic_swap.help import SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE
from cli.atomic_swap.service import AtomicSwap
from cli.constants import (
FAILED_EXIT_FROM_COMMAND_CODE,
Expand Down Expand Up @@ -54,3 +58,35 @@ def get_public_key(node_url):
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

print_result(result=result)


@click.option('--id', type=str, required=True, help=SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', type=str, required=False, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url())
@atomic_swap_commands.command('get-info')
def get_swap_info(id, node_url):
"""
Get information about atomic swap by its identifier.
"""
arguments, errors = GetAtomicSwapInformationForm().load({
'id': id,
'node_url': node_url,
})

if errors:
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

swap_id = arguments.get('id')
node_url = arguments.get('node_url')

remme = Remme(network_config={
'node_address': str(node_url) + ':8080',
})

result, errors = AtomicSwap(service=remme).get(swap_id=swap_id)

if errors is not None:
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

print_result(result=result)
14 changes: 13 additions & 1 deletion cli/atomic_swap/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
"""
from marshmallow import Schema

from cli.generic.forms.fields import NodeUrlField
from cli.generic.forms.fields import (
NodeUrlField,
SwapIdentifierField,
)


class GetAtomicSwapInformationForm(Schema):
"""
Get information about atomic swap by its identifier form.
"""

id = SwapIdentifierField(required=True)
node_url = NodeUrlField(required=False)


class GetAtomicSwapPublicKeyForm(Schema):
Expand Down
1 change: 1 addition & 0 deletions cli/atomic_swap/help.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""
Provide help messages for command line interface's atomic swap commands.
"""
SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE = 'Swap identifier to get information about swap by.'
6 changes: 6 additions & 0 deletions cli/atomic_swap/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ def get_public_key(self):
Get public key of atomic swap.
"""
pass

def get(self, swap_id):
"""
Get information about atomic swap by its identifier.
"""
pass
18 changes: 18 additions & 0 deletions cli/atomic_swap/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio

from accessify import implements
from aiohttp_json_rpc import RpcGenericServerDefinedError

from cli.atomic_swap.interfaces import AtomicSwapInterface

Expand Down Expand Up @@ -38,3 +39,20 @@ def get_public_key(self):
return {
'public_key': public_key,
}, None

def get(self, swap_id):
"""
Get information about atomic swap by its identifier.
"""
try:
swap_info = loop.run_until_complete(self.service.swap.get_info(swap_id=swap_id))

except RpcGenericServerDefinedError as error:
return None, str(error.message)

except Exception as error:
return None, str(error)

return {
'information': swap_info.data,
}, None
1 change: 1 addition & 0 deletions cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PRIVATE_KEY_REGEXP = r'^[a-f0-9]{64}$'
PUBLIC_KEY_ADDRESS_REGEXP = r'^[0-9a-f]{70}$'
HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
SWAP_IDENTIFIER_REGEXP = r'^[a-f0-9]{64}$'
TRANSACTION_HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
DOMAIN_NAME_REGEXP = r'(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]'

Expand Down
21 changes: 21 additions & 0 deletions cli/generic/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FAMILY_NAMES,
PRIVATE_KEY_REGEXP,
PUBLIC_KEY_ADDRESS_REGEXP,
SWAP_IDENTIFIER_REGEXP,
TRANSACTION_HEADER_SIGNATURE_REGEXP,
)

Expand Down Expand Up @@ -165,3 +166,23 @@ def _deserialize(self, value, attr, data, **kwargs):
raise ValidationError(f'The following public key address `{public_key_address}` is invalid.')

return value


class SwapIdentifierField(fields.Field):
"""
Implements validation of the swap identifier.
References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, data, **kwargs):
"""
Validate data (swap identifier) that was passed to field.
"""
swap_identifier = value

if re.match(pattern=SWAP_IDENTIFIER_REGEXP, string=swap_identifier) is None:
raise ValidationError(f'The following swap identifier `{swap_identifier}` is invalid.')

return swap_identifier
Loading

0 comments on commit f7852ff

Please sign in to comment.