forked from Concordium/concordium-ledger-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(init_contract): implement init contract instruction and tests
- Add new instruction INS_INIT_CONTRACT (0x07) for contract initialization - Create initContract.c/.h files with handler implementation - Add UI flow for displaying contract amount and module reference - Add new error codes for name/params length and module ref validation - Implement chunked processing for contract name and parameters - Add Python test client support for init contract instruction - Add test case with snapshots for contract initialization flow - Update handler.c and globals.h to support new instruction The implementation allows users to initialize contracts by providing the amount, module reference, contract name, and parameters, with appropriate UI confirmation steps and chunked data processing for larger inputs.
- Loading branch information
Showing
26 changed files
with
196 additions
and
8 deletions.
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; | ||
Check notice Code scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
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