forked from Biglup/cardano-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_lovelace_example.c
163 lines (130 loc) · 5.81 KB
/
send_lovelace_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* \file send_lovelace_example.c
*
* \author angel.castillo
* \date Nov 08, 2024
*
* Copyright 2024 Biglup Labs
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* INCLUDES ******************************************************************/
#include "providers/provider_factory.h"
#include "utils/console.h"
#include "utils/utils.h"
#include <cardano/cardano.h>
#include <stdlib.h>
#include <string.h>
/* CONSTANTS *****************************************************************/
static const char* RECEIVING_ADDRESS = "addr_test1qr0c3frkem9cqn5f73dnvqpena27k2fgqew6wct9eaka03agfwkvzr0zyq7nqvcj24zehrshx63zzdxv24x3a4tcnfeq9zwmn7";
static const char* SERIALIZED_BIP32_KEY_HANDLER = "0a0a0a0a01010000005c97db5e09b3a4919ec75ed1126056241a1e5278731c2e0b01bea0a5f42c22db4131e0a4bbe75633677eb0e60e2ecd3520178f85c7e0d4be77a449087fe9674ee52f946b07c1b56d228c496ec0d36dd44212ba8af0f6eed1a82194dd69f479c603";
static const uint64_t CONFIRM_TX_TIMEOUT_MS = 240000U;
static const int64_t LOVELACE_TO_SEND = 2000000U;
static const uint64_t PAYMENT_CRED_INDEX = 0U;
static const uint64_t STAKE_CRED_INDEX = 0U;
static const uint64_t SECONDS_IN_TWO_HOURS = 60UL * 60UL * 2UL;
static const cardano_account_derivation_path_t ACCOUNT_DERIVATION_PATH = {
.purpose = 1852U | 0x80000000,
.coin_type = 1815U | 0x80000000,
.account = 0U
};
static const cardano_derivation_path_t SIGNER_DERIVATION_PATH = {
.purpose = 1852U | 0x80000000,
.coin_type = 1815U | 0x80000000,
.account = 0U,
.role = 0U,
.index = 0U
};
/* DECLARATIONS **************************************************************/
/**
* \brief Retrieves the password for the secure key handler.
*
* \param buffer The buffer where to write the password.
* \param buffer_len The size of the buffer.
*
* \return The length of the password (or -1 on error).
*/
static int32_t
get_passphrase(byte_t* buffer, const size_t buffer_len)
{
console_warn("Enter passphrase: ");
char password[128] = { 0 };
const int32_t password_len = console_read_password(password, sizeof(password));
if (buffer_len < password_len)
{
return -1;
}
cardano_utils_safe_memcpy(buffer, buffer_len, &password[0], password_len);
// Clear local password from memory
cardano_memzero(password, sizeof(password));
return password_len;
}
/* MAIN **********************************************************************/
/**
* \brief Entry point of the program.
*
* \return Returns `0` on successful execution, or a non-zero value if there is an error.
*/
int
main(void)
{
console_info("Send lovelace Example");
console_info("libcardano-c: V-%s\n", cardano_get_lib_version());
console_info("This example will send %d lovelace to the receiving address: %s.", LOVELACE_TO_SEND, RECEIVING_ADDRESS);
console_set_foreground_color(CONSOLE_COLOR_GREEN);
console_write("\nUse passphrase: 'password'\n\n");
console_reset_color();
const char* api_key = getenv("BLOCKFROST_API_KEY");
if (api_key == NULL)
{
console_error("BLOCKFROST_API_KEY environment variable is not set.\n");
return EXIT_FAILURE;
}
// 0.- Initialize dependencies
cardano_secure_key_handler_t* key_handler = create_secure_key_handler(SERIALIZED_BIP32_KEY_HANDLER, cardano_utils_safe_strlen(SERIALIZED_BIP32_KEY_HANDLER, 256), get_passphrase);
cardano_provider_t* provider = create_provider(CARDANO_NETWORK_MAGIC_PREPROD, api_key);
cardano_address_t* payment_address = create_address_from_derivation_paths(key_handler, ACCOUNT_DERIVATION_PATH, PAYMENT_CRED_INDEX, STAKE_CRED_INDEX);
cardano_utxo_list_t* utxo_list = get_unspent_utxos(provider, payment_address);
cardano_protocol_parameters_t* protocol_params = get_protocol_parameters(provider);
// 2 hours from now in UNIX time (seconds)
const uint64_t invalid_after = cardano_utils_get_time() + SECONDS_IN_TWO_HOURS;
// 1.- Build transaction
console_info("Building transaction...");
cardano_tx_builder_t* tx_builder = cardano_tx_builder_new(protocol_params, provider);
cardano_tx_builder_set_utxos(tx_builder, utxo_list);
cardano_tx_builder_set_change_address(tx_builder, payment_address);
cardano_tx_builder_set_invalid_after_ex(tx_builder, invalid_after);
cardano_tx_builder_send_lovelace_ex(tx_builder, RECEIVING_ADDRESS, cardano_utils_safe_strlen(RECEIVING_ADDRESS, 128), LOVELACE_TO_SEND);
cardano_transaction_t* transaction = NULL;
cardano_error_t result = cardano_tx_builder_build(tx_builder, &transaction);
if (result != CARDANO_SUCCESS)
{
console_error("Failed to build transaction");
console_error("Error [%d]: %s", result, cardano_error_to_string(result));
console_error("%s", cardano_tx_builder_get_last_error(tx_builder));
return result;
}
// 2.- Sign transaction
sign_transaction(key_handler, SIGNER_DERIVATION_PATH, transaction);
// 3.- Submit transaction & confirm
submit_transaction(provider, CONFIRM_TX_TIMEOUT_MS, transaction);
// Cleanup
cardano_provider_unref(&provider);
cardano_address_unref(&payment_address);
cardano_utxo_list_unref(&utxo_list);
cardano_protocol_parameters_unref(&protocol_params);
cardano_tx_builder_unref(&tx_builder);
cardano_transaction_unref(&transaction);
cardano_secure_key_handler_unref(&key_handler);
return EXIT_SUCCESS;
}