1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-22 05:10:56 +00:00

firmware: refactor Stellar code; update trezor-common

This commit is contained in:
Pavol Rusnak 2018-06-11 19:13:34 +02:00
parent 3185eb0fd4
commit 217d5dd01d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
8 changed files with 87 additions and 78 deletions

View File

@ -81,6 +81,7 @@ void fsm_msgCosiCommit(CosiCommit *msg);
void fsm_msgCosiSign(CosiSign *msg);
// Stellar
void fsm_msgStellarGetAddress(StellarGetAddress *msg);
void fsm_msgStellarGetPublicKey(StellarGetPublicKey *msg);
void fsm_msgStellarSignTx(StellarSignTx *msg);
void fsm_msgStellarPaymentOp(StellarPaymentOp *msg);

View File

@ -1,3 +1,41 @@
void fsm_msgStellarGetAddress(StellarGetAddress *msg)
{
RESP_INIT(StellarAddress);
CHECK_INITIALIZED
CHECK_PIN
HDNode *node = stellar_deriveNode(msg->address_n, msg->address_n_count);
if (!node) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to derive private key"));
return;
}
if (msg->has_show_display && msg->show_display) {
const char **str_addr_rows = stellar_lineBreakAddress(node->public_key + 1);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Confirm"), _("Share public account ID?"),
str_addr_rows[0],
str_addr_rows[1],
str_addr_rows[2],
NULL,
NULL, NULL
);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
}
}
resp->has_address = true;
stellar_publicAddressAsStr(node->public_key + 1, resp->address, sizeof(resp->address));
msg_write(MessageType_MessageType_StellarAddress, resp);
layoutHome();
}
void fsm_msgStellarGetPublicKey(StellarGetPublicKey *msg)
{
RESP_INIT(StellarPublicKey);
@ -6,13 +44,34 @@ void fsm_msgStellarGetPublicKey(StellarGetPublicKey *msg)
CHECK_PIN
// Will exit if the user does not confirm
stellar_layoutGetPublicKey(msg->address_n, msg->address_n_count);
HDNode *node = stellar_deriveNode(msg->address_n, msg->address_n_count);
if (!node) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to derive private key"));
return;
}
if (msg->has_show_display && msg->show_display) {
char hex[32 * 2 + 1];
data2hex(node->public_key + 1, 32, hex);
const char **str_pubkey_rows = split_message((const uint8_t *)hex, 32 * 2, 16);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Confirm"), _("Share public account ID?"),
str_pubkey_rows[0],
str_pubkey_rows[1],
str_pubkey_rows[2],
str_pubkey_rows[3],
NULL, NULL
);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
}
}
// Read public key and write it to the response
resp->has_public_key = true;
resp->public_key.size = 32;
stellar_getPubkeyAtAddress(msg->address_n, msg->address_n_count, resp->public_key.bytes, sizeof(resp->public_key.bytes));
memcpy(resp->public_key.bytes, node->public_key + 1, 32);
msg_write(MessageType_MessageType_StellarPublicKey, resp);

View File

@ -153,7 +153,7 @@ static const char *address_n_str(const uint32_t *address_n, size_t address_n_cou
}
// split longer string into 4 rows, rowlen chars each
static const char **split_message(const uint8_t *msg, uint32_t len, uint32_t rowlen)
const char **split_message(const uint8_t *msg, uint32_t len, uint32_t rowlen)
{
static char str[4][32 + 1];
if (rowlen > 32) {

View File

@ -68,4 +68,6 @@ void layoutNEMLevy(const NEMMosaicDefinition *definition, uint8_t network);
void layoutCosiCommitSign(const uint32_t *address_n, size_t address_n_count, const uint8_t *data, uint32_t len, bool final_sign);
const char **split_message(const uint8_t *msg, uint32_t len, uint32_t rowlen);
#endif

View File

@ -177,6 +177,10 @@ CosiSignature.signature max_size:32
# Stellar
StellarGetAddress.address_n max_count:10
StellarAddress.address max_size:57
StellarGetPublicKey.address_n max_count:10
StellarPublicKey.public_key max_size:32
@ -242,6 +246,9 @@ LiskGetPublicKey skip_message:true
LiskAddress skip_message:true
LiskSignedTx skip_message:true
LiskPublicKey skip_message:true
LiskSignMessage skip_message:true
LiskMessageSignature skip_message:true
LiskVerifyMessage skip_message:true
# used only in debug firmware

View File

@ -45,32 +45,6 @@
static bool stellar_signing = false;
static StellarTransaction stellar_activeTx;
static const char **split_message(const uint8_t *msg, uint32_t len, uint32_t rowlen)
{
static char str[4][32 + 1];
if (rowlen > 32) {
rowlen = 32;
}
memset(str, 0, sizeof(str));
strlcpy(str[0], (char *)msg, rowlen + 1);
if (len > rowlen) {
strlcpy(str[1], (char *)msg + rowlen, rowlen + 1);
}
if (len > rowlen * 2) {
strlcpy(str[2], (char *)msg + rowlen * 2, rowlen + 1);
}
if (len > rowlen * 3) {
strlcpy(str[3], (char *)msg + rowlen * 3, rowlen + 1);
}
if (len > rowlen * 4) {
str[3][rowlen - 1] = '.';
str[3][rowlen - 2] = '.';
str[3][rowlen - 3] = '.';
}
static const char *ret[4] = { str[0], str[1], str[2], str[3] };
return ret;
}
/*
* Starts the signing process and parses the transaction header
*/
@ -100,16 +74,19 @@ void stellar_signingInit(StellarSignTx *msg)
stellar_hashupdate_bytes(tx_type_bytes, sizeof(tx_type_bytes));
// Public key comes from deriving the specified account path
uint8_t bytes_pubkey[32];
stellar_getPubkeyAtAddress(msg->address_n, msg->address_n_count, bytes_pubkey, sizeof(bytes_pubkey));
memcpy(&(stellar_activeTx.signing_pubkey), bytes_pubkey, sizeof(stellar_activeTx.signing_pubkey));
HDNode *node = stellar_deriveNode(msg->address_n, msg->address_n_count);
if (!node) {
// TODO: bail on error
return;
}
memcpy(&(stellar_activeTx.signing_pubkey), node->public_key + 1, sizeof(stellar_activeTx.signing_pubkey));
stellar_activeTx.address_n_count = msg->address_n_count;
// todo: fix sizeof check
memcpy(&(stellar_activeTx.address_n), &(msg->address_n), sizeof(stellar_activeTx.address_n));
// Hash: public key
stellar_hashupdate_address(bytes_pubkey);
stellar_hashupdate_address(node->public_key + 1);
// Hash: fee
stellar_hashupdate_uint32(msg->fee);
@ -1319,23 +1296,6 @@ uint16_t stellar_crc16(uint8_t *bytes, uint32_t length)
return crc & 0xffff;
}
/*
* Writes 32-byte public key to out
*/
void stellar_getPubkeyAtAddress(uint32_t *address_n, size_t address_n_count, uint8_t *out, size_t outlen)
{
if (outlen < 32) return;
HDNode *node = stellar_deriveNode(address_n, address_n_count);
if (node == 0) {
stellar_signingAbort();
return;
}
memcpy(out, node->public_key + 1, outlen);
}
/*
* Derives the HDNode at the given index
* Standard Stellar prefix is m/44'/148'/ and the default account is m/44'/148'/0'
@ -1637,8 +1597,11 @@ void stellar_layoutSigningDialog(const char *line1, const char *line2, const cha
int offset_y = 1;
int line_height = 9;
uint8_t public_key[32];
stellar_getPubkeyAtAddress(address_n, address_n_count, public_key, sizeof(public_key));
HDNode *node = stellar_deriveNode(address_n, address_n_count);
if (!node) {
// TODO: bail on error
return;
}
char str_pubaddr_truncated[12]; // G???? + null
memset(str_pubaddr_truncated, 0, sizeof(str_pubaddr_truncated));
@ -1650,7 +1613,7 @@ void stellar_layoutSigningDialog(const char *line1, const char *line2, const cha
// Load up public address
char str_pubaddr[56+1];
memset(str_pubaddr, 0, sizeof(str_pubaddr));
stellar_publicAddressAsStr(public_key, str_pubaddr, sizeof(str_pubaddr));
stellar_publicAddressAsStr(node->public_key + 1, str_pubaddr, sizeof(str_pubaddr));
memcpy(str_pubaddr_truncated, str_pubaddr, sizeof(str_pubaddr_truncated) - 1);
// Header
@ -1743,24 +1706,3 @@ void stellar_layoutTransactionDialog(const char *line1, const char *line2, const
false
);
}
void stellar_layoutGetPublicKey(uint32_t *address_n, size_t address_n_count)
{
// Derive node and calculate address
uint8_t pubkey_bytes[32];
stellar_getPubkeyAtAddress(address_n, address_n_count, pubkey_bytes, sizeof(pubkey_bytes));
const char **str_addr_rows = stellar_lineBreakAddress(pubkey_bytes);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Confirm"), _("Share public account ID?"),
str_addr_rows[0],
str_addr_rows[1],
str_addr_rows[2],
NULL,
NULL, NULL
);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
}
}

View File

@ -58,7 +58,6 @@ void stellar_confirmManageDataOp(StellarManageDataOp *msg);
void stellar_confirmBumpSequenceOp(StellarBumpSequenceOp *msg);
// Layout
void stellar_layoutGetPublicKey(uint32_t *address_n, size_t address_n_count);
void stellar_layoutTransactionDialog(const char *line1, const char *line2, const char *line3, const char *line4, const char *line5);
void stellar_layoutTransactionSummary(StellarSignTx *msg);
void stellar_layoutSigningDialog(const char *line1, const char *line2, const char *line3, const char *line4, const char *line5, uint32_t *address_n, size_t address_n_count, const char *warning, bool is_final_step);
@ -68,7 +67,6 @@ HDNode *stellar_deriveNode(uint32_t *address_n, size_t address_n_count);
size_t stellar_publicAddressAsStr(uint8_t *bytes, char *out, size_t outlen);
const char **stellar_lineBreakAddress(uint8_t *addrbytes);
void stellar_getPubkeyAtAddress(uint32_t *address_n, size_t address_n_count, uint8_t *out, size_t outlen);
void stellar_hashupdate_uint32(uint32_t value);
void stellar_hashupdate_uint64(uint64_t value);

@ -1 +1 @@
Subproject commit 018eebac7e64ed082486d746d78d279fe815c65d
Subproject commit 0bf60dbda9bd85db01ed36e5d9045a223744a35c