Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Tezos integration #292

Merged
merged 7 commits into from
Sep 13, 2018
Merged

Tezos integration #292

merged 7 commits into from
Sep 13, 2018

Conversation

Adman
Copy link
Contributor

@Adman Adman commented Jul 27, 2018

Updated fields of messages trezor/trezor-common#170

@prusnak
Copy link
Member

prusnak commented Jul 27, 2018

@Adman
Copy link
Contributor Author

Adman commented Jul 27, 2018

@prusnak fixed

@prusnak prusnak added this to the v2.0.8 milestone Jul 30, 2018
@tsusanka
Copy link
Contributor

@Adman could you add python-trezor support and some device tests along the way please? We have two types of tests:

  • unit tests, directly included in trezor-core/trezor-mcu repositories, which you've added so that's great
  • device tests (aka integration tests), which are part of the python-trezor repository and are "trezor model independent". In other words those are tests that can be run against both Trezor One and Trezor T. They actually send protobuf messages to the device and expect some other protobuf messages with results

Have a look on the Cardano's or Ripple's PR for example for some inspiration.

@Adman
Copy link
Contributor Author

Adman commented Aug 27, 2018

@tsusanka ok, I'll add python-trezor support and make a PR.

@Adman
Copy link
Contributor Author

Adman commented Aug 28, 2018

@tsusanka Can you give me a hint how to run device_tests in python-trezor?
When running python -m pytest -v from python-trezor/trezorlib/tests/device_tests folder it fails
StopIteration ERROR: could not load python-trezor/trezorlib/tests/device_tests/conftest.py.

unit_tests work properly.

@prusnak
Copy link
Member

prusnak commented Aug 28, 2018 via email

@Adman
Copy link
Contributor Author

Adman commented Aug 31, 2018

@tsusanka @prusnak Here's the python-trezor implementation: trezor/python-trezor#302

@tsusanka
Copy link
Contributor

tsusanka commented Sep 3, 2018

@tsusanka @prusnak Here's the python-trezor implementation: trezor/python-trezor#302

Awesome! We are currently in the process of finalizing our next release. When that's done we'll review your PR completely. Thanks

@tsusanka
Copy link
Contributor

tsusanka commented Sep 5, 2018

@Adman What do you think about supporting just one curve instead of all three? Supporting all three brings unnecessary complexity. We can add the other later but I believe in the first version of Tezos support we should use just one. You can choose (maybe do a bit of research what is used the most?) but I suggest secp256k or ed25519. What do you think?

@prusnak
Copy link
Member

prusnak commented Sep 5, 2018

As per agreement on Gitter, we'll support only Ed25519 curve = tz1. Relevant commit to trezor-common: trezor/trezor-common@b6f59d3

Please update this PR (and PR to python-trezor) to use only tz1/Ed25519 curve. Thank you!

@Adman
Copy link
Contributor Author

Adman commented Sep 7, 2018

@prusnak @tsusanka done.

@tsusanka
Copy link
Contributor

tsusanka commented Sep 7, 2018

@Adman please do a rebase (let me know if you need help), the tests should be passing then

@Adman
Copy link
Contributor Author

Adman commented Sep 7, 2018

@tsusanka rebased, but master is failing because of cardano's get_public_key and address tests https://travis-ci.org/trezor/trezor-core/jobs/425716279#L1550

@prusnak
Copy link
Member

prusnak commented Sep 7, 2018

You did not rebase the commits - you did merge. Rebase is being done with git rebase master. https://git-scm.com/docs/git-rebase

Be careful, this can be quite dangerous, but if there are no conflicts, it should be pretty straightforward.

@Adman
Copy link
Contributor Author

Adman commented Sep 7, 2018

@prusnak There were some conflicts, but I hope everything's fine now.

@tsusanka
Copy link
Contributor

tsusanka commented Sep 10, 2018

Now that python-trezor is merged, please do a rebase and set run_xfail = tezos in setup.cfg. That way we'll see the tests are passing. I'm working on a review, I'll publish it tonight/tomorrow.

Adrian Matejov added 5 commits September 10, 2018 18:19
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
@Adman
Copy link
Contributor Author

Adman commented Sep 10, 2018

@tsusanka Ok, I rebased and set run_xfail = tezos

Copy link
Contributor

@tsusanka tsusanka left a comment

Choose a reason for hiding this comment

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

Thanks for you PR, consider this as a preliminary review, it's mostly nit-picks. I've requested some changes and have a general question:

Do you plan to use the GetPublicKey functions in particular on your front-end side? Or did you implement them just to fit the others? Since ed25519 does not support non-hardened derivation, therefore no xpub functionality, I am not sure what the use case is. If we don't have any in mind, let's remove it.

node = await seed.derive_node(ctx, address_n, TEZOS_CURVE)

sk = node.private_key()
pk = ed25519.publickey(sk)
Copy link
Contributor

@tsusanka tsusanka Sep 10, 2018

Choose a reason for hiding this comment

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

Let's replace these two lines with:

pk = seed.remove_ed25519_prefix(node.public_key())

so it's consistent with stellar and others.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you mean seed.remove_ed25519_prefix(node.private_key()), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, no, you are right, sorry

Copy link
Member

@prusnak prusnak Sep 11, 2018

Choose a reason for hiding this comment

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

No, private_key does not have a prefix. node.public_key() is computed so you can use it.

sk = node.private_key()
pk = ed25519.publickey(sk)
pkh = hashlib.blake2b(pk, outlen=20).digest()
address = b58cencode(pkh, prefix=tezos_get_address_prefix(0))
Copy link
Contributor

@tsusanka tsusanka Sep 10, 2018

Choose a reason for hiding this comment

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

The zero in tezos_get_address_prefix(0) looks kind of magic. Maybe let's simplify this to b58cencode(pkh, prefix="tz1") or maybe better b58cencode(pkh, prefix=TEZOS_ED25519_PREFIX) ?

Also why is the function called b58cencode and not b58encode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's called b58cencode because it is "check_encode" (not pure "encode")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will call the prefix TEZOS_ED25519_ADDRESS_PREFIX

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not be afraid of long names and use base58_encode_check

node = await seed.derive_node(ctx, address_n, TEZOS_CURVE)

sk = node.private_key()
pk = ed25519.publickey(sk)
Copy link
Contributor

Choose a reason for hiding this comment

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

As above



def split_address(address):
return chunks(address, 18)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use common.layout.split_address. It has size=17, but that doesn't matter that much or will be refactored together later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I use 17, the address in transaction won't fit on display.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, keep it.


def _encode_common(operation, str_operation):
operation_tags = {"reveal": 7, "transaction": 8, "origination": 9, "delegation": 10}
result = ustruct.pack("<b", operation_tags[str_operation])
Copy link
Contributor

Choose a reason for hiding this comment

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

All those ustruct.pack are supposed to convert unsigned ints to bytes, aren't they? And the ints will be always from 0 to 255? I believe it should be <B, but one way or another let's refactor this to use app.common.writers. If you indeed want to serialize uint8 you can use write_uint8.



def _encode_contract_id(contract_id):
return ustruct.pack("<b", contract_id.tag) + contract_id.hash
Copy link
Contributor

Choose a reason for hiding this comment

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

For example this:

def _encode_contract_id(w: bytearray, contract_id):
    write_uint8(w, contract_id.tag)
    write_bytes(w, contract_id.hash)


opbytes = _get_operation_bytes(msg)

watermark = bytes([3])
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you maybe put a small comment why '3'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

else:
raise wire.DataError("Invalid operation")

opbytes = _get_operation_bytes(msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Have a look on Stellar, we usually declare a bytearray, which we then pass as an argument. I'm providing an example in one of the following comments.

@Adman
Copy link
Contributor Author

Adman commented Sep 11, 2018

@tsusanka Requested changes applied.

It would be cool if the common split_address function had optional parameter size, because using size=17 the address won't fit on display. I kept the function there for now.

GetPublicKey - @jurajselep will post a comment about it here.

@jurajselep
Copy link

jurajselep commented Sep 11, 2018

When we inject transaction, origination or delegation into Tezos node for first time, we need to "reveal" public_key.
http://tezos.gitlab.io/betanet/api/p2p.html?highlight=reveal

We are using GetPublicKey on front-end side for operation preapply and forge .

http://tezos.gitlab.io/betanet/api/rpc.html?highlight=reveal#post-block-id-helpers-forge-operations
http://tezos.gitlab.io/betanet/api/rpc.html?highlight=reveal#post-block-id-helpers-preapply-operations

{ 
 "kind": "reveal",
 "source": $contract_id,
 "fee": $mutez,
 "counter": $positive_bignum,
 "gas_limit": $positive_bignum,
 "storage_limit": $positive_bignum,
 "public_key": $Signature.Public_key 
}

pkh = hashlib.blake2b(pk, outlen=20).digest()
address = b58cencode(pkh, prefix=tezos_get_address_prefix(0))
address = b58cencode(pkh, prefix=TEZOS_ED25519_ADDRESS_PREFIX)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. Now we can use this also in sign_tx._get_address_by_tag and remove tezos_get_address_prefix completely.

@tsusanka
Copy link
Contributor

tsusanka commented Sep 11, 2018

@jurajselep thanks for the explanation, that's fine by me.

@Adman please note, that the tests are failing currently becuase of style

@Adman
Copy link
Contributor Author

Adman commented Sep 11, 2018

@tsusanka Tests should be passing, but travis build failed with message The command "curl -LO "https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"" failed and exited with 56.
I assume it's problem on travis side.

Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
@Adman
Copy link
Contributor Author

Adman commented Sep 11, 2018

@tsusanka Ok, I restarted the build and it is passing. If you have further comments on this PR I will fix them.

@tsusanka
Copy link
Contributor

tsusanka commented Sep 11, 2018

Great. Looks good to me. I'd like @jpochyla to have a final look later, but this is on a good way to be merged. Please be patient, he's busy with other things.

@jpochyla the only thing where I'm not sure is whether _encode_zarith could be simplified or moved to writers with a better name.

tACK dc6e405

@tsusanka tsusanka closed this Sep 11, 2018
@tsusanka tsusanka reopened this Sep 11, 2018
@tsusanka
Copy link
Contributor

tsusanka commented Sep 11, 2018

(grr, the closing was of course unintentional)

@tsusanka
Copy link
Contributor

Maybe one more thing. Could you add a brief README? Have a look on Ripple and feel free to omit the maintainer/reviewer fields. It doesn't have to be verbose, but some links to documentation/explorer etc. would be nice.

@Adman
Copy link
Contributor Author

Adman commented Sep 11, 2018

@tsusanka Readme added.

Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
@tsusanka tsusanka merged commit 7918e1f into trezor:master Sep 13, 2018
@tsusanka
Copy link
Contributor

Thank you!

@Adman
Copy link
Contributor Author

Adman commented Sep 13, 2018

Thanks all for the collaboration!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants