Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Wallet::sign_tx_input; #689

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions bindings/ergo-lib-wasm/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,27 @@ pub struct Transaction(chain::transaction::Transaction);

#[wasm_bindgen]
impl Transaction {
/// Create new transaction
#[wasm_bindgen(constructor)]
pub fn new(
inputs: &Inputs,
data_inputs: &DataInputs,
outputs: &ErgoBoxCandidates,
) -> Result<Transaction, JsValue> {
let inputs: Vec<chain::transaction::Input> = inputs.into();
let data_inputs: Vec<chain::transaction::DataInput> = data_inputs.into();
let outputs: Vec<ergo_lib::ergotree_ir::chain::ergo_box::ErgoBoxCandidate> =
outputs.clone().into();
Ok(Transaction(
chain::transaction::Transaction::new(
TxIoVec::try_from(inputs).map_err(to_js)?,
TxIoVec::try_from(data_inputs).map_err(to_js)?.into(),
TxIoVec::try_from(outputs).map_err(to_js)?,
)
.map_err(to_js)?,
))
}

/// Create Transaction from UnsignedTransaction and an array of proofs in the same order as
/// UnsignedTransaction.inputs with empty proof indicated with empty byte array
pub fn from_unsigned_tx(
Expand Down
26 changes: 26 additions & 0 deletions bindings/ergo-lib-wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod ext_secret_key;
pub mod mnemonic;

use crate::address::Address;
use crate::input::Input;
use crate::transaction::TransactionHintsBag;
use crate::{
box_coll::ErgoBoxes,
Expand Down Expand Up @@ -192,4 +193,29 @@ impl Wallet {
))
}
}

/// Sign a given tx input
#[wasm_bindgen]
pub fn sign_tx_input(
&self,
input_idx: usize,
state_context: &ErgoStateContext,
tx: &UnsignedTransaction,
boxes_to_spend: &ErgoBoxes,
data_boxes: &ErgoBoxes,
) -> Result<Input, JsValue> {
let boxes_to_spend = boxes_to_spend.clone().into();
let data_boxes = data_boxes.clone().into();
let tx_context = ergo_lib::wallet::signing::TransactionContext::new(
tx.0.clone(),
boxes_to_spend,
data_boxes,
)
.map_err(to_js)?;
let state_context_inner = state_context.clone().into();
self.0
.sign_tx_input(input_idx, tx_context, &state_context_inner, None)
.map_err(to_js)
.map(Input::from)
}
}
38 changes: 24 additions & 14 deletions ergo-lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use thiserror::Error;
use crate::chain::ergo_state_context::ErgoStateContext;
use crate::chain::transaction::reduced::ReducedTransaction;
use crate::chain::transaction::unsigned::UnsignedTransaction;
use crate::chain::transaction::Input;
use crate::chain::transaction::Transaction;
use crate::ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
use crate::wallet::mnemonic::Mnemonic;
Expand All @@ -36,6 +37,7 @@ use self::ext_secret_key::ExtSecretKey;
use self::ext_secret_key::ExtSecretKeyError;
use self::signing::sign_message;
use self::signing::sign_reduced_transaction;
use self::signing::sign_tx_input;
use self::signing::TransactionContext;

/// Wallet
Expand All @@ -48,10 +50,10 @@ pub struct Wallet {
#[derive(Error, Debug)]
pub enum WalletError {
#[error("Transaction signing error: {0}")]
TxSigningError(TxSigningError),
TxSigningError(#[from] TxSigningError),

#[error("Prover error: {0}")]
ProverError(ProverError),
ProverError(#[from] ProverError),

#[error("ExtSecretKeyError: {0}")]
ExtSecretKeyError(#[from] ExtSecretKeyError),
Expand All @@ -60,18 +62,6 @@ pub enum WalletError {
SecretKeyParsingError,
}

impl From<TxSigningError> for WalletError {
fn from(e: TxSigningError) -> Self {
WalletError::TxSigningError(e)
}
}

impl From<ProverError> for WalletError {
fn from(e: ProverError) -> Self {
WalletError::ProverError(e)
}
}

impl Wallet {
/// Create wallet instance loading secret key from mnemonic
/// Returns None if a DlogSecretKey cannot be parsed from the provided phrase
Expand Down Expand Up @@ -163,4 +153,24 @@ impl Wallet {
) -> Result<Vec<u8>, WalletError> {
sign_message(self.prover.as_ref(), sigma_tree, msg).map_err(WalletError::from)
}

/// Signs a transaction input
pub fn sign_tx_input(
&self,
input_idx: usize,
tx_context: TransactionContext<UnsignedTransaction>,
state_context: &ErgoStateContext,
tx_hints: Option<&TransactionHintsBag>,
) -> Result<Input, WalletError> {
let tx = tx_context.spending_tx.clone();
let message_to_sign = tx.bytes_to_sign().map_err(TxSigningError::from)?;
Ok(sign_tx_input(
self.prover.as_ref(),
&tx_context,
state_context,
tx_hints,
input_idx,
message_to_sign.as_slice(),
)?)
}
}
62 changes: 43 additions & 19 deletions ergo-lib/src/wallet/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,15 @@ pub fn sign_transaction(
) -> Result<Transaction, TxSigningError> {
let tx = tx_context.spending_tx.clone();
let message_to_sign = tx.bytes_to_sign()?;
let signed_inputs = tx.inputs.enumerated().try_mapped(|(idx, input)| {
let input_box = tx_context
.get_input_box(&input.box_id)
.ok_or(TxSigningError::InputBoxNotFound(idx))?;
let ctx = Rc::new(make_context(state_context, &tx_context, idx)?);
let mut hints_bag = HintsBag::empty();
if let Some(bag) = tx_hints {
hints_bag = bag.all_hints_for_input(idx);
}
prover
.prove(
&input_box.ergo_tree,
&Env::empty(),
ctx,
message_to_sign.as_slice(),
&hints_bag,
)
.map(|proof| Input::new(input.box_id, proof.into()))
.map_err(|e| TxSigningError::ProverError(e, idx))
let signed_inputs = tx.inputs.enumerated().try_mapped(|(idx, _)| {
sign_tx_input(
prover,
&tx_context,
state_context,
tx_hints,
idx,
message_to_sign.as_slice(),
)
})?;
Ok(Transaction::new(
signed_inputs,
Expand Down Expand Up @@ -240,6 +230,40 @@ pub fn sign_message(
.map(Vec::from)
}

/// Sign a transaction input
pub fn sign_tx_input(
prover: &dyn Prover,
tx_context: &TransactionContext<UnsignedTransaction>,
state_context: &ErgoStateContext,
tx_hints: Option<&TransactionHintsBag>,
input_idx: usize,
message_to_sign: &[u8],
) -> Result<Input, TxSigningError> {
let unsigned_input = tx_context
.spending_tx
.inputs
.get(input_idx)
.ok_or(TxSigningError::InputBoxNotFound(input_idx))?;
let input_box = tx_context
.get_input_box(&unsigned_input.box_id)
.ok_or(TxSigningError::InputBoxNotFound(input_idx))?;
let ctx = Rc::new(make_context(state_context, tx_context, input_idx)?);
let mut hints_bag = HintsBag::empty();
if let Some(bag) = tx_hints {
hints_bag = bag.all_hints_for_input(input_idx);
}
prover
.prove(
&input_box.ergo_tree,
&Env::empty(),
ctx,
message_to_sign,
&hints_bag,
)
.map(|proof| Input::new(unsigned_input.box_id, proof.into()))
.map_err(|e| TxSigningError::ProverError(e, input_idx))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
Expand Down