Skip to content

Commit

Permalink
Add UI for derive native script hash call
Browse files Browse the repository at this point in the history
  • Loading branch information
KubqoA committed Jul 27, 2021
1 parent 309a06e commit 9bf7143
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
94 changes: 89 additions & 5 deletions src/deriveNativeScriptHash.c
@@ -1,11 +1,17 @@
#include "deriveNativeScriptHash.h"
#include "state.h"
#include "uiScreens.h"

static ins_derive_native_script_hash_context_t* ctx = &(instructionState.deriveNativeScriptHashContext);

// Helper functions

#define TRACE_WITH_CTX(message, ...) TRACE(message "level = %u, remaining scripts = %u", ##__VA_ARGS__, ctx->level, ctx->complexScripts[ctx->level].remainingScripts);
#define SAFE_SPRINTF(str, format, ...) {\
explicit_bzero(str, SIZEOF(str));\
snprintf(str, SIZEOF(str), format, __VA_ARGS__);\
ASSERT(strlen(str) + 1 < SIZEOF(str));\
}

static void expectsMoreScripts()
{
Expand Down Expand Up @@ -34,12 +40,45 @@ static inline void complexScriptFinished()
TRACE_WITH_CTX("complex script finished, ");
}

// UI

static void deriveScriptHash_display_ui_callback() {}

// Start complex native script

void deriveNativeScriptHash_handleAllOrAny(read_view_t* view)
void deriveNativeScriptHash_handleAll(read_view_t* view)
{
VALIDATE(view_remainingSize(view) == 0, ERR_INVALID_DATA);
TRACE_WITH_CTX("");

// max possible length 26: "Confirm x scripts"
// where x is 2^32-1
char text[27];
SAFE_SPRINTF(text, "Confirm %d scripts", ctx->complexScripts[ctx->level].remainingScripts);

ui_displayPaginatedText(
// TODO: proper UI screen headers with numbered sections
"Script - ALL",
text,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handleAny(read_view_t* view)
{
VALIDATE(view_remainingSize(view) == 0, ERR_INVALID_DATA);
TRACE_WITH_CTX("");

// max possible length 26: "Confirm x scripts"
// where x is 2^32-1
char text[27];
SAFE_SPRINTF(text, "Confirm %d scripts", ctx->complexScripts[ctx->level].remainingScripts);

ui_displayPaginatedText(
"Script - ANY",
text,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handleNofK(read_view_t* view)
Expand All @@ -52,6 +91,17 @@ void deriveNativeScriptHash_handleNofK(read_view_t* view)

// validate that the received requiredScripts count makes sense
VALIDATE(ctx->complexScripts[ctx->level].remainingScripts >= requiredScripts, ERR_INVALID_DATA);

// max possible length 32: "Confirm x of x"
// where x is 2^32-1
char text[33];
SAFE_SPRINTF(text, "Confirm %d of %d", requiredScripts, ctx->complexScripts[ctx->level].remainingScripts);

ui_displayPaginatedText(
"Script - N of K",
text,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handleComplexScriptStart(read_view_t* view)
Expand All @@ -70,8 +120,8 @@ void deriveNativeScriptHash_handleComplexScriptStart(read_view_t* view)

switch(nativeScriptType) {
# define CASE(TYPE, HANDLER) case TYPE: ctx->complexScripts[ctx->level].remainingScripts = parse_u4be(view); ctx->complexScripts[ctx->level].type = TYPE; HANDLER(view); break;
CASE(NATIVE_SCRIPT_ALL, deriveNativeScriptHash_handleAllOrAny);
CASE(NATIVE_SCRIPT_ANY, deriveNativeScriptHash_handleAllOrAny);
CASE(NATIVE_SCRIPT_ALL, deriveNativeScriptHash_handleAll);
CASE(NATIVE_SCRIPT_ANY, deriveNativeScriptHash_handleAny);
CASE(NATIVE_SCRIPT_N_OF_K, deriveNativeScriptHash_handleNofK);
default:
THROW(ERR_INVALID_DATA);
Expand All @@ -92,6 +142,12 @@ void deriveNativeScriptHash_handleDeviceOwnedPubkey(read_view_t* view)
TRACE("pubkey given by path:");
BIP44_PRINTF(&path);
PRINTF("\n");

ui_displayPathScreen(
"Script - key path",
&path,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handleThirdPartyPubkey(read_view_t* view)
Expand All @@ -101,6 +157,14 @@ void deriveNativeScriptHash_handleThirdPartyPubkey(read_view_t* view)
view_memmove(pubkeyHash, view, ADDRESS_KEY_HASH_LENGTH);

TRACE_BUFFER(pubkeyHash, ADDRESS_KEY_HASH_LENGTH);

ui_displayBech32Screen(
"Script - key",
"pubkey",
pubkeyHash,
ADDRESS_KEY_HASH_LENGTH,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handlePubkey(read_view_t* view)
Expand Down Expand Up @@ -130,7 +194,12 @@ void deriveNativeScriptHash_handleInvalidBefore(read_view_t* view)

VALIDATE(view_remainingSize(view) == 0, ERR_INVALID_DATA);
TRACE_UINT64("invalid_before timelock", timelock);
UNUSED(timelock);

ui_displayUint64Screen(
"Script - invalid before",
timelock,
&deriveScriptHash_display_ui_callback
);
}

void deriveNativeScriptHash_handleInvalidHereafter(read_view_t* view)
Expand All @@ -139,7 +208,12 @@ void deriveNativeScriptHash_handleInvalidHereafter(read_view_t* view)

VALIDATE(view_remainingSize(view) == 0, ERR_INVALID_DATA);
TRACE_UINT64("invalid_hereafter timelock", timelock);
UNUSED(timelock);

ui_displayUint64Screen(
"Script - invalid hereafter",
timelock,
&deriveScriptHash_display_ui_callback
);
}

#undef TRACE_UINT64
Expand Down Expand Up @@ -179,6 +253,15 @@ void deriveNativeScriptHash_handleComplexScriptFinish(read_view_t* view)
// then this request is invalid in the current context
VALIDATE(ctx->complexScripts[ctx->level].remainingScripts == 0, ERR_INVALID_STATE);

switch(ctx->complexScripts[ctx->level].type) {
# define CASE(TYPE, HEADER) case TYPE: ui_displayPaginatedText(HEADER, "Finished", &deriveScriptHash_display_ui_callback); break;
CASE(NATIVE_SCRIPT_ALL, "Script - ALL");
CASE(NATIVE_SCRIPT_ANY, "Script - ANY");
CASE(NATIVE_SCRIPT_N_OF_K, "Script - N of K");
default: THROW(ERR_INVALID_STATE);
# undef CASE
}

complexScriptFinished();

io_send_buf(SUCCESS, NULL, 0);
Expand Down Expand Up @@ -255,4 +338,5 @@ void deriveNativeScriptHash_handleAPDU(
subhandler(&view);
}

#undef SAFE_SPRINTF
#undef TRACE_WITH_CTX
1 change: 1 addition & 0 deletions src/deriveNativeScriptHash.h
@@ -1,6 +1,7 @@
#ifndef H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH
#define H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH

#include "bip44.h"
#include "cardano.h"
#include "common.h"
#include "handlers.h"
Expand Down

0 comments on commit 9bf7143

Please sign in to comment.