-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get Atomic Swap public key #20
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
078a3d7
Create command line interface for function get public key of atomic swap
anastasiia-bilova 04d1ede
Merge branch 'develop' of github.com:Remmeauth/remme-core-cli into ge…
anastasiia-bilova 7930287
Add error processing, form validation for public key, create tests
anastasiia-bilova 519a158
Merge branch 'develop' of github.com:Remmeauth/remme-core-cli into ge…
anastasiia-bilova 51b800f
Merge branch 'develop' of github.com:Remmeauth/remme-core-cli into ge…
anastasiia-bilova aba10e4
Merge branch 'develop' of github.com:Remmeauth/remme-core-cli into ge…
anastasiia-bilova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Provide implementation of the command line interface's atomic swap commands. | ||
""" | ||
import sys | ||
|
||
import click | ||
from remme import Remme | ||
|
||
from cli.atomic_swap.forms import GetPublicKeyForm | ||
from cli.atomic_swap.service import AtomicSwap | ||
from cli.constants import ( | ||
FAILED_EXIT_FROM_COMMAND_CODE, | ||
NODE_URL_ARGUMENT_HELP_MESSAGE, | ||
) | ||
from cli.utils import ( | ||
default_node_url, | ||
print_errors, | ||
print_result, | ||
) | ||
|
||
|
||
@click.group('atomic-swap', chain=True) | ||
def atomic_swap_commands(): | ||
""" | ||
Provide commands for working with atomic swap. | ||
""" | ||
pass | ||
|
||
|
||
@click.option('--node-url', type=str, required=False, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url()) | ||
@atomic_swap_commands.command('get-public-key') | ||
def get_public_key(node_url): | ||
""" | ||
Get public key of atomic swap. | ||
""" | ||
arguments, errors = GetPublicKeyForm().load({ | ||
'node_url': node_url, | ||
}) | ||
|
||
if errors: | ||
print_errors(errors=errors) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
|
||
node_url = arguments.get('node_url') | ||
|
||
remme = Remme(network_config={ | ||
'node_address': str(node_url) + ':8080', | ||
}) | ||
|
||
result, errors = AtomicSwap(service=remme).get_public_key() | ||
|
||
if errors is not None: | ||
print_errors(errors=errors) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
|
||
print_result(result=result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Provide forms for command line interface's atomic swap commands. | ||
""" | ||
from marshmallow import Schema | ||
|
||
from cli.generic.forms.fields import NodeURLField | ||
|
||
|
||
class GetPublicKeyForm(Schema): | ||
""" | ||
Get public key of the atomic swap form. | ||
""" | ||
|
||
node_url = NodeURLField(allow_none=True, required=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Provide help messages for command line interface's atomic swap commands. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Provide implementation of the atomic swap interfaces. | ||
""" | ||
|
||
|
||
class AtomicSwapInterface: | ||
""" | ||
Implements atomic swap interface. | ||
""" | ||
|
||
def get_public_key(self): | ||
""" | ||
Get public key of atomic swap. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Provide implementation of the atomic swap. | ||
""" | ||
import asyncio | ||
|
||
from accessify import implements | ||
|
||
from cli.atomic_swap.interfaces import AtomicSwapInterface | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
|
||
@implements(AtomicSwapInterface) | ||
class AtomicSwap: | ||
""" | ||
Implements atomic swap. | ||
""" | ||
|
||
def __init__(self, service): | ||
""" | ||
Constructor. | ||
|
||
Arguments: | ||
service: object to interact with Remme core API. | ||
""" | ||
self.service = service | ||
|
||
def get_public_key(self): | ||
""" | ||
Get public key of atomic swap. | ||
""" | ||
try: | ||
public_key = loop.run_until_complete(self.service.swap.get_public_key()) | ||
|
||
except Exception as error: | ||
return None, str(error) | ||
|
||
return { | ||
'public_key': public_key, | ||
}, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
ADDRESS_REGEXP = r'^[0-9a-f]{70}$' | ||
BATCH_ID_REGEXP = r'^[0-9a-f]{128}$' | ||
PRIVATE_KEY_REGEXP = r'^[a-f0-9]{64}$' | ||
PUBLIC_KEY_REGEXP = r'^[0-9a-f]{66}$' | ||
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]' | ||
|
||
|
@@ -16,7 +17,9 @@ | |
CLI_CONFIG_FILE_NAME = 'remme-core-cli' | ||
|
||
NODE_IP_ADDRESS_FOR_TESTING = '159.89.104.9' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmytrostriletskyi, done. |
||
LATEST_RELEASE_NODE_IP_ADDRESS_FOR_TESTING = '165.22.75.163' | ||
RELEASE_0_9_0_ALPHA_NODE_ADDRESS = '165.227.169.119' | ||
NODE_27_IN_TESTNET_ADDRESS = 'node-27-testnet.remme.io' | ||
|
||
PRIVATE_KEY_FOR_TESTING = 'b03e31d2f310305eab249133b53b5fb3270090fc1692c9b022b81c6b9bb6029b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
""" | ||
Provide tests for command line interface's get public key of the atomic swap commands. | ||
""" | ||
import json | ||
import re | ||
|
||
from click.testing import CliRunner | ||
|
||
from cli.constants import ( | ||
FAILED_EXIT_FROM_COMMAND_CODE, | ||
NODE_27_IN_TESTNET_ADDRESS, | ||
PASSED_EXIT_FROM_COMMAND_CODE, | ||
PUBLIC_KEY_REGEXP, | ||
) | ||
from cli.entrypoint import cli | ||
from cli.utils import dict_to_pretty_json | ||
|
||
|
||
def test_get_public_key(): | ||
""" | ||
Case: get the public key of atomic swap. | ||
Expect: public key is returned. | ||
""" | ||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
'--node-url', | ||
NODE_27_IN_TESTNET_ADDRESS, | ||
]) | ||
|
||
public_key = json.loads(result.output).get('result').get('public_key') | ||
|
||
assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert re.match(pattern=PUBLIC_KEY_REGEXP, string=public_key) is not None | ||
|
||
|
||
def test_get_public_key_without_node_url(mocker): | ||
""" | ||
Case: get the public key of atomic swap without passing node URL. | ||
Expect: public key is returned from node on localhost. | ||
""" | ||
public_key = '03738df3f4ac3621ba8e89413d3ff4ad036c3a0a4dbb164b695885aab6aab614ad' | ||
mock_swap_get_public_key = mocker.patch('cli.node.service.loop.run_until_complete') | ||
mock_swap_get_public_key.return_value = public_key | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
]) | ||
|
||
expected_result = { | ||
'result': { | ||
'public_key': public_key, | ||
}, | ||
} | ||
|
||
assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert expected_result == json.loads(result.output) | ||
|
||
|
||
def test_get_public_key_invalid_node_url(): | ||
""" | ||
Case: get the public key of atomic swap by passing invalid node URL. | ||
Expect: the following node URL is invalid error message. | ||
""" | ||
invalid_node_url = 'domainwithoutextention' | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
'--node-url', | ||
invalid_node_url, | ||
]) | ||
|
||
expected_error = { | ||
'errors': { | ||
'node_url': [ | ||
f'The following node URL `{invalid_node_url}` is invalid.', | ||
], | ||
}, | ||
} | ||
|
||
assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert dict_to_pretty_json(expected_error) in result.output | ||
|
||
|
||
def test_get_public_key_node_url_with_http(): | ||
""" | ||
Case: get the public key of atomic swap by passing node URL with explicit HTTP protocol. | ||
Expect: the following node URL contains protocol error message. | ||
""" | ||
node_url_with_http_protocol = 'http://masternode.com' | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
'--node-url', | ||
node_url_with_http_protocol, | ||
]) | ||
|
||
expected_error = { | ||
'errors': { | ||
'node_url': [ | ||
f'Pass the following node URL `{node_url_with_http_protocol}` without protocol (http, https, etc.).', | ||
], | ||
}, | ||
} | ||
|
||
assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert dict_to_pretty_json(expected_error) in result.output | ||
|
||
|
||
def test_get_public_key_node_url_with_https(): | ||
""" | ||
Case: get the public key of atomic swap by passing node URL with explicit HTTPS protocol. | ||
Expect: the following node URL contains protocol error message. | ||
""" | ||
node_url_with_https_protocol = 'https://masternode.com' | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
'--node-url', | ||
node_url_with_https_protocol, | ||
]) | ||
|
||
expected_error = { | ||
'errors': { | ||
'node_url': [ | ||
f'Pass the following node URL `{node_url_with_https_protocol}` without protocol (http, https, etc.).', | ||
], | ||
}, | ||
} | ||
|
||
assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert dict_to_pretty_json(expected_error) in result.output | ||
|
||
|
||
def test_get_public_key_non_existing_node_url(): | ||
""" | ||
Case: get the public key of atomic swap by passing non existing node URL. | ||
Expect: check if node running at URL error message. | ||
""" | ||
non_existing_node_url = 'non-existing-node.com' | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, [ | ||
'atomic-swap', | ||
'get-public-key', | ||
'--node-url', | ||
non_existing_node_url, | ||
]) | ||
|
||
expected_error = { | ||
'errors': f'Please check if your node running at http://{non_existing_node_url}:8080.', | ||
} | ||
|
||
assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code | ||
assert dict_to_pretty_json(expected_error) in result.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetPublicKeyForm
>GetAtomicSwapPublicKeyForm
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmytrostriletskyi, done.