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

Add wallet command to compose transactions with inputs and outputs #1522

Merged
merged 8 commits into from
Feb 1, 2024
Merged
15 changes: 14 additions & 1 deletion chainstate/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use common::{
address::dehexify::to_dehexified_json,
chain::{
tokens::{RPCTokenInfo, TokenId},
ChainConfig, DelegationId, PoolId,
ChainConfig, DelegationId, PoolId, TxOutput, UtxoOutPoint,
},
primitives::{Amount, BlockHeight, Id},
};
Expand Down Expand Up @@ -63,6 +63,10 @@ trait ChainstateRpc {
max_count: usize,
) -> RpcResult<Vec<HexEncoded<Block>>>;

/// Returns the TxOutput for a specified UtxoOutPoint.
#[method(name = "get_utxo")]
async fn get_utxo(&self, outpoint: UtxoOutPoint) -> RpcResult<Option<TxOutput>>;

/// Submit a block to be included in the chain
#[method(name = "submit_block")]
async fn submit_block(&self, block_hex: HexEncoded<Block>) -> RpcResult<()>;
Expand Down Expand Up @@ -187,6 +191,15 @@ impl ChainstateRpcServer for super::ChainstateHandle {
Ok(blocks.into_iter().map(HexEncoded::new).collect())
}

async fn get_utxo(&self, outpoint: UtxoOutPoint) -> RpcResult<Option<TxOutput>> {
rpc::handle_result(
self.call_mut(move |this| {
this.utxo(&outpoint).map(|utxo| utxo.map(|utxo| utxo.take_output()))
})
.await,
)
}

async fn submit_block(&self, block: HexEncoded<Block>) -> RpcResult<()> {
let res = self
.call_mut(move |this| this.process_block(block.take(), BlockSource::Local))
Expand Down
82 changes: 76 additions & 6 deletions common/src/chain/tokens/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ use super::Destination;
use crate::primitives::Amount;
use serialization::{Decode, Encode};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum TokenTotalSupply {
#[codec(index = 0)]
Fixed(Amount), // fixed to a certain amount
Expand All @@ -28,7 +40,19 @@ pub enum TokenTotalSupply {
}

// Indicates whether a token an be frozen
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum IsTokenFreezable {
#[codec(index = 0)]
No,
Expand All @@ -37,7 +61,19 @@ pub enum IsTokenFreezable {
}

// Indicates whether a token an be unfrozen after being frozen
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum IsTokenUnfreezable {
#[codec(index = 0)]
No,
Expand All @@ -48,21 +84,55 @@ pub enum IsTokenUnfreezable {
// Indicates whether a token is frozen at the moment or not. If it is then no operations wish this token can be performed.
// Meaning transfers, burns, minting, unminting, supply locks etc. Frozen token can only be unfrozen
// is such an option was provided while freezing.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum IsTokenFrozen {
#[codec(index = 0)]
No(IsTokenFreezable),
#[codec(index = 1)]
Yes(IsTokenUnfreezable),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum TokenIssuance {
#[codec(index = 1)]
V1(TokenIssuanceV1),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub struct TokenIssuanceV1 {
pub token_ticker: Vec<u8>,
pub number_of_decimals: u8,
Expand Down
28 changes: 25 additions & 3 deletions common/src/chain/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,43 @@ impl TokenAuxiliaryData {
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub struct TokenTransfer {
pub token_id: TokenId,
pub amount: Amount,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub struct TokenIssuanceV0 {
pub token_ticker: Vec<u8>,
pub amount_to_issue: Amount,
pub number_of_decimals: u8,
pub metadata_uri: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub enum TokenData {
/// TokenTransfer data to another user. If it is a token, then the token data must also be transferred to the recipient.
#[codec(index = 1)]
Expand Down
19 changes: 15 additions & 4 deletions common/src/chain/tokens/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use crypto::key::PublicKey;
use serialization::{extras::non_empty_vec::DataOrNoVec, Decode, Encode};

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub enum NftIssuance {
#[codec(index = 0)]
V0(NftIssuanceV0),
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub struct NftIssuanceV0 {
pub metadata: Metadata,
// TODO: Implement after additional research payout, royalty and refund.
Expand All @@ -35,7 +35,18 @@ impl From<NftIssuanceV0> for NftIssuance {
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub struct TokenCreator {
pub public_key: PublicKey,
}
Expand All @@ -46,7 +57,7 @@ impl From<PublicKey> for TokenCreator {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub struct Metadata {
pub creator: Option<TokenCreator>,
pub name: Vec<u8>,
Expand Down
2 changes: 1 addition & 1 deletion common/src/chain/transaction/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Addressable for Destination {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub enum TxOutput {
#[codec(index = 0)]
Transfer(OutputValue, Destination),
Expand Down
2 changes: 1 addition & 1 deletion common/src/chain/transaction/output/output_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
primitives::Amount,
};

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, serde::Serialize, serde::Deserialize)]
pub enum OutputValue {
Coin(Amount),
TokenV0(Box<TokenData>),
Expand Down
13 changes: 12 additions & 1 deletion common/src/chain/transaction/output/stakelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ use crate::primitives::{per_thousand::PerThousand, Amount};

use super::Destination;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub struct StakePoolData {
value: Amount,
staker: Destination,
Expand Down
13 changes: 12 additions & 1 deletion common/src/chain/transaction/output/timelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ use serialization::{Decode, Encode};

use crate::{chain::block::timestamp::BlockTimestamp, primitives::BlockHeight};

#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Encode, Decode, serde::Serialize)]
#[derive(
Debug,
Clone,
Ord,
PartialOrd,
Eq,
PartialEq,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum OutputTimeLock {
#[codec(index = 0)]
UntilHeight(BlockHeight),
Expand Down
26 changes: 24 additions & 2 deletions common/src/chain/transaction/utxo_outpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ use crate::chain::{transaction::Transaction, Block, GenBlock, Genesis};
use crate::primitives::Id;
use serialization::{Decode, Encode};

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Ord, PartialOrd, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Encode,
Decode,
Ord,
PartialOrd,
serde::Serialize,
serde::Deserialize,
)]
pub enum OutPointSourceId {
#[codec(index = 0)]
Transaction(Id<Transaction>),
Expand Down Expand Up @@ -58,7 +69,18 @@ impl OutPointSourceId {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Ord, PartialOrd, serde::Serialize)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Encode,
Decode,
Ord,
PartialOrd,
serde::Serialize,
serde::Deserialize,
)]
pub struct UtxoOutPoint {
id: OutPointSourceId,
index: u32,
Expand Down
4 changes: 3 additions & 1 deletion common/src/primitives/per_thousand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use super::Amount;

const DENOMINATOR: u16 = 1000;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Debug, serde::Serialize)]
#[derive(
PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Debug, serde::Serialize, serde::Deserialize,
)]
pub struct PerThousand(u16);

impl PerThousand {
Expand Down
9 changes: 9 additions & 0 deletions crypto/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ impl serde::Serialize for PublicKey {
}
}

impl<'d> serde::Deserialize<'d> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'d>,
{
HexEncoded::<PublicKey>::deserialize(deserializer).map(|hex| hex.take())
}
}

impl PrivateKey {
pub fn new_from_entropy(key_kind: KeyKind) -> (PrivateKey, PublicKey) {
Self::new_from_rng(&mut make_true_rng(), key_kind)
Expand Down
9 changes: 9 additions & 0 deletions crypto/src/vrf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ impl serde::Serialize for VRFPublicKey {
}
}

impl<'d> serde::Deserialize<'d> for VRFPublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'d>,
{
HexEncoded::<VRFPublicKey>::deserialize(deserializer).map(|hex| hex.take())
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Decode, Encode)]
pub(crate) enum VRFPublicKeyHolder {
#[codec(index = 0)]
Expand Down
2 changes: 1 addition & 1 deletion node-gui/src/backend/backend_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use tokio::{
task::JoinHandle,
};
use wallet::{
account::{transaction_list::TransactionList, Currency},
account::{currency_grouper::Currency, transaction_list::TransactionList},
DefaultWallet,
};
use wallet_controller::{
Expand Down
Loading
Loading