Skip to content

Commit

Permalink
RGB anchor adds tweaking factor to PSBT
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 12, 2020
1 parent c497f59 commit 0e6bf27
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/bp/dbc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ pub use spk::{
ScriptEncodeData, ScriptEncodeMethod, SpkCommitment, SpkContainer,
};
pub use taproot::{TaprootCommitment, TaprootContainer};
pub use tx::{compute_lnpbp3_vout, TxCommitment, TxContainer, TxSupplement};
pub use tx::{TxCommitment, TxContainer, TxSupplement};
pub use txout::{TxoutCommitment, TxoutContainer};
pub use types::{Container, Proof};
54 changes: 23 additions & 31 deletions src/bp/dbc/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ pub struct TxContainer {
pub tweaking_factor: Option<Hmac<sha256::Hash>>,
}

pub fn compute_lnpbp3_vout(
tx: &Transaction,
supplement: &TxSupplement,
) -> usize {
compute_vout(supplement.fee, supplement.protocol_factor, tx)
}

fn compute_vout(fee: u64, entropy: u32, tx: &Transaction) -> usize {
let nouts = tx.output.len() as u16;
let vout = ((fee + (entropy as u64)) % (nouts as u64)) as u16;
vout as usize
}

fn get_mut_txout(fee: u64, entropy: u32, tx: &mut Transaction) -> &mut TxOut {
let tx2 = tx.clone();
&mut tx.output[compute_vout(fee, entropy, &tx2)]
}

#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[display(Debug)]
pub struct TxSupplement {
Expand All @@ -69,20 +51,28 @@ impl TxContainer {
source: ScriptEncodeData,
method: ScriptEncodeMethod,
) -> Self {
let txout = &tx.output[compute_vout(fee, protocol_factor, &tx)];
Self {
tx: tx.clone(),
let mut me = Self {
tx,
fee,
protocol_factor,
txout_container: TxoutContainer::construct(
protocol_tag,
txout.value,
0,
pubkey,
source,
method,
),
tweaking_factor: None,
}
};
me.txout_container.value = me.tx.output[me.vout()].value;
me
}

pub fn vout(&self) -> usize {
let nouts = self.tx.output.len() as u16;
let vout = ((self.fee + (self.protocol_factor as u64)) % (nouts as u64))
as u16;
vout as usize
}
}

Expand All @@ -95,19 +85,23 @@ impl Container for TxContainer {
supplement: &Self::Supplement,
host: &Self::Host,
) -> Result<Self, Error> {
let txout = &host.output
[compute_vout(supplement.fee, supplement.protocol_factor, host)];
Ok(Self {
let mut me = Self {
protocol_factor: supplement.protocol_factor,
fee: supplement.fee,
txout_container: TxoutContainer::reconstruct(
proof,
&supplement.tag,
txout,
&TxOut::default(),
)?,
tx: host.clone(),
tweaking_factor: None,
})
};
me.txout_container = TxoutContainer::reconstruct(
proof,
&supplement.tag,
&host.output[me.vout()],
)?;
Ok(me)
}

fn deconstruct(self) -> (Proof, Self::Supplement) {
Expand Down Expand Up @@ -149,14 +143,12 @@ where
msg: &MSG,
) -> Result<Self, Self::Error> {
let mut tx = container.tx.clone();
let fee = container.fee;
let entropy = container.protocol_factor;

let txout_commitment = TxoutCommitment::embed_commit(
&mut container.txout_container.clone(),
msg,
)?;
*get_mut_txout(fee, entropy, &mut tx) = txout_commitment.into_inner();
tx.output[container.vout()] = txout_commitment.into_inner();

container.tweaking_factor = container.txout_container.tweaking_factor;

Expand Down
8 changes: 7 additions & 1 deletion src/bp/strict_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use std::io;

use bitcoin::hashes::{hash160, sha256, sha256d, sha512};
use bitcoin::hashes::{hash160, hmac, sha256, sha256d, sha512, Hash};
use bitcoin::util::bip32::KeyApplication;
use bitcoin::util::psbt::PartiallySignedTransaction;
use bitcoin::{
Expand Down Expand Up @@ -51,6 +51,12 @@ impl strict_encoding::Strategy for sha512::Hash {
impl strict_encoding::Strategy for hash160::Hash {
type Strategy = strict_encoding::strategies::HashFixedBytes;
}
impl<T> strict_encoding::Strategy for hmac::Hmac<T>
where
T: Hash,
{
type Strategy = strict_encoding::strategies::HashFixedBytes;
}

impl strict_encoding::Strategy for OutPoint {
type Strategy = strict_encoding::strategies::BitcoinConsensus;
Expand Down
30 changes: 16 additions & 14 deletions src/rgb/stash/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use std::collections::{BTreeMap, HashMap};

use amplify::Wrapper;
use bitcoin::secp256k1;
use bitcoin::util::psbt::{
raw::ProprietaryKey, PartiallySignedTransaction as Psbt,
};
use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;
use bitcoin::util::uint::Uint256;
use bitcoin::{Transaction, Txid};
use bitcoin_hashes::{sha256, sha256t, Hash, HashEngine};
Expand All @@ -26,6 +24,7 @@ use crate::bp::dbc::{
self, Container, Proof, ScriptEncodeData, ScriptEncodeMethod, SpkContainer,
TxCommitment, TxContainer, TxSupplement, TxoutContainer,
};
use crate::bp::pasbt::ProprietaryKeyMap;
use crate::bp::resolvers::{Fee, FeeError};
use crate::client_side_validation::{
commit_strategy, CommitEncodeWithStrategy, ConsensusCommit,
Expand All @@ -35,7 +34,7 @@ use crate::lnpbp4::{MultimsgCommitment, TooManyMessagesError};
use crate::rgb::{ContractId, NodeId};

pub const PSBT_OUT_PUBKEY: u8 = 0x1;
//pub const PSBT_OUT_TWEAK: u8 = 0x2;
pub const PSBT_OUT_TWEAK: u8 = 0x2;

lazy_static! {
static ref LNPBP4_TAG: bitcoin::hashes::sha256::Hash =
Expand Down Expand Up @@ -91,12 +90,6 @@ impl Anchor {
let tx = &mut psbt.global.unsigned_tx;
let num_outs = tx.output.len() as u64;

let pubkey_key = ProprietaryKey {
prefix: b"RGB".to_vec(),
subtype: PSBT_OUT_PUBKEY,
key: vec![],
};

// Compute which transition commitments must go into which output and
// assemble them in per-output-packs of ContractId: Transition
// commitment type
Expand Down Expand Up @@ -127,10 +120,8 @@ impl Anchor {
let tx_out = &tx.output[vout];

let pubkey = psbt_out
.proprietary
.get(&pubkey_key)
.proprietary_key(b"RGB".to_vec(), PSBT_OUT_PUBKEY, vec![])
.ok_or(Error::NoRequiredPubkey(vout))?;
let pubkey = secp256k1::PublicKey::from_slice(pubkey)?;
// TODO: (new) Add support for Taproot parsing
let source = match psbt_out
.redeem_script
Expand Down Expand Up @@ -182,7 +173,18 @@ impl Anchor {
TxCommitment::embed_commit(&mut container, &mm_digest).unwrap();

*tx = commitment.into_inner().clone();
// TODO: Save tweaking factor from container into PSBT key
psbt.outputs
.get_mut(container.vout())
.map(|output| {
output.insert_proprietary_key(
b"RGB".to_vec(),
PSBT_OUT_TWEAK,
vec![],
&container.tweaking_factor.expect(
"Tweaking factor always present after commitment procedure"
)
)
});

multimsg.iter().for_each(|(id, _)| {
let contract_id = ContractId::from_inner(id.into_inner());
Expand Down

0 comments on commit 0e6bf27

Please sign in to comment.