forked from kaspanet/rusty-kaspa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
a797e1e
commit 6a56461
Showing
16 changed files
with
1,309 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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(), | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[package] | ||
name = "kaspa-wallet-pskt" | ||
keywords = ["kaspa", "wallet", "pskt", "psbt", "bip-370"] | ||
description = "Partially Signed Kaspa Transaction" | ||
categories = ["cryptography::cryptocurrencies"] | ||
rust-version.workspace = true | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
edition.workspace = true | ||
include.workspace = true | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[features] | ||
wasm32-sdk = ["kaspa-consensus-client/wasm32-sdk"] | ||
wasm32-types = ["kaspa-consensus-client/wasm32-types"] | ||
|
||
[dependencies] | ||
kaspa-bip32.workspace = true | ||
kaspa-consensus-client.workspace = true | ||
kaspa-consensus-core.workspace = true | ||
kaspa-txscript-errors.workspace = true | ||
kaspa-txscript.workspace = true | ||
kaspa-utils.workspace = true | ||
|
||
derive_builder.workspace = true | ||
secp256k1.workspace = true | ||
serde-value.workspace = true | ||
serde.workspace = true | ||
serde_repr.workspace = true | ||
thiserror.workspace = true | ||
|
||
[dev-dependencies] | ||
serde_json.workspace = true |
Oops, something went wrong.