Skip to content
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

Swap cosmos support (#3) #244

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 12 additions & 0 deletions .github/workflows/reusable_swap_functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ on:
default: 'LedgerHQ/app-cardano'
type: string

branch_for_cosmos:
required: false
default: 'main'
type: string
repo_for_cosmos:
required: false
default: 'cosmos/ledger-cosmos'
type: string
Comment on lines +118 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for now, I'll change it to our fork once it's merged


test_filter:
required: false
default: '""'
Expand Down Expand Up @@ -177,6 +186,9 @@ jobs:
- name: cardano
repo: ${{ inputs.repo_for_cardano }}
branch: ${{ inputs.branch_for_cardano }}
- name: cosmos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be used as COIN= value

Suggested change
- name: cosmos
- name: ATOM

repo: ${{ inputs.repo_for_cosmos }}
branch: ${{ inputs.branch_for_cosmos }}

uses: LedgerHQ/ledger-app-workflows/.github/workflows/reusable_build.yml@v1
with:
Expand Down
3 changes: 2 additions & 1 deletion test/python/apps/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .ton import TON_PACKED_DERIVATION_PATH, TON_CONF
from .tron import TRX_PACKED_DERIVATION_PATH, TRX_CONF
from .tron import TRX_USDT_CONF, TRX_USDC_CONF, TRX_TUSD_CONF, TRX_USDD_CONF
from .cosmos import COSMOS_PACKED_DERIVATION_PATH, COSMOS_CONF
from .cardano import ADA_BYRON_PACKED_DERIVATION_PATH, ADA_SHELLEY_PACKED_DERIVATION_PATH, ADA_CONF

@dataclass
Expand Down Expand Up @@ -55,10 +56,10 @@ def get_conf_for_ticker(self, overload_signer: Optional[SigningAuthority]=None)
USDC_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="USDC", conf=TRX_USDC_CONF, packed_derivation_path=TRX_PACKED_DERIVATION_PATH)
TUSD_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="TUSD", conf=TRX_TUSD_CONF, packed_derivation_path=TRX_PACKED_DERIVATION_PATH)
USDD_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="USDD", conf=TRX_USDD_CONF, packed_derivation_path=TRX_PACKED_DERIVATION_PATH)
COSMOS_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="ATOM", conf=COSMOS_CONF, packed_derivation_path=COSMOS_PACKED_DERIVATION_PATH)
ADA_BYRON_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="ADA", conf=ADA_CONF, packed_derivation_path=ADA_BYRON_PACKED_DERIVATION_PATH)
ADA_SHELLEY_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="ADA", conf=ADA_CONF, packed_derivation_path=ADA_SHELLEY_PACKED_DERIVATION_PATH)


# Helper that can be called from outside if we want to generate errors easily
def sign_currency_conf(currency_conf: bytes, overload_signer: Optional[SigningAuthority]=None) -> bytes:
if overload_signer is not None:
Expand Down
93 changes: 93 additions & 0 deletions test/python/apps/cosmos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import traceback
import requests
import json
from enum import IntEnum

from nacl.encoding import HexEncoder
from nacl.signing import VerifyKey,SigningKey
from nacl.exceptions import BadSignatureError

from ragger.utils import create_currency_config
from ragger.bip import pack_derivation_path, bitcoin_pack_derivation_path, BtcDerivationPathFormat
from ragger.error import ExceptionRAPDU
from scalecodec.base import RuntimeConfiguration
from scalecodec.type_registry import load_type_registry_preset
from scalecodec.utils.ss58 import ss58_decode

from ecdsa import VerifyingKey, SECP256k1
from ecdsa.util import string_to_number

COSMOS_CONF = create_currency_config("ATOM", "Cosmos")
COSMOS_PACKED_DERIVATION_PATH = bitcoin_pack_derivation_path(BtcDerivationPathFormat.LEGACY, "m/44'/118'/5'/0'/3")
COSMOS_PACKED_DERIVATION_PATH_SIGN_INIT = bytes([0x2c, 0x00, 0x00, 0x80,
0x76, 0x00, 0x00, 0x80,
0x05, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00])
MAX_CHUNK_SIZE = 250

class Errors:
ERR_SWAP_CHECK_WRONG_METHOD = 0x6984
ERR_SWAP_CHECK_WRONG_METHOD_ARGS_CNT = 0x6984
ERR_SWAP_CHECK_WRONG_DEST_ADDR = 0x6984
ERR_SWAP_CHECK_WRONG_AMOUNT = 0x6984
ERR_SWAP_CHECK_WRONG_FEES = 0x6984
ERR_SWAP_CHECK_WRONG_MEMO = 0x6984

class Ins():
GET_PUBLIC_KEY = 0x04
SIGN = 0x02

class GetAddrP1:
NO_CONFIRM = 0x00
CONFIRM = 0x01

class SignP1:
INIT = 0x00
ADD = 0x01
LAST = 0x02

class SignP2:
JSON_MODE = 0x00
TEXTUAL_MODE = 0x01

class CosmosClient:
CLA = 0x55
def __init__(self, client):
self._client = client

@property
def client(self):
return self._client

def get_pubkey(self):
# sizeof(cosmos) + cosmos
data = bytes([0x06,0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73]) + COSMOS_PACKED_DERIVATION_PATH_SIGN_INIT
msg = self.client.exchange(self.CLA, ins=Ins.GET_PUBLIC_KEY, p1=GetAddrP1.NO_CONFIRM, data=data)
return msg.data[:32].hex().encode()


def perform_cosmos_transaction(self, destination, send_amount, fees, memo) -> bytes:
# Get public key.
key = self.get_pubkey()

#Amounts are in uatom for the app aprser to return ATOM ticker and format
tx = f'''{{"account_number":"0","chain_id":"cosmoshub-4","fee":{{"amount":[{{"amount":"{fees}","denom":"uatom"}}],"gas":"10000"}},"memo":"{memo}","msgs":[{{"inputs":[{{"address":"{destination}","coins":[{{"amount":"{send_amount}","denom":"uatom"}}]}}],"outputs":[{{"address":"{destination}","coins":[{{"amount":"{send_amount}","denom":"uatom"}}]}}]}}],"sequence":"1"}}'''

# Convert the JSON to bytes
tx_blob = tx.encode('utf-8')

# Send the first chunk of the transaction path + cosmos hrp
chunk_0 = COSMOS_PACKED_DERIVATION_PATH_SIGN_INIT + bytes([0x06,0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73])
self.client.exchange(self.CLA, ins=Ins.SIGN, p1=SignP1.INIT, data=chunk_0)

message_splited = [tx_blob[x:x + MAX_CHUNK_SIZE] for x in range(0, len(tx_blob), MAX_CHUNK_SIZE)]
for index, chunk in enumerate(message_splited):
payload_type = SignP1.ADD
if index == len(message_splited) - 1:
payload_type = SignP1.LAST

response = self.client.exchange(self.CLA, ins=Ins.SIGN, p1=payload_type, p2=SignP2.JSON_MODE, data=chunk)

#TODO: Verify signature
return True
1 change: 1 addition & 0 deletions test/python/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"DOT": "Polkadot",
"tron": "Tron",
"ton": "TON",
"cosmos": "Cosmos",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be aligned with the value in the CI

Suggested change
"cosmos": "Cosmos",
"ATOM": "Cosmos",

"cardano": "Cardano ADA",
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading