forked from Blockstream/gdk
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.rs
1105 lines (982 loc) · 45.2 KB
/
interface.rs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use bitcoin::blockdata::script::Script;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::hashes::{hex::FromHex, Hash};
use bitcoin::secp256k1::{self, All, Message, Secp256k1};
use bitcoin::util::address::{Address, Payload};
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey};
use bitcoin::{BlockHash, PublicKey, SigHashType, Txid};
use elements;
use gdk_common::model::{AddressAmount, Balances, GetTransactionsOpt, SPVVerifyResult};
use hex;
use log::{info, trace};
use rand::Rng;
use serde::{Deserialize, Serialize};
use gdk_common::mnemonic::Mnemonic;
use gdk_common::model::{AddressPointer, CreateTransaction, Settings, TransactionMeta};
use gdk_common::network::{ElementsNetwork, Network, NetworkId};
use gdk_common::scripts::{p2pkh_script, p2shwpkh_script, p2shwpkh_script_sig};
use gdk_common::wally::*;
use crate::error::*;
use crate::store::*;
use bitcoin::util::bip143::SigHashCache;
use electrum_client::raw_client::RawClient;
use electrum_client::Client;
use elements::confidential::{Asset, Nonce, Value};
use gdk_common::be::{self, *};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::str::FromStr;
pub struct WalletCtx {
pub secp: Secp256k1<All>,
pub network: Network,
pub mnemonic: Mnemonic,
pub store: Store,
pub xprv: ExtendedPrivKey,
pub xpub: ExtendedPubKey,
pub master_blinding: Option<MasterBlindingKey>,
pub change_max_deriv: u32,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum ElectrumUrl {
Tls(String, bool), // the bool value indicates if the domain name should be validated
Plaintext(String),
}
impl ElectrumUrl {
pub fn build_client(&self) -> Result<Client, Error> {
match self {
ElectrumUrl::Tls(url, validate) => {
let client = RawClient::new_ssl(url.as_str(), *validate)?;
Ok(Client::SSL(client))
}
ElectrumUrl::Plaintext(url) => {
let client = RawClient::new(&url)?;
Ok(Client::TCP(client))
}
}
}
}
// Parse the standard <host>:<port>:<t|s> string format,
// with an optional non-standard `:noverify` suffix to skip tls validation
impl FromStr for ElectrumUrl {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
let mk_err = || Error::InvalidElectrumUrl(s.into());
let mut parts = s.split(":");
let hostname = parts.next().ok_or_else(mk_err)?;
let port: u16 = parts.next().ok_or_else(mk_err)?.parse().map_err(|_| mk_err())?;
let proto = parts.next().ok_or_else(mk_err)?;
let validate_tls = parts.next() != Some("noverify");
let url = format!("{}:{}", hostname, port);
Ok(match proto {
"s" => ElectrumUrl::Tls(url, validate_tls),
"t" => ElectrumUrl::Plaintext(url),
_ => return Err(mk_err()),
})
}
}
impl WalletCtx {
pub fn new(
store: Store,
mnemonic: Mnemonic,
network: Network,
xprv: ExtendedPrivKey,
xpub: ExtendedPubKey,
master_blinding: Option<MasterBlindingKey>,
) -> Result<Self, Error> {
Ok(WalletCtx {
mnemonic,
store,
network, // TODO: from db
secp: Secp256k1::gen_new(),
xprv,
xpub,
master_blinding,
change_max_deriv: 0,
})
}
pub fn get_mnemonic(&self) -> &Mnemonic {
&self.mnemonic
}
fn derive_address(&self, xpub: &ExtendedPubKey, path: [u32; 2]) -> Result<BEAddress, Error> {
let path: Vec<ChildNumber> = path
.iter()
.map(|x| ChildNumber::Normal {
index: *x,
})
.collect();
let derived = xpub.derive_pub(&self.secp, &path)?;
match self.network.id() {
NetworkId::Bitcoin(network) => {
Ok(BEAddress::Bitcoin(Address::p2shwpkh(&derived.public_key, network).unwrap()))
}
NetworkId::Elements(network) => {
let master_blinding_key = self
.master_blinding
.as_ref()
.expect("we are in elements but master blinding is None");
let script = p2shwpkh_script(&derived.public_key);
let blinding_key =
asset_blinding_key_to_ec_private_key(&master_blinding_key, &script);
let public_key = ec_public_key_from_private_key(blinding_key);
let blinder = Some(public_key);
let addr = elements::Address::p2shwpkh(
&derived.public_key,
blinder,
address_params(network),
);
Ok(BEAddress::Elements(addr))
}
}
}
pub fn get_settings(&self) -> Result<Settings, Error> {
Ok(self.store.read()?.get_settings().unwrap_or_default())
}
pub fn change_settings(&self, settings: &Settings) -> Result<(), Error> {
self.store.write()?.insert_settings(Some(settings.clone()))?;
Ok(())
}
pub fn get_tip(&self) -> Result<(u32, BlockHash), Error> {
Ok(self.store.read()?.cache.tip)
}
pub fn list_tx(&self, opt: &GetTransactionsOpt) -> Result<Vec<TransactionMeta>, Error> {
let store_read = self.store.read()?;
let mut txs = vec![];
let mut my_txids: Vec<(&Txid, &Option<u32>)> = store_read.cache.heights.iter().collect();
my_txids.sort_by(|a, b| {
let height_cmp = b.1.unwrap_or(std::u32::MAX).cmp(&a.1.unwrap_or(std::u32::MAX));
match height_cmp {
Ordering::Equal => b.0.cmp(a.0),
h @ _ => h,
}
});
for (tx_id, height) in my_txids.iter().skip(opt.first).take(opt.count) {
trace!("tx_id {}", tx_id);
let tx = store_read
.cache
.all_txs
.get(*tx_id)
.ok_or_else(fn_err(&format!("list_tx no tx {}", tx_id)))?;
let header = height.map(|h| store_read.cache.headers.get(&h)).flatten();
trace!("tx_id {} header {:?}", tx_id, header);
let mut addressees = vec![];
for i in 0..tx.output_len() as u32 {
let script = tx.output_script(i);
if !script.is_empty() && !store_read.cache.paths.contains_key(&script) {
let address = tx.output_address(i, self.network.id());
trace!("tx_id {}:{} not my script, address {:?}", tx_id, i, address);
addressees.push(AddressAmount {
address: address.unwrap_or_else(|| "".to_string()),
satoshi: 0, // apparently not needed in list_tx addressees
asset_tag: None,
});
}
}
let memo = store_read.get_memo(tx_id).map(|s| s.to_string());
let create_transaction = CreateTransaction {
addressees,
memo,
..Default::default()
};
let fee = tx.fee(
&store_read.cache.all_txs,
&store_read.cache.unblinded,
&self.network.policy_asset().ok(),
)?;
trace!("tx_id {} fee {}", tx_id, fee);
let satoshi = tx.my_balance_changes(
&store_read.cache.all_txs,
&store_read.cache.paths,
&store_read.cache.unblinded,
);
trace!("tx_id {} balances {:?}", tx_id, satoshi);
// We define an incoming txs if there are more assets received by the wallet than spent
// when they are equal it's an outgoing tx because the special asset liquid BTC
// is negative due to the fee being paid
// TODO how do we label issuance tx?
let negatives = satoshi.iter().filter(|(_, v)| **v < 0).count();
let positives = satoshi.iter().filter(|(_, v)| **v > 0).count();
let (type_, user_signed) = match (
positives > negatives,
tx.is_redeposit(&store_read.cache.paths, &store_read.cache.all_txs),
) {
(_, true) => ("redeposit", true),
(true, false) => ("incoming", false),
(false, false) => ("outgoing", true),
};
let spv_verified = if self.network.spv_enabled.unwrap_or(false) {
store_read
.cache
.txs_verif
.get(*tx_id)
.unwrap_or(&SPVVerifyResult::InProgress)
.clone()
} else {
SPVVerifyResult::Disabled
};
trace!(
"tx_id {} type {} user_signed {} spv_verified {:?}",
tx_id,
type_,
user_signed,
spv_verified
);
let tx_meta = TransactionMeta::new(
tx.clone(),
**height,
header.map(|h| h.time()),
satoshi,
fee,
self.network.id().get_bitcoin_network().unwrap_or(bitcoin::Network::Bitcoin),
type_.to_string(),
create_transaction,
user_signed,
spv_verified,
);
txs.push(tx_meta);
}
info!("list_tx {:?}", txs.iter().map(|e| &e.txid).collect::<Vec<&String>>());
Ok(txs)
}
pub fn utxos(&self) -> Result<Utxos, Error> {
info!("start utxos");
let store_read = self.store.read()?;
let mut utxos = vec![];
let spent = store_read.spent()?;
for (tx_id, height) in store_read.cache.heights.iter() {
let tx = store_read
.cache
.all_txs
.get(tx_id)
.ok_or_else(fn_err(&format!("utxos no tx {}", tx_id)))?;
let tx_utxos: Vec<(BEOutPoint, UTXOInfo)> = match tx {
BETransaction::Bitcoin(tx) => tx
.output
.clone()
.into_iter()
.enumerate()
.filter(|(_, output)| output.value > DUST_VALUE)
.map(|(vout, output)| (BEOutPoint::new_bitcoin(tx.txid(), vout as u32), output))
.filter_map(|(vout, output)| {
store_read
.cache
.paths
.get(&output.script_pubkey)
.map(|path| (vout, output, path))
})
.filter(|(outpoint, _, _)| !spent.contains(&outpoint))
.map(|(outpoint, output, path)| {
(
outpoint,
UTXOInfo::new(
"btc".to_string(),
output.value,
output.script_pubkey,
height.clone(),
path.clone(),
),
)
})
.collect(),
BETransaction::Elements(tx) => {
let policy_asset = self.network.policy_asset_id()?;
tx.output
.clone()
.into_iter()
.enumerate()
.map(|(vout, output)| {
(BEOutPoint::new_elements(tx.txid(), vout as u32), output)
})
.filter_map(|(vout, output)| {
store_read
.cache
.paths
.get(&output.script_pubkey)
.map(|path| (vout, output, path))
})
.filter(|(outpoint, _, _)| !spent.contains(&outpoint))
.filter_map(|(outpoint, output, path)| {
if let BEOutPoint::Elements(el_outpoint) = outpoint {
if let Some(unblinded) =
store_read.cache.unblinded.get(&el_outpoint)
{
if unblinded.value < DUST_VALUE
&& unblinded.asset == policy_asset
{
return None;
}
return Some((
outpoint,
UTXOInfo::new(
unblinded.asset_hex(),
unblinded.value,
output.script_pubkey,
height.clone(),
path.clone(),
),
));
}
}
None
})
.collect()
}
};
utxos.extend(tx_utxos);
}
utxos.sort_by(|a, b| (b.1).value.cmp(&(a.1).value));
Ok(utxos)
}
pub fn balance(&self) -> Result<Balances, Error> {
info!("start balance");
let mut result = HashMap::new();
match self.network.id() {
NetworkId::Bitcoin(_) => result.entry("btc".to_string()).or_insert(0),
NetworkId::Elements(_) => {
result.entry(self.network.policy_asset.as_ref().unwrap().clone()).or_insert(0)
}
};
for (_, info) in self.utxos()?.iter() {
*result.entry(info.asset.clone()).or_default() += info.value as i64;
}
Ok(result)
}
#[allow(clippy::cognitive_complexity)]
pub fn create_tx(&self, request: &mut CreateTransaction) -> Result<TransactionMeta, Error> {
info!("create_tx {:?}", request);
// TODO put checks into CreateTransaction::validate, add check asset_tag are valid asset hex
// eagerly check for address validity
for address in request.addressees.iter().map(|a| &a.address) {
match self.network.id() {
NetworkId::Bitcoin(network) => {
if let Ok(address) = bitcoin::Address::from_str(address) {
info!("address.network:{} network:{}", address.network, network);
if address.network == network
|| (address.network == bitcoin::Network::Testnet
&& network == bitcoin::Network::Regtest)
{
continue;
}
if let Payload::WitnessProgram {
version: v,
program: _p,
} = &address.payload
{
// Do not support segwit greater than v0
if v.to_u8() > 0 {
return Err(Error::InvalidAddress);
}
}
}
return Err(Error::InvalidAddress);
}
NetworkId::Elements(network) => {
if let Ok(address) = elements::Address::from_str(address) {
info!(
"address.params:{:?} address_params(network):{:?}",
address.params,
address_params(network)
);
if address.params == address_params(network) {
continue;
}
}
return Err(Error::InvalidAddress);
}
}
}
if request.addressees.is_empty() {
return Err(Error::EmptyAddressees);
}
let subaccount = request.subaccount.unwrap_or(0);
if subaccount != 0 {
return Err(Error::InvalidSubaccount(subaccount));
}
if !request.previous_transaction.is_empty() {
return Err(Error::Generic("bump not supported".into()));
}
let send_all = request.send_all.unwrap_or(false);
request.send_all = Some(send_all); // accept default false, but always return the value
if !send_all && request.addressees.iter().any(|a| a.satoshi == 0) {
return Err(Error::InvalidAmount);
}
if !send_all {
for address_amount in request.addressees.iter() {
if address_amount.satoshi <= be::DUST_VALUE {
match self.network.id() {
NetworkId::Bitcoin(_) => return Err(Error::InvalidAmount),
NetworkId::Elements(_) => {
if address_amount.asset_tag == self.network.policy_asset {
// we apply dust rules for liquid bitcoin as elements do
return Err(Error::InvalidAmount);
}
}
}
}
}
}
if let NetworkId::Elements(_) = self.network.id() {
if request.addressees.iter().any(|a| a.asset_tag.is_none()) {
return Err(Error::AssetEmpty);
}
}
// convert from satoshi/kbyte to satoshi/byte
let default_value = match self.network.id() {
NetworkId::Bitcoin(_) => 1000,
NetworkId::Elements(_) => 100,
};
let fee_rate = (request.fee_rate.unwrap_or(default_value) as f64) / 1000.0;
info!("target fee_rate {:?} satoshi/byte", fee_rate);
let utxos = match &request.utxos {
None => self.utxos()?,
Some(utxos) => utxos.try_into()?,
};
info!("utxos len:{} utxos:{:?}", utxos.len(), utxos);
if send_all {
// send_all works by creating a dummy tx with all utxos, estimate the fee and set the
// sending amount to `total_amount_utxos - estimated_fee`
info!("send_all calculating total_amount");
if request.addressees.len() != 1 {
return Err(Error::SendAll);
}
let asset = request.addressees[0].asset_tag.as_deref().unwrap_or("btc");
let all_utxos: Vec<&(BEOutPoint, UTXOInfo)> =
utxos.iter().filter(|(_, i)| i.asset == asset).collect();
let total_amount_utxos: u64 = all_utxos.iter().map(|(_, i)| i.value).sum();
let to_send = if asset == "btc" || Some(asset.to_string()) == self.network.policy_asset
{
let mut dummy_tx = BETransaction::new(self.network.id());
for utxo in all_utxos.iter() {
dummy_tx.add_input(utxo.0.clone());
}
let out = &request.addressees[0]; // safe because we checked we have exactly one recipient
dummy_tx
.add_output(&out.address, out.satoshi, out.asset_tag.clone())
.map_err(|_| Error::InvalidAddress)?;
let estimated_fee = dummy_tx.estimated_fee(fee_rate, 0) + 3; // estimating 3 satoshi more as estimating less would later result in InsufficientFunds
total_amount_utxos
.checked_sub(estimated_fee)
.ok_or_else(|| Error::InsufficientFunds)?
} else {
total_amount_utxos
};
info!("send_all asset: {} to_send:{}", asset, to_send);
request.addressees[0].satoshi = to_send;
}
let mut tx = BETransaction::new(self.network.id());
// transaction is created in 3 steps:
// 1) adding requested outputs to tx outputs
// 2) adding enough utxso to inputs such that tx outputs and estimated fees are covered
// 3) adding change(s)
// STEP 1) add the outputs requested for this transactions
for out in request.addressees.iter() {
tx.add_output(&out.address, out.satoshi, out.asset_tag.clone())
.map_err(|_| Error::InvalidAddress)?;
}
// STEP 2) add utxos until tx outputs are covered (including fees) or fail
let store_read = self.store.read()?;
let mut used_utxo: HashSet<BEOutPoint> = HashSet::new();
loop {
let mut needs = tx.needs(
fee_rate,
send_all,
self.network.policy_asset.clone(),
&store_read.cache.all_txs,
&store_read.cache.unblinded,
); // Vec<(asset_string, satoshi) "policy asset" is last, in bitcoin asset_string="btc" and max 1 element
info!("needs: {:?}", needs);
if needs.is_empty() {
// SUCCESS tx doesn't need other inputs
break;
}
let current_need = needs.pop().unwrap(); // safe to unwrap just checked it's not empty
// taking only utxos of current asset considered, filters also utxos used in this loop
let mut asset_utxos: Vec<&(BEOutPoint, UTXOInfo)> = utxos
.iter()
.filter(|(o, i)| i.asset == current_need.asset && !used_utxo.contains(o))
.collect();
// sort by biggest utxo, random maybe another option, but it should be deterministically random (purely random breaks send_all algorithm)
asset_utxos.sort_by(|a, b| (a.1).value.cmp(&(b.1).value));
let utxo = asset_utxos.pop().ok_or(Error::InsufficientFunds)?;
match self.network.id() {
NetworkId::Bitcoin(_) => {
// UTXO with same script must be spent together
for other_utxo in utxos.iter() {
if (other_utxo.1).script == (utxo.1).script {
used_utxo.insert(other_utxo.0.clone());
tx.add_input(other_utxo.0.clone());
}
}
}
NetworkId::Elements(_) => {
// Don't spend same script together in liquid. This would allow an attacker
// to cheaply send assets without value to the target, which will have to
// waste fees for the extra tx inputs and (eventually) outputs.
// While blinded address are required and not public knowledge,
// they are still available to whom transacted with us in the past
used_utxo.insert(utxo.0.clone());
tx.add_input(utxo.0.clone());
}
}
}
// STEP 3) adding change(s)
let estimated_fee = tx.estimated_fee(
fee_rate,
tx.estimated_changes(send_all, &store_read.cache.all_txs, &store_read.cache.unblinded),
);
let changes = tx.changes(
estimated_fee,
self.network.policy_asset.clone(),
&store_read.cache.all_txs,
&store_read.cache.unblinded,
); // Vec<Change> asset, value
for (i, change) in changes.iter().enumerate() {
let change_index = store_read.cache.indexes.internal + i as u32 + 1;
let change_address = self.derive_address(&self.xpub, [1, change_index])?.to_string();
info!(
"adding change to {} of {} asset {:?}",
&change_address, change.satoshi, change.asset
);
tx.add_output(&change_address, change.satoshi, Some(change.asset.clone()))?;
}
// randomize inputs and outputs, BIP69 has been rejected because lacks wallets adoption
tx.scramble();
let policy_asset = self.network.policy_asset().ok();
let fee_val =
tx.fee(&store_read.cache.all_txs, &store_read.cache.unblinded, &policy_asset)?; // recompute exact fee_val from built tx
tx.add_fee_if_elements(fee_val, &policy_asset)?;
info!("created tx fee {:?}", fee_val);
let mut satoshi = tx.my_balance_changes(
&store_read.cache.all_txs,
&store_read.cache.paths,
&store_read.cache.unblinded,
);
for (_, v) in satoshi.iter_mut() {
*v = v.abs();
}
let mut created_tx = TransactionMeta::new(
tx,
None,
None,
satoshi,
fee_val,
self.network.id().get_bitcoin_network().unwrap_or(bitcoin::Network::Bitcoin),
"outgoing".to_string(),
request.clone(),
true,
SPVVerifyResult::InProgress,
);
created_tx.changes_used = Some(changes.len() as u32);
info!("returning: {:?}", created_tx);
Ok(created_tx)
}
// TODO when we can serialize psbt
//pub fn sign(&self, psbt: PartiallySignedTransaction) -> Result<PartiallySignedTransaction, Error> { Err(Error::Generic("NotImplemented".to_string())) }
fn internal_sign_bitcoin(
&self,
tx: &Transaction,
input_index: usize,
path: &DerivationPath,
value: u64,
) -> (Script, Vec<Vec<u8>>) {
let xprv = self.xprv.derive_priv(&self.secp, &path).unwrap();
let private_key = &xprv.private_key;
let public_key = &PublicKey::from_private_key(&self.secp, private_key);
let witness_script = p2pkh_script(public_key);
let hash = SigHashCache::new(tx).signature_hash(
input_index,
&witness_script,
value,
SigHashType::All,
);
let message = Message::from_slice(&hash.into_inner()[..]).unwrap();
let signature = self.secp.sign(&message, &private_key.key);
let mut signature = signature.serialize_der().to_vec();
signature.push(SigHashType::All as u8);
let script_sig = p2shwpkh_script_sig(public_key);
let witness = vec![signature, public_key.to_bytes()];
info!(
"added size len: script_sig:{} witness:{}",
script_sig.len(),
witness.iter().map(|v| v.len()).sum::<usize>()
);
(script_sig, witness)
}
pub fn internal_sign_elements(
&self,
tx: &elements::Transaction,
input_index: usize,
derivation_path: &DerivationPath,
value: Value,
) -> (Script, Vec<Vec<u8>>) {
let xprv = self.xprv.derive_priv(&self.secp, &derivation_path).unwrap();
let private_key = &xprv.private_key;
let public_key = &PublicKey::from_private_key(&self.secp, private_key);
let script_code = p2pkh_script(public_key);
let sighash = tx_get_elements_signature_hash(
&tx,
input_index,
&script_code,
&value,
SigHashType::All.as_u32(),
true, // segwit
);
let message = secp256k1::Message::from_slice(&sighash[..]).unwrap();
let signature = self.secp.sign(&message, &private_key.key);
let mut signature = signature.serialize_der().to_vec();
signature.push(SigHashType::All as u8);
let script_sig = p2shwpkh_script_sig(public_key);
let witness = vec![signature, public_key.to_bytes()];
info!(
"added size len: script_sig:{} witness:{}",
script_sig.len(),
witness.iter().map(|v| v.len()).sum::<usize>()
);
(script_sig, witness)
}
pub fn sign(&self, request: &TransactionMeta) -> Result<TransactionMeta, Error> {
info!("sign");
let be_tx = BETransaction::deserialize(&hex::decode(&request.hex)?, self.network.id())?;
let store_read = self.store.read()?;
let mut betx: TransactionMeta = match be_tx {
BETransaction::Bitcoin(tx) => {
let mut out_tx = tx.clone();
for i in 0..tx.input.len() {
let prev_output = tx.input[i].previous_output;
info!("input#{} prev_output:{:?}", i, prev_output);
let prev_tx = store_read.get_bitcoin_tx(&prev_output.txid)?;
let out = prev_tx.output[prev_output.vout as usize].clone();
let derivation_path: DerivationPath = store_read
.cache
.paths
.get(&out.script_pubkey)
.ok_or_else(|| Error::Generic("can't find derivation path".into()))?
.clone();
info!(
"input#{} prev_output:{:?} derivation_path:{:?}",
i, prev_output, derivation_path
);
let (script_sig, witness) =
self.internal_sign_bitcoin(&tx, i, &derivation_path, out.value);
out_tx.input[i].script_sig = script_sig;
out_tx.input[i].witness = witness;
}
let tx = BETransaction::Bitcoin(out_tx);
info!(
"transaction final size is {} bytes and {} vbytes",
tx.serialize().len(),
tx.get_weight() / 4
);
info!("FINALTX inputs:{} outputs:{}", tx.input_len(), tx.output_len());
tx.into()
}
BETransaction::Elements(mut tx) => {
self.blind_tx(&mut tx)?;
for i in 0..tx.input.len() {
let prev_output = tx.input[i].previous_output;
info!("input#{} prev_output:{:?}", i, prev_output);
let prev_tx = store_read.get_liquid_tx(&prev_output.txid)?;
let out = prev_tx.output[prev_output.vout as usize].clone();
let derivation_path: DerivationPath = store_read
.cache
.paths
.get(&out.script_pubkey)
.ok_or_else(|| Error::Generic("can't find derivation path".into()))?
.clone();
let (script_sig, witness) =
self.internal_sign_elements(&tx, i, &derivation_path, out.value);
tx.input[i].script_sig = script_sig;
tx.input[i].witness.script_witness = witness;
}
let fee: u64 =
tx.output.iter().filter(|o| o.is_fee()).map(|o| o.minimum_value()).sum();
let tx = BETransaction::Elements(tx);
info!(
"transaction final size is {} bytes and {} vbytes and fee is {}",
tx.serialize().len(),
tx.get_weight() / 4,
fee
);
info!("FINALTX inputs:{} outputs:{}", tx.input_len(), tx.output_len());
tx.into()
}
};
betx.fee = request.fee;
betx.create_transaction = request.create_transaction.clone();
drop(store_read);
let mut store_write = self.store.write()?;
let changes_used = request.changes_used.unwrap_or(0);
if changes_used > 0 {
info!("tx used {} changes", changes_used);
// The next sync would update the internal index but we increment the internal index also
// here after sign so that if we immediately create another tx we are not reusing addresses
// This implies signing multiple times without broadcasting leads to gaps in the internal chain
store_write.cache.indexes.internal += changes_used;
}
if let Some(memo) = request.create_transaction.as_ref().and_then(|c| c.memo.as_ref()) {
store_write.insert_memo(Txid::from_hex(&betx.txid)?, memo)?;
}
Ok(betx)
}
fn blind_tx(&self, tx: &mut elements::Transaction) -> Result<(), Error> {
info!("blind_tx {}", tx.txid());
let mut input_assets = vec![];
let mut input_abfs = vec![];
let mut input_vbfs = vec![];
let mut input_ags = vec![];
let mut input_values = vec![];
let store_read = self.store.read()?;
for input in tx.input.iter() {
info!("input {:?}", input);
let unblinded = store_read
.cache
.unblinded
.get(&input.previous_output)
.ok_or_else(|| Error::Generic("cannot find unblinded values".into()))?;
info!(
"unblinded value: {} asset:{}",
unblinded.value,
hex::encode(&unblinded.asset[..])
);
input_values.push(unblinded.value);
input_assets.extend(unblinded.asset.to_vec());
input_abfs.extend(unblinded.abf.to_vec());
input_vbfs.extend(unblinded.vbf.to_vec());
let input_asset = asset_generator_from_bytes(&unblinded.asset, &unblinded.abf);
input_ags.extend(elements::encode::serialize(&input_asset));
}
let ct_exp = self.network.ct_exponent.expect("ct_exponent not set in network");
let ct_bits = self.network.ct_bits.expect("ct_bits not set in network");
info!("ct params ct_exp:{}, ct_bits:{}", ct_exp, ct_bits);
let mut output_blinded_values = vec![];
for output in tx.output.iter() {
if !output.is_fee() {
output_blinded_values.push(output.minimum_value());
}
}
info!("output_blinded_values {:?}", output_blinded_values);
let mut all_values = vec![];
all_values.extend(input_values);
all_values.extend(output_blinded_values);
let in_num = tx.input.len();
let out_num = tx.output.len();
let output_abfs: Vec<Vec<u8>> = (0..out_num - 1).map(|_| random32()).collect();
let mut output_vbfs: Vec<Vec<u8>> = (0..out_num - 2).map(|_| random32()).collect();
let mut all_abfs = vec![];
all_abfs.extend(input_abfs.to_vec());
all_abfs.extend(output_abfs.iter().cloned().flatten().collect::<Vec<u8>>());
let mut all_vbfs = vec![];
all_vbfs.extend(input_vbfs.to_vec());
all_vbfs.extend(output_vbfs.iter().cloned().flatten().collect::<Vec<u8>>());
let last_vbf = asset_final_vbf(all_values, in_num as u32, all_abfs, all_vbfs);
output_vbfs.push(last_vbf.to_vec());
for (i, mut output) in tx.output.iter_mut().enumerate() {
info!("output {:?}", output);
if !output.is_fee() {
match (output.value, output.asset, output.nonce) {
(Value::Explicit(value), Asset::Explicit(asset), Nonce::Confidential(_, _)) => {
info!("value: {}", value);
let nonce = elements::encode::serialize(&output.nonce);
let blinding_pubkey = PublicKey::from_slice(&nonce).unwrap();
let blinding_key = asset_blinding_key_to_ec_private_key(
self.master_blinding.as_ref().unwrap(),
&output.script_pubkey,
);
let blinding_public_key = ec_public_key_from_private_key(blinding_key);
let mut output_abf = [0u8; 32];
output_abf.copy_from_slice(&(&output_abfs[i])[..]);
let mut output_vbf = [0u8; 32];
output_vbf.copy_from_slice(&(&output_vbfs[i])[..]);
let asset = asset.clone().into_inner();
let output_generator =
asset_generator_from_bytes(&asset.into_inner(), &output_abf);
let output_value_commitment =
asset_value_commitment(value, output_vbf, output_generator);
let min_value = if output.script_pubkey.is_provably_unspendable() {
0
} else {
1
};
let rangeproof = asset_rangeproof(
value,
blinding_pubkey.key,
blinding_key,
asset.into_inner(),
output_abf,
output_vbf,
output_value_commitment,
&output.script_pubkey,
output_generator,
min_value,
ct_exp,
ct_bits,
);
trace!("asset: {}", hex::encode(&asset));
trace!("output_abf: {}", hex::encode(&output_abf));
trace!(
"output_generator: {}",
hex::encode(&elements::encode::serialize(&output_generator))
);
trace!("input_assets: {}", hex::encode(&input_assets));
trace!("input_abfs: {}", hex::encode(&input_abfs));
trace!("input_ags: {}", hex::encode(&input_ags));
trace!("in_num: {}", in_num);
let surjectionproof = asset_surjectionproof(
asset.into_inner(),
output_abf,
output_generator,
output_abf,
&input_assets,
&input_abfs,
&input_ags,
in_num,
);
trace!("surjectionproof: {}", hex::encode(&surjectionproof));
let bytes = blinding_public_key.serialize();
let byte32: [u8; 32] = bytes[1..].as_ref().try_into().unwrap();
output.nonce =
elements::confidential::Nonce::Confidential(bytes[0], byte32);
output.asset = output_generator;
output.value = output_value_commitment;
info!(
"added size len: surjectionproof:{} rangeproof:{}",
surjectionproof.len(),
rangeproof.len()
);
output.witness.surjection_proof = surjectionproof;
output.witness.rangeproof = rangeproof;
}
_ => panic!("create_tx created things not right"),
}
}
}
Ok(())
}
pub fn get_address(&self) -> Result<AddressPointer, Error> {
let pointer = {
let store = &mut self.store.write()?.cache;
store.indexes.external += 1;
store.indexes.external
};
let address = self.derive_address(&self.xpub, [0, pointer])?.to_string();
Ok(AddressPointer {
address,
pointer,
})
}
pub fn get_asset_icons(&self) -> Result<Option<serde_json::Value>, Error> {
self.store.read()?.read_asset_icons()
}
pub fn get_asset_registry(&self) -> Result<Option<serde_json::Value>, Error> {
self.store.read()?.read_asset_registry()
}
}
fn address_params(net: ElementsNetwork) -> &'static elements::AddressParams {
match net {
ElementsNetwork::Liquid => &elements::AddressParams::LIQUID,
ElementsNetwork::ElementsRegtest => &elements::AddressParams::ELEMENTS,
}
}
fn random32() -> Vec<u8> {
rand::thread_rng().gen::<[u8; 32]>().to_vec()
}
#[cfg(test)]
mod test {
use crate::interface::p2shwpkh_script_sig;
use bitcoin::consensus::deserialize;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{All, Message, Secp256k1, SecretKey};
use bitcoin::util::bip143::SighashComponents;
use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey};
use bitcoin::util::key::PrivateKey;
use bitcoin::util::key::PublicKey;
use bitcoin::Script;
use bitcoin::{Address, Network, Transaction};
use std::str::FromStr;
fn p2pkh_hex(pk: &str) -> (PublicKey, Script) {
let pk = hex::decode(pk).unwrap();