Skip to content

Commit

Permalink
rpc: support requesting and sending UserSignatures encoded as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Dec 16, 2024
1 parent f8bfb03 commit e5b12a4
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 21 deletions.
26 changes: 26 additions & 0 deletions crates/sui-rpc-api/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,16 @@
},
"style": "form"
},
{
"in": "query",
"name": "signatures_bytes",
"description": "Request `Vec<UserSignature>` encoded as bytes be included in the response\n\nDefaults to `false` if not provided.",
"schema": {
"description": "Request `Vec<UserSignature>` encoded as bytes be included in the response\n\nDefaults to `false` if not provided.",
"type": "boolean"
},
"style": "form"
},
{
"in": "query",
"name": "transaction",
Expand Down Expand Up @@ -728,6 +738,16 @@
},
"style": "form"
},
{
"in": "query",
"name": "signatures_bytes",
"description": "Request `Vec<UserSignature>` encoded as bytes be included in the response\n\nDefaults to `false` if not provided.",
"schema": {
"description": "Request `Vec<UserSignature>` encoded as bytes be included in the response\n\nDefaults to `false` if not provided.",
"type": "boolean"
},
"style": "form"
},
{
"in": "query",
"name": "transaction",
Expand Down Expand Up @@ -5515,6 +5535,12 @@
"$ref": "#/components/schemas/UserSignature"
}
},
"signatures_bytes": {
"type": "array",
"items": {
"type": "string"
}
},
"timestamp_ms": {
"description": "Radix-10 encoded 64-bit unsigned integer",
"type": "string",
Expand Down
14 changes: 12 additions & 2 deletions crates/sui-rpc-api/proto/sui.node.v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ message GetTransactionOptions {
//
// Defaults to true if not included
optional bool signatures = 3;
// Include the set of UserSignature's encoded as bytes in the response.
//
// Defaults to false if not included
optional bool signatures_bytes = 8;
// Include the sui.types.TransactionEffects message in the response.
//
// Defaults to true if not included
Expand All @@ -98,6 +102,7 @@ message GetTransactionResponse {
optional sui.types.Transaction transaction = 2;
optional sui.types.Bcs transaction_bcs = 3;
optional UserSignatures signatures = 4;
optional UserSignaturesBytes signatures_bytes = 11;
optional sui.types.TransactionEffects effects = 5;
optional sui.types.Bcs effects_bcs = 6;
optional sui.types.TransactionEvents events = 7;
Expand All @@ -110,6 +115,10 @@ message UserSignatures {
repeated sui.types.UserSignature signatures = 1;
}

message UserSignaturesBytes {
repeated bytes signatures = 1;
}

message GetObjectRequest {
optional sui.types.ObjectId object_id = 1;
optional uint64 version = 2;
Expand Down Expand Up @@ -322,9 +331,10 @@ message ExecuteTransactionResponse {
message ExecuteTransactionRequest {
optional sui.types.Transaction transaction = 1;
optional sui.types.Bcs transaction_bcs = 2;
repeated sui.types.UserSignature signatures = 3;
optional UserSignatures signatures = 3;
optional UserSignaturesBytes signatures_bytes = 4;

optional ExecuteTransactionOptions options = 4;
optional ExecuteTransactionOptions options = 5;
}

message ExecuteTransactionOptions {
Expand Down
11 changes: 5 additions & 6 deletions crates/sui-rpc-api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,18 @@ impl Client {
let signatures = transaction
.inner()
.tx_signatures
.clone()
.into_iter()
.map(sui_sdk_types::types::UserSignature::try_from)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| Status::from_error(e.into()))?;
.iter()
.map(|signature| signature.as_ref().to_vec().into())
.collect();

let request = crate::proto::node::ExecuteTransactionRequest {
transaction: None,
transaction_bcs: Some(
crate::proto::types::Bcs::serialize(&transaction.inner().intent_message.value)
.map_err(|e| Status::from_error(e.into()))?,
),
signatures: signatures.into_iter().map(Into::into).collect(),
signatures: None,
signatures_bytes: Some(crate::proto::node::UserSignaturesBytes { signatures }),

options: Some(crate::proto::node::ExecuteTransactionOptions {
effects: Some(false),
Expand Down
45 changes: 34 additions & 11 deletions crates/sui-rpc-api/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,40 @@ impl crate::proto::node::node_server::Node for crate::RpcService {
))
}
};
let signatures = request
.signatures
.iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.map_err(|e| {
tonic::Status::new(
tonic::Code::InvalidArgument,
format!("invalid signature: {e}"),
)
})?;
let mut signatures: Vec<sui_sdk_types::types::UserSignature> = Vec::new();

if let Some(proto_signatures) = request.signatures {
let from_proto_signatures = proto_signatures
.signatures
.iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| {
tonic::Status::new(
tonic::Code::InvalidArgument,
format!("invalid signature: {e}"),
)
})?;

signatures.extend(from_proto_signatures);
}

if let Some(signatures_bytes) = request.signatures_bytes {
let from_bytes_signatures = signatures_bytes
.signatures
.iter()
.map(|bytes| sui_sdk_types::types::UserSignature::from_bytes(bytes))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| {
tonic::Status::new(
tonic::Code::InvalidArgument,
format!("invalid signature: {e}"),
)
})?;

signatures.extend(from_bytes_signatures);
}

let signed_transaction = sui_sdk_types::types::SignedTransaction {
transaction,
signatures,
Expand Down
Binary file modified crates/sui-rpc-api/src/proto/generated/sui.node.v2.fds.bin
Binary file not shown.
18 changes: 16 additions & 2 deletions crates/sui-rpc-api/src/proto/generated/sui.node.v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ pub struct GetTransactionOptions {
/// Defaults to true if not included
#[prost(bool, optional, tag = "3")]
pub signatures: ::core::option::Option<bool>,
/// Include the set of UserSignature's encoded as bytes in the response.
///
/// Defaults to false if not included
#[prost(bool, optional, tag = "8")]
pub signatures_bytes: ::core::option::Option<bool>,
/// Include the sui.types.TransactionEffects message in the response.
///
/// Defaults to true if not included
Expand Down Expand Up @@ -91,6 +96,8 @@ pub struct GetTransactionResponse {
pub transaction_bcs: ::core::option::Option<super::super::types::Bcs>,
#[prost(message, optional, tag = "4")]
pub signatures: ::core::option::Option<UserSignatures>,
#[prost(message, optional, tag = "11")]
pub signatures_bytes: ::core::option::Option<UserSignaturesBytes>,
#[prost(message, optional, tag = "5")]
pub effects: ::core::option::Option<super::super::types::TransactionEffects>,
#[prost(message, optional, tag = "6")]
Expand All @@ -110,6 +117,11 @@ pub struct UserSignatures {
pub signatures: ::prost::alloc::vec::Vec<super::super::types::UserSignature>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UserSignaturesBytes {
#[prost(bytes = "bytes", repeated, tag = "1")]
pub signatures: ::prost::alloc::vec::Vec<::prost::bytes::Bytes>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetObjectRequest {
#[prost(message, optional, tag = "1")]
pub object_id: ::core::option::Option<super::super::types::ObjectId>,
Expand Down Expand Up @@ -400,9 +412,11 @@ pub struct ExecuteTransactionRequest {
pub transaction: ::core::option::Option<super::super::types::Transaction>,
#[prost(message, optional, tag = "2")]
pub transaction_bcs: ::core::option::Option<super::super::types::Bcs>,
#[prost(message, repeated, tag = "3")]
pub signatures: ::prost::alloc::vec::Vec<super::super::types::UserSignature>,
#[prost(message, optional, tag = "3")]
pub signatures: ::core::option::Option<UserSignatures>,
#[prost(message, optional, tag = "4")]
pub signatures_bytes: ::core::option::Option<UserSignaturesBytes>,
#[prost(message, optional, tag = "5")]
pub options: ::core::option::Option<ExecuteTransactionOptions>,
}
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
Expand Down
20 changes: 20 additions & 0 deletions crates/sui-rpc-api/src/proto/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl From<crate::types::GetTransactionOptions> for GetTransactionOptions {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand All @@ -290,6 +291,7 @@ impl From<crate::types::GetTransactionOptions> for GetTransactionOptions {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand All @@ -304,6 +306,7 @@ impl From<GetTransactionOptions> for crate::types::GetTransactionOptions {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand All @@ -314,6 +317,7 @@ impl From<GetTransactionOptions> for crate::types::GetTransactionOptions {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand Down Expand Up @@ -461,6 +465,7 @@ impl From<crate::types::TransactionResponse> for GetTransactionResponse {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand All @@ -473,11 +478,16 @@ impl From<crate::types::TransactionResponse> for GetTransactionResponse {
signatures: signatures.into_iter().map(Into::into).collect(),
});

let signatures_bytes = signatures_bytes.map(|signatures| UserSignaturesBytes {
signatures: signatures.into_iter().map(Into::into).collect(),
});

Self {
digest: Some(digest.into()),
transaction: transaction.map(Into::into),
transaction_bcs: transaction_bcs.map(Into::into),
signatures,
signatures_bytes,
effects: effects.map(Into::into),
effects_bcs: effects_bcs.map(Into::into),
events: events.map(Into::into),
Expand All @@ -497,6 +507,7 @@ impl TryFrom<&GetTransactionResponse> for crate::types::TransactionResponse {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand Down Expand Up @@ -524,6 +535,14 @@ impl TryFrom<&GetTransactionResponse> for crate::types::TransactionResponse {
})
.transpose()?;

let signatures_bytes = signatures_bytes.as_ref().map(|signatures| {
signatures
.signatures
.iter()
.map(|bytes| bytes.to_vec())
.collect()
});

let effects = effects.as_ref().map(TryInto::try_into).transpose()?;
let effects_bcs = effects_bcs.as_ref().map(Into::into);

Expand All @@ -537,6 +556,7 @@ impl TryFrom<&GetTransactionResponse> for crate::types::TransactionResponse {
transaction,
transaction_bcs,
signatures,
signatures_bytes,
effects,
effects_bcs,
events,
Expand Down
8 changes: 8 additions & 0 deletions crates/sui-rpc-api/src/service/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ impl RpcService {
.flatten()
.transpose()?;

let signatures_bytes = options.include_signatures_bytes().then(|| {
signatures
.iter()
.map(|signature| signature.to_bytes())
.collect()
});

TransactionResponse {
digest,
transaction: options.include_transaction().then_some(transaction),
transaction_bcs,
signatures: options.include_signatures().then_some(signatures),
signatures_bytes,
effects: options.include_effects().then_some(effects),
effects_bcs,
events: options.include_events().then_some(events).flatten(),
Expand Down
15 changes: 15 additions & 0 deletions crates/sui-rpc-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ pub struct TransactionResponse {
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures: Option<Vec<sui_sdk_types::types::UserSignature>>,

#[serde_as(as = "Option<Vec<fastcrypto::encoding::Base64>>")]
#[schemars(with = "Option<Vec<String>>")]
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures_bytes: Option<Vec<Vec<u8>>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub effects: Option<sui_sdk_types::types::TransactionEffects>,

Expand Down Expand Up @@ -267,6 +272,12 @@ pub struct GetTransactionOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures: Option<bool>,

/// Request `Vec<UserSignature>` encoded as bytes be included in the response
///
/// Defaults to `false` if not provided.
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures_bytes: Option<bool>,

/// Request `TransactionEffects` be included in the response
///
/// Defaults to `true` if not provided.
Expand Down Expand Up @@ -305,6 +316,10 @@ impl GetTransactionOptions {
self.signatures.unwrap_or(true)
}

pub fn include_signatures_bytes(&self) -> bool {
self.signatures.unwrap_or(false)
}

pub fn include_effects(&self) -> bool {
self.effects.unwrap_or(true)
}
Expand Down

0 comments on commit e5b12a4

Please sign in to comment.