Skip to content

Commit

Permalink
Merge pull request #264 from keepkey/uniswap-liquidity-tx
Browse files Browse the repository at this point in the history
Uniswap liquidity tx
  • Loading branch information
markrypto authored Apr 9, 2021
2 parents 588e427 + bb8d82a commit 3ddcc5d
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.7.2)

project(KeepKeyFirmware

VERSION 7.0.3
VERSION 7.1.0

LANGUAGES C CXX ASM)

Expand Down
33 changes: 33 additions & 0 deletions include/keepkey/firmware/ethereum_contracts/zxappliquid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2021 ShapeShift
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPKEY_FIRMWARE_ETHEREUMCONTRACTS_ZXAPPLIQUIDTX_H
#define KEEPKEY_FIRMWARE_ETHEREUMCONTRACTS_ZXAPPLIQUIDTX_H

#include <inttypes.h>
#include <stdbool.h>

#define MAX_ALLOWANCE "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"

typedef struct _EthereumSignTx EthereumSignTx;

bool zx_isZxApproveLiquid(const EthereumSignTx *msg);
bool zx_confirmApproveLiquidity(uint32_t data_total, const EthereumSignTx *msg);

#endif
33 changes: 33 additions & 0 deletions include/keepkey/firmware/ethereum_contracts/zxliquidtx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2021 ShapeShift
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPKEY_FIRMWARE_ETHEREUMCONTRACTS_ZXLIQUIDTX_H
#define KEEPKEY_FIRMWARE_ETHEREUMCONTRACTS_ZXLIQUIDTX_H

#include <inttypes.h>
#include <stdbool.h>

#define UNISWAP_ROUTER_ADDRESS "\x7a\x25\x0d\x56\x30\xB4\xcF\x53\x97\x39\xdF\x2C\x5d\xAc\xb4\xc6\x59\xF2\x48\x8D"

typedef struct _EthereumSignTx EthereumSignTx;

bool zx_isZxLiquidTx(const EthereumSignTx *msg);
bool zx_confirmZxLiquidTx(uint32_t data_total, const EthereumSignTx *msg);

#endif
2 changes: 2 additions & 0 deletions include/keepkey/firmware/ethereum_tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extern const TokenType tokens[];

extern const TokenType *UnknownToken;

const TokenType *tokenIter(int32_t *ctr);

const TokenType *tokenByChainAddress(uint8_t chain_id, const uint8_t *address);

/// Tokens don't have unique tickers, so this might not return the one you're
Expand Down
2 changes: 2 additions & 0 deletions lib/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set(sources
ethereum.c
ethereum_contracts.c
ethereum_contracts/makerdao.c
ethereum_contracts/zxappliquid.c
ethereum_contracts/zxliquidtx.c
ethereum_contracts/zxswap.c
ethereum_tokens.c
exchange.c
Expand Down
10 changes: 10 additions & 0 deletions lib/firmware/ethereum_contracts.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "keepkey/firmware/ethereum_contracts.h"

#include "keepkey/firmware/ethereum_contracts/zxappliquid.h"
#include "keepkey/firmware/ethereum_contracts/zxliquidtx.h"
#include "keepkey/firmware/ethereum_contracts/zxswap.h"
#include "keepkey/firmware/ethereum_contracts/makerdao.h"

Expand All @@ -28,6 +30,8 @@ bool ethereum_contractHandled(uint32_t data_total, const EthereumSignTx *msg,


if (zx_isZxSwap(msg)) return true;
if (zx_isZxLiquidTx(msg)) return true;
if (zx_isZxApproveLiquid(msg)) return true;

if (makerdao_isMakerDAO(data_total, msg)) return true;

Expand All @@ -40,6 +44,12 @@ bool ethereum_contractConfirmed(uint32_t data_total, const EthereumSignTx *msg,

if (zx_isZxSwap(msg))
return zx_confirmZxSwap(data_total, msg);

if (zx_isZxLiquidTx(msg))
return zx_confirmZxLiquidTx(data_total, msg);

if (zx_isZxApproveLiquid(msg))
return zx_confirmApproveLiquidity(data_total, msg);

if (makerdao_isMakerDAO(data_total, msg))
return makerdao_confirmMakerDAO(data_total, msg);
Expand Down
112 changes: 112 additions & 0 deletions lib/firmware/ethereum_contracts/zxappliquid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2021 ShapeShift
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "keepkey/firmware/ethereum_contracts/zxappliquid.h"
#include "keepkey/firmware/ethereum_contracts/zxliquidtx.h"

#include "keepkey/board/confirm_sm.h"
#include "keepkey/board/util.h"
#include "keepkey/firmware/app_confirm.h"
#include "keepkey/firmware/coins.h"
#include "keepkey/firmware/ethereum.h"
#include "keepkey/firmware/ethereum_tokens.h"
#include "keepkey/firmware/fsm.h"
#include "keepkey/firmware/storage.h"
#include "trezor/crypto/address.h"
#include "trezor/crypto/bip32.h"
#include "trezor/crypto/curves.h"
#include "trezor/crypto/memzero.h"
#include "trezor/crypto/sha3.h"

bool zx_confirmApproveLiquidity(uint32_t data_total, const EthereumSignTx *msg) {
(void)data_total;
const char *to, *tikstr, *poolstr, *allowance, *amt;
unsigned char data[40];
uint8_t digest[SHA3_256_DIGEST_LENGTH] = {0};
uint8_t tokdigest[SHA3_256_DIGEST_LENGTH] = {0};
char digestStr[2*SHA3_256_DIGEST_LENGTH+1], amtStr[2*32+1] = {0};
int32_t ctr, tokctr;
uint32_t wethord, ttokenord;
const TokenType *WETH, *ttoken;

if (!tokenByTicker(msg->chain_id, "WETH", &WETH)) return false;
wethord = read_be((const uint8_t *)WETH->address);
to = (const char *)msg->to.bytes;
tokctr = 0;
while (tokctr != -1) {
ttoken = tokenIter(&tokctr);

//https://uniswap.org/docs/v2/smart-contract-integration/getting-pair-addresses/
ttokenord = read_be((const uint8_t *)ttoken->address);
if (ttokenord < wethord) {
memcpy(data, ttoken->address, 20);
memcpy(&data[20], WETH->address, 20);
} else {
memcpy(data, WETH->address, 20);
memcpy(&data[20], ttoken->address, 20);
}
keccak_256(data, sizeof(data), tokdigest);
SHA3_CTX ctx = {0};
keccak_256_Init(&ctx);
keccak_Update(&ctx, (unsigned char *)"\xff", 1);
keccak_Update(&ctx, (unsigned char *)"\x5C\x69\xbE\xe7\x01\xef\x81\x4a\x2B\x6a\x3E\xDD\x4B\x16\x52\xCB\x9c\xc5\xaA\x6f", 20);
keccak_Update(&ctx, tokdigest, sizeof(tokdigest));
keccak_Update(&ctx, (unsigned char *)"\x96\xe8\xac\x42\x77\x19\x8f\xf8\xb6\xf7\x85\x47\x8a\xa9\xa3\x9f\x40\x3c\xb7\x68\xdd\x02\xcb\xee\x32\x6c\x3e\x7d\xa3\x48\x84\x5f", 32);
keccak_Final(&ctx, digest);
if (memcmp(to, &digest[12], 20) == 0) break;
}

if (tokctr != -1) {
for (ctr=0; ctr<SHA3_256_DIGEST_LENGTH; ctr++) {
snprintf(&digestStr[ctr*2], 3, "%02x", digest[ctr]);
}
tikstr = ttoken->ticker;
poolstr = &digestStr[12*2];
} else {
for (ctr=0; ctr<20; ctr++) {
snprintf(&digestStr[ctr*2], 3, "%02x", to[ctr]);
}
tikstr = "";
poolstr = digestStr;
}

allowance = (char *)(msg->data_initial_chunk.bytes + 4 +32);
if (memcmp(allowance, (uint8_t *)&MAX_ALLOWANCE, 32) == 0) {
amt = "full balance";
} else {
for (ctr=0; ctr<32; ctr++) {
snprintf(&amtStr[ctr*2], 3, "%02x", allowance[ctr]);
}
amt = amtStr;
}

char *appStr = "uniswap approve liquidity";
confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, appStr,
"Amount: %s", amt);
confirm(ButtonRequestType_ButtonRequest_ConfirmOutput, appStr,
"approve for pool %s %s", tikstr, poolstr);
return true;
}

bool zx_isZxApproveLiquid(const EthereumSignTx *msg) {
if (memcmp(msg->data_initial_chunk.bytes, "\x09\x5e\xa7\xb3", 4) == 0)
if (memcmp((uint8_t *)(msg->data_initial_chunk.bytes + 4 + 32 - 20), UNISWAP_ROUTER_ADDRESS, 20) == 0)
return true;
return false;
}
Loading

0 comments on commit 3ddcc5d

Please sign in to comment.