From 8b7b7d777c663ad0a88e3301814f6b297c4a6cc8 Mon Sep 17 00:00:00 2001 From: Sun Feng Date: Wed, 26 Jun 2024 15:55:20 +0800 Subject: [PATCH] add tests for bitcoin bip322 sign --- token-core/tcx-btc-kin/src/lib.rs | 4 +++ token-core/tcx/tests/sign_test.rs | 42 ++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/token-core/tcx-btc-kin/src/lib.rs b/token-core/tcx-btc-kin/src/lib.rs index 7eb15e99..f3fe6d15 100644 --- a/token-core/tcx-btc-kin/src/lib.rs +++ b/token-core/tcx-btc-kin/src/lib.rs @@ -84,6 +84,10 @@ pub mod bitcoincash { pub type TransactionInput = crate::transaction::BtcKinTxInput; pub type TransactionOutput = crate::transaction::BtcKinTxOutput; + + pub type MessageInput = crate::transaction::BtcMessageInput; + + pub type MessageOutput = crate::transaction::BtcMessageOutput; } pub mod omni { diff --git a/token-core/tcx/tests/sign_test.rs b/token-core/tcx/tests/sign_test.rs index 45908c7e..beb80fc0 100644 --- a/token-core/tcx/tests/sign_test.rs +++ b/token-core/tcx/tests/sign_test.rs @@ -27,7 +27,7 @@ use tcx::api::{ use tcx::handler::encode_message; use tcx::handler::get_derived_key; -use tcx_btc_kin::transaction::BtcKinTxInput; +use tcx_btc_kin::transaction::{BtcKinTxInput, BtcMessageInput, BtcMessageOutput}; use tcx_btc_kin::Utxo; use tcx_ckb::{CachedCell, CellInput, CkbTxInput, CkbTxOutput, OutPoint, Script, Witness}; use tcx_constants::{sample_key, CurveType}; @@ -542,6 +542,46 @@ fn test_tron_sign_message() { }); } +#[test] +#[serial] +fn test_bitcoin_sign_message() { + run_test(|| { + let wallet = import_default_wallet(); + + let input_expects = vec![ + (BtcMessageInput{ + message: "hello world".to_string(), + }, "02473044022062775640116afb7f17d23c222b0a6904fdaf2aea0d76e550d75c8fd362b80dcb022067c299fde774aaab689f8a53ebd0956395ff45b7ff6b7e99569d0abec85110c80121031aee5e20399d68cf0035d1a21564868f22bc448ab205292b4279136b15ecaebc"), + (BtcMessageInput{ + message: "test1".to_string(), + }, "02483045022100b805ccd16f1a664ae394bf292962ea6d76e0ddd5beb0b050cca4a1aa9ababc9a02201503132e39dc600957ec8f33663b10ab0cff0c4e37cab2811619152be8d919300121031aee5e20399d68cf0035d1a21564868f22bc448ab205292b4279136b15ecaebc"), + (BtcMessageInput{ + message: "test2".to_string(), + }, "02483045022100e96bfdb41b3562a1ff5a4c816da2620e82bcc8d702843ae1cec506666d4569c302206477d7d93c082cb42d462200a136e6aef7edde053722008a206ab8b9b356f0380121031aee5e20399d68cf0035d1a21564868f22bc448ab205292b4279136b15ecaebc"), + ]; + + for (input, expected) in input_expects { + let tx = SignParam { + id: wallet.id.to_string(), + key: Some(Key::Password(TEST_PASSWORD.to_string())), + chain_type: "BITCOIN".to_string(), + path: "m/49'/1'/0'".to_string(), + curve: "secp256k1".to_string(), + network: "TESTNET".to_string(), + seg_wit: "VERSION_0".to_string(), + input: Some(::prost_types::Any { + type_url: "imtoken".to_string(), + value: encode_message(input).unwrap(), + }), + }; + + let sign_result = call_api("sign_msg", tx).unwrap(); + let ret: BtcMessageOutput = BtcMessageOutput::decode(sign_result.as_slice()).unwrap(); + assert_eq!(expected, ret.signature); + } + }); +} + #[test] #[serial] fn test_sign_by_dk_hd_store() {