-
Notifications
You must be signed in to change notification settings - Fork 3
/
customizable_ui.c
136 lines (130 loc) · 4.12 KB
/
customizable_ui.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
/*******************************************************************************
* Ledger App - Bitcoin Wallet
* (c) 2016-2019 Ledger
*
* 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
*
* http://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.
********************************************************************************/
#include "customizable_ui.h"
#include "be_operations.h"
#include "cashaddr.h"
#include "context.h"
#include "customizable_helpers.h"
#include "display_utils.h"
#include "display_variables.h"
#include "helpers.h"
#include "read.h"
#include "segwit_addr.h"
/*
* Function: get_address_from_output_script
* -----------------------------------------
* Retrieves the address from a given output script and stores it in the
* provided buffer.
*
* Parameters:
* - script: Pointer to the output script data.
* - script_size: Size of the output script.
* - out: Pointer to the buffer where the address will be stored.
* - out_size: Size of the output buffer.
*
*/
WEAK void get_address_from_output_script(unsigned char *script, int script_size,
char *out, int out_size) {
if (output_script_is_op_return(script)) {
strncpy(out, "OP_RETURN", out_size);
return;
}
if ((COIN_KIND == COIN_KIND_HYDRA) &&
output_script_is_op_create(script, script_size)) {
strncpy(out, "OP_CREATE", out_size);
return;
}
if ((COIN_KIND == COIN_KIND_HYDRA) &&
output_script_is_op_call(script, script_size)) {
strncpy(out, "OP_CALL", out_size);
return;
}
if (output_script_is_native_witness(script)) {
if (COIN_NATIVE_SEGWIT_PREFIX) {
segwit_addr_encode(
out, (char *)PIC(COIN_NATIVE_SEGWIT_PREFIX), 0,
script + OUTPUT_SCRIPT_NATIVE_WITNESS_PROGRAM_OFFSET,
script[OUTPUT_SCRIPT_NATIVE_WITNESS_PROGRAM_OFFSET - 1]);
}
return;
}
unsigned char versionSize;
unsigned char address[22];
unsigned short textSize;
int addressOffset = 3;
unsigned short version = COIN_P2SH_VERSION;
if (output_script_is_regular(script)) {
addressOffset = 4;
version = COIN_P2PKH_VERSION;
}
if (version > 255) {
versionSize = 2;
address[0] = (version >> 8);
address[1] = version;
} else {
versionSize = 1;
address[0] = version;
}
memmove(address + versionSize, script + addressOffset, 20);
// Prepare address
if (context.usingCashAddr) {
cashaddr_encode(
address + versionSize, 20, (uint8_t *)out, out_size,
(version == COIN_P2SH_VERSION ? CASHADDR_P2SH : CASHADDR_P2PKH));
} else {
textSize = public_key_to_encoded_base58(
address, 20 + versionSize, (unsigned char *)out, out_size, version, 1);
out[textSize] = '\0';
}
}
/*
* Function: prepare_fees
* -----------------------
* Prepares the fee amount for display
*
* Returns:
* - 1 if the fee amount is successfully prepared.
* - 0 if an error occurs.
*/
WEAK uint8_t prepare_fees(void) {
if (context.transactionContext.relaxed) {
memmove(vars.tmp.feesAmount, "UNKNOWN", 7);
vars.tmp.feesAmount[7] = '\0';
} else {
unsigned char fees[8];
unsigned char borrow;
borrow = transaction_amount_sub_be(
fees, context.transactionContext.transactionAmount,
context.totalOutputAmount);
if (borrow && COIN_KIND == COIN_KIND_KOMODO) {
memmove(vars.tmp.feesAmount, "REWARD", 6);
vars.tmp.feesAmount[6] = '\0';
} else {
if (borrow) {
PRINTF("Error : Fees not consistent");
goto error;
}
format_sats_amount(
COIN_COINID_SHORT,
(uint64_t)read_u64_be(fees, 0), // Cast prevents weird compilo bug
vars.tmp.feesAmount);
}
}
return 1;
error:
return 0;
}