|
| 1 | +/* |
| 2 | + * This file is part of the KeepKey project. |
| 3 | + * |
| 4 | + * Copyright (C) 2022 markrypto |
| 5 | + * |
| 6 | + * This library is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "keepkey/firmware/ethereum_contracts/confuncs.h" |
| 21 | + |
| 22 | +#include "keepkey/board/confirm_sm.h" |
| 23 | +#include "keepkey/board/util.h" |
| 24 | +#include "keepkey/firmware/ethereum.h" |
| 25 | +#include "keepkey/firmware/ethereum_tokens.h" |
| 26 | +#include "keepkey/firmware/fsm.h" |
| 27 | +#include "trezor/crypto/address.h" |
| 28 | + |
| 29 | +bool cf_isExecTx(const EthereumSignTx *msg) { |
| 30 | + if (memcmp(msg->data_initial_chunk.bytes, EXEC_TRANSACTION, 4) == 0) |
| 31 | + return true; |
| 32 | + |
| 33 | + return false; |
| 34 | +} |
| 35 | + |
| 36 | +bool cf_confirmExecTx(uint32_t data_total, const EthereumSignTx *msg) { |
| 37 | + extern const ecdsa_curve secp256k1; |
| 38 | + (void)data_total; |
| 39 | + char confStr[131]; |
| 40 | + char contractStr[41]; |
| 41 | + uint8_t *to, *gasToken, *refundReceiver, *data; |
| 42 | + bignum256 bnNum, gasPrice; |
| 43 | + char txStr[32], safeGasStr[32], baseGasStr[32], gasPriceStr[32]; |
| 44 | + TokenType const *TokenData; |
| 45 | + int8_t *operation; |
| 46 | + uint32_t offset, dlen; |
| 47 | + char const *confDatStr, *confDatStr2; |
| 48 | + unsigned ctr, n, chunk, chunkSize; |
| 49 | + const char *title = "contract func exec_tx"; |
| 50 | + |
| 51 | + to = (uint8_t *)(msg->data_initial_chunk.bytes + 4 + 0*32 + 12); |
| 52 | + for (ctr=0; ctr<20; ctr++) { |
| 53 | + snprintf(&confStr[ctr*2], 3, "%02x", to[ctr]); |
| 54 | + } |
| 55 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 56 | + title, "Sending to %s", confStr)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // value |
| 61 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 1*32, 32, &bnNum); |
| 62 | + ethereumFormatAmount(&bnNum, NULL, 1 /*chainId*/, txStr, sizeof(txStr)); |
| 63 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 64 | + title, "amount %s", txStr)) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // get data bytes |
| 69 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 2*32, 32, &bnNum); // data offset |
| 70 | + offset = bn_write_uint32(&bnNum); |
| 71 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + offset, 32, &bnNum); // data len |
| 72 | + dlen = bn_write_uint32(&bnNum); |
| 73 | + data = (uint8_t *)(msg->data_initial_chunk.bytes + 4 + 32 + offset); |
| 74 | + |
| 75 | + n = 1; |
| 76 | + chunkSize = 39; |
| 77 | + while (true) { |
| 78 | + chunk=chunkSize*(n-1); |
| 79 | + for (ctr=chunk; ctr<chunkSize+chunk && ctr<dlen; ctr++) { |
| 80 | + snprintf(&confStr[(ctr-chunk)*2], 3, "%02x", data[ctr]); |
| 81 | + } |
| 82 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 83 | + title, "Data payload %d: %s", n, confStr)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (ctr >= dlen) { |
| 87 | + break; |
| 88 | + } |
| 89 | + n++; |
| 90 | + } |
| 91 | + |
| 92 | + // operation is an enum: https://github.com/safe-global/safe-contracts/blob/main/contracts/common/Enum.sol#L7 |
| 93 | + operation = (int8_t *)(msg->data_initial_chunk.bytes + 4 + 3*32); |
| 94 | + { |
| 95 | + char *opStr; |
| 96 | + switch (*operation) { |
| 97 | + case 0: |
| 98 | + opStr = "Call"; |
| 99 | + break; |
| 100 | + case 1: |
| 101 | + opStr = "DelegateCall"; |
| 102 | + break; |
| 103 | + default: |
| 104 | + opStr = "Unknown"; |
| 105 | + } |
| 106 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 107 | + title, "Operation: %s", opStr)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 6*32, 32, &gasPrice); // used for payment calc |
| 113 | + ethereumFormatAmount(&gasPrice, NULL, 1 /*chainId*/, gasPriceStr, sizeof(gasPriceStr)); |
| 114 | + |
| 115 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 4*32, 32, &bnNum); // safe transaction gas |
| 116 | + bn_multiply(&gasPrice, &bnNum, &secp256k1.prime); |
| 117 | + ethereumFormatAmount(&bnNum, NULL, 1 /*chainId*/, safeGasStr, sizeof(safeGasStr)); |
| 118 | + |
| 119 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 5*32, 32, &bnNum); // independent gas needed |
| 120 | + bn_multiply(&gasPrice, &bnNum, &secp256k1.prime); |
| 121 | + ethereumFormatAmount(&bnNum, NULL, 1 /*chainId*/, baseGasStr, sizeof(baseGasStr)); |
| 122 | + |
| 123 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 124 | + title, "Safe tx gas: %s\nBase gas: %s\nGas price: %s", safeGasStr, baseGasStr, gasPriceStr)) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + // gas token |
| 129 | + gasToken = (uint8_t *)(msg->data_initial_chunk.bytes + 4 + 7*32 + 12); // token to be used for gas payment |
| 130 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 7*32, 32, &bnNum); // used to check for zero |
| 131 | + if (bn_is_zero(&bnNum)) { |
| 132 | + // gas payment in ETH |
| 133 | + confDatStr = "ETH"; |
| 134 | + } else { |
| 135 | + // gas payment in token |
| 136 | + TokenData = tokenByChainAddress(1 /*chainId*/, (uint8_t *)gasToken); |
| 137 | + if (strncmp(TokenData->ticker, " UNKN", 5) == 0) { |
| 138 | + for (ctr=0; ctr<20; ctr++) { |
| 139 | + snprintf(&contractStr[2*ctr], 3, "%02x", TokenData->address[ctr]); |
| 140 | + } |
| 141 | + confDatStr = contractStr; |
| 142 | + } else { |
| 143 | + confDatStr = TokenData->ticker; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + refundReceiver = (uint8_t *)(msg->data_initial_chunk.bytes + 4 + 8*32 + 12); // gas refund receiver |
| 148 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 8*32, 32, &bnNum); // used to check for zero |
| 149 | + if (bn_is_zero(&bnNum)) { |
| 150 | + // gas refund receiver is origin |
| 151 | + confDatStr2 = "tx origin"; |
| 152 | + } else { |
| 153 | + // gas refund receiver address |
| 154 | + for (ctr=0; ctr<20; ctr++) { |
| 155 | + snprintf(&contractStr[2*ctr], 3, "%02x", refundReceiver[ctr]); |
| 156 | + } |
| 157 | + confDatStr2 = contractStr; |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 162 | + title, "Gas payment token: %s\nGas refund address: %s", confDatStr, confDatStr2)) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + // get signature data |
| 167 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + 9*32, 32, &bnNum); // sig offset |
| 168 | + offset = bn_write_uint32(&bnNum); |
| 169 | + bn_from_bytes(msg->data_initial_chunk.bytes + 4 + offset, 32, &bnNum); // sig data len |
| 170 | + dlen = bn_write_uint32(&bnNum); |
| 171 | + data = (uint8_t *)(msg->data_initial_chunk.bytes + 4 + 32 + offset); |
| 172 | + |
| 173 | + |
| 174 | + n = 1; |
| 175 | + chunkSize = 65; |
| 176 | + while (true) { |
| 177 | + chunk=chunkSize*(n-1); |
| 178 | + for (ctr=chunk; ctr<chunkSize+chunk && ctr<dlen; ctr++) { |
| 179 | + snprintf(&confStr[(ctr-chunk)*2], 3, "%02x", data[ctr]); |
| 180 | + } |
| 181 | + if (!confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, |
| 182 | + title, "Signature %d: %s", n, confStr)) { |
| 183 | + return false; |
| 184 | + } |
| 185 | + if (ctr >= dlen) { |
| 186 | + break; |
| 187 | + } |
| 188 | + n++; |
| 189 | + } |
| 190 | + return true; |
| 191 | +} |
0 commit comments