Skip to content

Commit

Permalink
Borsh Serialize solana_sdk::Transaction (#8)
Browse files Browse the repository at this point in the history
* borsh serialize transaction

* serialize message and signature

* add borsh

* add borsh io

* change io

* borsh0_10

* message header and compiled instruction

* message header and compiled instruction to borsh0_10

* remove maybestd

* std

* borsh

* serialize Signature manually

* JsonSchema

* remove json_schema

* remove json_schema

* derive arbitrary

* arbitrary flag

* add feature in cargo toml

* remove arbitrary

* remove arbitrary
  • Loading branch information
DudessaPr authored Sep 24, 2024
1 parent 52ac111 commit d3b8cb0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
5 changes: 3 additions & 2 deletions sdk/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#[cfg(target_arch = "wasm32")]
use crate::wasm_bindgen;
#[cfg(feature = "borsh")]
use borsh::BorshSerialize;
use borsh::{BorshDeserialize, BorshSerialize};
use {
crate::{pubkey::Pubkey, sanitize::Sanitize, short_vec},
bincode::serialize,
Expand Down Expand Up @@ -636,8 +636,9 @@ impl AccountMeta {
///
/// [`Message`]: crate::message::Message
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
#[borsh(crate = "borsh")]
pub struct CompiledInstruction {
/// Index into the transaction keys array indicating the program account that executes this instruction.
pub program_id_index: u8,
Expand Down
4 changes: 3 additions & 1 deletion sdk/program/src/message/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use {
},
std::{collections::HashSet, convert::TryFrom, str::FromStr},
};
use borsh::{BorshDeserialize, BorshSerialize};

#[deprecated(
since = "2.0.0",
Expand Down Expand Up @@ -125,8 +126,9 @@ fn compile_instructions(ixs: &[Instruction], keys: &[Pubkey]) -> Vec<CompiledIns
frozen_abi(digest = "2KnLEqfLcTBQqitE22Pp8JYkaqVVbAkGbCfdeHoyxcAU"),
derive(AbiExample)
)]
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
#[borsh(crate = "borsh")]
pub struct Message {
/// The message header, identifying signed and read-only `account_keys`.
// NOTE: Serialization-related changes must be paired with the direct read at sigverify.
Expand Down
5 changes: 3 additions & 2 deletions sdk/program/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod non_bpf_modules {

pub use {account_keys::*, address_loader::*, sanitized::*, versions::*};
}

use borsh::{BorshSerialize, BorshDeserialize};
#[cfg(not(target_os = "solana"))]
pub use non_bpf_modules::*;
pub use {compiled_keys::CompileError, legacy::Message};
Expand Down Expand Up @@ -92,8 +92,9 @@ pub const MESSAGE_HEADER_LENGTH: usize = 3;
///
/// [PoH]: https://docs.solanalabs.com/consensus/synchronization
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone, Copy)]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone, Copy)]
#[serde(rename_all = "camelCase")]
#[borsh(crate = "borsh")]
pub struct MessageHeader {
/// The number of signatures required for this message to be considered
/// valid. The signers of those signatures must match the first
Expand Down
25 changes: 25 additions & 0 deletions sdk/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Functionality for public and private keys.
#![cfg(feature = "full")]

use std::io::{Write, Read, Error as IoError};
// legacy module paths
pub use crate::signer::{keypair::*, null_signer::*, presigner::*, *};
use {
Expand All @@ -14,6 +15,7 @@ use {
},
thiserror::Error,
};
use borsh::{BorshDeserialize, BorshSerialize};

/// Number of bytes in a signature
pub const SIGNATURE_BYTES: usize = 64;
Expand All @@ -25,6 +27,29 @@ const MAX_BASE58_SIGNATURE_LEN: usize = 88;
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Signature(GenericArray<u8, U64>);



// Implement BorshSerialize for Signature
impl BorshSerialize for Signature {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), IoError> {
// Serialize the inner GenericArray as bytes
writer.write_all(self.0.as_slice())?;
Ok(())
}
}

// Implement BorshDeserialize for Signature
impl BorshDeserialize for Signature {
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, IoError> {
// Create an empty GenericArray
let mut array = GenericArray::<u8, U64>::default();
// Read bytes into the array
reader.read_exact(array.as_mut_slice())?;
// Return Signature with the array
Ok(Signature(array))
}
}

impl crate::sanitize::Sanitize for Signature {}

impl Signature {
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ use {
solana_sdk::feature_set,
std::result,
};

use borsh::{BorshDeserialize, BorshSerialize};
mod error;
mod sanitized;
mod versioned;
Expand Down Expand Up @@ -174,7 +174,7 @@ pub type Result<T> = result::Result<T, TransactionError>;
derive(AbiExample),
frozen_abi(digest = "FZtncnS1Xk8ghHfKiXE5oGiUbw2wJhmfXQuNgQR3K6Mc")
)]
#[derive(Debug, PartialEq, Default, Eq, Clone, Serialize, Deserialize)]
#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq, Default, Eq, Clone, Serialize, Deserialize)]
pub struct Transaction {
/// A set of signatures of a serialized [`Message`], signed by the first
/// keys of the `Message`'s [`account_keys`], where the number of signatures
Expand Down

0 comments on commit d3b8cb0

Please sign in to comment.