Skip to content
Eztero edited this page Dec 15, 2023 · 5 revisions

Welcome to the CardanoPlusPlus wiki!

Documentation


Create mnemotic from entropy

Function: Convert an entropy to mnemotic.

char * entropy2mnemotic(std::uint8_t const *const entropy, std::size_t const *const entropy_len, 
                        char const diccionary[][2048], std::size_t *const mnemotic_len) noexcept;

Return:

Returns a char pointer or nullptr in case of an error, mnemotic_len indicates the length of the array.

Example:

#include <cardanoplusplus.h>
#include <random>

std::random_device random_generator("/dev/random");
std::uniform_int_distribution<std::uint8_t> distribution(0,255);
std::size_t entropy_len = 32;
std::uint8_t entropy[entropy_len]{};

for (int i=0; i<32; ++i) {
    entropy[i] = distribution(random_generator);
}

std::size_t mnemotic_len = 0;
char * mnemotic = entropy2mnemotic(entropy, &entropy_len, Cardano::Diccionary::English, &mnemotic_len);

.................
.................

sodium_memzero(mnemotic, mnemotic_len);
delete[] mnemotic;


Create entropy from mnemotic

Function: Convert an mnemotic to entropy.

std::uint8_t * mnemotic2entropy( char const *const mnemotic, char const diccionary[][2048], 
                                 std::size_t *const entropy_len ) noexcept;

Return:

Returns a std::uint8_t pointer or nullptr in case of an error, entropy_len indicates the length of the array.

Example:

#include <cardanoplusplus.h>

std::string seed = "draw maximum video autumn canal hero victory worth color equal office wing guess century unique dismiss weasel manual evil animal predict scale truly snake";
std::size_t entropy_len = 0;
std::uint8_t *entropy = Cardano::mnemotic2entropy(seed.data(), Cardano::Diccionary::English, &entropy_len);
palabras_seed.clear();

........
........

sodium_memzero(entropy, entropy_len);
delete[] entropy;


Create master key

Function: Creates a master extended private key (length 96), which is used as the basis for creating the other public and private keys.

bool getRawMasterKey( std::uint8_t const * const entropy, std::size_t const entropy_len, std::uint8_t const * const password,
                      std::size_t const password_len, std::uint8_t * const mastersecretkey_out ) noexcept;

Return:

Returns a bool, true if successful or false if an error is raised. mastersecretkey_out receives the 96 bytes extended master key, You can indicate the length of the array, with the help of the macro MASTERSECRETKEY_LENGTH.

Example:

#include <cardanoplusplus.h>

....
....

std::uint8_t masterkey[MASTERSECRETKEY_LENGTH];
Cardano::getRawMasterKey(entropy, entropy_len, nullptr, 0 , masterkey);


Create Address and Raw Address

Function:

Generates an address in bech32 format ( for example: addr1....), from a master key or a public/private account key.

void getBech32Address( std::uint8_t const * const input_key, Cardano::InputKey const input_key_type, 
                       Cardano::Network const network_id, Cardano::Wallet const wallet_type, 
                       Cardano::Address const address_type, std::uint32_t const account_path, 
                       std::uint32_t const address_index_path, std::string & address_out );

Return:

Does not return, but the function generates exceptions of type std::logic_error. Address_out receives the address in bech32 (addr1..).

Example:

#include <cardanoplusplus.h>
// 1852H/1815H/2H/0/1

.....
.....

std::uint32_t account_index = 2;
std::uint32_t address_index = 1;
std::string address_bech32_out;

Cardano::getBech32Address( masterkey, Cardano::InputKey::MasterKey, Cardano::Network::Testnet, Cardano::Wallet::HD,
                           Cardano::Address::Base_Extern, account_index, address_index, address_bech32_out );

Function:

Generates an address in hexadecimal format ( for example: 0xe0a112..), from a Master key or a Public/Private account key.

void getRawAddress( std::uint8_t const * const input_key, Cardano::InputKey const input_key_type, 
                    Cardano::Network const network_id, Cardano::Wallet const wallet_type, 
                    Cardano::Address const address_type, std::uint32_t const account_path, 
                    std::uint32_t const address_index_path,
                    std::uint8_t * const output_raw, std::uint8_t * const output_raw_len );

Return:

Does not return, but the function generates exceptions of type std::logic_error. output_raw contains the address in raw and output_raw_len indicate the length.

Example:

#include <cardanoplusplus.h>
// 1852H/1815H/1H/2/0

.....
.....

std::uint32_t account_index = 1;
std::uint32_t address_index = 0;
std::uint8_t raw_address_stake[RAWADDRESS_LENGTH_MAX];
std::uint8_t raw_address_stake_len;

void Cardano::getRawAddress( masterkey, Cardano::InputKey::MasterKey, Cardano::Network::Testnet, Cardano::Wallet::HD,
                        Cardano::Address::Stake, account_index, address_index, raw_address_stake, &raw_address_stake_len );

Function:

Generates an address in bech32 format ( for example: addr1....), from a script plutus V1/V2.


void getBech32AddressfromScript( std::string const &script, Cardano::ScriptType const script_type, 
                                 Cardano::Network const network_id,Cardano::ScriptAddress const address_type, 
                                 std::string & address_out );

Return:

Does not return, but the function generates exceptions of type std::logic_error. Address_out contains the address in bech32 (addr1..).

Example:

#include <cardanoplusplus.h>
.....
.....
std::string script_v1 = "49480100002221200101";
std::string scriptv1_address;
    
Cardano::getBech32AddressfromScript( script_v1, Cardano::ScriptType::Plutus_Script_V1, Cardano::Network::Testnet,
                                     Cardano::ScriptAddress::Payment, scriptv1_address );

Create public and private keys

Function: Generates a public/private extended key in hexadecimal format, from a private extended master key or account key (public/private).

bool getRawKey( std::uint8_t const * const input_key, Cardano::InputKey input_key_type, Cardano::Wallet wallet_type, 
                Cardano::OutputKey output_key_type, std::uint32_t const account_path, Cardano::Role role_path, 
                std::uint32_t const address_index_path, std::uint8_t * const output_key ) noexcept;
bool getRawKey( std::uint8_t const * const input_key, Cardano::InputKey input_key_type, Cardano::Wallet wallet_type,
                Cardano::OutputKey output_key_type,std::uint32_t const account_path, Cardano::Role OnlyAccount, 
                std::uint8_t * const output_key ) noexcept;

Return:

Returns a bool, true if successful or false if an error is raised. output_key receives the public/private key, the length of the key varies depending on the case, for public keys the length is 64 bytes, for private keys it is 96 bytes. You can indicate the length of the array, with the help of the macros XVK_LENGTH (for public key) and XSK_LENGTH (for private key)

Example:

#include <cardanoplusplus.h>

// 1852H/1815H/2H/1/0
// create a private change address key
// Role --> Extern = base address(0), Intern = change address(1), Staking = (2), OnlyAccount = (3)

std::uint32_t account_index = 2;
std::uint32_t address_index = 0;
std::uint8_t xsk_changeaddress[XSK_LENGTH]; // extended signing key

Cardano::getRawKey( masterkey, Cardano::InputKey::MasterKey, Cardano::Wallet::HD, Cardano::OutputKey::Private,
                    account_index, Cardano::Role::Intern, address_index, xsk_changeaddress );
#include <cardanoplusplus.h>

// 1852H/1815H/2H
// create a public account key

std::uint32_t account_index = 2;
std::uint8_t xvk_account[XVK_LENGTH]; // extended verify key

Cardano::getRawKey( masterkey, Cardano::InputKey::MasterKey, Cardano::Wallet::HD, Cardano::OutputKey::Public, 
                    account_index, Cardano::Role::OnlyAccount, xvk_account ); 

Transactions

For any action that requires interacting with the blockchain, the Transaction class must be used. The transaction class has a varied palette of subclasses, which are divided according to their function, as can be seen below:

Image

Send Adas

In this example, 40 adas are sent to the address "enterprise_address" and the remaining adas are returned to the "change_address" of the wallet. For this, the ADAs that are contained in two utxos will be used. The transaction is built by calling the Build() function, which is serialized in cbor and the content of this construction must be uploaded to the blockchain by the method you like the most (graphql, cardano-submit, ogmios).

std::string enterprise_address = "addr1q45t2..........................h567";
std::string change_address = "addr1q68h3..............................q567";
std::uint64_t fee = 18000;
std::uint64_t slot = 1980000;
std::uint8_t xsk_baseaddress[XSK_LENGTH] = {0x81,0x64,...........,0xa2};

Cardano::Transaction tx;
tx.Body.TransactionInput.addInput("eb163cfdf6033c39b5e148b18949af8711a6c72d50f080dd4ddf6b9d33ed0b20", 1).addInput("eb163cfdf6033c39b5e148b18949af8711a6c72d50f080dd4ddf6b9d33ed0b20", 0);
tx.Body.TransactionOutput.addOutput(enterprise_address, 40000000 ).addOutput(change_address, 12000000);
tx.Body.addFee(fee).addInvalidAfter(slot + 300);
tx.addExtendedSigningKey(xsk_baseaddress);
std::vector<std::uint8_t> const & tx_build = tx.Build();

Send Tokens

In this example, two different tokens are sent to the address "enterprise_address" and the remaining tokens are returned to the "change_address" of the wallet. The transaction is built by calling the Build() function, which is serialized in cbor and the content of this construction must be uploaded to the blockchain by the method you like the most (graphql, cardano-submit, ogmios).

std::string enterprise_address = "addr1q45t2..........................h567";
std::string change_address = "addr1q68h3..............................q567";
std::uint64_t fee = 18000;
std::uint64_t slot = 1980000;
std::uint8_t xsk_baseaddress[XSK_LENGTH] = {0x81,0x64,...........,0xa2};

Cardano::Transaction tx;
tx.Body.TransactionInput.addInput("eb163cfdf6033c39b5e148b18949af8711a6c72d50f080dd4ddf6b9d33ed0b20", 1);
tx.Body.TransactionOutput.addOutput(enterprise_address, 5000000 )
                         .addAsset("0ea7b6e1ac188ed5cc00a6ce904736071b86be7c884a14f5686a9c25","Tric",60)
                         .addAsset("065270479316f1d92e00f7f9f095ebeaac9d009c878dc35ce36d3404","Ahue",10);
tx.Body.TransactionOutput.addOutput(change_address, 85000000)
                         .addAsset("0ea7b6e1ac188ed5cc00a6ce904736071b86be7c884a14f5686a9c25","Tric",140)
                         .addAsset("065270479316f1d92e00f7f9f095ebeaac9d009c878dc35ce36d3404","Ahue",290);
tx.Body.addFee(fee).addInvalidAfter(slot + 300);
tx.addExtendedSigningKey(xsk_baseaddress);
std::vector<std::uint8_t> const & tx_build = tx.Build();