Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 6a56461
Author: Maxim <59533214+biryukovmaxim@users.noreply.github.com>
Date:   Tue Jun 18 18:09:29 2024 +0300

    Implement  PSKT(Partially Signed Kaspa Transaction) (kaspanet#481)

    * initial support of pskt:
        supported roles: creator, constructor, updater, signer roles

    * add builder

    * handle combine errors

    * finalize

    * extractor

    * chore: typo

    * style: fmt

    * expose txid to global

    * chore: change version

    * feat: serde for optional bytes

    * feat: impl (de)serialization

    * style: fmt

    * add example, fixes

    * style: fmt

    * style: clippy

    * rollback unrelated changes

    * psbt -> pskt

    * refactor: avoid copy-paste by using recursion

    * docs: add description of roles

commit a797e1e
Author: George Bogodukhov <gvbgduh@gmail.com>
Date:   Mon Jun 17 15:50:16 2024 +1000

    Add support for IP only for --rpclisten-borsh/json (kaspanet#402) (kaspanet#439)

    * Add support for IP only for --rpclisten-borsh/json

    * Fix cehck complaints
  • Loading branch information
michaelsutton committed Jul 14, 2024
1 parent b917e21 commit 99eed7c
Show file tree
Hide file tree
Showing 19 changed files with 1,359 additions and 7 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"wallet/wasm",
"wallet/bip32",
"wallet/keys",
"wallet/pskt",
"consensus",
"consensus/core",
"consensus/client",
Expand Down Expand Up @@ -123,6 +124,7 @@ kaspa-utxoindex = { version = "0.14.2", path = "indexes/utxoindex" }
kaspa-wallet = { version = "0.14.2", path = "wallet/native" }
kaspa-wallet-cli-wasm = { version = "0.14.2", path = "wallet/wasm" }
kaspa-wallet-keys = { version = "0.14.2", path = "wallet/keys" }
kaspa-wallet-pskt = { version = "0.14.1", path = "wallet/pskt" }
kaspa-wallet-core = { version = "0.14.2", path = "wallet/core" }
kaspa-wallet-macros = { version = "0.14.2", path = "wallet/macros" }
kaspa-wasm = { version = "0.14.2", path = "wasm" }
Expand Down Expand Up @@ -162,6 +164,7 @@ ctrlc = "3.4.1"
crypto_box = { version = "0.9.1", features = ["chacha20"] }
dashmap = "5.5.3"
derivative = "2.2.0"
derive_builder = "0.20.0"
derive_more = "0.99.17"
dhat = "0.3.2"
dirs = "5.0.1"
Expand Down Expand Up @@ -228,6 +231,7 @@ serde = { version = "1.0.190", features = ["derive", "rc"] }
serde_bytes = "0.11.12"
serde_json = "1.0.107"
serde_repr = "0.1.18"
serde-value = "0.7.0"
serde-wasm-bindgen = "0.6.1"
sha1 = "0.10.6"
sha2 = "0.10.8"
Expand Down
3 changes: 2 additions & 1 deletion consensus/core/src/hashing/sighash_type.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

pub const SIG_HASH_ALL: SigHashType = SigHashType(0b00000001);
Expand All @@ -18,7 +19,7 @@ const ALLOWED_SIG_HASH_TYPES_VALUES: [u8; 6] = [
SIG_HASH_SINGLE.0 | SIG_HASH_ANY_ONE_CAN_PAY.0,
];

#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[wasm_bindgen]
pub struct SigHashType(pub(crate) u8);

Expand Down
4 changes: 2 additions & 2 deletions consensus/core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub type TransactionId = kaspa_hashes::Hash;
/// score of the block that accepts the tx, its public key script, and how
/// much it pays.
/// @category Consensus
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
#[wasm_bindgen(inspectable, js_name = TransactionUtxoEntry)]
pub struct UtxoEntry {
Expand All @@ -53,7 +53,7 @@ impl MemSizeEstimator for UtxoEntry {}
pub type TransactionIndexType = u32;

/// Represents a Kaspa transaction outpoint
#[derive(Eq, Hash, PartialEq, Debug, Copy, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[derive(Eq, Default, Hash, PartialEq, Debug, Copy, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionOutpoint {
#[serde(with = "serde_bytes_fixed_ref")]
Expand Down
2 changes: 1 addition & 1 deletion kaspad/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Setting to 0 prevents the preallocation and sets the maximum to {}, leading to 0
.long("ram-scale")
.require_equals(true)
.value_parser(clap::value_parser!(f64))
.help("Apply a scale factor to memory allocation bounds. Nodes with limited RAM (~4-8GB) should set this to ~0.3-0.5 respectively. Nodes with
.help("Apply a scale factor to memory allocation bounds. Nodes with limited RAM (~4-8GB) should set this to ~0.3-0.5 respectively. Nodes with
a large RAM (~64GB) can set this value to ~3.0-4.0 and gain superior performance especially for syncing peers faster"),
)
;
Expand Down
40 changes: 39 additions & 1 deletion rpc/wrpc/server/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ impl WrpcNetAddress {
};
format!("0.0.0.0:{port}").parse().unwrap()
}
WrpcNetAddress::Custom(address) => *address,
WrpcNetAddress::Custom(address) => {
if address.port_not_specified() {
let port = match encoding {
WrpcEncoding::Borsh => network_type.default_borsh_rpc_port(),
WrpcEncoding::SerdeJson => network_type.default_json_rpc_port(),
};
address.with_port(port)
} else {
*address
}
}
}
}
}
Expand Down Expand Up @@ -63,3 +73,31 @@ impl TryFrom<String> for WrpcNetAddress {
WrpcNetAddress::from_str(&s)
}
}

#[cfg(test)]
mod tests {
use super::*;
use kaspa_utils::networking::IpAddress;

#[test]
fn test_wrpc_net_address_from_str() {
// Addresses
let port: u16 = 8080;
let addr = format!("1.2.3.4:{port}").parse::<WrpcNetAddress>().unwrap();
let addr_without_port = "1.2.3.4".parse::<WrpcNetAddress>().unwrap();
let ip_addr = "1.2.3.4".parse::<IpAddress>().unwrap();
// Test
for schema in WrpcEncoding::iter() {
for network in NetworkType::iter() {
let expected_port = match schema {
WrpcEncoding::Borsh => Some(network.default_borsh_rpc_port()),
WrpcEncoding::SerdeJson => Some(network.default_json_rpc_port()),
};
// Custom address with port
assert_eq!(addr.to_address(&network, schema), ContextualNetAddress::new(ip_addr, Some(port)));
// Custom address without port
assert_eq!(addr_without_port.to_address(&network, schema), ContextualNetAddress::new(ip_addr, expected_port))
}
}
}
}
1 change: 1 addition & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub mod as_slice;
/// assert_eq!(test_struct, from_json);
/// ```
pub mod serde_bytes;
pub mod serde_bytes_optional;

/// # Examples
///
Expand Down
12 changes: 10 additions & 2 deletions utils/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const TS_IP_ADDRESS: &'static str = r#"
/// A bucket based on an ip's prefix bytes.
/// for ipv4 it consists of 6 leading zero bytes, and the first two octets,
/// for ipv6 it consists of the first 8 octets,
/// encoded into a big endian u64.
/// encoded into a big endian u64.
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
pub struct PrefixBucket(u64);

Expand Down Expand Up @@ -271,7 +271,7 @@ pub struct ContextualNetAddress {
}

impl ContextualNetAddress {
fn new(ip: IpAddress, port: Option<u16>) -> Self {
pub fn new(ip: IpAddress, port: Option<u16>) -> Self {
Self { ip, port }
}

Expand All @@ -286,6 +286,14 @@ impl ContextualNetAddress {
pub fn loopback() -> Self {
Self { ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).into(), port: None }
}

pub fn port_not_specified(&self) -> bool {
self.port.is_none()
}

pub fn with_port(&self, port: u16) -> Self {
Self { ip: self.ip, port: Some(port) }
}
}

impl From<NetAddress> for ContextualNetAddress {
Expand Down
111 changes: 111 additions & 0 deletions utils/src/serde_bytes_optional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
pub use de::Deserialize;
pub use ser::Serialize;

pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: ?Sized + Serialize,
S: serde::Serializer,
{
Serialize::serialize(bytes, serializer)
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de>,
D: serde::Deserializer<'de>,
{
Deserialize::deserialize(deserializer)
}

mod de {
use std::fmt::Display;

pub trait Deserialize<'de>: Sized {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>;
}

impl<'de, T: crate::serde_bytes::Deserialize<'de>> Deserialize<'de> for Option<T>
where
<T as TryFrom<&'de [u8]>>::Error: Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct OptionalVisitor<T> {
out: std::marker::PhantomData<T>,
}

impl<'de, T> serde::de::Visitor<'de> for OptionalVisitor<T>
where
T: crate::serde_bytes::Deserialize<'de>,
{
type Value = Option<T>;

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("optional string, str or slice, vec of bytes")
}

fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}

fn visit_none<E: serde::de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}

fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
T::deserialize(deserializer).map(Some)
}
}

let visitor = OptionalVisitor { out: std::marker::PhantomData };
deserializer.deserialize_option(visitor)
}
}
}

mod ser {
use serde::Serializer;

pub trait Serialize {
#[allow(missing_docs)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer;
}

impl<T> Serialize for Option<T>
where
T: crate::serde_bytes::Serialize + std::convert::AsRef<[u8]>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
struct AsBytes<T>(T);

impl<T> serde::Serialize for AsBytes<T>
where
T: crate::serde_bytes::Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
crate::serde_bytes::Serialize::serialize(&self.0, serializer)
}
}

match self {
Some(b) => serializer.serialize_some(&AsBytes(b)),
None => serializer.serialize_none(),
}
}
}
}
40 changes: 40 additions & 0 deletions wallet/bip32/src/derivation_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
fmt::{self, Display},
str::FromStr,
};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

/// Prefix for all derivation paths.
const PREFIX: &str = "m";
Expand All @@ -16,6 +17,45 @@ pub struct DerivationPath {
path: Vec<ChildNumber>,
}

impl<'de> Deserialize<'de> for DerivationPath {
fn deserialize<D>(deserializer: D) -> std::result::Result<DerivationPath, D::Error>
where
D: Deserializer<'de>,
{
struct DerivationPathVisitor;
impl<'de> de::Visitor<'de> for DerivationPathVisitor {
type Value = DerivationPath;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string containing list of permissions separated by a '+'")
}

fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
DerivationPath::from_str(value).map_err(|err| de::Error::custom(err.to_string()))
}
fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
DerivationPath::from_str(v).map_err(|err| de::Error::custom(err.to_string()))
}
}

deserializer.deserialize_str(DerivationPathVisitor)
}
}

impl Serialize for DerivationPath {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

impl DerivationPath {
/// Iterate over the [`ChildNumber`] values in this derivation path.
pub fn iter(&self) -> impl Iterator<Item = ChildNumber> + '_ {
Expand Down
Loading

0 comments on commit 99eed7c

Please sign in to comment.