21 #include "esp_system.h" 25 #include "sodium/crypto_generichash.h" 26 #include "sodium/crypto_sign.h" 31 #include "sodium/private/curve25519_ref10.h" 35 #include "sodium/private/ed25519_ref10.h" 37 #define ge_p3 ge25519_p3 38 #define sc_reduce sc25519_reduce 39 #define sc_muladd sc25519_muladd 40 #define ge_scalarmult_base ge25519_scalarmult_base 41 #define ge_p3_tobytes ge25519_p3_tobytes 140 #define F_NANO_POW_MAX_THREAD (size_t)10 144 #undef F_NANO_POW_MAX_THREAD 152 #define MAX_STR_NANO_CHAR (size_t)70 //5+56+8+1 158 #define PUB_KEY_EXTENDED_MAX_LEN (size_t)40 164 #define NANO_PREFIX "nano_" 170 #define XRB_PREFIX "xrb_" 178 #define BIP39_DICTIONARY "/spiffs/dictionary.dic" 182 #define BIP39_DICTIONARY_SAMPLE "../../dictionary.dic" 183 #define BIP39_DICTIONARY "dictionary.dic" 194 #define NANO_ENCRYPTED_SEED_FILE "/spiffs/secure/nano.nse" 200 #define NANO_PASSWD_MAX_LEN (size_t)80 206 #define STR_NANO_SZ (size_t)66// 65+1 Null included 212 #define NANO_FILE_WALLETS_INFO "/spiffs/secure/walletsinfo.i" 233 #define EXPORT_KEY_TO_CHAR_SZ (size_t)sizeof(NANO_SEED)+1 291 #define F_BLOCK_TRANSFER_SIZE (size_t)sizeof(F_BLOCK_TRANSFER) 292 #define F_P2POW_BLOCK_TRANSFER_SIZE 2*F_BLOCK_TRANSFER_SIZE 295 #define F_BLOCK_TRANSFER_SIGNABLE_SZ (size_t)(sizeof(F_BLOCK_TRANSFER)-64-sizeof(uint64_t)-sizeof(uint8_t)) 358 #define READ_SEED_FROM_STREAM (int)1 359 #define READ_SEED_FROM_FILE (int)2 360 #define WRITE_SEED_TO_STREAM (int)4 361 #define WRITE_SEED_TO_FILE (int)8 362 #define PARSE_JSON_READ_SEED_GENERIC (int)16 363 #define F_STREAM_DATA_FILE_VERSION (uint32_t)((1<<16)|0) 389 static const uint8_t NANO_WALLET_MAGIC[] = {
'_',
'n',
'a',
'n',
'o',
'w',
'a',
'l',
'l',
'e',
't',
'f',
'i',
'l',
'e',
'_'};
390 #define F_NANO_FILE_DESC "NANO Seed Encrypted file/stream. Keep it safe and backup it. This file is protected by password. BUY BITCOIN and NANO !!!" 391 #define F_DESC_SZ (size_t) (160-sizeof(uint32_t)) 419 _Static_assert((
sizeof(F_NANO_CRYPTOWALLET)&0x1F)==0,
"Error 1");
420 _Static_assert((
sizeof(F_ENCRYPTED_BLOCK)&0x1F)==0,
"Error 2");
428 #define REP_XRB (uint8_t)0x4 434 #define SENDER_XRB (uint8_t)0x02 440 #define DEST_XRB (uint8_t)0x01 471 #define F_RAW_TO_STR_UINT128 (int)1 472 #define F_RAW_TO_STR_STRING (int)2 473 #define F_RAW_STR_MAX_SZ (size_t)41 // 39 + '\0' + '.' -> 39 = log10(2^128) 474 #define F_MAX_STR_RAW_BALANCE_MAX (size_t)40 //39+'\0' 475 #define F_NANO_EMPTY_BALANCE "0.0" 501 _Static_assert((
sizeof(F_NANO_WALLET_INFO_BODY)&0x1F)==0,
"Error F_NANO_WALLET_INFO_BODY is not byte aligned");
503 #define F_NANO_WALLET_INFO_DESC "Nano file descriptor used for fast custom access. BUY BITCOIN AND NANO." 504 #define F_NANO_WALLET_INFO_VERSION (uint16_t)((1<<8)|1) 505 static const uint8_t F_NANO_WALLET_INFO_MAGIC[] = {
'_',
'n',
'a',
'n',
'o',
'w',
'a',
'l',
'l',
'e',
't',
'_',
'n',
'f',
'o',
'_'};
507 #define F_NANO_DESC_SZ (size_t)78 520 uint8_t
header[
sizeof(F_NANO_WALLET_INFO_MAGIC)];
535 _Static_assert((
sizeof(F_NANO_WALLET_INFO)&0x1F)==0,
"Error F_NANO_WALLET_INFO is not byte aligned");
581 #define F_NANO_ADD_A_B (uint32_t)(1<<0) 582 #define F_NANO_SUB_A_B (uint32_t)(1<<1) 583 #define F_NANO_A_RAW_128 (uint32_t)(1<<2) 584 #define F_NANO_A_RAW_STRING (uint32_t)(1<<3) 585 #define F_NANO_A_REAL_STRING (uint32_t)(1<<4) 586 #define F_NANO_B_RAW_128 (uint32_t)(1<<5) 587 #define F_NANO_B_RAW_STRING (uint32_t)(1<<6) 588 #define F_NANO_B_REAL_STRING (uint32_t)(1<<7) 589 #define F_NANO_RES_RAW_128 (uint32_t)(1<<8) 590 #define F_NANO_RES_RAW_STRING (uint32_t)(1<<9) 591 #define F_NANO_RES_REAL_STRING (uint32_t)(1<<10) 592 #define F_NANO_C_RAW_128 (uint32_t)(F_NANO_B_RAW_128<<16) 593 #define F_NANO_C_RAW_STRING (uint32_t)(F_NANO_B_RAW_STRING<<16) 594 #define F_NANO_C_REAL_STRING (uint32_t)(F_NANO_B_REAL_STRING<<16) 596 #define F_NANO_COMPARE_EQ (uint32_t)(1<<16) //Equal 597 #define F_NANO_COMPARE_LT (uint32_t)(1<<17) // Lesser than 598 #define F_NANO_COMPARE_LEQ (F_NANO_COMPARE_LT|F_NANO_COMPARE_EQ) // Less or equal 599 #define F_NANO_COMPARE_GT (uint32_t)(1<<18) // Greater 600 #define F_NANO_COMPARE_GEQ (F_NANO_COMPARE_GT|F_NANO_COMPARE_EQ) // Greater or equal 601 #define DEFAULT_MAX_FEE "0.001" 766 int pk_to_wallet(
char *,
char *, NANO_PUBLIC_KEY_EXTENDED);
938 int f_read_seed(uint8_t *,
const char *,
void *,
int,
int);
1155 int f_nano_sign_block(F_BLOCK_TRANSFER *, F_BLOCK_TRANSFER *, NANO_PRIVATE_KEY_EXTENDED);
1170 f_write_seed_err
f_write_seed(
void *,
int, uint8_t *,
char *);
1191 #define F_BRAIN_WALLET_VERY_POOR (uint32_t)0 1197 #define F_BRAIN_WALLET_POOR (uint32_t)1 1203 #define F_BRAIN_WALLET_VERY_BAD (uint32_t)2 1209 #define F_BRAIN_WALLET_BAD (uint32_t)3 1215 #define F_BRAIN_WALLET_VERY_WEAK (uint32_t)4 1221 #define F_BRAIN_WALLET_WEAK (uint32_t)5 1227 #define F_BRAIN_WALLET_STILL_WEAK (uint32_t)6 1233 #define F_BRAIN_WALLET_MAYBE_GOOD (uint32_t)7 1240 #define F_BRAIN_WALLET_GOOD (uint32_t)8 1246 #define F_BRAIN_WALLET_VERY_GOOD (uint32_t)9 1252 #define F_BRAIN_WALLET_NICE (uint32_t)10 1258 #define F_BRAIN_WALLET_PERFECT (uint32_t)11 1299 int f_verify_work(uint64_t *,
const unsigned char *, uint64_t *, uint64_t);
1306 #define F_SIGNATURE_RAW (uint32_t)1 1313 #define F_SIGNATURE_STRING (uint32_t)2 1320 #define F_SIGNATURE_OUTPUT_RAW_PK (uint32_t)4 1327 #define F_SIGNATURE_OUTPUT_STRING_PK (uint32_t)8 1334 #define F_SIGNATURE_OUTPUT_XRB_PK (uint32_t)16 1341 #define F_SIGNATURE_OUTPUT_NANO_PK (uint32_t)32 1348 #define F_IS_SIGNATURE_RAW_HEX_STRING (uint32_t)64 1355 #define F_MESSAGE_IS_HASH_STRING (uint32_t)128 1361 #define F_DEFAULT_THRESHOLD (uint64_t) 0xffffffc000000000 1388 void *out_public_key,
1389 uint32_t ouput_type,
1390 const unsigned char *message,
1392 const unsigned char *private_key);
1399 #define F_VERIFY_SIG_NANO_WALLET (uint32_t)1 1406 #define F_PUBLIC_KEY_RAW_HEX (uint32_t)2 1413 #define F_PUBLIC_KEY_ASCII_HEX (uint32_t)4 1435 int f_verify_signed_data(
const unsigned char *,
const unsigned char *,
size_t,
const void *, uint32_t);
1452 #define F_BALANCE_RAW_128 F_NANO_A_RAW_128 1458 #define F_BALANCE_REAL_STRING F_NANO_A_REAL_STRING 1464 #define F_BALANCE_RAW_STRING F_NANO_A_RAW_STRING 1470 #define F_VALUE_SEND_RECEIVE_RAW_128 F_NANO_B_RAW_128 1476 #define F_VALUE_SEND_RECEIVE_REAL_STRING F_NANO_B_REAL_STRING 1482 #define F_VALUE_SEND_RECEIVE_RAW_STRING F_NANO_B_RAW_STRING 1488 #define F_VALUE_TO_SEND (int)(1<<0) 1494 #define F_VALUE_TO_RECEIVE (int)(1<<1) 1500 #define F_FEE_VALUE_RAW_128 F_NANO_B_RAW_128 1506 #define F_FEE_VALUE_REAL_STRING F_NANO_B_REAL_STRING 1512 #define F_FEE_VALUE_RAW_STRING F_NANO_B_RAW_STRING 1561 F_BLOCK_TRANSFER **,
1577 F_BLOCK_TRANSFER **,
1601 int f_nano_pow(uint64_t *,
unsigned char *,
const uint64_t,
int);
f_write_seed_err f_write_seed(void *, int, uint8_t *, char *)
Writes a SEED into a ecrypted with password with non deterministic stream in memory or file...
uint8_t account[32]
Account in raw binary data.
int f_nano_pow(uint64_t *, unsigned char *, const uint64_t, int)
Calculates a Proof of Work given a hash, threshold and number of threads n_thr
struct of the block of encrypted file to store Nano SEED
int f_nano_get_block_hash(uint8_t *, F_BLOCK_TRANSFER *)
Gets a hash from Nano block.
int f_sign_data(unsigned char *signature, void *out_public_key, uint32_t ouput_type, const unsigned char *message, size_t msg_len, const unsigned char *private_key)
Signs a message with a deterministic signature given a private key
uint8_t hash_sk_unencrypted[32]
hash of Nano SEED when unencrypted
uint8_t NANO_PUBLIC_KEY_EXTENDED[PUB_KEY_EXTENDED_MAX_LEN]
Size of Public Key Extended.
uint64_t from_multiplier(double, uint64_t)
Calculates a PoW given a multiplier and base difficulty.
enum f_nano_err_t f_nano_err
Error function enumerator.
uint8_t nano_hdr[sizeof(NANO_WALLET_MAGIC)]
Header of the file.
int f_nano_is_valid_block(F_BLOCK_TRANSFER *)
Checks if Binary Nano Block is valid.
Can not delete Nano info file.
int f_verify_work(uint64_t *, const unsigned char *, uint64_t *, uint64_t)
Verifies if Proof of Work of a given hash is valid.
f_uint128_t balance
Big number 128 bit raw balance.
f_nano_create_block_dyn_err_t
uint8_t NANO_PRIVATE_KEY_EXTENDED[crypto_sign_ed25519_SECRETKEYBYTES]
Size of Nano Private Key extended.
int valid_raw_balance(const char *)
Checks if a string buffer pointed in balance is a valid raw balance.
enum f_nano_p2pow_block_dyn_err_t F_NANO_P2POW_BLOCK_DYN_ERR
F_FILE_INFO_ERR f_set_nano_file_info(F_NANO_WALLET_INFO *, int)
Saves wallet information stored at buffer struct info to file walletsinfo.i
f_nano_err f_nano_balance_to_str(char *, size_t, size_t *, f_uint128_t)
Converts a raw Nano balance to string raw balance.
char desc[F_NANO_DESC_SZ]
Description.
int f_nano_raw_to_string(char *, size_t *, size_t, void *, int)
Converts Nano raw balance [string | f_uint128_t] to real string value.
int is_null_hash(uint8_t *)
Check if 32 bytes hash is filled with zeroes.
uint8_t wallet_prefix
Wallet prefix: 0 for NANO; 1 for XRB.
#define MAX_STR_NANO_CHAR
Defines a max size of Nano char (70 bytes)
int f_nano_block_to_json(char *, size_t *, size_t, F_BLOCK_TRANSFER *)
Parse a Nano Block to JSON.
int f_nano_p2pow_to_JSON(char *, size_t *, size_t, F_BLOCK_TRANSFER *)
Parse binary P2PoW block to JSON.
struct of the block of encrypted file to store Nano SEED
int f_nano_sign_block(F_BLOCK_TRANSFER *, F_BLOCK_TRANSFER *, NANO_PRIVATE_KEY_EXTENDED)
Signs user_block and worker fee_block given a private key private_key
int f_verify_signed_data(const unsigned char *, const unsigned char *, size_t, const void *, uint32_t)
Verifies if a signed message is valid.
uint8_t iv[16]
Initial sub vector.
Can not complete NULL char.
Does not make sense send empty value.
int nano_base_32_2_hex(uint8_t *, char *)
Parse Nano Base32 wallet string to public key binary.
uint32_t ver
Version of the file.
Can't open info file.
uint8_t salt[32]
Salt of the first encryption layer.
uint8_t nanoseed_hash[32]
Nano SEED hash file.
f_nano_p2pow_block_dyn_err_t
int pk_to_wallet(char *, char *, NANO_PUBLIC_KEY_EXTENDED)
Parse a Nano public key to Base32 Nano wallet string.
F_ENCRYPTED_BLOCK seed_block
Second encrypted block for Nano SEED.
uint8_t NANO_SEED[crypto_sign_SEEDBYTES]
Size of Nano SEED.
This ABI is a utility for myNanoEmbedded library and sub routines are implemented here...
int f_nano_valid_nano_str_value(const char *)
Check if a real string or raw string are valid Nano balance.
Invalid SHA256 info file.
int valid_nano_wallet(const char *)
Check if a string containing a Base32 Nano wallet is valid.
int f_verify_token(F_TOKEN, void *, size_t, const char *)
Verifies if a token is valid given data and password.
int f_bip39_to_nano_seed(uint8_t *, char *, char *)
Parse Nano Bip39 encoded string to raw Nano SEED given a dictionary file.
int nano_create_p2pow_block_dynamic(F_BLOCK_TRANSFER **, F_BLOCK_TRANSFER *, const void *, size_t, const void *, uint32_t, const void *, size_t)
int f_nano_transaction_to_JSON(char *, size_t, size_t *, NANO_PRIVATE_KEY_EXTENDED, F_BLOCK_TRANSFER *)
Sign a block pointed in block_transfer with a given private_key and stores signed block to block_tran...
char * f_nano_key_to_str(char *, unsigned char *)
Parse a raw binary public key to string.
enum f_write_seed_err_t f_write_seed_err
#define PUB_KEY_EXTENDED_MAX_LEN
Max size of public key (extended)
int f_read_seed(uint8_t *, const char *, void *, int, int)
Extracts a Nano SEED from encrypted stream in memory or in a file.
int f_cloud_crypto_wallet_nano_create_seed(size_t, char *, char *)
Generates a new SEED and saves it to an non deterministic encrypted file.
Encrypted file with Nano SEED not found.
Nano signed block raw data defined in this reference
Does not make sense send negativative balance.
void f_set_dictionary_path(const char *)
Set default dictionary file and path to myNanoEmbedded library.
struct of the body block of the info file
uint8_t NANO_PUBLIC_KEY[crypto_sign_ed25519_PUBLICKEYBYTES]
Size of Nano Public Key.
char max_fee[F_RAW_STR_MAX_SZ]
Custom preferred max fee of Proof of Work.
Can not parse to block transfer.
int f_extract_seed_from_brainwallet(uint8_t *, char **, uint32_t, const char *, const char *)
Analyzes a text given a mode and if pass then the text in braiwallet is translated to a Nano SEED...
uint8_t representative[32]
Representative for current account.
F_NANO_WALLET_INFO_BODY body
Body of the file info.
uint8_t sub_salt[32]
Salt of the sub block to be stored.
int f_verify_signed_block(F_BLOCK_TRANSFER *)
f_nano_err f_nano_verify_nano_funds(void *, void *, void *, uint32_t)
Check if Nano balance has sufficient funds.
double to_multiplier(uint64_t, uint64_t)
Calculates a relative difficulty compared PoW with another.
enum f_nano_create_block_dyn_err_t F_NANO_CREATE_BLOCK_DYN_ERR
int f_nano_seed_to_bip39(char *, size_t, size_t *, NANO_SEED, char *)
Parse Nano SEED to Bip39 encoding given a dictionary file.
f_nano_err f_nano_parse_raw_str_to_raw128_t(uint8_t *, const char *)
Parse a raw string balance to raw big number 128 bit.
uint8_t file_info_integrity[32]
File info integrity of the body block.
Can not parse temporary memory to uint_128_t.
uint8_t prefixes
Internal use for this API.
char wallet_representative[MAX_STR_NANO_CHAR]
Wallet representative.
uint8_t f_uint128_t[16]
128 bit big number of Nano balance
uint8_t reserved[16]
Reserved (not used)
Can not generate main private key.
Can not parse raw A value to MPI.
uint64_t work
Internal use for this API.
Invalid info file header.
Error multiplication MPI.
enum f_file_info_err_t F_FILE_INFO_ERR
Typedef Error enumerator for info file functions.
Can not read encrypted Nano SEED in file.
Can not open info file for write.
int nano_create_block_dynamic(F_BLOCK_TRANSFER **, const void *, size_t, const void *, size_t, const void *, size_t, const void *, const void *, uint32_t, const void *, size_t, int)
Creates a Nano block dynamically in memory.
f_nano_err f_nano_add_sub(void *, void *, void *, uint32_t)
Add/Subtract two Nano balance values and stores value in res
uint8_t description[F_DESC_SZ]
File description.
uint8_t NANO_PRIVATE_KEY[sizeof(NANO_SEED)]
Size of Nano Private Key.
uint32_t last_used_wallet_number
Last used wallet number.
Can not encrypt sub block.
int f_generate_token(F_TOKEN, void *, size_t, const char *)
Generates a non deterministic token given a message data and a password.
Can not parse string big number.
char * f_get_dictionary_path(void)
Get default dictionary path in myNanoEmbedded library.
uint8_t sk_encrypted[32]
Secret.
uint8_t signature[64]
Signature of the block.
uint8_t link[32]
link or destination account
Can not parse raw B value to MPI.
Error encrypt private key.
int f_seed_to_nano_wallet(NANO_PRIVATE_KEY, NANO_PUBLIC_KEY, NANO_SEED, uint32_t)
Extracts one key pair from Nano SEED given a wallet number.
uint8_t preamble[32]
Block preamble.
Does not make sense negative balance.
int f_parse_nano_seed_and_bip39_to_JSON(char *, size_t, size_t *, void *, int, const char *)
Parse Nano SEED and Bip39 to JSON given a encrypted data in memory or encrypted data in file or unenc...
Can not parse big number factor.
uint8_t header[sizeof(F_NANO_WALLET_INFO_MAGIC)]
Header magic.
int f_is_valid_nano_seed_encrypted(void *, size_t, int)
Verifies if ecrypted Nano SEED is valid.
int f_nano_get_p2pow_block_hash(uint8_t *, uint8_t *, F_BLOCK_TRANSFER *)
Get Nano user block hash and Nano fee block hashes from P2PoW block.
int is_nano_prefix(const char *, const char *)
Checks prefix in nano_wallet
F_FILE_INFO_ERR f_get_nano_file_info(F_NANO_WALLET_INFO *)
Opens default file walletsinfo.i (if exists) containing information F_NANO_WALLET_INFO structure and ...
f_nano_err f_nano_parse_real_str_to_raw128_t(uint8_t *, const char *)
Parse a real string balance to raw big number 128 bit.
f_nano_err f_nano_value_compare_value(void *, void *, uint32_t *)
Comparare two Nano balance.
Can not parse MPI to string.
struct of the body block of the info file
Error enumerator for info file functions.
uint8_t previous[32]
Previous block.
Can not generate sub private key.
struct f_block_transfer_t __attribute__((packed)) F_BLOCK_TRANSFER
int f_generate_nano_seed(NANO_SEED, uint32_t)
Generates a new SEED and stores it to seed pointer.