Skip to content

Commit

Permalink
XS: Byron address derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Aug 30, 2023
1 parent c3d9ba7 commit d08ed27
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 53 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ DEFINES += APP_XS_DISABLE_TOKEN_MINTING
DEFINES += APP_XS_DISABLE_POOL_REGISTRATION
DEFINES += APP_XS_DISABLE_POOL_RETIREMENT
DEFINES += APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK
DEFINES += APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION
endif

##############
Expand Down
106 changes: 57 additions & 49 deletions src/addressUtilsByron.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
#include "crc32.h"
#include "bufView.h"

#if !defined(APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION) || !defined(APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK)

static const size_t ADDRESS_ROOT_SIZE = 28;
static const size_t PROTOCOL_MAGIC_ADDRESS_ATTRIBUTE_KEY = 2;

#endif

#ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION

enum {
CARDANO_ADDRESS_TYPE_PUBKEY = 0,
/*
Expand All @@ -15,9 +24,6 @@ enum {
*/
};

static const size_t ADDRESS_ROOT_SIZE = 28;
static const size_t PROTOCOL_MAGIC_ADDRESS_ATTRIBUTE_KEY = 2;

void addressRootFromExtPubKey(
const extendedPublicKey_t* extPubKey,
uint8_t* outBuffer, size_t outSize
Expand Down Expand Up @@ -136,6 +142,54 @@ size_t cborPackRawAddressWithChecksum(
return view_processedSize(&output);
}

size_t deriveRawAddress(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t addressRoot[28] = {0};
{
extendedPublicKey_t extPubKey;

deriveExtendedPublicKey(pathSpec, &extPubKey);

addressRootFromExtPubKey(
&extPubKey,
addressRoot, SIZEOF(addressRoot)
);
}

return cborEncodePubkeyAddressInner(
addressRoot, SIZEOF(addressRoot),
protocolMagic,
outBuffer, outSize
);
}

size_t deriveAddress_byron(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t rawAddressBuffer[40] = {0};
size_t rawAddressSize = deriveRawAddress(
pathSpec, protocolMagic,
rawAddressBuffer, SIZEOF(rawAddressBuffer)
);

return cborPackRawAddressWithChecksum(
rawAddressBuffer, rawAddressSize,
outBuffer, outSize
);

}

#endif // APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION

#ifndef APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK

static uint64_t parseToken(read_view_t* view, uint8_t type)
Expand Down Expand Up @@ -253,49 +307,3 @@ uint32_t extractProtocolMagic(
}

#endif // APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK

size_t deriveRawAddress(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t addressRoot[28] = {0};
{
extendedPublicKey_t extPubKey;

deriveExtendedPublicKey(pathSpec, &extPubKey);

addressRootFromExtPubKey(
&extPubKey,
addressRoot, SIZEOF(addressRoot)
);
}

return cborEncodePubkeyAddressInner(
addressRoot, SIZEOF(addressRoot),
protocolMagic,
outBuffer, outSize
);
}

size_t deriveAddress_byron(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t rawAddressBuffer[40] = {0};
size_t rawAddressSize = deriveRawAddress(
pathSpec, protocolMagic,
rawAddressBuffer, SIZEOF(rawAddressBuffer)
);

return cborPackRawAddressWithChecksum(
rawAddressBuffer, rawAddressSize,
outBuffer, outSize
);

}
4 changes: 4 additions & 0 deletions src/addressUtilsByron.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#include "common.h"
#include "bip44.h"

#ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION

size_t deriveAddress_byron(
const bip44_path_t* pathSpec,
uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
);

#endif // APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION

#ifndef APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK

// Note: validates the overall address structure at the same time
Expand Down
2 changes: 1 addition & 1 deletion src/addressUtilsByron_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef DEVEL
#if defined(DEVEL) && !defined(APP_XS)

#include "addressUtilsByron.h"
#include "cardano.h"
Expand Down
17 changes: 14 additions & 3 deletions src/addressUtilsShelley.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,6 @@ size_t deriveAddress(const addressParams_t* addressParams, uint8_t* outBuffer, s
ASSERT(outSize < BUFFER_SIZE_PARANOIA);
ASSERT(isValidAddressParams(addressParams));

const bip44_path_t* spendingPath = &addressParams->spendingKeyPath;

// shelley
switch (addressParams->type) {
case BASE_PAYMENT_KEY_STAKE_KEY:
Expand All @@ -458,8 +456,16 @@ size_t deriveAddress(const addressParams_t* addressParams, uint8_t* outBuffer, s
case REWARD_KEY:
case REWARD_SCRIPT:
return deriveAddress_reward(addressParams, outBuffer, outSize);

#ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION
case BYRON:
return deriveAddress_byron(spendingPath, addressParams->protocolMagic, outBuffer, outSize);
return deriveAddress_byron(
&addressParams->spendingKeyPath,
addressParams->protocolMagic,
outBuffer, outSize
);
#endif // APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION

default:
ASSERT(false);
}
Expand Down Expand Up @@ -698,6 +704,11 @@ bool isValidAddressParams(const addressParams_t* params)
#define CHECK(cond) if (!(cond)) return false
if (params->type != BYRON) {
CHECK(isValidNetworkId(params->networkId));
} else {
// code for Byron address derivation not available in XS app
#ifdef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION
return false;
#endif
}

CHECK(isValidStakingInfo(params));
Expand Down

0 comments on commit d08ed27

Please sign in to comment.