Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tron2 #17

Open
wants to merge 9 commits into
base: feature/polkadot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ members = [
"wallet/coin-eos",
"wallet/coin-cosmos",
"wallet/coin-substrate",
"wallet/coin-tron",
]
1 change: 1 addition & 0 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ coin-eos = {path = "../wallet/coin-eos"}
coin-cosmos = {path = "../wallet/coin-cosmos"}
coin-filecoin = {path = "../wallet/coin-filecoin"}
coin-substrate = {path = "../wallet/coin-substrate"}
coin-tron = {path = "../wallet/coin-tron"}
common = {path = "../common"}
bitcoin = "0.25.0"
ethereum-types = "0.6.0"
Expand Down
8 changes: 8 additions & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub mod filecoin_signer;
pub mod message_handler;
pub mod substrate_address;
pub mod substrate_signer;
pub mod tron_address;
pub mod tron_signer;
use std::sync::Mutex;

#[macro_use]
Expand Down Expand Up @@ -123,6 +125,7 @@ pub unsafe extern "C" fn call_imkey_api(hex_str: *const c_char) -> *const c_char
"FILECOIN" => filecoin_address::get_address(&param),
"POLKADOT" => substrate_address::get_address(&param),
"KUSAMA" => substrate_address::get_address(&param),
"TRON" => tron_address::get_address(&param),
_ => Err(format_err!("get_address unsupported_chain")),
}
}),
Expand Down Expand Up @@ -155,6 +158,7 @@ pub unsafe extern "C" fn call_imkey_api(hex_str: *const c_char) -> *const c_char
"FILECOIN" => filecoin_address::display_filecoin_address(&param),
"POLKADOT" => substrate_address::display_address(&param),
"KUSAMA" => substrate_address::display_address(&param),
"TRON" => tron_address::display_address(&param),
_ => Err(format_err!("register_address unsupported_chain")),
}
}),
Expand Down Expand Up @@ -187,6 +191,9 @@ pub unsafe extern "C" fn call_imkey_api(hex_str: *const c_char) -> *const c_char
"KUSAMA" => {
substrate_signer::sign_transaction(&param.clone().input.unwrap().value, &param)
}
"TRON" => {
tron_signer::sign_transaction(&param.clone().input.unwrap().value, &param)
}
_ => Err(format_err!("sign_tx unsupported_chain")),
}
}),
Expand All @@ -203,6 +210,7 @@ pub unsafe extern "C" fn call_imkey_api(hex_str: *const c_char) -> *const c_char
param.clone().input.unwrap().value.as_slice(),
&param,
),
"TRON" => tron_signer::sign_message(&param.clone().input.unwrap().value, &param),
_ => Err(format_err!(
"sign message is not supported the chain {}",
param.chain_type
Expand Down
2 changes: 1 addition & 1 deletion api/src/substrate_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use common::SignParam;
use prost::Message;

pub fn sign_transaction(data: &[u8], sign_param: &SignParam) -> Result<Vec<u8>> {
let input: SubstrateRawTxIn = SubstrateRawTxIn::decode(data).unwrap();
let input: SubstrateRawTxIn = SubstrateRawTxIn::decode(data).expect("decode proto error");
let signed = Transaction::sign_transaction(&input, sign_param)?;
encode_message(signed)
}
27 changes: 27 additions & 0 deletions api/src/tron_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::api::{AddressParam, AddressResult};
use crate::error_handling::Result;
use crate::message_handler::encode_message;
use coin_tron::address::TronAddress;
use prost::Message;

pub fn get_address(param: &AddressParam) -> Result<Vec<u8>> {
let address = TronAddress::get_address(param.path.as_ref())?;

let address_message = AddressResult {
path: param.path.to_owned(),
chain_type: param.chain_type.to_string(),
address,
};
encode_message(address_message)
}

pub fn display_address(param: &AddressParam) -> Result<Vec<u8>> {
let address = TronAddress::display_address(param.path.as_ref())?;

let address_message = AddressResult {
path: param.path.to_owned(),
chain_type: param.chain_type.to_string(),
address,
};
encode_message(address_message)
}
18 changes: 18 additions & 0 deletions api/src/tron_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::error_handling::Result;
use crate::message_handler::encode_message;
use coin_tron::signer::TronSigner;
use coin_tron::tronapi::{TronMessageInput, TronTxInput};
use common::SignParam;
use prost::Message;

pub fn sign_transaction(data: &[u8], sign_param: &SignParam) -> Result<Vec<u8>> {
let input: TronTxInput = TronTxInput::decode(data).expect("decode proto error");
let signed = TronSigner::sign_transaction(input, sign_param)?;
encode_message(signed)
}

pub fn sign_message(data: &[u8], sign_param: &SignParam) -> Result<Vec<u8>> {
let input: TronMessageInput = TronMessageInput::decode(data).expect("decode proto error");
let signed = TronSigner::sign_message(input, sign_param)?;
encode_message(signed)
}
2 changes: 2 additions & 0 deletions common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub const FILECOIN_AID: &str = "695F6B315F66696C";
pub const IMK_AID: &str = "695F696D6B";
pub const POLKADOT_AID: &str = "695F656473725F646F74";
pub const KUSAMA_AID: &str = "695F656473725F6B736D";
pub const TRON_AID: &str = "695F6B315F74726F6E";

pub const BL_AID: &str = "D0426F6F746C6F61646572";

Expand All @@ -33,6 +34,7 @@ pub const ETH_PATH: &str = "m/44'/60'/0'/0/0";
pub const FILECOIN_PATH: &str = "m/44'/461'/0/0/0";
pub const POLKADOT_PATH: &str = "m/44'/354'/0'/0'/0'";
pub const KUSAMA_PATH: &str = "m/44'/434'/0'/0'/0'";
pub const TRON_PATH: &str = "m/44'/195'/0'/0/0";

pub const MAX_UTXO_NUMBER: usize = 252;
pub const EACH_ROUND_NUMBER: usize = 5;
Expand Down
9 changes: 7 additions & 2 deletions device/src/cos_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app_download::AppDownloadRequest;
use crate::device_manager::{get_cert, get_firmware_version, get_se_id, get_sn};
use crate::device_manager::{get_bl_version, get_cert, get_firmware_version, get_se_id, get_sn};
use crate::error::ImkeyError;
use crate::ServiceResponse;
use crate::{Result, TsmService};
Expand All @@ -25,6 +25,7 @@ pub struct CosUpgradeRequest {
#[serde(rename = "commandID")]
pub command_id: String,
pub card_ret_data_list: Option<Vec<String>>,
pub se_bl_versioon: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -42,13 +43,13 @@ impl CosUpgradeRequest {
pub fn cos_upgrade(sdk_version: Option<String>) -> Result<()> {
//read se device cert
let mut device_cert = get_cert()?;
// ApduCheck::checke_response(&device_cert)?; //TODO 在所有manager里的接口中增加check方法

let mut is_jump = false;
let seid;
let sn;
let mut se_cos_version = String::new();
let mut is_bl_status = true;
let mut se_bl_version = None;
//read seid and sn number
if device_cert.starts_with("bf21") || device_cert.starts_with("BF21") {
seid = get_se_id()?;
Expand All @@ -68,6 +69,7 @@ impl CosUpgradeRequest {
.iter(),
);
device_cert = hex::encode_upper(temp_device_cert);
se_bl_version = Some(get_bl_version()?);
} else {
return Err(ImkeyError::ImkeyTsmCosUpgradeFail.into());
}
Expand All @@ -86,6 +88,7 @@ impl CosUpgradeRequest {
status_word: None,
command_id: String::from(constants::TSM_ACTION_COS_UPGRADE),
card_ret_data_list: None,
se_bl_versioon: se_bl_version,
};

loop {
Expand Down Expand Up @@ -118,6 +121,8 @@ impl CosUpgradeRequest {
{
if "03".eq(next_step_key.as_str()) {
reconnect()?;
se_bl_version = Some(get_bl_version()?);
request_data.se_bl_versioon = se_bl_version;
} else if "05".eq(next_step_key.as_str()) {
reconnect()?;
se_cos_version = get_firmware_version()?;
Expand Down
13 changes: 13 additions & 0 deletions device/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ pub fn get_firmware_version() -> Result<String> {
Ok(firmware_version)
}

pub fn get_bl_version() -> Result<String> {
select_isd();
let res = send_apdu("80CA800900".to_string())?;
ApduCheck::check_response(res.as_str())?;
let bl_version = format!(
"{}.{}.{}",
res[0..1].to_string(),
res[1..2].to_string(),
res[2..res.len() - 4].to_string()
);
Ok(bl_version)
}

pub fn get_battery_power() -> Result<String> {
select_isd();
let res = send_apdu("00D6FEED01".to_string())?;
Expand Down
12 changes: 12 additions & 0 deletions examples/ios/iosExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
D86ADCF725473BBA008CBEAE /* Data+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86ADCF625473BBA008CBEAE /* Data+Extension.swift */; };
D86ADD1125473F94008CBEAE /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86ADD1025473F94008CBEAE /* String+Extension.swift */; };
D86ADD1325473FE4008CBEAE /* Hex.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86ADD1225473FE4008CBEAE /* Hex.swift */; };
D87733EC2582323F00037FFE /* substrate.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87733EA2582323E00037FFE /* substrate.pb.swift */; };
D87733ED2582323F00037FFE /* tron.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87733EB2582323F00037FFE /* tron.pb.swift */; };
D87733EF2582336500037FFE /* common.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87733EE2582336500037FFE /* common.pb.swift */; };
D87BBB912462068900F09CFB /* DeviceManageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87BBB902462068900F09CFB /* DeviceManageViewController.swift */; };
D87BBB9624621F2100F09CFB /* CosmosViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87BBB9224621F2100F09CFB /* CosmosViewController.swift */; };
D87BBB9724621F2100F09CFB /* EOSViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87BBB9324621F2100F09CFB /* EOSViewController.swift */; };
Expand Down Expand Up @@ -104,6 +107,9 @@
D86ADCF625473BBA008CBEAE /* Data+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Data+Extension.swift"; sourceTree = "<group>"; };
D86ADD1025473F94008CBEAE /* String+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
D86ADD1225473FE4008CBEAE /* Hex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Hex.swift; sourceTree = "<group>"; };
D87733EA2582323E00037FFE /* substrate.pb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = substrate.pb.swift; sourceTree = "<group>"; };
D87733EB2582323F00037FFE /* tron.pb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = tron.pb.swift; sourceTree = "<group>"; };
D87733EE2582336500037FFE /* common.pb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = common.pb.swift; sourceTree = "<group>"; };
D87BBB902462068900F09CFB /* DeviceManageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceManageViewController.swift; sourceTree = "<group>"; };
D87BBB9224621F2100F09CFB /* CosmosViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CosmosViewController.swift; sourceTree = "<group>"; };
D87BBB9324621F2100F09CFB /* EOSViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EOSViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,6 +314,9 @@
D8B0C355244A43EB009F1B06 /* proto */ = {
isa = PBXGroup;
children = (
D87733EE2582336500037FFE /* common.pb.swift */,
D87733EA2582323E00037FFE /* substrate.pb.swift */,
D87733EB2582323F00037FFE /* tron.pb.swift */,
D8B0C35A244A463F009F1B06 /* api.pb.swift */,
D8B0C35B244A463F009F1B06 /* btc.pb.swift */,
D8B0C357244A463F009F1B06 /* cosmos.pb.swift */,
Expand Down Expand Up @@ -572,6 +581,8 @@
D8B0C362244A463F009F1B06 /* device.pb.swift in Sources */,
D87BBBBE2462245B00F09CFB /* CosmosTest.swift in Sources */,
D87BBB9824621F2100F09CFB /* BTCViewController.swift in Sources */,
D87733EC2582323F00037FFE /* substrate.pb.swift in Sources */,
D87733EF2582336500037FFE /* common.pb.swift in Sources */,
D87BBB9624621F2100F09CFB /* CosmosViewController.swift in Sources */,
D8B0C36A244A520F009F1B06 /* API.swift in Sources */,
D8B0C35D244A463F009F1B06 /* cosmos.pb.swift in Sources */,
Expand All @@ -587,6 +598,7 @@
D8B0C35E244A463F009F1B06 /* eos.pb.swift in Sources */,
D86ADCF525473B62008CBEAE /* ImkeyError.swift in Sources */,
D87BBBC32462250C00F09CFB /* FeatTest.swift in Sources */,
D87733ED2582323F00037FFE /* tron.pb.swift in Sources */,
D8B0C360244A463F009F1B06 /* api.pb.swift in Sources */,
D87BBB9724621F2100F09CFB /* EOSViewController.swift in Sources */,
);
Expand Down
22 changes: 22 additions & 0 deletions examples/ios/iosExample/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,28 @@ public class API{
return bindResponse.bindResult
}

public class func substrateSignTX(){
var input = Substrateapi_SubstrateRawTxIn()
input.rawData = "0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c0703d148e25901007b000000dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025bde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7"
var signParam = Common_SignParam()
signParam.chainType = "POLKADOT"
signParam.path = BIP44.polkadot
signParam.payment = "25 DOT"
signParam.receiver = "12pWV6LvG4iAfNpFNTvvkWy3H9H8wtCkjiXupAzo2BCmPViM"
signParam.sender = "147mvrDYhFpZzvFASKBDNVcxoyz8XCVNyyFKSZcpbQxN33TT"
signParam.fee = "15.4000 milli DOT"
signParam.input.value = try! input.serializedData()

var action = Api_ImkeyAction()
action.param.value = try! signParam.serializedData()
action.method = "sign_tx"

let paramHex = try! action.serializedData().toHexString()
let res = call_imkey_api(paramHex)
// let hexRes = String(cString:res!).toHexString()
// Log.d(hexRes)
}

public class func btcSignTX(){
Log.d("btc sign ...")
var btcInput = Btcapi_BtcTxInput()
Expand Down
1 change: 1 addition & 0 deletions examples/ios/iosExample/BIP44.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public struct BIP44 {
public static let eos = "m/44'/194'"
public static let EOS_LEDGER = "m/44'/194'/0'/0/0"
public static let cosmos = "m/44'/118'/0'/0/0"
public static let polkadot = "m/44'/354'/0'/0'/0'"

}
Loading