Add 'build_output' endpoint to owner api (#641)

* add 'build_output' endpoint to owner api

* merge fix
This commit is contained in:
scilio
2022-02-18 10:06:04 +00:00
committed by GitHub
parent c424a0ed10
commit 2924a0ddb2
6 changed files with 245 additions and 14 deletions
+45 -5
View File
@@ -17,7 +17,8 @@
use uuid::Uuid;
use crate::grin_core::core::hash::Hashed;
use crate::grin_core::core::Transaction;
use crate::grin_core::core::{Output, OutputFeatures, Transaction};
use crate::grin_core::libtx::proof;
use crate::grin_keychain::ViewKey;
use crate::grin_util::secp::key::SecretKey;
use crate::grin_util::Mutex;
@@ -25,14 +26,14 @@ use crate::grin_util::ToHex;
use crate::util::{OnionV3Address, OnionV3AddressError};
use crate::api_impl::owner_updater::StatusMessage;
use crate::grin_keychain::{Identifier, Keychain};
use crate::grin_keychain::{BlindingFactor, Identifier, Keychain, SwitchCommitmentType};
use crate::internal::{keys, scan, selection, tx, updater};
use crate::slate::{PaymentInfo, Slate, SlateState};
use crate::types::{AcctPathMapping, NodeClient, TxLogEntry, WalletBackend, WalletInfo};
use crate::{
address, wallet_lock, InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult, OutputCommitMapping,
PaymentProof, ScannedBlockInfo, Slatepack, SlatepackAddress, Slatepacker, SlatepackerArgs,
TxLogEntryType, ViewWallet, WalletInitStatus, WalletInst, WalletLCProvider,
address, wallet_lock, BuiltOutput, InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult,
OutputCommitMapping, PaymentProof, ScannedBlockInfo, Slatepack, SlatepackAddress, Slatepacker,
SlatepackerArgs, TxLogEntryType, ViewWallet, WalletInitStatus, WalletInst, WalletLCProvider,
};
use crate::{Error, ErrorKind};
use ed25519_dalek::PublicKey as DalekPublicKey;
@@ -1376,3 +1377,42 @@ where
}
Ok(true)
}
/// Builds an output for the wallet's next available key
pub fn build_output<'a, T: ?Sized, C, K>(
w: &mut T,
keychain_mask: Option<&SecretKey>,
features: OutputFeatures,
amount: u64,
) -> Result<BuiltOutput, Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let k = w.keychain(keychain_mask)?;
let key_id = keys::next_available_key(&mut *w, keychain_mask)?;
let blind = k.derive_key(amount, &key_id, SwitchCommitmentType::Regular)?;
let commit = k.secp().commit(amount, blind.clone())?;
let proof_builder = proof::ProofBuilder::new(&k);
let proof = proof::create(
&k,
&proof_builder,
amount,
&key_id,
SwitchCommitmentType::Regular,
commit,
None,
)?;
let output = Output::new(features, commit, proof);
Ok(BuiltOutput {
blind: BlindingFactor::from_secret_key(blind),
key_id: key_id,
output: output,
})
}
+22 -1
View File
@@ -14,8 +14,9 @@
//! Types specific to the wallet api, mostly argument serialization
use crate::grin_core::core::Output;
use crate::grin_core::libtx::secp_ser;
use crate::grin_keychain::Identifier;
use crate::grin_keychain::{BlindingFactor, Identifier};
use crate::grin_util::secp::pedersen;
use crate::slate_versions::ser as dalek_ser;
use crate::slate_versions::SlateVersion;
@@ -24,6 +25,11 @@ use crate::SlatepackAddress;
use ed25519_dalek::Signature as DalekSignature;
/// Type for storing amounts (in nanogrins).
/// Serializes as a string but can deserialize from a string or u64.
#[derive(Serialize, Deserialize)]
pub struct Amount(#[serde(with = "secp_ser::string_or_u64")] pub u64);
/// V2 Init / Send TX API Args
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InitTxArgs {
@@ -216,3 +222,18 @@ pub struct PaymentProof {
#[serde(with = "dalek_ser::dalek_sig_serde")]
pub sender_sig: DalekSignature,
}
/// Build output result
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BuiltOutput {
/// Blinding Factor
#[serde(
serialize_with = "secp_ser::as_hex",
deserialize_with = "secp_ser::blind_from_hex"
)]
pub blind: BlindingFactor,
/// Key Identifier
pub key_id: Identifier,
/// Output
pub output: Output,
}
+2 -2
View File
@@ -67,8 +67,8 @@ pub use crate::slatepack::{
};
pub use api_impl::owner_updater::StatusMessage;
pub use api_impl::types::{
BlockFees, InitTxArgs, InitTxSendArgs, IssueInvoiceTxArgs, NodeHeightResult,
OutputCommitMapping, PaymentProof, VersionInfo,
Amount, BlockFees, BuiltOutput, InitTxArgs, InitTxSendArgs, IssueInvoiceTxArgs,
NodeHeightResult, OutputCommitMapping, PaymentProof, VersionInfo,
};
pub use internal::scan::scan;
pub use slate_versions::ser as dalek_ser;