mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 01:18:28 +00:00
tezos: supporting only ed25519 curve
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
This commit is contained in:
parent
e009c0ca22
commit
203c850779
@ -1,26 +1,8 @@
|
||||
from trezor.messages.MessageType import TezosGetAddress, TezosGetPublicKey, TezosSignTx
|
||||
from trezor.wire import protobuf_workflow, register
|
||||
|
||||
|
||||
def dispatch_TezosGetAddress(*args, **kwargs):
|
||||
from .get_address import tezos_get_address
|
||||
|
||||
return tezos_get_address(*args, **kwargs)
|
||||
|
||||
|
||||
def dispatch_TezosSignTx(*args, **kwargs):
|
||||
from .sign_tx import tezos_sign_tx
|
||||
|
||||
return tezos_sign_tx(*args, **kwargs)
|
||||
|
||||
|
||||
def dispatch_TezosGetPublicKey(*args, **kwargs):
|
||||
from .get_public_key import tezos_get_public_key
|
||||
|
||||
return tezos_get_public_key(*args, **kwargs)
|
||||
from trezor import wire
|
||||
from trezor.messages import MessageType
|
||||
|
||||
|
||||
def boot():
|
||||
register(TezosGetAddress, protobuf_workflow, dispatch_TezosGetAddress)
|
||||
register(TezosSignTx, protobuf_workflow, dispatch_TezosSignTx)
|
||||
register(TezosGetPublicKey, protobuf_workflow, dispatch_TezosGetPublicKey)
|
||||
wire.add(MessageType.TezosGetAddress, __name__, "get_address")
|
||||
wire.add(MessageType.TezosSignTx, __name__, "sign_tx")
|
||||
wire.add(MessageType.TezosGetPublicKey, __name__, "get_public_key")
|
||||
|
@ -1,25 +1,20 @@
|
||||
from trezor.crypto import hashlib
|
||||
from trezor.crypto.curve import ed25519
|
||||
from trezor.messages.TezosAddress import TezosAddress
|
||||
|
||||
from apps.common import seed
|
||||
from apps.common.show import show_address, show_qr
|
||||
from apps.tezos.helpers import (
|
||||
b58cencode,
|
||||
get_address_prefix,
|
||||
get_curve_module,
|
||||
get_curve_name,
|
||||
)
|
||||
from apps.common.layout import show_address, show_qr
|
||||
from apps.tezos.helpers import TEZOS_CURVE, b58cencode, tezos_get_address_prefix
|
||||
|
||||
|
||||
async def tezos_get_address(ctx, msg):
|
||||
async def get_address(ctx, msg):
|
||||
address_n = msg.address_n or ()
|
||||
curve = msg.curve or 0
|
||||
node = await seed.derive_node(ctx, address_n, get_curve_name(curve))
|
||||
node = await seed.derive_node(ctx, address_n, TEZOS_CURVE)
|
||||
|
||||
sk = node.private_key()
|
||||
pk = get_curve_module(curve).publickey(sk)
|
||||
pk = ed25519.publickey(sk)
|
||||
pkh = hashlib.blake2b(pk, outlen=20).digest()
|
||||
address = b58cencode(pkh, prefix=get_address_prefix(curve))
|
||||
address = b58cencode(pkh, prefix=tezos_get_address_prefix(0))
|
||||
|
||||
if msg.show_display:
|
||||
while True:
|
||||
|
@ -1,4 +1,5 @@
|
||||
from trezor import ui
|
||||
from trezor.crypto.curve import ed25519
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.messages.TezosPublicKey import TezosPublicKey
|
||||
from trezor.ui.text import Text
|
||||
@ -6,22 +7,16 @@ from trezor.utils import chunks
|
||||
|
||||
from apps.common import seed
|
||||
from apps.common.confirm import require_confirm
|
||||
from apps.tezos.helpers import (
|
||||
b58cencode,
|
||||
get_curve_module,
|
||||
get_curve_name,
|
||||
get_pk_prefix,
|
||||
)
|
||||
from apps.tezos.helpers import TEZOS_CURVE, TEZOS_PUBLICKEY_PREFIX, b58cencode
|
||||
|
||||
|
||||
async def tezos_get_public_key(ctx, msg):
|
||||
async def get_public_key(ctx, msg):
|
||||
address_n = msg.address_n or ()
|
||||
curve = msg.curve or 0
|
||||
node = await seed.derive_node(ctx, address_n, get_curve_name(curve))
|
||||
node = await seed.derive_node(ctx, address_n, TEZOS_CURVE)
|
||||
|
||||
sk = node.private_key()
|
||||
pk = get_curve_module(curve).publickey(sk)
|
||||
pk_prefixed = b58cencode(pk, prefix=get_pk_prefix(curve))
|
||||
pk = ed25519.publickey(sk)
|
||||
pk_prefixed = b58cencode(pk, prefix=TEZOS_PUBLICKEY_PREFIX)
|
||||
|
||||
if msg.show_display:
|
||||
await _show_tezos_pubkey(ctx, pk_prefixed)
|
||||
|
@ -2,11 +2,13 @@ from micropython import const
|
||||
|
||||
from trezor import wire
|
||||
from trezor.crypto import base58
|
||||
from trezor.crypto.curve import ed25519, nist256p1, secp256k1
|
||||
|
||||
TEZOS_CURVE = "ed25519"
|
||||
TEZOS_AMOUNT_DIVISIBILITY = const(6)
|
||||
|
||||
PREFIXES = {
|
||||
TEZOS_ORIGINATED_ADDRESS_PREFIX = "KT1"
|
||||
TEZOS_PUBLICKEY_PREFIX = "edpk"
|
||||
TEZOS_SIGNATURE_PREFIX = "edsig"
|
||||
TEZOS_PREFIX_BYTES = {
|
||||
# addresses
|
||||
"tz1": [6, 161, 159],
|
||||
"tz2": [6, 161, 161],
|
||||
@ -14,79 +16,29 @@ PREFIXES = {
|
||||
"KT1": [2, 90, 121],
|
||||
# public keys
|
||||
"edpk": [13, 15, 37, 217],
|
||||
"sppk": [3, 254, 226, 86],
|
||||
"p2pk": [3, 178, 139, 127],
|
||||
# signatures
|
||||
"edsig": [9, 245, 205, 134, 18],
|
||||
"spsig1": [13, 115, 101, 19, 63],
|
||||
"p2sig": [54, 240, 44, 52],
|
||||
# operation hash
|
||||
"o": [5, 116],
|
||||
}
|
||||
|
||||
TEZOS_CURVES = [
|
||||
{
|
||||
"name": "ed25519",
|
||||
"address_prefix": "tz1",
|
||||
"pk_prefix": "edpk",
|
||||
"sig_prefix": "edsig",
|
||||
"sig_remove_first_byte": False,
|
||||
"module": ed25519,
|
||||
},
|
||||
{
|
||||
"name": "secp256k1",
|
||||
"address_prefix": "tz2",
|
||||
"pk_prefix": "sppk",
|
||||
"sig_prefix": "spsig1",
|
||||
"sig_remove_first_byte": True,
|
||||
"module": secp256k1,
|
||||
},
|
||||
{
|
||||
"name": "nist256p1",
|
||||
"address_prefix": "tz3",
|
||||
"pk_prefix": "p2pk",
|
||||
"sig_prefix": "p2sig",
|
||||
"sig_remove_first_byte": True,
|
||||
"module": nist256p1,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def get_curve_name(index):
|
||||
if 0 <= index < len(TEZOS_CURVES):
|
||||
return TEZOS_CURVES[index]["name"]
|
||||
raise wire.DataError("Invalid type of curve")
|
||||
|
||||
|
||||
def get_curve_module(curve):
|
||||
return TEZOS_CURVES[curve]["module"]
|
||||
|
||||
|
||||
def get_address_prefix(curve):
|
||||
return TEZOS_CURVES[curve]["address_prefix"]
|
||||
|
||||
|
||||
def get_pk_prefix(curve):
|
||||
return TEZOS_CURVES[curve]["pk_prefix"]
|
||||
|
||||
|
||||
def get_sig_prefix(curve):
|
||||
return TEZOS_CURVES[curve]["sig_prefix"]
|
||||
|
||||
|
||||
def check_sig_remove_first_byte(curve):
|
||||
return TEZOS_CURVES[curve]["sig_remove_first_byte"]
|
||||
def tezos_get_address_prefix(tag):
|
||||
prefixes = ["tz1", "tz2", "tz3"]
|
||||
if 0 <= tag < len(prefixes):
|
||||
return prefixes[tag]
|
||||
raise wire.DataError("Invalid tag in address hash")
|
||||
|
||||
|
||||
def b58cencode(payload, prefix=None):
|
||||
result = payload
|
||||
if prefix is not None:
|
||||
result = bytes(PREFIXES[prefix]) + payload
|
||||
result = bytes(TEZOS_PREFIX_BYTES[prefix]) + payload
|
||||
return base58.encode_check(result)
|
||||
|
||||
|
||||
def b58cdecode(enc, prefix=None):
|
||||
decoded = base58.decode_check(enc)
|
||||
if prefix is not None:
|
||||
decoded = decoded[len(PREFIXES[prefix]) :]
|
||||
decoded = decoded[len(TEZOS_PREFIX_BYTES[prefix]) :]
|
||||
return decoded
|
||||
|
@ -2,26 +2,24 @@ import ustruct
|
||||
|
||||
from trezor import wire
|
||||
from trezor.crypto import hashlib
|
||||
from trezor.crypto.curve import ed25519
|
||||
from trezor.messages import TezosContractType
|
||||
from trezor.messages.TezosSignedTx import TezosSignedTx
|
||||
|
||||
from apps.common import seed
|
||||
from apps.tezos.helpers import (
|
||||
TEZOS_CURVE,
|
||||
TEZOS_ORIGINATED_ADDRESS_PREFIX,
|
||||
TEZOS_SIGNATURE_PREFIX,
|
||||
b58cencode,
|
||||
check_sig_remove_first_byte,
|
||||
get_address_prefix,
|
||||
get_curve_module,
|
||||
get_curve_name,
|
||||
get_sig_prefix,
|
||||
tezos_get_address_prefix,
|
||||
)
|
||||
from apps.tezos.layout import *
|
||||
|
||||
|
||||
async def tezos_sign_tx(ctx, msg):
|
||||
async def sign_tx(ctx, msg):
|
||||
address_n = msg.address_n or ()
|
||||
curve = msg.curve or 0
|
||||
|
||||
node = await seed.derive_node(ctx, address_n, get_curve_name(curve))
|
||||
node = await seed.derive_node(ctx, address_n, TEZOS_CURVE)
|
||||
|
||||
if msg.transaction is not None:
|
||||
to = _get_address_from_contract(msg.transaction.destination)
|
||||
@ -37,9 +35,12 @@ async def tezos_sign_tx(ctx, msg):
|
||||
|
||||
elif msg.delegation is not None:
|
||||
source = _get_address_from_contract(msg.delegation.source)
|
||||
delegate = _get_address_by_tag(msg.delegation.delegate)
|
||||
|
||||
if source != delegate:
|
||||
delegate = None
|
||||
if msg.delegation.delegate is not None:
|
||||
delegate = _get_address_by_tag(msg.delegation.delegate)
|
||||
|
||||
if delegate is not None and source != delegate:
|
||||
await require_confirm_delegation_baker(ctx, delegate)
|
||||
await require_confirm_set_delegate(ctx, msg.delegation.fee)
|
||||
# if account registers itself as a delegate
|
||||
@ -55,17 +56,13 @@ async def tezos_sign_tx(ctx, msg):
|
||||
wm_opbytes = watermark + opbytes
|
||||
wm_opbytes_hash = hashlib.blake2b(wm_opbytes, outlen=32).digest()
|
||||
|
||||
curve_module = get_curve_module(curve)
|
||||
signature = curve_module.sign(node.private_key(), wm_opbytes_hash)
|
||||
|
||||
if check_sig_remove_first_byte(curve):
|
||||
signature = signature[1:]
|
||||
signature = ed25519.sign(node.private_key(), wm_opbytes_hash)
|
||||
|
||||
sig_op_contents = opbytes + signature
|
||||
sig_op_contents_hash = hashlib.blake2b(sig_op_contents, outlen=32).digest()
|
||||
ophash = b58cencode(sig_op_contents_hash, prefix="o")
|
||||
|
||||
sig_prefixed = b58cencode(signature, prefix=get_sig_prefix(curve))
|
||||
sig_prefixed = b58cencode(signature, prefix=TEZOS_SIGNATURE_PREFIX)
|
||||
|
||||
return TezosSignedTx(
|
||||
signature=sig_prefixed, sig_op_contents=sig_op_contents, operation_hash=ophash
|
||||
@ -74,7 +71,7 @@ async def tezos_sign_tx(ctx, msg):
|
||||
|
||||
def _get_address_by_tag(address_hash):
|
||||
tag = int(address_hash[0])
|
||||
return b58cencode(address_hash[1:], prefix=get_address_prefix(tag))
|
||||
return b58cencode(address_hash[1:], prefix=tezos_get_address_prefix(tag))
|
||||
|
||||
|
||||
def _get_address_from_contract(address):
|
||||
@ -82,7 +79,7 @@ def _get_address_from_contract(address):
|
||||
return _get_address_by_tag(address.hash)
|
||||
|
||||
elif address.tag == TezosContractType.Originated:
|
||||
return b58cencode(address.hash[:-1], prefix="KT1")
|
||||
return b58cencode(address.hash[:-1], prefix=TEZOS_ORIGINATED_ADDRESS_PREFIX)
|
||||
|
||||
raise wire.DataError("Invalid contract type")
|
||||
|
||||
|
@ -1,5 +0,0 @@
|
||||
# Automatically generated by pb2py
|
||||
# fmt: off
|
||||
Ed25519 = 0
|
||||
Secp256k1 = 1
|
||||
P256 = 2
|
@ -13,16 +13,13 @@ class TezosGetAddress(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 150
|
||||
FIELDS = {
|
||||
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),
|
||||
2: ('curve', p.UVarintType, 0), # default=Ed25519
|
||||
3: ('show_display', p.BoolType, 0),
|
||||
2: ('show_display', p.BoolType, 0),
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
address_n: List[int] = None,
|
||||
curve: int = None,
|
||||
show_display: bool = None,
|
||||
) -> None:
|
||||
self.address_n = address_n if address_n is not None else []
|
||||
self.curve = curve
|
||||
self.show_display = show_display
|
||||
|
@ -13,16 +13,13 @@ class TezosGetPublicKey(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 154
|
||||
FIELDS = {
|
||||
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),
|
||||
2: ('curve', p.UVarintType, 0), # default=Ed25519
|
||||
3: ('show_display', p.BoolType, 0),
|
||||
2: ('show_display', p.BoolType, 0),
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
address_n: List[int] = None,
|
||||
curve: int = None,
|
||||
show_display: bool = None,
|
||||
) -> None:
|
||||
self.address_n = address_n if address_n is not None else []
|
||||
self.curve = curve
|
||||
self.show_display = show_display
|
||||
|
Loading…
Reference in New Issue
Block a user