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

Decrypt transactions with record view key in gettransaction RPC #1479

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Decrypt transactions with record view key in gettransaction RPC…
… endpoint
  • Loading branch information
collinc97 committed Dec 20, 2021
commit 8eabbc3c992d78cdbcd5a72f18c2473a91802bb0
34 changes: 29 additions & 5 deletions src/rpc/documentation/public_endpoints/gettransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Returns a transaction with metadata given the transaction ID.

### Response

| Parameter | Type | Description |
|:-------------:|:------:|:-----------------------------------------:|
| `metadata` | object | The metadata of the requested transaction |
| `transaction` | object | The transaction object |
| Parameter | Type | Description |
|:-------------------:|:------:|:------------------------------------------------------------------:|
| `decrypted_records` | array | The decrypted records using record view key events, if they exist. |
| `metadata` | object | The metadata of the requested transaction |
| `transaction` | object | The transaction object |

#### Transaction Metadata

Expand All @@ -32,6 +33,18 @@ Returns a transaction with metadata given the transaction ID.
| `transaction_id` | string | The ID of this transaction. |
| `transitions` | array | The state transitions. |

#### Record

| Parameter | Type | Description |
|:-----------------:|:------:|:---------------------------------------:|
| `commitment` | string | The commitment of this record. |
| `owner` | string | The record owner. |
| `payload` | string | The record payload. |
| `program_id` | string | The program id of this record. |
| `randomizer` | string | The randomizer used for the ciphertext. |
| `record_view_key` | string | The view key of this record. |
| `value` | number | The record value. |

### Example Request
```ignore
curl --data-binary '{"jsonrpc": "2.0", "id":"1", "method": "gettransaction", "params": ["at1mka6m3kfsgt5dpnfurk2ydjefqjzng4aawj7lkpc32pjkg86hyysrke9nf"] }' -H 'content-type: application/json' http://127.0.0.1:3030/
Expand Down Expand Up @@ -83,7 +96,18 @@ curl --data-binary '{"jsonrpc": "2.0", "id":"1", "method": "gettransaction", "pa
"value_balance": -1000000000000000
}
]
}
},
"decrypted_records": [
{
"commitment":"cm1xck4eyf3a3qnz69yyrr3jf698mqzwpjgkqu0j359p0sdr5wyjyqsn0604p",
"owner":"aleo1h35g4ld7wqahxw3puelmntaeddzr2rukmhty5a8cw5vqe65s2cpsrd4ghl",
"payload":"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"program_id":"ap1lhj3g5uzervu3km7rl0rsd0u5j6pj9ujum6yxrvms4mx8r2qhew88ga849hnjypghswxceh02frszs45qmd",
"randomizer":"rr1v76mftwzagt9k9nsjjpdqgytv4ddk24e9q7f240daar7avcv3q9qku3vtg",
"record_view_key":"rcvk1mujt98tc2r04l58haxjv48s5a7vnhx8ws24fxpdruuk3z37vscqsjtvlg5",
"value":1000000000000000
}
]
},
"id": "1"
}
Expand Down
9 changes: 8 additions & 1 deletion src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ mod tests {
use hyper::Request;
use rand::{thread_rng, SeedableRng};
use rand_chacha::ChaChaRng;
use snarkvm::dpc::Record;
use std::{
path::{Path, PathBuf},
sync::atomic::AtomicBool,
Expand Down Expand Up @@ -1166,6 +1167,7 @@ mod tests {
pub struct GetTransactionResponse {
pub transaction: Transaction<Testnet2>,
pub metadata: snarkos_storage::Metadata<Testnet2>,
pub decrypted_records: Vec<Record<Testnet2>>,
}

// Initialize a new ledger.
Expand Down Expand Up @@ -1199,11 +1201,16 @@ mod tests {
let actual: GetTransactionResponse = process_response(response).await;

// Check the transaction.
assert_eq!(*Testnet2::genesis_block().transactions().first().unwrap(), actual.transaction);
let expected_transaction = Testnet2::genesis_block().transactions().first().unwrap();
assert_eq!(*expected_transaction, actual.transaction);

// Check the metadata.
let expected_transaction_metadata = ledger.get_transaction_metadata(&transaction_id).unwrap();
assert_eq!(expected_transaction_metadata, actual.metadata);

// Check the records.
let expected_decrypted_records: Vec<Record<Testnet2>> = expected_transaction.to_records().collect();
assert_eq!(expected_decrypted_records, actual.decrypted_records)
}

#[tokio::test]
Expand Down
7 changes: 4 additions & 3 deletions src/rpc/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use snarkvm::{
};

use jsonrpc_core::Value;
use snarkvm::utilities::ToBytes;
use snarkvm::{dpc::Record, utilities::ToBytes};
use std::{cmp::max, net::SocketAddr, ops::Deref, sync::Arc};
use tokio::sync::RwLock;

Expand Down Expand Up @@ -264,12 +264,13 @@ impl<N: Network, E: Environment> RpcFunctions<N> for RpcImpl<N, E> {
Ok(self.memory_pool.read().await.transactions())
}

/// Returns a transaction with metadata given the transaction ID.
/// Returns a transaction with metadata and decrypted records given the transaction ID.
async fn get_transaction(&self, transaction_id: serde_json::Value) -> Result<Value, RpcError> {
let transaction_id: N::TransactionID = serde_json::from_value(transaction_id)?;
let transaction: Transaction<N> = self.ledger.get_transaction(&transaction_id)?;
let metadata: Metadata<N> = self.ledger.get_transaction_metadata(&transaction_id)?;
Ok(serde_json::json!({ "transaction": transaction, "metadata": metadata }))
let decrypted_records: Vec<Record<N>> = transaction.to_records().collect();
Ok(serde_json::json!({ "transaction": transaction, "metadata": metadata, "decrypted_records": decrypted_records }))
}

/// Returns a transition given the transition ID.
Expand Down