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
use super::*;
use frame_support::{pallet_prelude::*, sp_io::hashing::blake2_256};
use frame_system::offchain::{SignedPayload, SigningTypes};
use sp_core::crypto::KeyTypeId;
use sp_runtime::sp_std::vec::Vec;

pub type Description<T> = BoundedVec<u8, <T as Config>::VaultDescriptionMaxLen>;
pub type PSBT<T> = BoundedVec<u8, <T as Config>::PSBTMaxLen>;
//pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
/* --- Constants section --- */
//pub const BDK_SERVICES_URL: &[u8] = b"https://bdk.hashed.systems";
pub const UNSIGNED_TXS_PRIORITY: u64 = 100;
pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"bdks");

pub const LOCK_BLOCK_EXPIRATION: u32 = 5; // in block number
pub const LOCK_TIMEOUT_EXPIRATION: u64 = 10000; // in milli-seconds

/* --- Crypto module section--- */
pub mod crypto {
  use super::KEY_TYPE;
  use sp_core::sr25519::Signature as Sr25519Signature;
  use sp_runtime::{
    app_crypto::{app_crypto, sr25519},
    traits::Verify,
    MultiSignature, MultiSigner,
  };
  app_crypto!(sr25519, KEY_TYPE);

  pub struct TestAuthId;

  // implemented for runtime
  impl frame_system::offchain::AppCrypto<MultiSigner, MultiSignature> for TestAuthId {
    type RuntimeAppPublic = Public;
    type GenericSignature = sp_core::sr25519::Signature;
    type GenericPublic = sp_core::sr25519::Public;
  }

  // implemented for mock runtime in test
  impl frame_system::offchain::AppCrypto<<Sr25519Signature as Verify>::Signer, Sr25519Signature>
    for TestAuthId
  {
    type RuntimeAppPublic = Public;
    type GenericSignature = sp_core::sr25519::Signature;
    type GenericPublic = sp_core::sr25519::Public;
  }
}

// Struct for holding Vaults information.
#[derive(Encode, Decode, RuntimeDebugNoBound, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct Vault<T: Config> {
  pub owner: T::AccountId,
  pub threshold: u32,
  pub description: BoundedVec<u8, T::VaultDescriptionMaxLen>,
  pub cosigners: BoundedVec<T::AccountId, T::MaxCosignersPerVault>,
  pub descriptors: Descriptors<T::OutputDescriptorMaxLen>,
  pub offchain_status: BDKStatus<T::VaultDescriptionMaxLen>,
}

impl<T: Config> Vault<T> {
  pub fn is_vault_member(&self, account: &T::AccountId) -> bool {
    Self::get_vault_members(self).contains(account)
  }

  pub fn get_vault_members(&self) -> Vec<T::AccountId> {
    let mut members = [self.cosigners.clone().as_slice(), &[self.owner.clone()]].concat();
    members.sort();
    members.dedup();
    members
  }

  pub fn signers_are_unique(&self) -> bool {
    let mut filtered_signers = self.cosigners.clone().to_vec();
    filtered_signers.sort();
    filtered_signers.dedup();
    self.cosigners.len() == filtered_signers.len()
  }

  /// A vault must have valid descriptors in order to produce psbt's
  pub fn is_valid(&self) -> bool {
    self.offchain_status.eq(&BDKStatus::Valid) && self.descriptors.are_not_empty()
  }
}

impl<T: Config> PartialEq for Vault<T> {
  fn eq(&self, other: &Self) -> bool {
    self.using_encoded(blake2_256) == other.using_encoded(blake2_256)
  }
}

impl<T: Config> Clone for Vault<T> {
  fn clone(&self) -> Self {
    Vault {
      owner: self.owner.clone(),
      threshold: self.threshold.clone(),
      cosigners: self.cosigners.clone(),
      description: self.description.clone(),
      descriptors: self.descriptors.clone(),
      offchain_status: self.offchain_status.clone(),
    }
  }
}

#[derive(Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct ProposalSignatures<T: Config> {
  pub signer: T::AccountId,
  pub signature: BoundedVec<u8, T::PSBTMaxLen>,
}

impl<T: Config> Clone for ProposalSignatures<T> {
  fn clone(&self) -> Self {
    Self { signer: self.signer.clone(), signature: self.signature.clone() }
  }
}
// Struct for holding Proposal information.
#[derive(Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct Proposal<T: Config> {
  pub proposer: T::AccountId,
  pub vault_id: [u8; 32],
  pub status: ProposalStatus,
  pub offchain_status: BDKStatus<T::VaultDescriptionMaxLen>,
  pub to_address: BoundedVec<u8, T::XPubLen>,
  pub amount: u64,
  pub fee_sat_per_vb: u32,
  pub description: BoundedVec<u8, T::VaultDescriptionMaxLen>,
  pub tx_id: Option<BoundedVec<u8, T::VaultDescriptionMaxLen>>,
  pub psbt: BoundedVec<u8, T::PSBTMaxLen>,
  pub signed_psbts: BoundedVec<ProposalSignatures<T>, T::MaxCosignersPerVault>,
}

impl<T: Config> Proposal<T> {
  pub fn can_be_finalized(&self) -> bool {
    self.status.is_ready_to_finalize() && self.offchain_status.eq(&BDKStatus::Valid)
  }

  // pub fn can_be_broadcasted(&self) -> bool {
  // 	self.status.eq(&ProposalStatus::ReadyToBroadcast) && self.offchain_status.eq(&BDKStatus::Valid)
  // }
}

impl<T: Config> Clone for Proposal<T> {
  fn clone(&self) -> Self {
    Self {
      proposer: self.proposer.clone(),
      vault_id: self.vault_id.clone(),
      status: self.status.clone(),
      offchain_status: self.offchain_status.clone(),
      to_address: self.to_address.clone(),
      amount: self.amount.clone(),
      fee_sat_per_vb: self.fee_sat_per_vb.clone(),
      description: self.description.clone(),
      tx_id: self.tx_id.clone(),
      psbt: self.psbt.clone(),
      signed_psbts: self.signed_psbts.clone(),
    }
  }
}

// Struct for holding Proof of reserve information.
#[derive(CloneNoBound, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct ProofOfReserve<T: Config> {
  pub status: ProposalStatus,
  pub message: Description<T>,
  pub psbt: PSBT<T>,
  pub signed_psbts: BoundedVec<ProposalSignatures<T>, T::MaxCosignersPerVault>,
}

impl<T: Config> ProofOfReserve<T> {
  pub fn can_be_finalized(&self) -> bool {
    self.status.is_ready_to_finalize()
  }

  pub fn is_already_broadcasted(&self) -> bool {
    self.status.is_broadcasted()
  }
}

#[derive(
  Encode, Decode, Default, Eq, PartialEq, CloneNoBound, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen,
)]
#[scale_info(skip_type_params(MaxLen))]
#[codec(mel_bound())]
pub struct Descriptors<MaxLen: Get<u32>> {
  pub output_descriptor: BoundedVec<u8, MaxLen>,
  pub change_descriptor: Option<BoundedVec<u8, MaxLen>>,
}

impl<MaxLen: Get<u32>> Descriptors<MaxLen> {
  pub fn are_not_empty(&self) -> bool {
    !self.output_descriptor.is_empty() && self.change_descriptor.is_some()
  }
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[codec(mel_bound())]
pub struct VaultsPayload<Public> {
  pub vaults_payload: Vec<SingleVaultPayload>,
  pub public: Public,
}

#[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(MaxLen))]
#[codec(mel_bound())]
pub struct SingleVaultPayload {
  // Not successful, macros/generics issue
  // descriptors: Descriptors<u8>,
  pub vault_id: [u8; 32],
  pub output_descriptor: Vec<u8>,
  pub change_descriptor: Vec<u8>,
  pub status: OffchainStatus,
}

impl Clone for SingleVaultPayload {
  fn clone(&self) -> Self {
    Self {
      vault_id: self.vault_id.clone(),
      output_descriptor: self.output_descriptor.clone(),
      change_descriptor: self.change_descriptor.clone(),
      status: self.status.clone(),
    }
  }
}

impl<S: SigningTypes> SignedPayload<S> for VaultsPayload<S::Public> {
  fn public(&self) -> S::Public {
    self.public.clone()
  }
}

/// Struct for requesting a descriptor generation
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ProposalRequest<DescriptorMaxLen: Get<u32>, XPubLen: Get<u32>> {
  pub descriptors: Descriptors<DescriptorMaxLen>,
  pub to_address: BoundedVec<u8, XPubLen>,
  pub amount: u64,
  pub fee_sat_per_vb: u32,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[codec(mel_bound())]
pub struct ProposalsPayload<Public> {
  pub proposals_payload: Vec<SingleProposalPayload>,
  pub public: Public,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[codec(mel_bound())]
pub struct SingleProposalPayload {
  pub proposal_id: [u8; 32],
  pub psbt: Vec<u8>,
  pub status: OffchainStatus,
}

impl<S: SigningTypes> SignedPayload<S> for ProposalsPayload<S::Public> {
  fn public(&self) -> S::Public {
    self.public.clone()
  }
}

pub enum XpubStatus {
  Owned,
  Free,
  Taken,
}

#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum ProposalStatus {
  Pending,
  ReadyToFinalize(bool), //bool is the flag to broadcast automatically once finalized
  Finalized,
  //ReadyToBroadcast,
  Broadcasted,
}

impl ProposalStatus {
  pub fn is_ready_to_finalize(&self) -> bool {
    match *self {
      ProposalStatus::ReadyToFinalize(_) => true,
      _ => false,
    }
  }

  pub fn is_broadcasted(&self) -> bool {
    match *self {
      ProposalStatus::Broadcasted => true,
      _ => false,
    }
  }

  pub fn next_status(&self) -> Self {
    use ProposalStatus::*;
    match *self {
      Pending => ReadyToFinalize(false),
      ReadyToFinalize(false) => Finalized, // it will be finalized but the broadcast is still pending
      ReadyToFinalize(true) => Broadcasted, // the "true" flag value will finalize and broadcast it
      Finalized => ReadyToFinalize(true),  // this will broadcast the tx
      //ReadyToBroadcast => Broadcasted, // not used, but not discarded
      Broadcasted => Broadcasted,
    }
  }
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo)]
pub enum OffchainStatus {
  Pending,
  Valid,
  RecoverableError(Vec<u8>),
  IrrecoverableError(Vec<u8>),
}

//Default macro didnt work
impl Default for OffchainStatus {
  fn default() -> Self {
    OffchainStatus::Pending
  }
}

impl<MaxLen: Get<u32>> From<OffchainStatus> for BDKStatus<MaxLen> {
  fn from(status: OffchainStatus) -> Self {
    match status {
      OffchainStatus::Pending => BDKStatus::Pending,
      OffchainStatus::Valid => BDKStatus::Valid,
      OffchainStatus::RecoverableError(msj) => {
        BDKStatus::RecoverableError(BoundedVec::<u8, MaxLen>::try_from(msj).unwrap_or_default())
      },
      OffchainStatus::IrrecoverableError(msj) => {
        BDKStatus::IrrecoverableError(BoundedVec::<u8, MaxLen>::try_from(msj).unwrap_or_default())
      },
    }
  }
}

#[derive(Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxLen))]
#[codec(mel_bound())]
pub enum BDKStatus<MaxLen: Get<u32>> {
  Pending,
  Valid,
  RecoverableError(BoundedVec<u8, MaxLen>),
  IrrecoverableError(BoundedVec<u8, MaxLen>),
}
impl<MaxLen: Get<u32>> Default for BDKStatus<MaxLen> {
  fn default() -> Self {
    BDKStatus::Pending
  }
}
// Clone macro didnt work
impl<MaxLen: Get<u32>> Clone for BDKStatus<MaxLen> {
  fn clone(&self) -> Self {
    match self {
      Self::Pending => Self::Pending,
      Self::Valid => Self::Valid,
      Self::RecoverableError(arg0) => Self::RecoverableError(arg0.clone()),
      Self::IrrecoverableError(arg0) => Self::IrrecoverableError(arg0.clone()),
    }
  }
}

impl<MaxLen: Get<u32>> PartialEq for BDKStatus<MaxLen> {
  fn eq(&self, other: &Self) -> bool {
    match (self, other) {
      (Self::RecoverableError(_), Self::RecoverableError(_)) => true,
      (Self::IrrecoverableError(l0), Self::IrrecoverableError(r0)) => l0 == r0,
      _ => core::mem::discriminant(self) == core::mem::discriminant(other),
    }
  }

  fn ne(&self, other: &Self) -> bool {
    match (self, other) {
      (Self::RecoverableError(l0), Self::RecoverableError(r0)) => l0 == r0,
      (Self::IrrecoverableError(l0), Self::IrrecoverableError(r0)) => l0 == r0,
      _ => core::mem::discriminant(self) == core::mem::discriminant(other),
    }
  }
}