Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions pallets/bitcoin-vaults/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<T: Config> Pallet<T> {
}

pub fn do_save_psbt(signer: T::AccountId, proposal_id: [u8;32], signature_payload: BoundedVec<u8, T::PSBTMaxLen>) -> DispatchResult{
// validations: proposal exists, signer is member of vault, proposal is pending,
// validations: proposal exists, signer is member of vault, proposal is pending,
let vault_id = <Proposals<T>>::get(proposal_id).ok_or(Error::<T>::ProposalNotFound)?.vault_id;
let vault = <Vaults<T>>::get(vault_id).ok_or(Error::<T>::VaultNotFound)?;
ensure!(vault.is_vault_member(&signer), Error::<T>::SignerPermissionsNeeded);
Expand All @@ -121,6 +121,9 @@ impl<T: Config> Pallet<T> {
let signed_already = p.signed_psbts.iter().find(|&signature|{ signature.signer ==signer }).is_some();
ensure!(!signed_already, Error::<T>::AlreadySigned);
p.signed_psbts.try_push(signature).map_err(|_| Error::<T>::ExceedMaxCosignersPerVault)?;
if p.signed_psbts.len() as u32 == vault.threshold {
p.status.clone_from(&ProposalStatus::ReadyToFinalize(false));
}
}
Ok(())
})?;
Expand All @@ -136,9 +139,9 @@ impl<T: Config> Pallet<T> {
// can be called by any vault signer
ensure!(vault.is_vault_member(&signer), Error::<T>::SignerPermissionsNeeded);
// if its finalized then fire error "already finalized" or "already broadcasted"
ensure!(proposal.status.eq(&ProposalStatus::Pending) || proposal.status.eq(&ProposalStatus::Finalized),
ensure!(proposal.status.eq(&ProposalStatus::Pending) || proposal.status.eq(&ProposalStatus::Finalized),
Error::<T>::PendingProposalRequired );
// signs must be greater or equal than threshold
// signs must be greater or equal than threshold
ensure!(proposal.signed_psbts.len() as u32 >= vault.threshold, Error::<T>::NotEnoughSignatures);
// set status to: ready to be finalized
<Proposals<T>>::try_mutate::<_,(),DispatchError,_>(proposal_id, |proposal|{
Expand Down Expand Up @@ -174,7 +177,7 @@ impl<T: Config> Pallet<T> {
}

/*---- Offchain extrinsics ----*/

pub fn do_insert_descriptors(vault_id: [u8;32], descriptors: Descriptors<T::OutputDescriptorMaxLen>, status: BDKStatus<T::VaultDescriptionMaxLen>) -> DispatchResult {
<Vaults<T>>::try_mutate(vault_id, | v |{
match v {
Expand Down Expand Up @@ -225,8 +228,8 @@ impl<T: Config> Pallet<T> {
pub fn get_pending_vaults() -> Vec<[u8; 32]> {
<Vaults<T>>::iter()
.filter_map(|(entry, vault)| {
if vault.descriptors.output_descriptor.is_empty() &&
(vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
if vault.descriptors.output_descriptor.is_empty() &&
(vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
vault.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::RecoverableError(
BoundedVec::<u8,T::VaultDescriptionMaxLen>::default() )) ) {
Some(entry)
Expand All @@ -240,8 +243,8 @@ impl<T: Config> Pallet<T> {
pub fn get_pending_proposals() -> Vec<[u8; 32]>{
<Proposals<T>>::iter()
.filter_map(|(id, proposal)|{
if proposal.psbt.is_empty() &&
(proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
if proposal.psbt.is_empty() &&
(proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::Pending) ||
proposal.offchain_status.eq(&BDKStatus::<T::VaultDescriptionMaxLen>::RecoverableError(
BoundedVec::<u8,T::VaultDescriptionMaxLen>::default() )) ){
Some(id)
Expand Down Expand Up @@ -353,7 +356,7 @@ impl<T: Config> Pallet<T> {
};
body.push(("threshold".chars().collect::<Vec<char>>(), JsonValue::Number(threshold)));
let vault_signers = vault.cosigners.clone().to_vec();

//get the xpub for each cosigner
let xpubs = Self::get_accounts_xpubs(vault_signers).map_err(|_|
Self::build_offchain_err(false, "One of the cosigner xpubs wasn't found"))?;
Expand Down Expand Up @@ -428,7 +431,7 @@ impl<T: Config> Pallet<T> {
vault_payload.change_descriptor.clone_from(&descriptors.1);
},
Err(status) => {vault_payload.status.clone_from(&status)},
};
};
// Build offchain vaults struct and push it to a Vec
generated_vaults.push(vault_payload);
});
Expand Down Expand Up @@ -492,7 +495,7 @@ impl<T: Config> Pallet<T> {
Ok(response_body)
}

pub fn gen_proposals_payload_by_bulk(pending_proposals : Vec<[u8;32]>, api_endpoint: Vec<u8>,
pub fn gen_proposals_payload_by_bulk(pending_proposals : Vec<[u8;32]>, api_endpoint: Vec<u8>,
json_builder: &dyn Fn([u8;32])-> Result<Vec<u8>,OffchainStatus>
) -> Vec<SingleProposalPayload>{
let mut generated_proposals = Vec::<SingleProposalPayload>::new();
Expand Down Expand Up @@ -531,7 +534,7 @@ impl<T: Config> Pallet<T> {
let mapped_signatures: Vec<JsonValue> = proposal.signed_psbts.iter().map(|psbt|{
JsonValue::String(str::from_utf8(&psbt.signature).unwrap_or_default().chars().collect())
}).collect();

let broadcast= match proposal.status{
ProposalStatus::ReadyToFinalize(flag) => flag,
_ => false,
Expand All @@ -544,7 +547,7 @@ impl<T: Config> Pallet<T> {
// // Parse the JSON and print the resulting lite-json structure.
Ok(jsonSerialize::format(&json_object, 4) )
}

// pub fn bdk_gen_finalized_proposal(proposal_id: [u8;32])-> Result<Vec<u8>,OffchainStatus >{
// let raw_json = Self::gen_finalize_json_body(proposal_id)?;
// let request_body =
Expand All @@ -564,7 +567,7 @@ impl<T: Config> Pallet<T> {
// let mut finalized_proposals = Vec::<SingleProposalPayload>::new();
// finalized_proposals
// }

fn build_offchain_err(recoverable: bool, msj: &str )-> OffchainStatus{
let bounded_msj = msj.as_bytes().to_vec();
match recoverable{
Expand Down Expand Up @@ -638,4 +641,4 @@ impl<T: Config> BlockNumberProvider for Pallet<T> {
fn current_block_number() -> Self::BlockNumber {
<frame_system::Pallet<T>>::block_number()
}
}
}