Skip to content

Commit

Permalink
Merge pull request #44 from ElrondNetwork/get-address-auth-token
Browse files Browse the repository at this point in the history
Get address auth token
  • Loading branch information
bogdan-rosianu authored Aug 11, 2021
2 parents ddcf54b + 7f16081 commit 268b301
Show file tree
Hide file tree
Showing 37 changed files with 649 additions and 281 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ APP_LOAD_PARAMS= --curve ed25519 --path "44'/508'" --appFlags 0x240 $(COMMON_LOA

APPVERSION_M:=$(if $(APPVERSION_M),$(APPVERSION_M),1)
APPVERSION_N:=$(if $(APPVERSION_N),$(APPVERSION_N),0)
APPVERSION_P:=$(if $(APPVERSION_P),$(APPVERSION_P),13)
APPVERSION_P:=$(if $(APPVERSION_P),$(APPVERSION_P),14)
APPVERSION=$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)
APPNAME = "Elrond"

Expand Down
66 changes: 66 additions & 0 deletions src/address_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdint.h>
#include <stddef.h>

#include "bech32.h"
#include "globals.h"
#include "get_private_key.h"
#include "os.h"
#include "ux.h"

/* return false in case of error, true otherwise */
bool get_public_key(uint32_t account_number, uint32_t index, uint8_t *public_key_array) {
cx_ecfp_private_key_t private_key;
cx_ecfp_public_key_t public_key;
bool error = false;

if (!get_private_key(account_number, index, &private_key)) {
return false;
}

BEGIN_TRY {
TRY {
cx_ecfp_generate_pair(CX_CURVE_Ed25519, &public_key, &private_key, 1);
}
CATCH_ALL {
error = true;
}
FINALLY {
memset(&private_key, 0, sizeof(private_key));
}
}
END_TRY;

if (error) {
return false;
}

for (int i = 0; i < 32; i++) {
public_key_array[i] = public_key.W[64 - i];
}
if ((public_key.W[32] & 1) != 0) {
public_key_array[31] |= 0x80;
}

return true;
}

// TODO: maybe make this function more general and extract to a new file binary <-> hex converters
void get_address_hex_from_binary(uint8_t *public_key, char *address) {
const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
uint8_t i;

for (i = 0; i < 32; i++) {
address[i * 2] = hex[public_key[i] >> 4];
address[i * 2 + 1] = hex[public_key[i] & 0xf];
}
address[64] = '\0';
}

// TODO: analyze and refactor this function
void get_address_bech32_from_binary(uint8_t *public_key, char *address) {
uint8_t buffer[33];

memmove(buffer, public_key, 32);
buffer[32] = '\0';
bech32EncodeFromBytes(address, HRP, buffer, 33);
}
12 changes: 12 additions & 0 deletions src/address_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _ADDRESS_HELPERS_H_
#define _ADDRESS_HELPERS_H_

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

bool get_public_key(uint32_t account_number, uint32_t index, uint8_t *public_key_array);
void get_address_hex_from_binary(uint8_t *public_key, char *address);
void get_address_bech32_from_binary(uint8_t *public_ey, char *address);

#endif
9 changes: 9 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ typedef enum {
#define GAS_PER_DATA_BYTE 1500
#define GAS_PRICE_DIVIDER 100
#define MIN_GAS_LIMIT 50000
#define MAX_AUTH_TOKEN_LEN 100
#define BECH32_ADDRESS_LEN 62
#define HASH_LEN 32
#define MESSAGE_SIGNATURE_LEN 64
#define SHA3_KECCAK_BITS 256
#define PUBLIC_KEY_LEN 32

static const char PREPEND[] = "\x17"
"Elrond Signed Message:\n";
150 changes: 0 additions & 150 deletions src/getAddress.c

This file was deleted.

11 changes: 0 additions & 11 deletions src/getAddress.h

This file was deleted.

41 changes: 0 additions & 41 deletions src/getPrivateKey.c

This file was deleted.

9 changes: 0 additions & 9 deletions src/getPrivateKey.h

This file was deleted.

Loading

0 comments on commit 268b301

Please sign in to comment.