1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-28 14:05:43 +00:00

ripple: client functions moved to ripple.py

This commit is contained in:
Tomas Susanka 2018-07-12 14:06:36 +02:00 committed by matejcik
parent 77414ad761
commit 37847fb56f
5 changed files with 49 additions and 31 deletions

View File

@ -38,6 +38,7 @@ from trezorlib import messages as proto
from trezorlib import protobuf from trezorlib import protobuf
from trezorlib import stellar from trezorlib import stellar
from trezorlib import tools from trezorlib import tools
from trezorlib import ripple
class ChoiceType(click.Choice): class ChoiceType(click.Choice):
@ -1060,8 +1061,16 @@ def stellar_sign_transaction(connect, b64envelope, address, network_passphrase):
@click.pass_obj @click.pass_obj
def ripple_get_address(connect, address, show_display): def ripple_get_address(connect, address, show_display):
client = connect() client = connect()
address_n = tools.parse_path(address) return ripple.ripple_get_address(client, address, show_display)
return client.ripple_get_address(address_n, show_display)
@cli.command(help='Sign Ripple transaction')
@click.option('-n', '--address', required=True, help="BIP-32 path to key, e.g. m/44'/144'/0'/0/0")
@click.pass_obj
def ripple_sign_transaction(connect, address, show_display):
client = connect()
# todo load from json
return ripple.ripple_sign_tx(client, address, show_display)
# #
# Main # Main

View File

@ -33,7 +33,6 @@ from . import mapping
from . import nem from . import nem
from . import protobuf from . import protobuf
from . import stellar from . import stellar
from . import ripple
from .debuglink import DebugLink from .debuglink import DebugLink
if sys.version_info.major < 3: if sys.version_info.major < 3:
@ -1083,19 +1082,6 @@ class ProtocolMixin(object):
return self.call(proto.SelfTest(payload=b'\x00\xFF\x55\xAA\x66\x99\x33\xCCABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\x00\xFF\x55\xAA\x66\x99\x33\xCC')) return self.call(proto.SelfTest(payload=b'\x00\xFF\x55\xAA\x66\x99\x33\xCCABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\x00\xFF\x55\xAA\x66\x99\x33\xCC'))
@field('address')
@expect(proto.RippleAddress)
def ripple_get_address(self, address_n, show_display=False):
return self.call(
proto.RippleGetAddress(
address_n=address_n, show_display=show_display))
@expect(proto.RippleSignedTx)
def ripple_sign_tx(self, n, transaction):
msg = ripple.create_sign_tx(transaction)
msg.address_n = tools.parse_path(n)
return self.call(msg)
@field('public_key') @field('public_key')
@expect(proto.StellarPublicKey) @expect(proto.StellarPublicKey)
def stellar_get_public_key(self, address_n, show_display=False): def stellar_get_public_key(self, address_n, show_display=False):

View File

@ -18,9 +18,27 @@ import base64
import struct import struct
from . import messages from . import messages
from . import tools
from .client import field
from .client import expect
def create_sign_tx(transaction) -> messages.RippleSignTx: @field('address')
@expect(messages.RippleAddress)
def ripple_get_address(client, address_n, show_display=False):
return client.call(
messages.RippleGetAddress(
address_n=address_n, show_display=show_display))
@expect(messages.RippleSignedTx)
def ripple_sign_tx(client, address_n, transaction):
msg = _create_sign_tx(transaction)
msg.address_n = address_n
return client.call(msg)
def _create_sign_tx(transaction) -> messages.RippleSignTx:
if not all(transaction.get(k) for k in ("Fee", "Sequence", "TransactionType", "Amount", "Destination")): if not all(transaction.get(k) for k in ("Fee", "Sequence", "TransactionType", "Amount", "Destination")):
raise ValueError("Some of the required fields missing (Fee, Sequence, TransactionType, Amount, Destination") raise ValueError("Some of the required fields missing (Fee, Sequence, TransactionType, Amount, Destination")
if transaction["TransactionType"] != "Payment": if transaction["TransactionType"] != "Payment":
@ -31,11 +49,11 @@ def create_sign_tx(transaction) -> messages.RippleSignTx:
sequence=transaction.get("Sequence"), sequence=transaction.get("Sequence"),
flags=transaction.get("Flags"), flags=transaction.get("Flags"),
last_ledger_sequence=transaction.get("LastLedgerSequence"), last_ledger_sequence=transaction.get("LastLedgerSequence"),
payment=create_payment(transaction), payment=_create_payment(transaction),
) )
def create_payment(transaction) -> messages.RipplePayment: def _create_payment(transaction) -> messages.RipplePayment:
return messages.RipplePayment( return messages.RipplePayment(
amount=transaction.get("Amount"), amount=transaction.get("Amount"),
destination=transaction.get("Destination") destination=transaction.get("Destination")

View File

@ -20,6 +20,7 @@ from .common import TrezorTest
from .conftest import TREZOR_VERSION from .conftest import TREZOR_VERSION
from binascii import hexlify from binascii import hexlify
from trezorlib.client import CallException from trezorlib.client import CallException
from trezorlib.ripple import ripple_get_address
from trezorlib.tools import parse_path from trezorlib.tools import parse_path
@ -32,11 +33,11 @@ class TestMsgRippleGetAddress(TrezorTest):
# data from https://iancoleman.io/bip39/#english # data from https://iancoleman.io/bip39/#english
self.setup_mnemonic_allallall() self.setup_mnemonic_allallall()
address = self.client.ripple_get_address(parse_path("m/44'/144'/0'/0/0")) address = ripple_get_address(self.client, parse_path("m/44'/144'/0'/0/0"))
assert address == 'rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H' assert address == 'rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H'
address = self.client.ripple_get_address(parse_path("m/44'/144'/0'/0/1")) address = ripple_get_address(self.client, parse_path("m/44'/144'/0'/0/1"))
assert address == 'rBKz5MC2iXdoS3XgnNSYmF69K1Yo4NS3Ws' assert address == 'rBKz5MC2iXdoS3XgnNSYmF69K1Yo4NS3Ws'
address = self.client.ripple_get_address(parse_path("m/44'/144'/1'/0/0")) address = ripple_get_address(self.client, parse_path("m/44'/144'/1'/0/0"))
assert address == 'rJX2KwzaLJDyFhhtXKi3htaLfaUH2tptEX' assert address == 'rJX2KwzaLJDyFhhtXKi3htaLfaUH2tptEX'
def test_ripple_get_address_other(self): def test_ripple_get_address_other(self):
@ -47,7 +48,7 @@ class TestMsgRippleGetAddress(TrezorTest):
passphrase_protection=False, passphrase_protection=False,
label='test', label='test',
language='english') language='english')
address = self.client.ripple_get_address(parse_path("m/44'/144'/0'/0/0")) address = ripple_get_address(self.client, parse_path("m/44'/144'/0'/0/0"))
assert address == 'r4ocGE47gm4G4LkA9mriVHQqzpMLBTgnTY' assert address == 'r4ocGE47gm4G4LkA9mriVHQqzpMLBTgnTY'
address = self.client.ripple_get_address(parse_path("m/44'/144'/0'/0/1")) address = ripple_get_address(self.client, parse_path("m/44'/144'/0'/0/1"))
assert address == 'rUt9ULSrUvfCmke8HTFU1szbmFpWzVbBXW' assert address == 'rUt9ULSrUvfCmke8HTFU1szbmFpWzVbBXW'

View File

@ -19,9 +19,9 @@ import pytest
from .common import TrezorTest from .common import TrezorTest
from .conftest import TREZOR_VERSION from .conftest import TREZOR_VERSION
from binascii import unhexlify from binascii import unhexlify
from trezorlib import ripple from trezorlib import messages
from trezorlib import messages as proto
from trezorlib.client import CallException from trezorlib.client import CallException
from trezorlib.ripple import ripple_sign_tx
from trezorlib.tools import parse_path from trezorlib.tools import parse_path
@ -33,7 +33,8 @@ class TestMsgRippleSignTx(TrezorTest):
def test_ripple_sign_simple_tx(self): def test_ripple_sign_simple_tx(self):
self.setup_mnemonic_allallall() self.setup_mnemonic_allallall()
resp = self.client.ripple_sign_tx( resp = ripple_sign_tx(
self.client,
parse_path("m/44'/144'/0'/0/0"), { parse_path("m/44'/144'/0'/0/0"), {
"TransactionType": "Payment", "TransactionType": "Payment",
"Destination": "rBKz5MC2iXdoS3XgnNSYmF69K1Yo4NS3Ws", "Destination": "rBKz5MC2iXdoS3XgnNSYmF69K1Yo4NS3Ws",
@ -45,7 +46,8 @@ class TestMsgRippleSignTx(TrezorTest):
assert resp.signature == unhexlify('3045022100e243ef623675eeeb95965c35c3e06d63a9fc68bb37e17dc87af9c0af83ec057e02206ca8aa5eaab8396397aef6d38d25710441faf7c79d292ee1d627df15ad9346c0') assert resp.signature == unhexlify('3045022100e243ef623675eeeb95965c35c3e06d63a9fc68bb37e17dc87af9c0af83ec057e02206ca8aa5eaab8396397aef6d38d25710441faf7c79d292ee1d627df15ad9346c0')
assert resp.serialized_tx == unhexlify('12000022800000002400000019614000000005f5e1006840000000000186a0732102131facd1eab748d6cddc492f54b04e8c35658894f4add2232ebc5afe7521dbe474473045022100e243ef623675eeeb95965c35c3e06d63a9fc68bb37e17dc87af9c0af83ec057e02206ca8aa5eaab8396397aef6d38d25710441faf7c79d292ee1d627df15ad9346c081148fb40e1ffa5d557ce9851a535af94965e0dd098883147148ebebf7304ccdf1676fefcf9734cf1e780826') assert resp.serialized_tx == unhexlify('12000022800000002400000019614000000005f5e1006840000000000186a0732102131facd1eab748d6cddc492f54b04e8c35658894f4add2232ebc5afe7521dbe474473045022100e243ef623675eeeb95965c35c3e06d63a9fc68bb37e17dc87af9c0af83ec057e02206ca8aa5eaab8396397aef6d38d25710441faf7c79d292ee1d627df15ad9346c081148fb40e1ffa5d557ce9851a535af94965e0dd098883147148ebebf7304ccdf1676fefcf9734cf1e780826')
resp = self.client.ripple_sign_tx( resp = ripple_sign_tx(
self.client,
parse_path("m/44'/144'/0'/0/2"), { parse_path("m/44'/144'/0'/0/2"), {
"TransactionType": "Payment", "TransactionType": "Payment",
"Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H", "Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H",
@ -56,7 +58,8 @@ class TestMsgRippleSignTx(TrezorTest):
assert resp.signature == unhexlify('3044022069900e6e578997fad5189981b74b16badc7ba8b9f1052694033fa2779113ddc002206c8006ada310edf099fb22c0c12073550c8fc73247b236a974c5f1144831dd5f') assert resp.signature == unhexlify('3044022069900e6e578997fad5189981b74b16badc7ba8b9f1052694033fa2779113ddc002206c8006ada310edf099fb22c0c12073550c8fc73247b236a974c5f1144831dd5f')
assert resp.serialized_tx == unhexlify('1200002280000000240000000161400000000000000168400000000000000a732103dbed1e77cb91a005e2ec71afbccce5444c9be58276665a3859040f692de8fed274463044022069900e6e578997fad5189981b74b16badc7ba8b9f1052694033fa2779113ddc002206c8006ada310edf099fb22c0c12073550c8fc73247b236a974c5f1144831dd5f8114bdf86f3ae715ba346b7772ea0e133f48828b766483148fb40e1ffa5d557ce9851a535af94965e0dd0988') assert resp.serialized_tx == unhexlify('1200002280000000240000000161400000000000000168400000000000000a732103dbed1e77cb91a005e2ec71afbccce5444c9be58276665a3859040f692de8fed274463044022069900e6e578997fad5189981b74b16badc7ba8b9f1052694033fa2779113ddc002206c8006ada310edf099fb22c0c12073550c8fc73247b236a974c5f1144831dd5f8114bdf86f3ae715ba346b7772ea0e133f48828b766483148fb40e1ffa5d557ce9851a535af94965e0dd0988')
resp = self.client.ripple_sign_tx( resp = ripple_sign_tx(
self.client,
parse_path("m/44'/144'/0'/0/2"), { parse_path("m/44'/144'/0'/0/2"), {
"TransactionType": "Payment", "TransactionType": "Payment",
"Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H", "Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H",
@ -72,7 +75,8 @@ class TestMsgRippleSignTx(TrezorTest):
def test_ripple_sign_invalid_fee(self): def test_ripple_sign_invalid_fee(self):
with pytest.raises(CallException) as exc: with pytest.raises(CallException) as exc:
self.client.ripple_sign_tx( ripple_sign_tx(
self.client,
parse_path("m/44'/144'/0'/0/2"), { parse_path("m/44'/144'/0'/0/2"), {
"TransactionType": "Payment", "TransactionType": "Payment",
"Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H", "Destination": "rNaqKtKrMSwpwZSzRckPf7S96DkimjkF4H",
@ -81,5 +85,5 @@ class TestMsgRippleSignTx(TrezorTest):
"Fee": 1, "Fee": 1,
"Sequence": 1, "Sequence": 1,
}) })
assert exc.value.args[0] == proto.FailureType.ProcessError assert exc.value.args[0] == messages.FailureType.ProcessError
assert exc.value.args[1].endswith('Fee must be in the range of 10 to 10,000 drops') assert exc.value.args[1].endswith('Fee must be in the range of 10 to 10,000 drops')