Skip to content

Commit b8b8ffd

Browse files
authored
Merge pull request #209 from hashed-io/develop
Release 118
2 parents 3c44e0d + e1760a7 commit b8b8ffd

File tree

6 files changed

+225
-410
lines changed

6 files changed

+225
-410
lines changed

pallets/bitcoin-vaults/src/functions.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<T: Config> Pallet<T> {
107107
}
108108

109109
pub fn do_save_psbt(signer: T::AccountId, proposal_id: [u8;32], signature_payload: BoundedVec<u8, T::PSBTMaxLen>) -> DispatchResult{
110-
// validations: proposal exists, signer is member of vault, proposal is pending,
110+
// validations: proposal exists, signer is member of vault, proposal is pending,
111111
let vault_id = <Proposals<T>>::get(proposal_id).ok_or(Error::<T>::ProposalNotFound)?.vault_id;
112112
let vault = <Vaults<T>>::get(vault_id).ok_or(Error::<T>::VaultNotFound)?;
113113
ensure!(vault.is_vault_member(&signer), Error::<T>::SignerPermissionsNeeded);
@@ -121,6 +121,9 @@ impl<T: Config> Pallet<T> {
121121
let signed_already = p.signed_psbts.iter().find(|&signature|{ signature.signer ==signer }).is_some();
122122
ensure!(!signed_already, Error::<T>::AlreadySigned);
123123
p.signed_psbts.try_push(signature).map_err(|_| Error::<T>::ExceedMaxCosignersPerVault)?;
124+
if p.signed_psbts.len() as u32 == vault.threshold {
125+
p.status.clone_from(&ProposalStatus::ReadyToFinalize(false));
126+
}
124127
}
125128
Ok(())
126129
})?;
@@ -136,9 +139,9 @@ impl<T: Config> Pallet<T> {
136139
// can be called by any vault signer
137140
ensure!(vault.is_vault_member(&signer), Error::<T>::SignerPermissionsNeeded);
138141
// if its finalized then fire error "already finalized" or "already broadcasted"
139-
ensure!(proposal.status.eq(&ProposalStatus::Pending) || proposal.status.eq(&ProposalStatus::Finalized),
142+
ensure!(proposal.status.eq(&ProposalStatus::Pending) || proposal.status.eq(&ProposalStatus::Finalized),
140143
Error::<T>::PendingProposalRequired );
141-
// signs must be greater or equal than threshold
144+
// signs must be greater or equal than threshold
142145
ensure!(proposal.signed_psbts.len() as u32 >= vault.threshold, Error::<T>::NotEnoughSignatures);
143146
// set status to: ready to be finalized
144147
<Proposals<T>>::try_mutate::<_,(),DispatchError,_>(proposal_id, |proposal|{
@@ -174,7 +177,7 @@ impl<T: Config> Pallet<T> {
174177
}
175178

176179
/*---- Offchain extrinsics ----*/
177-
180+
178181
pub fn do_insert_descriptors(vault_id: [u8;32], descriptors: Descriptors<T::OutputDescriptorMaxLen>, status: BDKStatus<T::VaultDescriptionMaxLen>) -> DispatchResult {
179182
<Vaults<T>>::try_mutate(vault_id, | v |{
180183
match v {
@@ -225,8 +228,8 @@ impl<T: Config> Pallet<T> {
225228
pub fn get_pending_vaults() -> Vec<[u8; 32]> {
226229
<Vaults<T>>::iter()
227230
.filter_map(|(entry, vault)| {
228-
if vault.descriptors.output_descriptor.is_empty() &&
229-
(vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
231+
if vault.descriptors.output_descriptor.is_empty() &&
232+
(vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
230233
vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::RecoverableError(
231234
BoundedVec::<u8,T::VaultDescriptionMaxLen>::default() )) ) {
232235
Some(entry)
@@ -240,8 +243,8 @@ impl<T: Config> Pallet<T> {
240243
pub fn get_pending_proposals() -> Vec<[u8; 32]>{
241244
<Proposals<T>>::iter()
242245
.filter_map(|(id, proposal)|{
243-
if proposal.psbt.is_empty() &&
244-
(proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
246+
if proposal.psbt.is_empty() &&
247+
(proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
245248
proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::RecoverableError(
246249
BoundedVec::<u8,T::VaultDescriptionMaxLen>::default() )) ){
247250
Some(id)
@@ -353,7 +356,7 @@ impl<T: Config> Pallet<T> {
353356
};
354357
body.push(("threshold".chars().collect::<Vec<char>>(), JsonValue::Number(threshold)));
355358
let vault_signers = vault.cosigners.clone().to_vec();
356-
359+
357360
//get the xpub for each cosigner
358361
let xpubs = Self::get_accounts_xpubs(vault_signers).map_err(|_|
359362
Self::build_offchain_err(false, "One of the cosigner xpubs wasn't found"))?;
@@ -428,7 +431,7 @@ impl<T: Config> Pallet<T> {
428431
vault_payload.change_descriptor.clone_from(&descriptors.1);
429432
},
430433
Err(status) => {vault_payload.status.clone_from(&status)},
431-
};
434+
};
432435
// Build offchain vaults struct and push it to a Vec
433436
generated_vaults.push(vault_payload);
434437
});
@@ -492,7 +495,7 @@ impl<T: Config> Pallet<T> {
492495
Ok(response_body)
493496
}
494497

495-
pub fn gen_proposals_payload_by_bulk(pending_proposals : Vec<[u8;32]>, api_endpoint: Vec<u8>,
498+
pub fn gen_proposals_payload_by_bulk(pending_proposals : Vec<[u8;32]>, api_endpoint: Vec<u8>,
496499
json_builder: &dyn Fn([u8;32])-> Result<Vec<u8>,OffchainStatus>
497500
) -> Vec<SingleProposalPayload>{
498501
let mut generated_proposals = Vec::<SingleProposalPayload>::new();
@@ -531,7 +534,7 @@ impl<T: Config> Pallet<T> {
531534
let mapped_signatures: Vec<JsonValue> = proposal.signed_psbts.iter().map(|psbt|{
532535
JsonValue::String(str::from_utf8(&psbt.signature).unwrap_or_default().chars().collect())
533536
}).collect();
534-
537+
535538
let broadcast= match proposal.status{
536539
ProposalStatus::ReadyToFinalize(flag) => flag,
537540
_ => false,
@@ -544,7 +547,7 @@ impl<T: Config> Pallet<T> {
544547
// // Parse the JSON and print the resulting lite-json structure.
545548
Ok(jsonSerialize::format(&json_object, 4) )
546549
}
547-
550+
548551
// pub fn bdk_gen_finalized_proposal(proposal_id: [u8;32])-> Result<Vec<u8>,OffchainStatus >{
549552
// let raw_json = Self::gen_finalize_json_body(proposal_id)?;
550553
// let request_body =
@@ -564,7 +567,7 @@ impl<T: Config> Pallet<T> {
564567
// let mut finalized_proposals = Vec::<SingleProposalPayload>::new();
565568
// finalized_proposals
566569
// }
567-
570+
568571
fn build_offchain_err(recoverable: bool, msj: &str )-> OffchainStatus{
569572
let bounded_msj = msj.as_bytes().to_vec();
570573
match recoverable{
@@ -638,4 +641,4 @@ impl<T: Config> BlockNumberProvider for Pallet<T> {
638641
fn current_block_number() -> Self::BlockNumber {
639642
<frame_system::Pallet<T>>::block_number()
640643
}
641-
}
644+
}

0 commit comments

Comments
 (0)