forked from Concordium/concordium-ledger-app
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/LDG-537--nano-app-implement-initialize-method #40
Merged
n4l5u0r
merged 8 commits into
main
from
feat/LDG-537--nano-app-implement-initialize-method
Dec 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e619bb
test(init_contract): implement test for the method init contract
keiff3r 8acba8c
feat(init_contract): implement init contract instruction and tests
keiff3r 226a8e2
fix(test): correct a typo in the init_contract method
keiff3r e54a545
chore: remove debug prints
keiff3r db3d151
fix(test): use less than the max supported amount
keiff3r 0619980
fix(init_contract): increase amount display buffer size
keiff3r b413ab5
fix(test): adjust chunk size calculation for APDU length limits
keiff3r ee776eb
test(init_contract): add a test with long name and params
keiff3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "os.h" | ||
#include "format.h" | ||
#include "common/ui/display.h" | ||
#include "common/responseCodes.h" | ||
#include "common/sign.h" | ||
#include "common/util.h" | ||
#include "initContract.h" | ||
|
||
// TODO: ADAPT THIS TO THE NEW INSTRUCTION | ||
|
||
static initContract_t *ctx_init_contract = &global.initContract; | ||
static tx_state_t *tx_state = &global_tx_state; | ||
|
||
#define P1_INITIAL 0x00 | ||
#define P1_NAME 0x01 | ||
#define P1_PARAMS 0x02 | ||
|
||
void handleInitContract(uint8_t *cdata, uint8_t p1, uint8_t lc) { | ||
if (p1 == P1_INITIAL) { | ||
cx_sha256_init(&tx_state->hash); | ||
cdata += parseKeyDerivationPath(cdata); | ||
cdata += hashAccountTransactionHeaderAndKind(cdata, INIT_CONTRACT); | ||
// hash the amount | ||
updateHash((cx_hash_t *) &tx_state->hash, cdata, 8); | ||
// extract the amount | ||
ctx_init_contract->amount = U8BE(cdata, 0); | ||
// Format the amount | ||
amountToGtuDisplay((uint8_t *) ctx_init_contract->amountDisplay, | ||
sizeof(ctx_init_contract->amountDisplay), | ||
ctx_init_contract->amount); | ||
cdata += 8; | ||
// hash the module ref | ||
updateHash((cx_hash_t *) &tx_state->hash, cdata, 32); | ||
// extract the module ref | ||
memmove(ctx_init_contract->moduleRef, cdata, 32); | ||
// Format the module ref | ||
if (format_hex(ctx_init_contract->moduleRef, 32, ctx_init_contract->moduleRefDisplay, 65) == | ||
-1) { | ||
THROW(ERROR_INVALID_MODULE_REF); | ||
} | ||
ctx_init_contract->state = INIT_CONTRACT_NAME_FIRST; | ||
sendSuccessNoIdle(); | ||
} | ||
|
||
else if (p1 == P1_NAME) { | ||
uint8_t lengthSize = 2; | ||
if (ctx_init_contract->state == INIT_CONTRACT_NAME_FIRST) { | ||
// extract the name length | ||
ctx_init_contract->nameLength = U2BE(cdata, 0); | ||
// calculate the remaining name length | ||
ctx_init_contract->remainingNameLength = ctx_init_contract->nameLength + lengthSize; | ||
// set the state to the next state | ||
ctx_init_contract->state = INIT_CONTRACT_NAME_NEXT; | ||
} else if (ctx_init_contract->remainingNameLength < lc) { | ||
THROW(ERROR_INVALID_NAME_LENGTH); | ||
} | ||
// hash the whole chunk | ||
updateHash((cx_hash_t *) &tx_state->hash, cdata, lc); | ||
// subtract the length of the chunk from the remaining name length | ||
ctx_init_contract->remainingNameLength -= lc; | ||
if (ctx_init_contract->remainingNameLength > 0) { | ||
sendSuccessNoIdle(); | ||
} else if (ctx_init_contract->remainingNameLength == 0) { | ||
ctx_init_contract->state = INIT_CONTRACT_PARAMS_FIRST; | ||
sendSuccessNoIdle(); | ||
} | ||
|
||
} else if (p1 == P1_PARAMS) { | ||
uint8_t lengthSize = 2; | ||
if (ctx_init_contract->state == INIT_CONTRACT_PARAMS_FIRST) { | ||
// extract the params length | ||
ctx_init_contract->paramsLength = U2BE(cdata, 0); | ||
// calculate the remaining params length | ||
ctx_init_contract->remainingParamsLength = ctx_init_contract->paramsLength + lengthSize; | ||
// set the state to the next state | ||
ctx_init_contract->state = INIT_CONTRACT_PARAMS_NEXT; | ||
} else if (ctx_init_contract->remainingParamsLength < lc) { | ||
THROW(ERROR_INVALID_PARAMS_LENGTH); | ||
} | ||
// hash the whole chunk | ||
updateHash((cx_hash_t *) &tx_state->hash, cdata, lc); | ||
// subtract the length of the chunk from the remaining params length | ||
ctx_init_contract->remainingParamsLength -= lc; | ||
if (ctx_init_contract->remainingParamsLength > 0) { | ||
sendSuccessNoIdle(); | ||
} else if (ctx_init_contract->remainingParamsLength == 0) { | ||
uiInitContractDisplay(); | ||
} | ||
|
||
} else { | ||
THROW(ERROR_INVALID_STATE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
// TODO: ADAPT THIS TO THE NEW INSTRUCTION | ||
|
||
typedef enum { | ||
INIT_CONTRACT_INITIAL = 60, | ||
INIT_CONTRACT_NAME_FIRST = 61, | ||
INIT_CONTRACT_NAME_NEXT = 62, | ||
INIT_CONTRACT_PARAMS_FIRST = 63, | ||
INIT_CONTRACT_PARAMS_NEXT = 64, | ||
INIT_CONTRACT_END = 65 | ||
} initContractState_t; | ||
|
||
/** | ||
* Handles the INIT_CONTRACT instruction, which initializes a contract | ||
* | ||
* | ||
*/ | ||
void handleInitContract(uint8_t *cdata, uint8_t p1, uint8_t lc); | ||
|
||
typedef struct { | ||
uint64_t amount; | ||
uint8_t moduleRef[32]; | ||
char amountDisplay[21]; | ||
char moduleRefDisplay[65]; | ||
uint32_t nameLength; | ||
uint32_t remainingNameLength; | ||
uint32_t paramsLength; | ||
uint32_t remainingParamsLength; | ||
initContractState_t state; | ||
} initContract_t; | ||
|
||
// typedef struct { | ||
// uint8_t version[32]; | ||
// uint8_t sourceLength[32]; | ||
// } deployModuleBlob_t; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
|
||
from application_client.boilerplate_command_sender import ( | ||
BoilerplateCommandSender, | ||
Errors, | ||
) | ||
from application_client.boilerplate_response_unpacker import ( | ||
unpack_get_public_key_response, | ||
) | ||
from ragger.bip import calculate_public_key_and_chaincode, CurveChoice | ||
from ragger.error import ExceptionRAPDU | ||
from ragger.navigator import NavInsID, NavIns | ||
from ragger.firmware import Firmware | ||
from utils import navigate_until_text_and_compare, instructions_builder | ||
|
||
|
||
@pytest.mark.active_test_scope | ||
def test_init_contract( | ||
backend, firmware, navigator, test_name, default_screenshot_path | ||
): | ||
client = BoilerplateCommandSender(backend) | ||
path = "m/1105/0/0/0/0/2/0/0" | ||
header_and_type = bytes.fromhex( | ||
"20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da701" | ||
) | ||
amount = 0xFFFFFFFFFFFFFFFF | ||
module_ref = bytes.fromhex( | ||
"a00000000000000000000000000000000000000000000000000000000000000a" | ||
) | ||
name = bytes.fromhex("696e69745f5465737420436f6e7472616374") | ||
params = bytes.fromhex("0102030405060708090a") | ||
|
||
with client.init_contract( | ||
path=path, | ||
header_and_type=header_and_type, | ||
amount=amount, | ||
module_ref=module_ref, | ||
name=name, | ||
params=params, | ||
): | ||
navigate_until_text_and_compare( | ||
firmware, navigator, "Sign", default_screenshot_path, test_name | ||
) | ||
response = client.get_async_response() | ||
print(response.data.hex()) | ||
assert response.status == 0x9000 | ||
assert response.data == bytes.fromhex( | ||
"ca0e947d521063cc40c3bf8a65dd05a6bb66c799957417f94123c0642162020fed8dd80cd9aadd51f24d697bb9bcce26b72115dbd80bfe893a8395b54f8bb10c" | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Commented-out code Note