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

feat: imKey bitcon support taproot and native segwit transaction[R2D2-11241] #102

Merged
merged 38 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f233ae2
feat: add p2wpkhp2tr address generation
xiaoguang1010 May 16, 2024
a02b22f
feat: add bitcoin Mixed signature function
xiaoguang1010 May 21, 2024
09abaad
test: modify p2wpkh test case
xiaoguang1010 May 21, 2024
06e2093
feat: add bitcoin p2wpkh sign
xiaoguang1010 May 23, 2024
410e93f
feat: add test case and add p2tr sign
xiaoguang1010 Jun 6, 2024
0b99f2e
test: add test case
xiaoguang1010 Jun 7, 2024
b9298fb
test: modify btc test case
xiaoguang1010 Jun 7, 2024
e05eaeb
feat: code optimization
xiaoguang1010 Jun 9, 2024
c883be6
feat: modify display_addres and get_address
xiaoguang1010 Jun 10, 2024
f39540f
feat: add bitcoin p2tr transaction
xiaoguang1010 Jun 17, 2024
f08713e
test: add p2tr test case
xiaoguang1010 Jun 25, 2024
b9b7079
chore: remove useless serde-aux library (#103)
xiaoguang1010 Jun 26, 2024
dfd4ae1
test: add bitcoin transaction sign function test
xiaoguang1010 Jun 26, 2024
2f5bf01
feat: code optimization
xiaoguang1010 Jul 2, 2024
82548af
fix: import mnemonic return wrong existed id (#104)
XuNeal Jul 2, 2024
76884c0
feat: pass in the tweaked public key when signing
xiaoguang1010 Jul 4, 2024
a11c4ac
chore: code format
xiaoguang1010 Jul 5, 2024
c223227
feat: allow import test wif in production env (#105)
XuNeal Jul 8, 2024
74b634b
feat: taproot sign script (#98)
XuNeal Jul 11, 2024
1d4c8a9
fix: remove deprecated fil library (#106)
XuNeal Jul 11, 2024
e5cd228
feat: add p2wpkhp2tr address generation
xiaoguang1010 May 16, 2024
0e4190a
feat: add bitcoin Mixed signature function
xiaoguang1010 May 21, 2024
7abd9db
test: modify p2wpkh test case
xiaoguang1010 May 21, 2024
e2a75cd
feat: add bitcoin p2wpkh sign
xiaoguang1010 May 23, 2024
e11cdff
feat: add test case and add p2tr sign
xiaoguang1010 Jun 6, 2024
79b39fd
test: add test case
xiaoguang1010 Jun 7, 2024
1d79188
test: modify btc test case
xiaoguang1010 Jun 7, 2024
b3eaa56
feat: code optimization
xiaoguang1010 Jun 9, 2024
427f48c
feat: modify display_addres and get_address
xiaoguang1010 Jun 10, 2024
dbecab0
feat: add bitcoin p2tr transaction
xiaoguang1010 Jun 17, 2024
04acde3
test: add p2tr test case
xiaoguang1010 Jun 25, 2024
8de14fc
test: add bitcoin transaction sign function test
xiaoguang1010 Jun 26, 2024
d1d913a
feat: code optimization
xiaoguang1010 Jul 2, 2024
ec7e4a1
feat: pass in the tweaked public key when signing
xiaoguang1010 Jul 4, 2024
71d4ca0
chore: code format
xiaoguang1010 Jul 5, 2024
8cf91d5
Merge branch 'feature/taproot' of github.com:consenlabs/token-core-mo…
xiaoguang1010 Jul 15, 2024
17c7722
chore: switch to staging env
xiaoguang1010 Jul 16, 2024
9e5a111
feat: derive_account and derive_sub_account support native segwit add…
xiaoguang1010 Jul 23, 2024
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
67 changes: 67 additions & 0 deletions imkey-core/ikc-common/src/apdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ impl BtcApdu {
apdu.to_hex().to_uppercase()
}

pub fn btc_taproot_sign(last_one: bool, data: Vec<u8>) -> String {
if data.len() as u32 > LC_MAX {
panic!("data to long");
}

let mut apdu = match last_one {
true => ApduHeader::new(0x80, 0x40, 0x80, 0x00, data.len() as u8).to_array(),
_ => ApduHeader::new(0x80, 0x40, 0x00, 0x00, data.len() as u8).to_array(),
};

apdu.extend(data.iter());
apdu.push(0x00);
apdu.to_hex().to_uppercase()
}

pub fn omni_prepare_data(p1: u8, data: Vec<u8>) -> String {
if data.len() as u32 > LC_MAX {
panic!("data to long");
Expand All @@ -111,6 +126,58 @@ impl BtcApdu {
data.extend(name);
Apdu::register_address(0x37, &data)
}

pub fn btc_single_utxo_sign_prepare(ins: u8, data: &Vec<u8>) -> Vec<String> {
let mut apdu_vec = Vec::new();
let apdu_number = (data.len() - 1) / LC_MAX as usize + 1;
for index in 0..apdu_number {
if index == 0 && index == apdu_number - 1 {
let length = if data.len() % LC_MAX as usize == 0 {
LC_MAX
} else {
(data.len() % LC_MAX as usize) as u32
};
let mut temp_apdu_vec =
ApduHeader::new(0x80, ins, 0x00, 0x80, length as u8).to_array();
temp_apdu_vec.extend_from_slice(&data[index * LC_MAX as usize..]);
apdu_vec.push(hex::encode_upper(temp_apdu_vec));
} else if index == 0 && index != apdu_number - 1 {
let mut temp_apdu_vec =
ApduHeader::new(0x80, ins, 0x00, 0x00, LC_MAX as u8).to_array();
temp_apdu_vec.extend_from_slice(
&data[index * LC_MAX as usize..((index + 1) * LC_MAX as usize) as usize],
);
apdu_vec.push(hex::encode_upper(temp_apdu_vec));
} else if index != 0 && index != apdu_number - 1 {
let mut temp_apdu_vec =
ApduHeader::new(0x80, ins, 0x80, 0x00, LC_MAX as u8).to_array();
temp_apdu_vec.extend_from_slice(
&data[index * LC_MAX as usize..((index + 1) * LC_MAX as usize) as usize],
);
apdu_vec.push(hex::encode_upper(temp_apdu_vec));
} else if index != 0 && index == apdu_number - 1 {
let length = if data.len() % LC_MAX as usize == 0 {
LC_MAX
} else {
(data.len() % LC_MAX as usize) as u32
};
let mut temp_apdu_vec =
ApduHeader::new(0x80, ins, 0x80, 0x80, length as u8).to_array();
temp_apdu_vec.extend_from_slice(&data[index * LC_MAX as usize..]);
apdu_vec.push(hex::encode_upper(temp_apdu_vec));
}
}
return apdu_vec;
}

pub fn btc_single_utxo_sign(index: u8, hash_type: u8, path: &str) -> String {
let path_bytes = path.as_bytes();
let mut apdu =
ApduHeader::new(0x80, 0x45, index, hash_type, path_bytes.len() as u8).to_array();
apdu.extend(path_bytes.iter());
apdu.push(0x00);
apdu.to_hex().to_uppercase()
}
}

pub struct EthApdu();
Expand Down
6 changes: 3 additions & 3 deletions imkey-core/ikc-common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ 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;
pub const DUST_THRESHOLD: i64 = 2730;
pub const MIN_NONDUST_OUTPUT: i64 = 546;
pub const DUST_THRESHOLD: u64 = 2730;
pub const MIN_NONDUST_OUTPUT: u64 = 546;
// max op return size
pub const MAX_OPRETURN_SIZE: usize = 80;
pub const BTC_FORK_DUST: i64 = 546;
pub const BTC_FORK_DUST: u64 = 546;

// imkey device status
pub const IMKEY_DEV_STATUS_INACTIVATED: &str = "inactivated";
Expand Down
2 changes: 2 additions & 0 deletions imkey-core/ikc-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@ pub enum CoinError {
InvalidVersion,
#[error("invalid addr length")]
InvalidAddrLength,
#[error("invalid_utxo")]
InvalidUtxo,
}
2 changes: 1 addition & 1 deletion imkey-core/ikc-common/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn secp256k1_sign_verify(public: &[u8], signed: &[u8], message: &[u8]) -> Re
.is_ok())
}

pub fn bigint_to_byte_vec(val: i64) -> Vec<u8> {
pub fn bigint_to_byte_vec(val: u64) -> Vec<u8> {
let mut return_data = BigInt::from(val).to_signed_bytes_be();
while return_data.len() < 8 {
return_data.insert(0, 0x00);
Expand Down
2 changes: 1 addition & 1 deletion imkey-core/ikc-device/src/device_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn bind_test() {
// pub const TEST_KEY_PATH: &str = "/tmp/";
// pub const TEST_BIND_CODE: &str = "MCYNK5AH";
pub const TEST_KEY_PATH: &str = "/tmp/";
pub const TEST_BIND_CODE: &str = "7FVRAJJ7";
pub const TEST_BIND_CODE: &str = "FT2Z3LT2";

#[cfg(test)]
mod test {
Expand Down
2 changes: 1 addition & 1 deletion imkey-core/ikc-proto/src/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ message AddressParam {
string chainType = 1;
string path = 2;
string network = 3;
bool isSegWit = 4;
string segWit = 4;
}

message AddressResult {
Expand Down
12 changes: 6 additions & 6 deletions imkey-core/ikc-proto/src/btc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package btcapi;

message Utxo {
string tx_hash = 1;
int32 vout = 2;
int64 amount = 3;
uint32 vout = 2;
uint64 amount = 3;
string address = 4;
string script_pubKey = 5;
string derived_path = 6;
Expand All @@ -18,13 +18,13 @@ message BtcTxExtra {
}
message BtcTxInput {
string to = 1;
int64 amount = 2;
int64 fee = 3;
uint32 change_address_index = 4;
uint64 amount = 2;
uint64 fee = 3;
optional uint32 change_address_index = 4;
repeated Utxo unspents = 5;
string segWit = 6;
string protocol = 7;
BtcTxExtra extra = 8;
optional BtcTxExtra extra = 8;
}

message BtcTxOutput {
Expand Down
8 changes: 4 additions & 4 deletions imkey-core/ikc-proto/src/btcfork.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package btcforkapi;

message Utxo {
string txHash = 1;
int32 vout = 2;
int64 amount = 3;
uint32 vout = 2;
uint64 amount = 3;
string address = 4;
string scriptPubKey = 5;
string derivedPath = 6;
Expand All @@ -13,9 +13,9 @@ message Utxo {

message BtcForkTxInput {
string to = 1;
int64 amount = 2;
uint64 amount = 2;
repeated Utxo unspents = 3;
int64 fee = 4;
uint64 fee = 4;
uint32 changeAddressIndex = 5;
string changeAddress = 6;
string segWit = 7;
Expand Down
14 changes: 7 additions & 7 deletions imkey-core/ikc-wallet/coin-bch/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use std::str::FromStr;
#[derive(Clone)]
pub struct Utxo {
pub txhash: String,
pub vout: i32,
pub amount: i64,
pub vout: u32,
pub amount: u64,
pub address: String,
pub script_pubkey: String,
pub derive_path: String,
Expand All @@ -35,9 +35,9 @@ pub struct Utxo {

pub struct BchTransaction {
pub to: String,
pub amount: i64,
pub amount: u64,
pub unspents: Vec<Utxo>,
pub fee: i64,
pub fee: u64,
}

impl BchTransaction {
Expand Down Expand Up @@ -278,15 +278,15 @@ impl BchTransaction {
})
}

pub fn get_total_amount(&self) -> i64 {
let mut total_amount: i64 = 0;
pub fn get_total_amount(&self) -> u64 {
let mut total_amount = 0;
for unspent in &self.unspents {
total_amount += unspent.amount;
}
total_amount
}

pub fn get_change_amount(&self) -> i64 {
pub fn get_change_amount(&self) -> u64 {
let total_amount = self.get_total_amount();
let change_amout = total_amount - self.amount - self.fee;
change_amout
Expand Down
Loading
Loading