Skip to content

Commit

Permalink
Merge pull request #109 from KeystoneHQ/v1.0.0
Browse files Browse the repository at this point in the history
merge V1.0.0
  • Loading branch information
soralit authored Sep 26, 2023
2 parents b3fc80d + 1c12aaa commit c474d3c
Show file tree
Hide file tree
Showing 53 changed files with 1,347 additions and 716 deletions.
8 changes: 8 additions & 0 deletions external/lvgl/src/widgets/lv_btnmatrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ static void lv_btnmatrix_event(const lv_obj_class_t * class_p, lv_event_t * e)
/*Search the pressed area*/
lv_indev_get_point(param, &p);
btn_pr = get_button_from_point(obj, &p);
if (btn_pr != LV_BTNMATRIX_BTN_NONE) {
if (button_is_inactive(btnm->ctrl_bits[btn_pr]) == true) {
btnm->btn_id_sel = btn_pr;
invalidate_button_area(obj, btnm->btn_id_sel); /*Invalidate the new area*/
return;
}
}

/*Handle the case where there is no button there*/
if (btn_pr != LV_BTNMATRIX_BTN_NONE) {
if (button_is_inactive(btnm->ctrl_bits[btn_pr]) == false &&
Expand Down
2 changes: 1 addition & 1 deletion rust/apps/bitcoin/src/transactions/psbt/wrapped_psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl WrappedPsbt {
if !tx_in.previous_output.txid.eq(&prev_tx.txid()) {
return Err(BitcoinError::InvalidInput);
}
let prevout = unsigned_tx.output.get(tx_in.previous_output.vout as usize);
let prevout = prev_tx.output.get(tx_in.previous_output.vout as usize);
match prevout {
Some(out) => {
value = out.value;
Expand Down
4 changes: 2 additions & 2 deletions rust/apps/cosmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ mod tests {
let root_path = "44'/118'/0'";
let root_xpub = "xpub6DGzViq8bmgMLYdVZ3xnLVEdKwzBnGdzzJZ4suG8kVb9TTLAbrwv8YdKBb8FWKdBNinaHKmBv7JpQvqBYx4rxch7WnHzNFzSVrMf8hQepTP";
let hd_path = "44'/118'/0'/0/0";
let address = derive_address(hd_path, root_xpub, root_path, "certik").unwrap();
assert_eq!("certik19rl4cm2hmr8afy4kldpxz3fka4jguq0amg3277", address);
let address = derive_address(hd_path, root_xpub, root_path, "shentu").unwrap();
assert_eq!("shentu19rl4cm2hmr8afy4kldpxz3fka4jguq0a55fydg", address);
}
{
//general address CRO-0
Expand Down
2 changes: 1 addition & 1 deletion rust/apps/cosmos/src/transaction/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn get_chain_id_by_address(address: &str) -> String {
map.insert("akash", "akashnet-2");
map.insert("cro", "crypto-org-chain-mainnet-1");
map.insert("sif", "sifchain-1");
map.insert("certik", "shentu-2.2");
map.insert("shentu", "shentu-2.2");
map.insert("iaa", "irishub-1");
map.insert("regen", "regen-1");
map.insert("persistence", "core-1");
Expand Down
140 changes: 85 additions & 55 deletions rust/rust_c/src/interfaces/ethereum/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::string::{String, ToString};
use alloc::{format, slice};

use app_ethereum::errors::EthereumError;
use app_ethereum::{
parse_fee_market_tx, parse_legacy_tx, parse_personal_message, parse_typed_data_message,
};
use keystore::algorithms::secp256k1::derive_public_key;
use third_party::hex;
use third_party::ur_registry::ethereum::eth_sign_request::EthSignRequest;
use third_party::ur_registry::ethereum::eth_signature::EthSignature;
use third_party::ur_registry::traits::RegistryItem;
Expand All @@ -21,7 +20,7 @@ use crate::interfaces::ethereum::structs::{
use crate::interfaces::structs::{TransactionCheckResult, TransactionParseResult};
use crate::interfaces::types::{PtrBytes, PtrString, PtrT, PtrUR};
use crate::interfaces::ur::{UREncodeResult, FRAGMENT_MAX_LENGTH_DEFAULT};
use crate::interfaces::utils::recover_c_char;
use crate::interfaces::utils::{convert_c_char, recover_c_char};
use crate::interfaces::KEYSTONE;

mod abi;
Expand Down Expand Up @@ -64,30 +63,75 @@ pub extern "C" fn eth_check(
}
}

#[no_mangle]
pub extern "C" fn eth_get_root_path(ptr: PtrUR) -> PtrString
{
let eth_sign_request = extract_ptr_with_type!(ptr, EthSignRequest);
let derivation_path: third_party::ur_registry::crypto_key_path::CryptoKeyPath =
eth_sign_request.get_derivation_path();
if let Some(path) = derivation_path.get_path() {
if let Some(root_path) = parse_eth_root_path(path) {
return convert_c_char(root_path);
}
}
return convert_c_char("".to_string())
}

fn parse_eth_root_path(path: String) -> Option<String> {
let root_path = "44'/60'/";
match path.strip_prefix(root_path) {
Some(path) => {
if let Some(index) = path.find("/") {
let sub_path = &path[..index];
Some(format!("{}{}", root_path, sub_path))
} else {
None
}
}
None => None,
}
}

fn parse_eth_sub_path(path: String) -> Option<String> {
let root_path = "44'/60'/";
match path.strip_prefix(root_path) {
Some(path) => {
if let Some(index) = path.find("/") {
Some(path[index + 1..].to_string())
} else {
None
}
}
None => None,
}
}

fn try_get_eth_public_key(
xpub: String,
eth_sign_request: EthSignRequest,
) -> Result<third_party::secp256k1::PublicKey, RustCError> {
match eth_sign_request.get_derivation_path().get_path() {
None => Err(RustCError::InvalidHDPath),
Some(path) => {
let _path = path.clone();
if let Some(sub_path) = parse_eth_sub_path(_path) {
derive_public_key(&xpub, &format!("m/{}", sub_path))
.map_err(|e| RustCError::UnexpectedError(format!("unable to derive pubkey")))
} else {
Err(RustCError::InvalidHDPath)
}
}
}
}

#[no_mangle]
pub extern "C" fn eth_parse(
ptr: PtrUR,
xpub: PtrString,
) -> PtrT<TransactionParseResult<DisplayETH>> {
let crypto_eth = extract_ptr_with_type!(ptr, EthSignRequest);
let pubkey = match crypto_eth.get_derivation_path().get_path() {
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
Some(path) => {
let xpub = recover_c_char(xpub);
let mut _path = path.clone();
let root_path = "44'/60'/0'/";
let sub_path = match _path.strip_prefix(root_path) {
Some(path) => path,
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
};
derive_public_key(&xpub, &format!("m/{}", sub_path))
}
};

let xpub = recover_c_char(xpub);
let pubkey = try_get_eth_public_key(xpub, crypto_eth.clone());
let transaction_type = TransactionType::from(crypto_eth.get_data_type());

match (pubkey, transaction_type) {
Expand Down Expand Up @@ -142,23 +186,8 @@ pub extern "C" fn eth_parse_personal_message(
xpub: PtrString,
) -> PtrT<TransactionParseResult<DisplayETHPersonalMessage>> {
let crypto_eth = extract_ptr_with_type!(ptr, EthSignRequest);
let pubkey = match crypto_eth.get_derivation_path().get_path() {
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
Some(path) => {
let xpub = recover_c_char(xpub);
let mut _path = path.clone();
let root_path = "44'/60'/0'/";
let sub_path = match _path.strip_prefix(root_path) {
Some(path) => path,
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
};
derive_public_key(&xpub, &format!("m/{}", sub_path))
}
};
let xpub = recover_c_char(xpub);
let pubkey = try_get_eth_public_key(xpub, crypto_eth.clone());

let transaction_type = TransactionType::from(crypto_eth.get_data_type());

Expand Down Expand Up @@ -189,23 +218,8 @@ pub extern "C" fn eth_parse_typed_data(
xpub: PtrString,
) -> PtrT<TransactionParseResult<DisplayETHTypedData>> {
let crypto_eth = extract_ptr_with_type!(ptr, EthSignRequest);
let pubkey = match crypto_eth.get_derivation_path().get_path() {
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
Some(path) => {
let xpub = recover_c_char(xpub);
let mut _path = path.clone();
let root_path = "44'/60'/0'/";
let sub_path = match _path.strip_prefix(root_path) {
Some(path) => path,
None => {
return TransactionParseResult::from(RustCError::InvalidHDPath).c_ptr();
}
};
derive_public_key(&xpub, &format!("m/{}", sub_path))
}
};
let xpub = recover_c_char(xpub);
let pubkey = try_get_eth_public_key(xpub, crypto_eth.clone());

let transaction_type = TransactionType::from(crypto_eth.get_data_type());

Expand Down Expand Up @@ -290,11 +304,27 @@ pub extern "C" fn eth_sign_tx(ptr: PtrUR, seed: PtrBytes, seed_len: u32) -> PtrT
#[cfg(test)]
mod tests {
extern crate std;

use std::println;

#[test]
fn test() {
let p = "m/44'/60'/0'/0/0";
let prefix = "m/44'/60'/0'/";
println!("{:?}", p.strip_prefix(prefix))
}

#[test]
fn test_test() {
let _path = "44'/60'/1'/0/0";
let root_path = "44'/60'/";
let sub_path = match _path.strip_prefix(root_path) {
Some(path) => {
if let Some(index) = path.find("/") {
println!("{}", &path[index..]);
}
}
None => {}
};
}
}
2 changes: 2 additions & 0 deletions rust/third_party/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/config/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef _VERSION_H
#define _VERSION_H

#define SOFTWARE_VERSION_MAJOR 0
#define SOFTWARE_VERSION_MINOR 9
#define SOFTWARE_VERSION_MAJOR 1
#define SOFTWARE_VERSION_MINOR 0
#define SOFTWARE_VERSION_BUILD 0
#define SOFTWARE_VERSION (SOFTWARE_VERSION_MAJOR * 10000 + SOFTWARE_VERSION_MINOR * 100 + SOFTWARE_VERSION_BUILD)

Expand Down
7 changes: 5 additions & 2 deletions src/device_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ void WipeDevice(void)
ret = Gd25FlashWriteBuffer(SPI_FLASH_ADDR_PROTECT_PARAM, (uint8_t *)&wipeFlag, sizeof(wipeFlag));
SetShowPowerOffPage(false);
FpWipeManageInfo();
ErasePublicInfo();
DestroyAccount(0);
DestroyAccount(1);
DestroyAccount(2);
ErasePublicInfo();
Gd25FlashChipErase();
for (uint32_t addr = 0; addr < GD25QXX_FLASH_SIZE; addr += 1024 * 64) {
Gd25FlashBlockErase(addr);
printf("flash erase address: %#x\n", addr);
}
}


Expand Down
3 changes: 2 additions & 1 deletion src/driver/drv_aw32001.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define PARM_EN_ICC_DIVD (0x00 << 7)
#define PARM_ICC (0x38 << 0)
#define PARM_ITERM (0x01 << 0)
#define PARM_ITERM_31MA (0x0f << 0)
#define PARM_TJ_REG (0x03 << 4)

/********************AW320XX Set config end*******************/
Expand Down Expand Up @@ -330,7 +331,7 @@ static void Aw32001RegValueInit(void)
Aw32001WriteRegBits(AW320XX_REG4_CVR, AW320XX_BIT_CVR_VRECH_MASK, PARM_VRECH); //PARM_VRECH 200mV
Aw32001WriteRegBits(AW320XX_REGB_CCR3, AW320XX_BIT_CCR3_EN_ICC_DIVD_MASK, PARM_EN_ICC_DIVD); //EN_ICC_DIVD Keep the current value of REG02[5:0] configuration.
Aw32001WriteRegBits(AW320XX_REG2_CCR, AW320XX_BIT_CCR_ICC_MASK, PARM_ICC); //ICC 456mA
Aw32001WriteRegBits(AW320XX_REG3_CCR2, AW320XX_BIT_CCR2_ITERM_MASK, PARM_ITERM); //ITERM 3mA
Aw32001WriteRegBits(AW320XX_REG3_CCR2, AW320XX_BIT_CCR2_ITERM_MASK, PARM_ITERM_31MA); //ITERM 3mA
Aw32001WriteRegBits(AW320XX_REG7_SVCR, AW320XX_BIT_SVCR_TJ_REG_MASK, PARM_TJ_REG); //Thermal regulation threshold 120℃
//Disable watchdog timer
Aw32001WriteRegBits(AW320XX_REG5_TIMCR, AW320XX_BIT_TIMCR_WD_CFG_MASK, AW320XX_BIT_TIMCR_WD_CFG_DISABLE);
Expand Down
31 changes: 31 additions & 0 deletions src/driver/drv_gd25qxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ int32_t Gd25FlashSectorErase(uint32_t addr)
return SUCCESS_CODE;
}

int32_t Gd25FlashBlockErase(uint32_t addr)
{
#if (FLASH_USE_MUTEX)
osMutexAcquire(g_flashMutex, osWaitForever);
#endif

if (addr >= GD25QXX_FLASH_SIZE) {
return ERR_GD25_BAD_PARAM;
}

Gd25FlashWriteEnable();
if (GD25QXX_FLASH_STATUS_WEL != Gd25FlashReadStatus()) {
return ERR_GD25_WEL_FAILED;
}

FLASH_CS_LOW();

Gd25FlashSendByte(GD25QXX_CMD_BLOCK_ERASE);
Gd25FlashSendByte((addr >> 16) & 0xFF);
Gd25FlashSendByte((addr >> 8) & 0xFF);
Gd25FlashSendByte(addr & 0xFF);

FLASH_CS_HIGH();

while (Gd25FlashReadStatus() & GD25QXX_FLASH_STATUS_WIP);
#if (FLASH_USE_MUTEX)
osMutexRelease(g_flashMutex);
#endif
return SUCCESS_CODE;
}

/***********************************************************************
flash supports fatfs, use with caution
***********************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions src/driver/drv_gd25qxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define GD25QXX_CMD_PAGE_PROGRAM 0x02
#define GD25QXX_CMD_CHIP_ERASE 0xC7
#define GD25QXX_CMD_SECTOR_ERASE 0x20
#define GD25QXX_CMD_BLOCK_ERASE 0xD8
#define GD25QXX_CMD_POWER_DOWN 0xB9
#define GD25QXX_CMD_RELEASE_POWER_DOWN 0xAB

Expand All @@ -46,6 +47,7 @@ void Gd25FlashOpen(void);
uint32_t Gd25FlashReadID(void);
int32_t Gd25FlashSectorErase(uint32_t addr);
int32_t Gd25FlashChipErase(void);
int32_t Gd25FlashBlockErase(uint32_t addr);
int32_t Gd25FlashReadBuffer(uint32_t addr, uint8_t *buffer, uint32_t size);
int32_t Gd25FlashWriteBuffer(uint32_t addr, const uint8_t *buffer, uint32_t size);

Expand Down
12 changes: 8 additions & 4 deletions src/driver/power_on_self_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ void PowerOnSelfCheck(void)
assert(ret == SUCCESS_CODE);

// check after wipe device
uint32_t wipeFlag = 0;
Gd25FlashReadBuffer(SPI_FLASH_ADDR_PROTECT_PARAM, (uint8_t *)&wipeFlag, sizeof(wipeFlag));
uint32_t wipeFlag = 0xFFFFFFFF;
int32_t readSize = Gd25FlashReadBuffer(SPI_FLASH_ADDR_PROTECT_PARAM, (uint8_t *)&wipeFlag, sizeof(wipeFlag));
assert(readSize == sizeof(wipeFlag));
if (wipeFlag == DEVICE_WIPE_FLAG_MAGIC_NUM) {
DrawStringOnLcd(190, 408, "Loading...", 0xFFFF, &openSans_24);
uint32_t c = 0x666666;
DrawStringOnLcd(170, 456, "About 1 minute", (uint16_t)(((c & 0xF80000) >> 16) | ((c & 0xFC00) >> 13) | ((c & 0x1C00) << 3) | ((c & 0xF8) << 5)), &openSans_20);
FpWipeManageInfo();
ErasePublicInfo();
DestroyAccount(0);
DestroyAccount(1);
DestroyAccount(2);
ErasePublicInfo();
Gd25FlashChipErase();
for (uint32_t addr = 0; addr < GD25QXX_FLASH_SIZE; addr += 1024 * 64) {
Gd25FlashBlockErase(addr);
printf("flash erase address: %#x\n", addr);
}
NVIC_SystemReset();
}
}
Loading

0 comments on commit c474d3c

Please sign in to comment.