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
use std::collections::BTreeSet;
use chrono::prelude::*;
use wallet::hd::{Bip43, HardenedIndex, SegmentIndexes};
use super::{DerivationType, PublicNetwork, SpendingCondition};
use crate::model::{DescriptorClass, SigsReq};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum Requirement {
Allow,
Require,
Deny,
}
impl Default for Requirement {
fn default() -> Self { Requirement::Allow }
}
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct WalletTemplate {
pub default_derivation: DerivationType,
pub descriptor_class: DescriptorClass,
pub min_signer_count: u16,
pub max_signer_count: Option<u16>,
pub hardware_req: Requirement,
pub watch_only_req: Requirement,
pub conditions: BTreeSet<(u8, SpendingCondition)>,
pub network: PublicNetwork,
}
impl WalletTemplate {
pub fn singlesig(
descriptor_class: DescriptorClass,
network: PublicNetwork,
require_hardware: bool,
) -> WalletTemplate {
let format = descriptor_class.bip43(1);
let hardware_req = match require_hardware {
true => Requirement::Require,
false => Requirement::Deny,
};
let watch_only_req = match require_hardware {
true => Requirement::Deny,
false => Requirement::Require,
};
WalletTemplate {
default_derivation: format.into(),
descriptor_class,
min_signer_count: 1,
max_signer_count: Some(1),
hardware_req,
watch_only_req,
conditions: bset![(0, SpendingCondition::default())],
network,
}
}
pub fn hodling(
descriptor_class: DescriptorClass,
network: PublicNetwork,
sigs_required: u16,
hardware_req: Requirement,
watch_only_req: Requirement,
) -> WalletTemplate {
let now = Utc::now();
if sigs_required < 3 {
unreachable!("WalletTemplate::hodling must require at least 3 signers")
}
let conditions = bset![
(1, SpendingCondition::all()),
(
2,
SpendingCondition::anybody_after_date(now.with_year(now.year() + 5).unwrap())
)
];
WalletTemplate {
default_derivation: Bip43::multisig_descriptor().into(),
descriptor_class,
min_signer_count: sigs_required,
max_signer_count: None,
hardware_req,
watch_only_req,
conditions,
network,
}
}
pub fn multisig(
descriptor_class: DescriptorClass,
network: PublicNetwork,
sigs_required: Option<u16>,
hardware_req: Requirement,
watch_only_req: Requirement,
) -> WalletTemplate {
let now = Utc::now();
let conditions = match sigs_required {
None => bset![(0, SpendingCondition::default())],
Some(0) | Some(1) => unreachable!("WalletTemplate::multisig must expect > 1 signature"),
Some(2) => bset![
(1, SpendingCondition::all()),
(
2,
SpendingCondition::anybody_after_date(now.with_year(now.year() + 5).unwrap())
)
],
Some(3) => bset![
(1, SpendingCondition::at_least(2)),
(
2,
SpendingCondition::anybody_after_date(now.with_year(now.year() + 5).unwrap())
)
],
Some(count) => bset![
(1, SpendingCondition::at_least(count - 1)),
(
2,
SpendingCondition::after_date(
SigsReq::AtLeast(count / 2 + count % 2),
now.with_year(now.year() + 3).unwrap(),
)
),
(
3,
SpendingCondition::anybody_after_date(now.with_year(now.year() + 5).unwrap())
)
],
};
let default_derivation = match descriptor_class {
DescriptorClass::PreSegwit => Bip43::multisig_ordered_sh(),
DescriptorClass::SegwitV0 => Bip43::multisig_segwit0(),
DescriptorClass::NestedV0 => Bip43::multisig_nested0(),
DescriptorClass::TaprootC0 => Bip43::multisig_descriptor(),
}
.into();
WalletTemplate {
default_derivation,
descriptor_class,
min_signer_count: sigs_required.unwrap_or(2),
max_signer_count: None,
hardware_req,
watch_only_req,
conditions,
network,
}
}
pub fn bip43(&self) -> Bip43 {
self.default_derivation.bip43().unwrap_or(Bip43::Bip43 {
purpose: HardenedIndex::zero(),
})
}
}