mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-23 06:48:16 +00:00
stellar: init; get public key
This commit is contained in:
parent
dca61a4c7f
commit
4cfcfb4ff4
@ -43,3 +43,8 @@ def derive_node_without_passphrase(
|
||||
node = bip32.from_seed(seed, curve_name)
|
||||
node.derive_path(path)
|
||||
return node
|
||||
|
||||
|
||||
def remove_ed25519_public_key_prefix(pubkey: bytes) -> bytes:
|
||||
# 0x01 prefix is not part of the actual public key, hence removed
|
||||
return pubkey[1:]
|
||||
|
11
src/apps/stellar/README.md
Normal file
11
src/apps/stellar/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Stellar
|
||||
|
||||
MAINTAINER = Tomas Susanka <tomas.susanka@satoshilabs.com>
|
||||
|
||||
AUTHOR = Tomas Susanka <tomas.susanka@satoshilabs.com>
|
||||
|
||||
REVIEWER = Jan Pochyla <jan.pochyla@satoshilabs.com>
|
||||
|
||||
-----
|
||||
|
||||
TODO
|
11
src/apps/stellar/__init__.py
Normal file
11
src/apps/stellar/__init__.py
Normal file
@ -0,0 +1,11 @@
|
||||
from trezor.wire import register, protobuf_workflow
|
||||
from trezor.messages.wire_types import StellarGetPublicKey
|
||||
|
||||
|
||||
def dispatch_StellarGetPublicKey(*args, **kwargs):
|
||||
from .get_public_key import get_public_key
|
||||
return get_public_key(*args, **kwargs)
|
||||
|
||||
|
||||
def boot():
|
||||
register(StellarGetPublicKey, protobuf_workflow, dispatch_StellarGetPublicKey)
|
74
src/apps/stellar/get_public_key.py
Normal file
74
src/apps/stellar/get_public_key.py
Normal file
@ -0,0 +1,74 @@
|
||||
from apps.common import seed
|
||||
from apps.common.confirm import confirm
|
||||
from trezor import ui
|
||||
from trezor.messages.StellarPublicKey import StellarPublicKey
|
||||
from trezor.messages.StellarGetPublicKey import StellarGetPublicKey
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.ui.text import Text
|
||||
from trezor.utils import chunks
|
||||
from trezor.crypto import base32
|
||||
import ustruct
|
||||
|
||||
STELLAR_CURVE = 'ed25519'
|
||||
|
||||
|
||||
async def get_public_key(ctx, msg: StellarGetPublicKey):
|
||||
node = await seed.derive_node(ctx, msg.address_n, STELLAR_CURVE)
|
||||
pubkey = seed.remove_ed25519_public_key_prefix(node.public_key()) # todo better?
|
||||
|
||||
while True:
|
||||
if await _show(ctx, _address_from_public_key(pubkey)):
|
||||
break
|
||||
|
||||
return StellarPublicKey(public_key=pubkey)
|
||||
|
||||
|
||||
async def _show(ctx, address: str):
|
||||
lines = _split_address(address)
|
||||
content = Text('Export Stellar ID', ui.ICON_RECEIVE,
|
||||
ui.NORMAL, 'Share public account ID?',
|
||||
ui.MONO, *lines,
|
||||
icon_color=ui.GREEN)
|
||||
|
||||
return await confirm(
|
||||
ctx,
|
||||
content,
|
||||
code=ButtonRequestType.Address,
|
||||
cancel_style=ui.BTN_KEY)
|
||||
|
||||
|
||||
def _split_address(address: str):
|
||||
return chunks(address, 17)
|
||||
|
||||
|
||||
def _address_from_public_key(pubkey: bytes):
|
||||
"""Returns the base32-encoded version of public key bytes (G...)"""
|
||||
|
||||
address = bytearray()
|
||||
address.append(6 << 3) # version -> 'G'
|
||||
address.extend(pubkey)
|
||||
address.extend(ustruct.pack("<H", _crc16_checksum(address))) # checksum
|
||||
|
||||
print(base32.encode(address))
|
||||
return base32.encode(address)
|
||||
|
||||
|
||||
def _crc16_checksum(data: bytearray):
|
||||
"""Returns the CRC-16 checksum of bytearray bytes
|
||||
|
||||
Ported from Java implementation at: http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java.html
|
||||
|
||||
Initial value changed to 0x0000 to match Stellar configuration.
|
||||
"""
|
||||
crc = 0x0000
|
||||
polynomial = 0x1021
|
||||
|
||||
for byte in data:
|
||||
for i in range(8):
|
||||
bit = ((byte >> (7 - i) & 1) == 1)
|
||||
c15 = ((crc >> 15 & 1) == 1)
|
||||
crc <<= 1
|
||||
if c15 ^ bit:
|
||||
crc ^= polynomial
|
||||
|
||||
return crc & 0xffff
|
@ -15,6 +15,7 @@ import apps.wallet
|
||||
import apps.ethereum
|
||||
import apps.lisk
|
||||
import apps.nem
|
||||
import apps.stellar
|
||||
|
||||
if __debug__:
|
||||
import apps.debug
|
||||
@ -28,6 +29,7 @@ apps.wallet.boot()
|
||||
apps.ethereum.boot()
|
||||
apps.lisk.boot()
|
||||
apps.nem.boot()
|
||||
apps.stellar.boot()
|
||||
if __debug__:
|
||||
apps.debug.boot()
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user