Skip to content

Commit

Permalink
implement single account mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkv-vcm committed Oct 13, 2021
1 parent f03e1f2 commit f16aa5b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/securityPolicy.c
Expand Up @@ -2,6 +2,7 @@
#include "addressUtilsByron.h"
#include "bip44.h"
#include "cardano.h"
#include "singleAccount.h"

#include "securityPolicy.h"

Expand Down Expand Up @@ -365,6 +366,7 @@ security_policy_t policyForSignTxOutputAddressParams(
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_ORDINARY_TX: {
DENY_UNLESS(determineSpendingChoice(params->type) == SPENDING_PATH);
DENY_IF(violatesSingleAccount(&params->spendingKeyPath));
SHOW_UNLESS(is_standard_base_address(params));
ALLOW();
break;
Expand Down Expand Up @@ -504,6 +506,7 @@ security_policy_t policyForSignTxCertificateStaking(
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_KEY_PATH);
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&stakeCredential->keyPath));
DENY_IF(violatesSingleAccount(&stakeCredential->keyPath));
break;
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_SCRIPT_HASH);
Expand Down Expand Up @@ -606,6 +609,7 @@ security_policy_t policyForSignTxStakePoolRegistrationOwner(
{
if (owner->keyReferenceType == KEY_REFERENCE_PATH) {
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&owner->path));
DENY_IF(violatesSingleAccount(&owner->path));
}

switch (txSigningMode) {
Expand Down Expand Up @@ -675,6 +679,7 @@ security_policy_t policyForSignTxWithdrawal(
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_KEY_PATH);
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&stakeCredential->keyPath));
DENY_IF(violatesSingleAccount(&stakeCredential->keyPath));
SHOW();
break;

Expand All @@ -694,6 +699,8 @@ static inline security_policy_t _ordinaryWitnessPolicy(const bip44_path_t* path,
{
switch (bip44_classifyPath(path)) {
case PATH_ORDINARY_SPENDING_KEY:
DENY_IF(violatesSingleAccount(path));
// intentional fallthrough
case PATH_ORDINARY_STAKING_KEY:
case PATH_POOL_COLD_KEY:
WARN_UNLESS(bip44_isPathReasonable(path));
Expand Down
7 changes: 7 additions & 0 deletions src/signTx.h
Expand Up @@ -54,12 +54,19 @@ enum {
SIGN_MAX_WITNESSES = SIGN_MAX_INPUTS + SIGN_MAX_OUTPUTS + SIGN_MAX_CERTIFICATES + SIGN_MAX_REWARD_WITHDRAWALS,
};

typedef struct {
bool pathSet;
bip44_path_t path;
} single_account_data_t;

typedef struct {
// significantly affects restrictions on the tx
sign_tx_signingmode_t txSigningMode;

uint8_t networkId; // part of Shelley address
uint32_t protocolMagic; // part of Byron address

single_account_data_t singleAccountData;
} common_tx_data_t;

typedef struct {
Expand Down
24 changes: 24 additions & 0 deletions src/singleAccount.c
@@ -0,0 +1,24 @@
#include "state.h"

static common_tx_data_t* commonData = &(instructionState.signTxContext.commonTxData);

bool violatesSingleAccount(const bip44_path_t* path)
{
if (!bip44_hasOrdinaryWalletKeyPrefix(path) || !bip44_containsAccount(path)) {
return false;
}
if (commonData->singleAccountData.pathSet) {
const uint32_t storedAccount = bip44_getAccount(&commonData->singleAccountData.path);
if (bip44_getAccount(path) != storedAccount) {
return true;
}
if ((bip44_hasByronPrefix(path) || bip44_hasByronPrefix(&commonData->singleAccountData.path))
&& storedAccount != 0) {
return true;
}
} else {
memcpy(&commonData->singleAccountData.path, path, SIZEOF(*path));
commonData->singleAccountData.pathSet = true;
}
return false;
}
8 changes: 8 additions & 0 deletions src/singleAccount.h
@@ -0,0 +1,8 @@
#ifndef H_CARDANO_APP_SINGLE_ACCOUNT
#define H_CARDANO_APP_SINGLE_ACCOUNT

#include "bip44.h"

bool violatesSingleAccount(const bip44_path_t* path);

#endif // H_CARDANO_APP_SINGLE_ACCOUNT

0 comments on commit f16aa5b

Please sign in to comment.