Skip to content

Commit c9f8e09

Browse files
committed
rust: get_unspent_outputs: add public_key
Needed for single sig to set the script sig and/or witness.
1 parent b924a79 commit c9f8e09

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

subprojects/gdk_rust/gdk_common/src/model.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ pub struct UnspentOutput {
706706
pub sequence: Option<u32>,
707707
#[serde(rename = "prevout_script")]
708708
pub script_code: String,
709+
pub public_key: String,
709710
}
710711

711712
/// Partially parse the derivation path and return (is_internal, address_pointer)
@@ -837,7 +838,7 @@ mod test {
837838

838839
#[test]
839840
fn test_unspent() {
840-
let json_str = r#"{"btc": [{"address_type": "p2wsh", "block_height": 1806588, "pointer": 3509, "pt_idx": 1, "satoshi": 3650144, "subaccount": 0, "txhash": "08711d45d4867d7834b133a425da065b252eb6a9b206d57e2bbb226a344c5d13", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 1], "prevout_script": "51", "is_segwit": true}, {"address_type": "p2wsh", "block_height": 1835681, "pointer": 3510, "pt_idx": 0, "satoshi": 5589415, "subaccount": 0, "txhash": "fbd00e5b9e8152c04214c72c791a78a65fdbab68b5c6164ff0d8b22a006c5221", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 2], "prevout_script": "51", "is_segwit": true}, {"address_type": "p2wsh", "block_height": 1835821, "pointer": 3511, "pt_idx": 0, "satoshi": 568158, "subaccount": 0, "txhash": "e5b358fb8366960130b97794062718d7f4fbe721bf274f47493a19326099b811", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 3], "prevout_script": "51", "is_segwit": true}]}"#;
841+
let json_str = r#"{"btc": [{"address_type": "p2wsh", "block_height": 1806588, "pointer": 3509, "pt_idx": 1, "satoshi": 3650144, "subaccount": 0, "txhash": "08711d45d4867d7834b133a425da065b252eb6a9b206d57e2bbb226a344c5d13", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 1], "prevout_script": "51", "is_segwit": true, "public_key": "020202020202020202020202020202020202020202020202020202020202020202"}, {"address_type": "p2wsh", "block_height": 1835681, "pointer": 3510, "pt_idx": 0, "satoshi": 5589415, "subaccount": 0, "txhash": "fbd00e5b9e8152c04214c72c791a78a65fdbab68b5c6164ff0d8b22a006c5221", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 2], "prevout_script": "51", "is_segwit": true, "public_key": "020202020202020202020202020202020202020202020202020202020202020202"}, {"address_type": "p2wsh", "block_height": 1835821, "pointer": 3511, "pt_idx": 0, "satoshi": 568158, "subaccount": 0, "txhash": "e5b358fb8366960130b97794062718d7f4fbe721bf274f47493a19326099b811", "is_internal": false, "confidential": false, "user_path": [2147483692, 2147483649, 2147483648, 0, 3], "prevout_script": "51", "is_segwit": true, "public_key": "020202020202020202020202020202020202020202020202020202020202020202"}]}"#;
841842
let json: GetUnspentOutputs = serde_json::from_str(json_str).unwrap();
842843
println!("{:#?}", json);
843844
}

subprojects/gdk_rust/gdk_electrum/src/account.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,15 @@ impl Account {
300300
Ok(txs)
301301
}
302302

303-
pub fn script_code(&self, path: &DerivationPath) -> BEScript {
304-
// FIXME: (leo) is this the correct path?
303+
pub fn public_key(&self, path: &DerivationPath) -> PublicKey {
305304
let xpub = self.xpub.derive_pub(&crate::EC, path).unwrap();
306-
let public_key = &xpub.public_key;
305+
xpub.public_key
306+
}
307+
308+
pub fn script_code(&self, path: &DerivationPath) -> BEScript {
309+
let public_key = self.public_key(path);
307310
// script code is the same for the currently supported script type
308-
p2pkh_script(public_key).into()
311+
p2pkh_script(&public_key).into()
309312
}
310313

311314
pub fn tx_outputs(&self, tx: &BETransaction) -> Result<Vec<TransactionOutput>, Error> {
@@ -385,6 +388,7 @@ impl Account {
385388
scriptpubkey: tx.output_script(vout),
386389
sequence: None,
387390
script_code: self.script_code(&account_path).to_hex(),
391+
public_key: self.public_key(&account_path).to_string(),
388392
user_path: self.get_full_path(&account_path).into(),
389393
})
390394
}

0 commit comments

Comments
 (0)