-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc9fec7
commit 57447ea
Showing
19 changed files
with
351 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
use itertools::Itertools; | ||
use std::cmp; | ||
use tw_keypair::schnorr; | ||
use tw_utxo::signature::BitcoinSchnorrSignature; | ||
|
||
pub struct CovenantCommittee; | ||
|
||
impl CovenantCommittee { | ||
/// Sort the keys in lexicographical order bytes. | ||
pub fn sort_public_keys(pks: &mut [schnorr::XOnlyPublicKey]) { | ||
pks.sort_by(Self::cmp_pk); | ||
} | ||
|
||
/// Sort the signatures by corresponding public keys in lexicographical order bytes. | ||
pub fn sort_signatures<'a, I>(sigs: I) -> Vec<BitcoinSchnorrSignature> | ||
where | ||
I: IntoIterator<Item = &'a (schnorr::XOnlyPublicKey, BitcoinSchnorrSignature)>, | ||
{ | ||
sigs.into_iter() | ||
// Sort the elements by public keys. | ||
.sorted_by(|x, y| Self::cmp_pk(&x.0, &y.0)) | ||
.map(|(_pk, sig)| sig.clone()) | ||
.collect() | ||
} | ||
|
||
fn cmp_pk(pk1: &schnorr::XOnlyPublicKey, pk2: &schnorr::XOnlyPublicKey) -> cmp::Ordering { | ||
pk1.as_slice().cmp(pk2.as_slice()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
use crate::babylon::covenant_committee::CovenantCommittee; | ||
use tw_keypair::schnorr; | ||
use tw_memory::Data; | ||
use tw_utxo::script::standard_script::claims; | ||
use tw_utxo::script::Script; | ||
use tw_utxo::signature::BitcoinSchnorrSignature; | ||
use tw_utxo::spending_data::{SchnorrSpendingDataConstructor, SpendingData}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct BabylonUnbondingPath { | ||
unbonding_script: Script, | ||
control_block: Data, | ||
/// Signatures signed by covenant committees. Sorted by covenant committees public keys. | ||
covenant_committee_signatures: Vec<BitcoinSchnorrSignature>, | ||
} | ||
|
||
impl BabylonUnbondingPath { | ||
pub fn new( | ||
unbonding_script: Script, | ||
control_block: Data, | ||
covenant_committee_signatures: &[(schnorr::XOnlyPublicKey, BitcoinSchnorrSignature)], | ||
) -> Self { | ||
let covenant_committee_signatures = | ||
CovenantCommittee::sort_signatures(covenant_committee_signatures); | ||
BabylonUnbondingPath { | ||
unbonding_script, | ||
control_block, | ||
covenant_committee_signatures, | ||
} | ||
} | ||
} | ||
|
||
impl SchnorrSpendingDataConstructor for BabylonUnbondingPath { | ||
fn get_spending_data(&self, sig: &BitcoinSchnorrSignature) -> SpendingData { | ||
// User's signature is always first. | ||
// Then, covenant committee signatures sorted by their public keys. | ||
// For more info, see [`babylon::conditions::new_unbonding_script`]. | ||
let unbonding_sigs: Vec<_> = std::iter::once(sig.clone()) | ||
.chain(self.covenant_committee_signatures.iter().cloned()) | ||
.collect(); | ||
|
||
SpendingData { | ||
script_sig: Script::default(), | ||
witness: claims::new_p2tr_script_path( | ||
&unbonding_sigs, | ||
self.unbonding_script.clone(), | ||
self.control_block.clone(), | ||
), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.