Skip to content

Commit

Permalink
Updated dependencies, bumped version, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeUrban committed Dec 17, 2019
1 parent ca0006d commit 980c267
Show file tree
Hide file tree
Showing 39 changed files with 619 additions and 470 deletions.
86 changes: 31 additions & 55 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
author = 'Jake Urban'

# The short X.Y version
version = '0.9.10'
version = '0.9.11'
# The full version, including alpha/beta/rc tags
release = '0.9.10'
release = '0.9.11'


# -- General configuration ---------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions example/server/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ class AnchorConfig(AppConfig):

def ready(self):
from polaris.integrations import register_integrations
from .integrations import (MyDepositIntegration,
MyWithdrawalIntegration)
from .integrations import MyDepositIntegration, MyWithdrawalIntegration

register_integrations(
deposit=MyDepositIntegration(),
withdrawal=MyWithdrawalIntegration()
deposit=MyDepositIntegration(), withdrawal=MyWithdrawalIntegration()
)
2 changes: 1 addition & 1 deletion example/server/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ def get_stellar_toml():
"CURRENCIES": [
{"code": asset.code, "issuer": settings.STELLAR_ISSUER_ACCOUNT_ADDRESS}
for asset in Asset.objects.all().iterator()
]
],
}
2 changes: 1 addition & 1 deletion polaris/polaris/collectstatic/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Placeholder for git tracking
# Placeholder for git tracking
4 changes: 3 additions & 1 deletion polaris/polaris/deposit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

urlpatterns = [
path("transactions/deposit/interactive", csrf_exempt(deposit)),
path("transactions/deposit/webapp", interactive_deposit, name="interactive_deposit")
path(
"transactions/deposit/webapp", interactive_deposit, name="interactive_deposit"
),
]
9 changes: 4 additions & 5 deletions polaris/polaris/deposit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def create_stellar_deposit(transaction_id: str) -> bool:
builder = TransactionBuilder(
source_account=server_account,
network_passphrase=settings.STELLAR_NETWORK_PASSPHRASE,
base_fee=base_fee
base_fee=base_fee,
)
try:
server.load_account(stellar_account)
Expand All @@ -69,16 +69,15 @@ def create_stellar_deposit(transaction_id: str) -> bool:
if address_exc.status != 404:
transaction.status = Transaction.STATUS.error
transaction.status_message = (
"Horizon error when loading stellar account: "
f"{address_exc.message}"
"Horizon error when loading stellar account: " f"{address_exc.message}"
)
transaction.save()
return False

transaction_envelope = builder.append_create_account_op(
destination=stellar_account,
starting_balance=starting_balance,
source=settings.STELLAR_DISTRIBUTION_ACCOUNT_ADDRESS
source=settings.STELLAR_DISTRIBUTION_ACCOUNT_ADDRESS,
).build()
transaction_envelope.sign(settings.STELLAR_DISTRIBUTION_ACCOUNT_SEED)
try:
Expand All @@ -105,7 +104,7 @@ def create_stellar_deposit(transaction_id: str) -> bool:
destination=stellar_account,
asset_code=asset,
asset_issuer=settings.STELLAR_ISSUER_ACCOUNT_ADDRESS,
amount=str(payment_amount)
amount=str(payment_amount),
).build()
transaction_envelope.sign(settings.STELLAR_DISTRIBUTION_ACCOUNT_SEED)
try:
Expand Down
49 changes: 21 additions & 28 deletions polaris/polaris/deposit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@
validate_sep10_token,
interactive_authentication,
invalidate_session,
generate_interactive_jwt
generate_interactive_jwt,
)
from polaris.models import Asset, Transaction
from polaris.integrations.forms import TransactionForm
from polaris.integrations import registered_deposit_integration as rdi
from polaris.middleware import import_path


def _construct_interactive_url(request: Request,
transaction_id: str,
account: str,
asset_code: str) -> str:
qparams = urlencode({
"asset_code": asset_code,
"transaction_id": transaction_id,
"token": generate_interactive_jwt(request, transaction_id, account)
})
def _construct_interactive_url(
request: Request, transaction_id: str, account: str, asset_code: str
) -> str:
qparams = urlencode(
{
"asset_code": asset_code,
"transaction_id": transaction_id,
"token": generate_interactive_jwt(request, transaction_id, account),
}
)
url_params = f"{reverse('interactive_deposit')}?{qparams}"
return request.build_absolute_uri(url_params)

Expand Down Expand Up @@ -95,7 +96,9 @@ def check_middleware(content_type: str = "text/html") -> Optional[Response]:
err_msg = f"{import_path} is not installed"
elif session_middleware_path not in django_settings.MIDDLEWARE:
err_msg = f"{session_middleware_path} is not installed"
elif django_settings.MIDDLEWARE.index(import_path) > django_settings.MIDDLEWARE.index(session_middleware_path):
elif django_settings.MIDDLEWARE.index(
import_path
) > django_settings.MIDDLEWARE.index(session_middleware_path):
err_msg = f"{import_path} must be listed before {session_middleware_path}"

if err_msg:
Expand Down Expand Up @@ -126,15 +129,12 @@ def interactive_deposit(request: Request) -> Response:

# Ensure the transaction exists
try:
transaction = Transaction.objects.get(
id=transaction_id,
asset=asset
)
transaction = Transaction.objects.get(id=transaction_id, asset=asset)
except Transaction.objects.DoesNotExist:
return render_error_response(
"Transaction with ID and asset_code not found",
content_type="text/html",
status_code=status.HTTP_404_NOT_FOUND
status_code=status.HTTP_404_NOT_FOUND,
)

if request.method == "GET":
Expand Down Expand Up @@ -165,15 +165,12 @@ def interactive_deposit(request: Request) -> Response:
form_class = rdi.form_for_transaction(transaction)

if form_class:
return Response(
{"form": form_class()},
template_name="deposit/form.html"
)
return Response({"form": form_class()}, template_name="deposit/form.html")
else: # Last form has been submitted
invalidate_session(request)
transaction.status = Transaction.STATUS.pending_user_transfer_start
transaction.save()
url, args = reverse('more_info'), urlencode({'id': transaction_id})
url, args = reverse("more_info"), urlencode({"id": transaction_id})
return redirect(f"{url}?{args}")

else:
Expand Down Expand Up @@ -220,16 +217,12 @@ def deposit(account: str, request: Request) -> Response:
asset=asset,
kind=Transaction.KIND.deposit,
status=Transaction.STATUS.incomplete,
to_address=account
to_address=account,
)
url = _construct_interactive_url(
request, str(transaction_id), stellar_account, asset_code
)
return Response(
{
"type": "interactive_customer_info_needed",
"url": url,
"id": transaction_id
},
status=status.HTTP_200_OK
{"type": "interactive_customer_info_needed", "url": url, "id": transaction_id},
status=status.HTTP_200_OK,
)
28 changes: 16 additions & 12 deletions polaris/polaris/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def calc_fee(asset: Asset, operation: str, amount: Decimal) -> Decimal:
# `op_type` is not used in this context, since there is no fee variation
# based on operation type in this example implementation, but that can
# occur in real-life applications.
return fee_fixed + (fee_percent / Decimal('100.0')) * amount
return fee_fixed + (fee_percent / Decimal("100.0")) * amount


def render_error_response(description: str,
status_code: int = status.HTTP_400_BAD_REQUEST,
content_type: str = "application/json") -> Response:
def render_error_response(
description: str,
status_code: int = status.HTTP_400_BAD_REQUEST,
content_type: str = "application/json",
) -> Response:
"""
Renders an error response in Django.
Expand All @@ -42,7 +44,7 @@ def render_error_response(description: str,
resp_data = {
"data": {"error": description},
"status": status_code,
"content_type": content_type
"content_type": content_type,
}
if content_type == "text/html":
resp_data["data"]["status_code"] = status_code
Expand Down Expand Up @@ -112,7 +114,9 @@ def validate_jwt_request(request: Request) -> str:

# Validate the JWT contents.
try:
jwt_dict = jwt.decode(encoded_jwt, settings.SERVER_JWT_KEY, algorithms=["HS256"])
jwt_dict = jwt.decode(
encoded_jwt, settings.SERVER_JWT_KEY, algorithms=["HS256"]
)
except InvalidTokenError as e:
raise ValueError(str(e))

Expand Down Expand Up @@ -165,8 +169,7 @@ def authenticate_session(r: Request):
"""
if r.session.get("authenticated") and r.session.get("account", ""):
transaction_qs = Transaction.objects.filter(
id=r.GET.get("transaction_id"),
stellar_account=r.session["account"]
id=r.GET.get("transaction_id"), stellar_account=r.session["account"]
)
if not transaction_qs.exists():
raise ValueError("Transaction for account not found")
Expand Down Expand Up @@ -208,8 +211,7 @@ def check_authentication(r: Request):
raise ValueError("Session is not authenticated")

transaction_qs = Transaction.objects.filter(
id=r.GET.get("transaction_id"),
stellar_account=r.session.get("account")
id=r.GET.get("transaction_id"), stellar_account=r.session.get("account")
)
if not transaction_qs.exists():
raise ValueError("Transaction for account not found")
Expand All @@ -222,7 +224,9 @@ def invalidate_session(request: Request):
request.session["authenticated"] = False


def generate_interactive_jwt(request: Request, transaction_id: str, account: str) -> str:
def generate_interactive_jwt(
request: Request, transaction_id: str, account: str
) -> str:
"""
Generates a 30-second JWT for the client to use in the GET URL for
the interactive flow.
Expand All @@ -233,7 +237,7 @@ def generate_interactive_jwt(request: Request, transaction_id: str, account: str
"iat": issued_at,
"exp": issued_at + 30,
"sub": account,
"jti": transaction_id
"jti": transaction_id,
}
encoded_jwt = jwt.encode(payload, settings.SERVER_JWT_KEY, algorithm="HS256")
return encoded_jwt.decode("ascii")
Loading

0 comments on commit 980c267

Please sign in to comment.