Skip to content

Commit

Permalink
fix(cardano): path not involved
Browse files Browse the repository at this point in the history
  • Loading branch information
soralit committed Oct 26, 2023
1 parent c8862a0 commit 9613892
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 61 deletions.
2 changes: 1 addition & 1 deletion rust/apps/cardano/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl ParsedCardanoTx {
index: utxo.index,
value: Some(utxo.value),
address: Some(address),
path: None,
path: Some(utxo.path.to_string()),
is_mine: utxo.master_fingerprint.eq(&context.master_fingerprint),
})
}
Expand Down
42 changes: 23 additions & 19 deletions rust/apps/cardano/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::errors::{CardanoError, R};
use crate::structs::{ParseContext, ParsedCardanoTx};
use alloc::string::ToString;
use alloc::vec::Vec;
use alloc::format;
use cardano_serialization_lib;
use cardano_serialization_lib::crypto::{Ed25519Signature, PublicKey, Vkey, Vkeywitness};
use third_party::cryptoxide::hashing::blake2b_256;
Expand All @@ -26,6 +27,11 @@ pub fn sign_tx(
let hash = blake2b_256(cardano_tx.body().to_bytes().as_ref());
let mut witness_set = cardano_serialization_lib::TransactionWitnessSet::new();
let mut vkeys = cardano_serialization_lib::crypto::Vkeywitnesses::new();
rust_tools::debug!(format!("before generate key"));
let icarus_master_key = keystore::algorithms::ed25519::bip32_ed25519::get_icarus_master_key_by_entropy(
entropy, passphrase,
).map_err(|e| CardanoError::SigningFailed(e.to_string()))?;
rust_tools::debug!(format!("after generate key"));
let mut utxo_signatures: Vec<([u8; 32], [u8; 64])> = context
.get_utxos()
.iter()
Expand All @@ -35,20 +41,18 @@ pub fn sign_tx(
})
.map(|v| {
let pubkey =
keystore::algorithms::ed25519::bip32_ed25519::get_extended_public_key_by_entropy(
entropy,
passphrase,
keystore::algorithms::ed25519::bip32_ed25519::derive_extended_pubkey_by_xprv(
&icarus_master_key,
&v.get_path().to_string(),
)
.map(|v| v.public_key())
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
let signature = keystore::algorithms::ed25519::bip32_ed25519::sign_message_by_entropy(
entropy,
passphrase,
.map(|v| v.public_key())
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
let signature = keystore::algorithms::ed25519::bip32_ed25519::sign_message_by_xprv(
&icarus_master_key,
&hash,
&v.get_path().to_string(),
)
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
pubkey.and_then(|_pubkey| signature.map(|_signature| (_pubkey, _signature)))
})
.collect::<R<Vec<([u8; 32], [u8; 64])>>>()?;
Expand All @@ -61,20 +65,18 @@ pub fn sign_tx(
})
.map(|v| {
let pubkey =
keystore::algorithms::ed25519::bip32_ed25519::get_extended_public_key_by_entropy(
entropy,
passphrase,
keystore::algorithms::ed25519::bip32_ed25519::derive_extended_pubkey_by_xprv(
&icarus_master_key,
&v.get_path().to_string(),
)
.map(|v| v.public_key())
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
let signature = keystore::algorithms::ed25519::bip32_ed25519::sign_message_by_entropy(
entropy,
passphrase,
.map(|v| v.public_key())
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
let signature = keystore::algorithms::ed25519::bip32_ed25519::sign_message_by_xprv(
&icarus_master_key,
&hash,
&v.get_path().to_string(),
)
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
.map_err(|e| CardanoError::SigningFailed(e.to_string()));
pubkey.and_then(|v| signature.map(|_v| (v, _v)))
})
.collect::<R<Vec<([u8; 32], [u8; 64])>>>()?;
Expand All @@ -90,7 +92,7 @@ pub fn sign_tx(
);
vkeys.add(&v);
}

rust_tools::debug!(format!("after sign"));
witness_set.set_vkeys(&vkeys);

Ok(witness_set.to_bytes())
Expand All @@ -99,7 +101,9 @@ pub fn sign_tx(
#[cfg(test)]
mod test {
use cardano_serialization_lib::Transaction;

extern crate std;

use std::println;

#[test]
Expand Down
2 changes: 0 additions & 2 deletions rust/keystore/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions rust/keystore/src/algorithms/ed25519/bip32_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ pub fn sign_message_by_icarus_master_key(
Ok(*sig.to_bytes())
}

pub fn sign_message_by_xprv(
xprv: &XPrv,
message: &[u8],
path: &String,
) -> Result<[u8; 64]> {
let xprv = derive_extended_privkey_by_xprv(xprv, path)?;
let sig = xprv.sign::<Vec<u8>>(message);
Ok(*sig.to_bytes())
}

pub fn derive_extended_pubkey_by_icarus_master_key(
master_key: &[u8],
path: &String,
Expand All @@ -82,6 +92,14 @@ pub fn derive_extended_pubkey_by_icarus_master_key(
Ok(privkey.public())
}

pub fn derive_extended_pubkey_by_xprv(
xprv: &XPrv,
path: &String,
) -> Result<XPub> {
let privkey = derive_extended_privkey_by_xprv(xprv, path)?;
Ok(privkey.public())
}

pub fn derive_extended_privkey_by_icarus_master_key(
master_key: &[u8],
path: &String,
Expand All @@ -91,6 +109,13 @@ pub fn derive_extended_privkey_by_icarus_master_key(
derive_bip32_ed25519_privkey(xprv, path)
}

pub fn derive_extended_privkey_by_xprv(
xprv: &XPrv,
path: &String,
) -> Result<XPrv> {
derive_bip32_ed25519_privkey(xprv.clone(), path)
}

fn derive_bip32_ed25519_privkey(root: XPrv, path: &String) -> Result<XPrv> {
let path = normalize_path(path);
let derivation_path = DerivationPath::from_str(path.as_str())
Expand Down
7 changes: 0 additions & 7 deletions src/tasks/qrdecode_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,10 @@ void handleURResult(void *urResult, UrViewType_t urViewType, bool is_multi)
break;
}

printf("sora %d\r\n", urViewType.viewType);
printf("sora %d\r\n", urViewType.urType);
printf("sora %d\r\n", viewType);


if (urViewType.viewType == WebAuthResult || urViewType.viewType == KeyDerivationRequest || viewType != REMAPVIEW_BUTT) {
printf("sora here\r\n");
StopQrDecode();
UserDelay(500);
GuiApiEmitSignal(SIG_QRCODE_VIEW_SCAN_PASS, &urViewType, sizeof(urViewType));
printf("sora here 2\r\n");
} else {
printf("unhandled viewType=%d\r\n", urViewType.viewType);
}
Expand Down
4 changes: 0 additions & 4 deletions src/ui/gui_analyze/gui_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,6 @@ static void *GuiWidgetFactoryCreate(lv_obj_t *parent, cJSON *json)
{
lv_obj_t *obj = NULL;
cJSON *item = cJSON_GetObjectItem(json, "type");
printf("sora: here \r\n");
if (item == NULL)
{
item = cJSON_GetObjectItem(json, "table");
Expand Down Expand Up @@ -1569,8 +1568,6 @@ static void *GuiWidgetFactoryCreate(lv_obj_t *parent, cJSON *json)
}
}

printf("sora: here2 \r\n");

if (0 == strcmp(type, "list"))
{
GuiWidgetList(parent, json);
Expand Down Expand Up @@ -1786,7 +1783,6 @@ void *GuiTemplateReload(lv_obj_t *parent, uint8_t index)
g_tableView = NULL;
g_analyzeTabview.tabviewIndex = 0;
g_reMapIndex = ViewTypeReMap(index);
printf("sora g_reMapIndex: %d\r\n", g_reMapIndex);
if (g_reMapIndex == REMAPVIEW_BUTT)
{
return NULL;
Expand Down
26 changes: 14 additions & 12 deletions src/ui/gui_chain/gui_ada.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
#include "secret_cache.h"
#include "assert.h"
#include "gui_ada.h"
#include "gui_hintbox.h"
#include "gui.h"

static bool g_isMulti = false;
static void *g_urResult = NULL;
static void *g_parseResult = NULL;
static char *xpub = NULL;
static lv_obj_t *g_waitAnimCont = NULL;

static uint8_t GetXPubIndexByPath(char *path);

Expand Down Expand Up @@ -180,14 +183,12 @@ void *GetAdaInputDetail(uint8_t *row, uint8_t *col, void *param)
*col = 1;
*row = 3 * tx->from->size;
int i = 0, j = 0;
printf("sora here: \r\n");
char ***indata = (char ***)malloc(sizeof(char **) * *col);
for (i = 0; i < *col; i++)
{
indata[i] = malloc(sizeof(char *) * *row);
for (j = 0; j < *row; j++)
{
printf("sora row: %d\r\n", j);
uint32_t index = j / 3;
indata[i][j] = malloc(128);
if (j % 3 == 0)
Expand All @@ -202,7 +203,6 @@ void *GetAdaInputDetail(uint8_t *row, uint8_t *col, void *param)
{
if (tx->from->data[index].has_path)
{
printf("path: %s\r\n", tx->from->data[index].path);
strcpy(indata[i][j], tx->from->data[index].path);
}
}
Expand All @@ -223,7 +223,6 @@ void *GetAdaOutputDetail(uint8_t *row, uint8_t *col, void *param)
for (j = 0; j < *row; j++)
{
uint32_t index = j / 2;
printf("sora row: %d\r\n", j);
indata[i][j] = malloc(128);
if (j % 2 == 0)
{
Expand Down Expand Up @@ -275,7 +274,6 @@ void *GetAdaCertificatesData(uint8_t *row, uint8_t *col, void *param)
for (j = 0; j < *row; j++)
{
uint32_t index = j / 3;
printf("sora row: %d\r\n", j);
indata[i][j] = malloc(128);
if (j % 3 == 0)
{
Expand Down Expand Up @@ -312,7 +310,6 @@ void GetAdaWithdrawalsSize(uint16_t *width, uint16_t *height, void *param)

void *GetAdaWithdrawalsData(uint8_t *row, uint8_t *col, void *param)
{
printf("sora: GetAdaWithdrawalsData\r\n");
DisplayCardanoTx *tx = (DisplayCardanoTx *)param;
*col = 1;
*row = 2 * tx->withdrawals->size;
Expand All @@ -324,7 +321,6 @@ void *GetAdaWithdrawalsData(uint8_t *row, uint8_t *col, void *param)
for (j = 0; j < *row; j++)
{
uint32_t index = j / 2;
printf("sora row: %d\r\n", j);
indata[i][j] = malloc(128);
if (j % 2 == 0)
{
Expand All @@ -341,13 +337,22 @@ void *GetAdaWithdrawalsData(uint8_t *row, uint8_t *col, void *param)

UREncodeResult *GuiGetAdaSignQrCodeData(void)
{
printf("GuiGetAdaSignQrCodeData\r\n");
bool enable = IsPreviousLockScreenEnable();
SetLockScreen(false);
#ifndef COMPILE_SIMULATOR
GUI_DEL_OBJ(g_waitAnimCont);

g_waitAnimCont = GuiCreateAnimHintBox(lv_scr_act(), 480, 386, 82);
lv_obj_t *title = GuiCreateTextLabel(g_waitAnimCont, _("sign_transaction"));
lv_obj_align(title, LV_ALIGN_BOTTOM_MID, 0, -194);
lv_obj_t *desc = GuiCreateNoticeLabel(g_waitAnimCont, _("sign_transaction_desc"));
lv_obj_align(desc, LV_ALIGN_BOTTOM_MID, 0, -86);
lv_obj_set_style_text_align(desc, LV_TEXT_ALIGN_CENTER, 0);

UREncodeResult *encodeResult;
uint8_t mfp[4];
GetMasterFingerPrint(mfp);

void *data = g_isMulti ? ((URParseMultiResult *)g_urResult)->data : ((URParseResult *)g_urResult)->data;
do
{
Expand All @@ -357,15 +362,12 @@ UREncodeResult *GuiGetAdaSignQrCodeData(void)
char *path = cardano_get_path(data);
char pubkeyIndex = GetXPubIndexByPath(path);
char *pubKey = GetCurrentAccountPublicKey(pubkeyIndex);
printf("GuiGetAdaSignQrCodeData before sign\r\n");
encodeResult = cardano_sign_tx(data, mfp, xpub, entropy, len, GetPassphrase(GetCurrentAccountIndex()));
printf("GuiGetAdaSignQrCodeData after sign\r\n");
printf("GuiGetAdaSignQrCodeData, result: %d\r\n", encodeResult->error_code);
printf("GuiGetAdaSignQrCodeData, result: %s\r\n", encodeResult->error_message);
ClearSecretCache();
CHECK_CHAIN_BREAK(encodeResult);
} while (0);
SetLockScreen(enable);
GUI_DEL_OBJ(g_waitAnimCont);
return encodeResult;
#else
UREncodeResult *encodeResult = NULL;
Expand Down
32 changes: 16 additions & 16 deletions src/ui/gui_widgets/gui_qrcode_widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ void GuiQrCodeScanResult(bool result, void *param)
UrViewType_t urViewType = *(UrViewType_t *)param;
g_qrcodeViewType = urViewType.viewType;
g_chainType = ViewTypeToChainTypeSwitch(g_qrcodeViewType);
printf("sora: %d\r\n", g_qrcodeViewType);
// Not a chain based transaction, e.g. WebAuth
if (g_chainType == CHAIN_BUTT)
{
Expand Down Expand Up @@ -397,6 +396,21 @@ void GuiQrCodeShowQrMessage(lv_obj_t *parent)
GuiFullscreenModeInit(480, 800, WHITE_COLOR);
GuiFullscreenModeCreateObject(GuiCreateQRCode, 420, 420);

lv_obj_align(qrcode, LV_ALIGN_TOP_MID, 0, 36);
lv_obj_t *label = GuiCreateNoticeLabel(cont, _("transaction_parse_scan_by_software"));
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 576 - GUI_MAIN_AREA_OFFSET);

lv_obj_t *btn = GuiCreateBtn(cont, _("Done"));
lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -24);
lv_obj_set_size(btn, 408, 66);
lv_obj_add_event_cb(btn, CloseTimerCurrentViewHandler, LV_EVENT_CLICKED, NULL);

uint8_t chainType = ViewTypeToChainTypeSwitch(g_qrcodeViewType);
if (g_qrcodeViewType == EthPersonalMessage || g_qrcodeViewType == EthTypedData)
{
SetCoinWallet(g_pageWidget->navBarWidget, chainType, _("transaction_parse_broadcast_message"));
}

char *data = NULL;
switch (g_qrcodeViewType)
{
Expand Down Expand Up @@ -444,21 +458,6 @@ void GuiQrCodeShowQrMessage(lv_obj_t *parent)
}
break;
}

lv_obj_align(qrcode, LV_ALIGN_TOP_MID, 0, 36);
lv_obj_t *label = GuiCreateNoticeLabel(cont, _("transaction_parse_scan_by_software"));
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 576 - GUI_MAIN_AREA_OFFSET);

lv_obj_t *btn = GuiCreateBtn(cont, _("Done"));
lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -24);
lv_obj_set_size(btn, 408, 66);
lv_obj_add_event_cb(btn, CloseTimerCurrentViewHandler, LV_EVENT_CLICKED, NULL);

uint8_t chainType = ViewTypeToChainTypeSwitch(g_qrcodeViewType);
if (g_qrcodeViewType == EthPersonalMessage || g_qrcodeViewType == EthTypedData)
{
SetCoinWallet(g_pageWidget->navBarWidget, chainType, _("transaction_parse_broadcast_message"));
}
}

void GuiQrCodeVerifyPasswordSuccess(void)
Expand All @@ -467,6 +466,7 @@ void GuiQrCodeVerifyPasswordSuccess(void)
GUI_DEL_OBJ(g_fingerSingContainer)
GUI_DEL_OBJ(g_scanErrorHintBox)
g_qrCodeWidgetView.analysis = NULL;
GUI_DEL_OBJ(g_keyboardWidget->keyboardHintBox);
GuiDeleteKeyboardWidget(g_keyboardWidget);
GuiQrCodeShowQrMessage(g_qrCodeWidgetView.cont);
}
Expand Down

0 comments on commit 9613892

Please sign in to comment.