-
Notifications
You must be signed in to change notification settings - Fork 25
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 mintscript::SigntureOnlyChecker
and use it in the wallet
#1791
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f4debe3
Initial SignatureOnly checker impl
azarovh c8d87cd
Move ptx to common
azarovh 4b5a252
Use tx_verifier::verify_signature in wallet
azarovh 5fd87fd
Make InputInfo utxo source optional
azarovh 7a90af9
Impl InputInfoProvider for signature only
azarovh 3875543
Add SignatureOnly translation tests
azarovh 8282283
Fix wallet test
azarovh a059999
Prevent BlockRewardTransactable to be used in sigonly
azarovh 711ab1f
Verify tx after signing in wallet
azarovh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
178 changes: 178 additions & 0 deletions
178
chainstate/tx-verifier/src/transaction_verifier/input_check/signature_only_check.rs
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,178 @@ | ||
// Copyright (c) 2024 RBB S.r.l | ||
// opensource@mintlayer.org | ||
// SPDX-License-Identifier: MIT | ||
// Licensed under the MIT License; | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::convert::Infallible; | ||
|
||
use common::chain::{ | ||
partially_signed_transaction::PartiallySignedTransaction, | ||
signature::{inputsig::InputWitness, DestinationSigError, Transactable}, | ||
tokens::TokenId, | ||
ChainConfig, DelegationId, Destination, PoolId, SignedTransaction, TxInput, TxOutput, | ||
}; | ||
use mintscript::{ | ||
script::ScriptError, translate::InputInfoProvider, InputInfo, SignatureContext, TranslateInput, | ||
}; | ||
use utils::ensure; | ||
|
||
use super::{InputCheckError, InputCheckErrorPayload, PerInputData}; | ||
|
||
struct InputVerifyContextSignature<'a, T> { | ||
chain_config: &'a ChainConfig, | ||
tx: &'a T, | ||
outpoint_destination: &'a Destination, | ||
inputs_utxos: &'a [Option<&'a TxOutput>], | ||
input_num: usize, | ||
input_data: PerInputData<'a>, | ||
} | ||
|
||
impl<T: Transactable> SignatureContext for InputVerifyContextSignature<'_, T> { | ||
type Tx = T; | ||
|
||
fn chain_config(&self) -> &ChainConfig { | ||
self.chain_config | ||
} | ||
|
||
fn transaction(&self) -> &Self::Tx { | ||
self.tx | ||
} | ||
|
||
fn input_utxos(&self) -> &[Option<&TxOutput>] { | ||
self.inputs_utxos | ||
} | ||
|
||
fn input_num(&self) -> usize { | ||
self.input_num | ||
} | ||
} | ||
|
||
impl<T: Transactable> mintscript::translate::SignatureInfoProvider | ||
for InputVerifyContextSignature<'_, T> | ||
{ | ||
fn get_pool_decommission_destination( | ||
&self, | ||
_pool_id: &PoolId, | ||
) -> Result<Option<Destination>, pos_accounting::Error> { | ||
Ok(Some(self.outpoint_destination.clone())) | ||
} | ||
|
||
fn get_delegation_spend_destination( | ||
&self, | ||
_delegation_id: &DelegationId, | ||
) -> Result<Option<Destination>, pos_accounting::Error> { | ||
Ok(Some(self.outpoint_destination.clone())) | ||
} | ||
|
||
fn get_tokens_authority( | ||
&self, | ||
_token_id: &TokenId, | ||
) -> Result<Option<Destination>, tokens_accounting::Error> { | ||
Ok(Some(self.outpoint_destination.clone())) | ||
} | ||
|
||
fn get_orders_conclude_destination( | ||
&self, | ||
_order_id: &common::chain::OrderId, | ||
) -> Result<Option<Destination>, orders_accounting::Error> { | ||
Ok(Some(self.outpoint_destination.clone())) | ||
} | ||
} | ||
|
||
impl<T: Transactable> InputInfoProvider for InputVerifyContextSignature<'_, T> { | ||
fn input_info(&self) -> &InputInfo { | ||
self.input_data.input_info() | ||
} | ||
|
||
fn witness(&self) -> &InputWitness { | ||
self.input_data.witness() | ||
} | ||
} | ||
|
||
// Prevent BlockRewardTransactable from being used here | ||
pub trait SignatureOnlyVerifiable {} | ||
impl SignatureOnlyVerifiable for SignedTransaction {} | ||
impl SignatureOnlyVerifiable for PartiallySignedTransaction {} | ||
|
||
pub fn verify_tx_signature<T: Transactable + SignatureOnlyVerifiable>( | ||
chain_config: &ChainConfig, | ||
outpoint_destination: &Destination, | ||
tx: &T, | ||
inputs_utxos: &[Option<&TxOutput>], | ||
input_num: usize, | ||
) -> Result<(), InputCheckError> { | ||
let map_sig_err = |e: DestinationSigError| { | ||
InputCheckError::new( | ||
input_num, | ||
ScriptError::<DestinationSigError, Infallible, Infallible>::Signature(e), | ||
) | ||
}; | ||
|
||
let inputs = tx | ||
.inputs() | ||
.ok_or(DestinationSigError::SignatureVerificationWithoutInputs) | ||
.map_err(map_sig_err)?; | ||
let input = inputs | ||
.get(input_num) | ||
.ok_or(DestinationSigError::InvalidInputIndex( | ||
input_num, | ||
inputs.len(), | ||
)) | ||
.map_err(map_sig_err)?; | ||
|
||
ensure!( | ||
inputs.len() == inputs_utxos.len(), | ||
map_sig_err(DestinationSigError::InvalidUtxoCountVsInputs( | ||
inputs_utxos.len(), | ||
inputs.len() | ||
)) | ||
); | ||
|
||
let input_info = match input { | ||
TxInput::Utxo(outpoint) => { | ||
let utxo = inputs_utxos[input_num] | ||
.ok_or(InputCheckError::new( | ||
input_num, | ||
InputCheckErrorPayload::MissingUtxo(outpoint.clone()), | ||
))? | ||
.clone(); | ||
InputInfo::Utxo { | ||
outpoint, | ||
utxo, | ||
utxo_source: None, | ||
} | ||
} | ||
TxInput::Account(outpoint) => InputInfo::Account { outpoint }, | ||
TxInput::AccountCommand(_, command) => InputInfo::AccountCommand { command }, | ||
}; | ||
let input_witness = tx.signatures()[input_num] | ||
.clone() | ||
.ok_or(DestinationSigError::SignatureNotFound) | ||
.map_err(map_sig_err)?; | ||
|
||
let input_data = PerInputData::new(input_info, input_witness); | ||
let context = InputVerifyContextSignature { | ||
chain_config, | ||
tx, | ||
outpoint_destination, | ||
inputs_utxos, | ||
input_num, | ||
input_data, | ||
}; | ||
let script = mintscript::translate::SignatureOnlyTx::translate_input(&context) | ||
.map_err(|e| InputCheckError::new(input_num, e))?; | ||
let mut checker = mintscript::ScriptChecker::signature_only(context); | ||
script.verify(&mut checker).map_err(|e| InputCheckError::new(input_num, e))?; | ||
|
||
Ok(()) | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very confusing name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just a name in line with others like
InputVerifyContextTimelock