Skip to content

Commit 531d30b

Browse files
committed
Add test for sign_tx
Also made necessary changes to automation.json (for ledger devices). Add deserializing method for PSBT.
1 parent 6489251 commit 531d30b

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

ci/automation.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": 1,
33
"rules": [
44
{
5-
"regexp": "Address \\(\\d/\\d\\)|Message hash \\(\\d/\\d\\)",
5+
"regexp": "Address \\(\\d/\\d\\)|Message hash \\(\\d/\\d\\)|Confirm|Fees",
66
"actions": [
77
[ "button", 2, true ],
88
[ "button", 2, false ]
@@ -20,7 +20,7 @@
2020
]
2121
},
2222
{
23-
"regexp": "Approve|Sign",
23+
"regexp": "Approve|Sign|Accept",
2424
"actions": [
2525
[ "button", 3, true ],
2626
[ "button", 3, false ]

src/lib.rs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ pub mod types;
3737
mod tests {
3838
use crate::types;
3939
use crate::HWIClient;
40+
use std::collections::BTreeMap;
4041
use std::str::FromStr;
4142

42-
use bitcoin::util::bip32::DerivationPath;
43+
use bitcoin::psbt::{Input, Output};
44+
use bitcoin::util::bip32::{DerivationPath, KeySource};
45+
use bitcoin::{secp256k1, Transaction};
46+
use bitcoin::{TxIn, TxOut};
4347

4448
#[test]
4549
#[serial]
@@ -145,10 +149,62 @@ mod tests {
145149
// #[serial]
146150
// fn test_display_address_with_path_taproot() {}
147151

148-
// TODO: Create PSBT with scratch using given Hardware Wallet
149-
// #[test]
150-
// #[serial]
151-
// fn test_sign_tx() {}
152+
#[test]
153+
#[serial]
154+
fn test_sign_tx() {
155+
let devices = HWIClient::enumerate().unwrap();
156+
let device = devices.first().unwrap();
157+
let client = HWIClient::get_client(device, true, types::HWIChain::Test).unwrap();
158+
let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
159+
160+
let address = client
161+
.display_address_with_path(&derivation_path, types::HWIAddressType::Legacy)
162+
.unwrap();
163+
164+
let pk = client.get_xpub(&derivation_path, true).unwrap();
165+
let mut hd_keypaths: BTreeMap<secp256k1::PublicKey, KeySource> = Default::default();
166+
hd_keypaths.insert(pk.public_key, (device.fingerprint, derivation_path));
167+
168+
let tx = Transaction {
169+
version: 1,
170+
lock_time: 0,
171+
input: vec![TxIn::default()],
172+
output: vec![TxOut {
173+
value: 100,
174+
script_pubkey: address.address.script_pubkey(),
175+
}],
176+
};
177+
178+
let txin = TxIn {
179+
previous_output: bitcoin::OutPoint {
180+
txid: tx.txid(),
181+
vout: Default::default(),
182+
},
183+
..Default::default()
184+
};
185+
let psbt = bitcoin::psbt::PartiallySignedTransaction {
186+
unsigned_tx: Transaction {
187+
version: 1,
188+
lock_time: 0,
189+
input: vec![txin],
190+
output: vec![],
191+
},
192+
xpub: Default::default(),
193+
version: 0,
194+
proprietary: BTreeMap::new(),
195+
unknown: BTreeMap::new(),
196+
197+
inputs: vec![Input {
198+
non_witness_utxo: Some(tx),
199+
witness_utxo: None,
200+
bip32_derivation: hd_keypaths,
201+
..Default::default()
202+
}],
203+
outputs: vec![Output::default(), Output::default()],
204+
};
205+
let client = get_first_device();
206+
client.sign_tx(&psbt).unwrap();
207+
}
152208

153209
#[test]
154210
#[serial]

src/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,19 @@ pub struct HWIAddress {
4949

5050
#[derive(Clone, Eq, PartialEq, Debug, Deserialize)]
5151
pub struct HWIPartiallySignedTransaction {
52+
#[serde(deserialize_with = "deserialize_psbt")]
5253
pub psbt: PartiallySignedTransaction,
5354
}
5455

56+
fn deserialize_psbt<'de, D: Deserializer<'de>>(
57+
d: D,
58+
) -> Result<PartiallySignedTransaction, D::Error> {
59+
let b64_string = String::deserialize(d)?;
60+
let bytes = bitcoin::base64::decode(&b64_string).map_err(serde::de::Error::custom)?;
61+
bitcoin::consensus::deserialize::<PartiallySignedTransaction>(&bytes[..])
62+
.map_err(serde::de::Error::custom)
63+
}
64+
5565
impl Deref for HWIPartiallySignedTransaction {
5666
type Target = PartiallySignedTransaction;
5767

0 commit comments

Comments
 (0)