-
Notifications
You must be signed in to change notification settings - Fork 2
/
unused-stuff.txt
231 lines (178 loc) · 8.44 KB
/
unused-stuff.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// pub fn create_psbt_and_broadcast(
// CreatePsbtInput { descriptor, amount, recipient }: CreatePsbtInput
// ){
// let wallet = BdkWallet::create_wallet(descriptor)?;
// let dest_script = bdk::bitcoin::Address::from_str(recipient); // .script_pubkey();
// let dest_script = match dest_script {
// Ok(script) => script.script_pubkey(),
// Err(_) => return Err(bdk::Error::Generic("Invalid address".to_string())),
// };
// let mut tx_builder = wallet.build_tx().coin_selection(DefaultCoinSelectionAlgorithm::default());
// // // The Coldcard requires an output redeem witness script
// tx_builder.include_output_redeem_witness_script();
// // // Enable signaling replace-by-fee
// tx_builder.enable_rbf();
// // // Add our script and the amount in sats to send
// tx_builder.add_recipient(dest_script, amount);
// // "Finish" the builder which returns a tuple:
// // A `PartiallySignedTransaction` which serializes as a psbt
// // And `TransactionDetails` which has helpful info about the transaction we just built
// let (mut psbt, details) = tx_builder.finish()?;
// // temporary, going to go ahead and sign and broadcast here:
// let signed = wallet.sign(&mut psbt, SignOptions::default())?;
// let tx = psbt.extract_tx();
// // Broadcast the transaction using our chosen backend, returning a `Txid` or an error
// let client = Client::new("tcp://localhost:50000")?;
// let blockchain = ElectrumBlockchain::from(client);
// let txid = blockchain.broadcast(&tx)?;
// println!("{:#?}", txid);
// /////
// // let serialized_psbt = base64::encode(&serialize(&psbt));
// //let serialized_psbt = base64::encode(&serialize(&psbt));
// // println!("{:#?}", details);
// // println!("{}", serialized_psbt);
// // Ok()
// }
// pub fn sign_psbt_with_mnemonic(
// mnemonic: &str,
// mut psbt: &mut PartiallySignedTransaction
// ) -> Result<Wallet<MemoryDatabase>, bdk::Error>{
// // let client = Client::new("ssl://electrum.blockstream.info:60002")?;
// let client = Client::new("tcp://localhost:50000")?;
// let blockchain = ElectrumBlockchain::from(client);
// // Parse a mnemonic
// let mnemonic = Mnemonic::parse(mnemonic).unwrap();
// // Generate the extended key
// let xkey: ExtendedKey = mnemonic.into_extended_key().unwrap();
// // Get xprv from the extended key
// let xprv = xkey.into_xprv(bdk::bitcoin::Network::Regtest).unwrap();
// let wallet = Wallet::new(
// Bip84(xprv, KeychainKind::External),
// Some(Bip84(xprv, KeychainKind::Internal)),
// bdk::bitcoin::Network::Regtest,
// MemoryDatabase::default(),
// )
// .unwrap();
// wallet.sync(&blockchain, SyncOptions::default())?;
// let sign_result = wallet.sign(&mut psbt, SignOptions::default())?;
// println!("sign_result: {:#?}", sign_result);
// let _psbt_is_finalized = wallet.finalize_psbt(&mut psbt, SignOptions::default())?;
// println!("psbt_is_finalized: {:#?}", _psbt_is_finalized);
// let tx = psbt.clone().extract_tx();
// // Broadcast the transaction using our chosen backend, returning a `Txid` or an error
// let txid = blockchain.broadcast(&tx)?;
// // println!("broadcasted txid: {:#?}", txid);
// Ok(wallet)
// }
// #[tauri::command]
// fn hwiget() -> Result<(), Error> {
// // Find information about devices
// let devices = HWIClient::enumerate()?;
// let device = devices.first().expect("No devices");
// // Create a client for a device
// let client = HWIClient::get_client(&device, true, bitcoin::Network::Regtest.into())?;
// // Display the address from path
// let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0")?;
// let hwi_address =
// client.display_address_with_path(&derivation_path, types::HWIAddressType::Tap)?;
// println!("{}", hwi_address.address);
// Ok(())
// }
// fn get_hwi() -> Result<(), Error> {
// // Find information about devices
// let mut devices = HWIClient::enumerate()?;
// if devices.is_empty() {
// panic!("No device found!");
// }
// let device = devices.remove(0)?;
// // Create a client for a device
// let client = HWIClient::get_client(&device, true, bitcoin::Network::Regtest.into())?;
// // Display the address from path
// // let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
// let account: Option<u32> = Some(1);
// let descriptors = client.get_descriptors(account).into()?;
// //let hwi_address =
// // client.display_address_with_path(&derivation_path, types::HWIAddressType::Tap)?;
// // println!("{}", hwi_address.address.assume_checked());
// // let printable_descriptors = descriptors.iter().map(|descriptor| {
// // format!("Descriptor: {}", descriptor)
// // }).collect::<Vec<String>>();
// // println!("descriptors from HW {}", );
// Ok(())
// }
// #[tauri::command]
// fn get_hw_info() -> Result<(), tauri::Error> {
// match get_hw() {
// Ok(_) => {
// println!("HWI get_hw_info success");
// Ok(())
// },
// Err(e) => {
// eprintln!("Error get_hw_info: {}", e);
// Err(e)
// },
// }
// }
#[tauri::command]
fn get_hw_descriptors() -> Result<Vec<String>, String> {
get_descriptors::<String>().map_err(|e| e.to_string())
}
fn get_descriptors<T>() -> Result<Vec<String>, Error>
where T: ToDescriptor + DeserializeOwned + Debug,
{
println!("get_descriptors");
// Find information about devices
// let mut devices = HWIClient::enumerate()?;
let mut devices = match HWIClient::enumerate() {
Ok(devices) => devices,
Err(e) => {
println!("Error: {:?}", e);
return Err(e);
}
};
if devices.is_empty() {
panic!("No device found!");
}
// print devices
println!("devices: {:?}", devices);
let device = devices.remove(0)?;
// Create a client for a device
let client = HWIClient::get_client(&device, true, bitcoin::Network::Regtest.into())?;
// Get descriptors
// let account: Option<u32> = Some(1);
//let descriptors: types::HWIDescriptor<hwi::types::HWIExtendedPubKey> = client.get_descriptors(account)?;
let descriptors: HWIDescriptor<T> = client.get_descriptors(None)?;
let internal_descriptor = descriptors.internal.iter().map(|descriptor| {
format!("Descriptor: {:?}", descriptor)
}).collect::<Vec<String>>();
Ok(internal_descriptor)
}
// pub fn create_new_wallet() -> Result<(), bdk::Error> {
// // let client = Client::new("ssl://electrum.blockstream.info:60002")?;
// let network = bdk::bitcoin::Network::Regtest; // Or this can be Network::Bitcoin, Network::Signet or Network::Regtest
// // Generate fresh mnemonic
// let mnemonic: GeneratedKey<_, miniscript::Segwitv0> = Mnemonic::generate((WordCount::Words12, Language::English)).unwrap();
// // Convert mnemonic to string
// let mnemonic_words = mnemonic.to_string();
// // Parse a mnemonic
// let mnemonic = Mnemonic::parse(&mnemonic_words).unwrap();
// // Generate the extended key
// let xkey: ExtendedKey = mnemonic.into_extended_key().unwrap();
// // Get xprv from the extended key
// let xprv = xkey.into_xprv(network).unwrap();
// // Create a BDK wallet structure using BIP 84 descriptor ("m/84h/1h/0h/0" and "m/84h/1h/0h/1")
// let wallet = Wallet::new(
// Bip84(xprv, KeychainKind::External),
// Some(Bip84(xprv, KeychainKind::Internal)),
// network,
// MemoryDatabase::default(),
// )
// .unwrap();
// println!("mnemonic: {}\n\nrecv desc (pub key): {:#?}\n\nchng desc (pub key): {:#?}",
// mnemonic_words,
// wallet.get_descriptor_for_keychain(KeychainKind::External).to_string(),
// wallet.get_descriptor_for_keychain(KeychainKind::Internal).to_string());
// // print the recovery words:
// println!("Recovery words: {}", mnemonic_words);
// Ok(())
// }