-
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.
feat(btc): Add
tw_bitcoin_sign_psbt
and tw_bitcoin_plan_psbt
- Loading branch information
1 parent
a4385a6
commit c7d8407
Showing
5 changed files
with
82 additions
and
30 deletions.
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
pub mod psbt; |
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,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
#![allow(clippy::missing_safety_doc)] | ||
|
||
use tw_coin_registry::coin_type::CoinType; | ||
use tw_coin_registry::dispatcher::{coin_dispatcher, utxo_dispatcher}; | ||
use tw_memory::ffi::tw_data::TWData; | ||
use tw_memory::ffi::RawPtrTrait; | ||
use tw_misc::try_or_else; | ||
|
||
/// Signs a PSBT (Partially Signed Bitcoin Transaction) specified by the signing input and coin type. | ||
/// | ||
/// \param input The serialized data of a `Proto.PsbtSigningInput` protobuf message. | ||
/// \param coin The given coin type to sign the PSBT for. | ||
/// \return The serialized data of a `Proto.PsbtSigningOutput` protobuf message. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn tw_bitcoin_sign_psbt(coin: u32, input: *const TWData) -> *mut TWData { | ||
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut); | ||
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut); | ||
let utxo_dispatcher = try_or_else!(utxo_dispatcher(coin), std::ptr::null_mut); | ||
let (coin_context, _) = try_or_else!(coin_dispatcher(coin), std::ptr::null_mut); | ||
|
||
utxo_dispatcher | ||
.sign_psbt(&coin_context, input_data.as_slice()) | ||
.map(|data| TWData::from(data).into_ptr()) | ||
.unwrap_or_else(|_| std::ptr::null_mut()) | ||
} | ||
|
||
/// Plans a PSBT (Partially Signed Bitcoin Transaction). | ||
/// Can be used to get the transaction detailed decoded from PSBT. | ||
/// | ||
/// \param input The serialized data of a `Proto.PsbtSigningInput` protobuf message. | ||
/// \param coin The given coin type to sign the PSBT for. | ||
/// \return The serialized data of a `Proto.TransactionPlan` protobuf message. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn tw_bitcoin_plan_psbt(coin: u32, input: *const TWData) -> *mut TWData { | ||
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut); | ||
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut); | ||
let utxo_dispatcher = try_or_else!(utxo_dispatcher(coin), std::ptr::null_mut); | ||
let (coin_context, _) = try_or_else!(coin_dispatcher(coin), std::ptr::null_mut); | ||
|
||
utxo_dispatcher | ||
.plan_psbt(&coin_context, input_data.as_slice()) | ||
.map(|data| TWData::from(data).into_ptr()) | ||
.unwrap_or_else(|_| std::ptr::null_mut()) | ||
} |