|
1 | 1 | use anyhow::{Context, Result};
|
| 2 | +use bincode; |
2 | 3 | use celestia_types::Blob;
|
| 4 | +use prism_errors::GeneralError; |
3 | 5 | use serde::{Deserialize, Serialize};
|
4 |
| -use std::fmt::Display; |
| 6 | +use std::{self, fmt::Display, str::FromStr}; |
5 | 7 |
|
6 | 8 | #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
|
7 | 9 | /// Represents a public key supported by the system.
|
8 | 10 | pub enum PublicKey {
|
9 |
| - Secp256k1(Vec<u8>), // Bitcoin, Ethereum |
10 |
| - Ed25519(Vec<u8>), // Cosmos, OpenSSH, GnuPG |
11 |
| - Curve25519(Vec<u8>), // Signal, Tor |
| 11 | + // Secp256k1(Vec<u8>), // Bitcoin, Ethereum |
| 12 | + Ed25519(Vec<u8>), // Cosmos, OpenSSH, GnuPG |
| 13 | + // Curve25519(Vec<u8>), // Signal, Tor |
12 | 14 | }
|
13 | 15 |
|
14 | 16 | impl PublicKey {
|
15 | 17 | pub fn as_bytes(&self) -> &[u8] {
|
16 | 18 | match self {
|
17 |
| - PublicKey::Secp256k1(bytes) => bytes, |
| 19 | + // PublicKey::Secp256k1(bytes) => bytes, |
18 | 20 | PublicKey::Ed25519(bytes) => bytes,
|
19 |
| - PublicKey::Curve25519(bytes) => bytes, |
| 21 | + // PublicKey::Curve25519(bytes) => bytes, |
20 | 22 | }
|
21 | 23 | }
|
22 | 24 | }
|
@@ -50,8 +52,9 @@ pub enum Operation {
|
50 | 52 | #[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
|
51 | 53 | /// Arguments for creating an account with a service.
|
52 | 54 | pub struct CreateAccountArgs {
|
53 |
| - pub id: String, // Account ID |
54 |
| - pub value: PublicKey, // Public Key |
| 55 | + pub id: String, // Account ID |
| 56 | + pub value: PublicKey, // Public Key |
| 57 | + pub signature: Vec<u8>, |
55 | 58 | pub service_id: String, // Associated service ID
|
56 | 59 | pub challenge: ServiceChallengeInput, // Challenge input for verification
|
57 | 60 | }
|
@@ -80,6 +83,65 @@ impl Operation {
|
80 | 83 | Operation::CreateAccount(args) => Some(args.value.clone()),
|
81 | 84 | }
|
82 | 85 | }
|
| 86 | + |
| 87 | + pub fn validate(&self) -> Result<()> { |
| 88 | + match &self { |
| 89 | + Operation::AddKey(KeyOperationArgs { |
| 90 | + id, |
| 91 | + value, |
| 92 | + signature, |
| 93 | + }) |
| 94 | + | Operation::RevokeKey(KeyOperationArgs { |
| 95 | + id, |
| 96 | + value, |
| 97 | + signature, |
| 98 | + }) => { |
| 99 | + if id.is_empty() { |
| 100 | + return Err( |
| 101 | + GeneralError::MissingArgumentError("id is empty".to_string()).into(), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if signature.signature.is_empty() { |
| 106 | + return Err(GeneralError::MissingArgumentError( |
| 107 | + "signature is empty".to_string(), |
| 108 | + ) |
| 109 | + .into()); |
| 110 | + } |
| 111 | + |
| 112 | + // verify_signature(self, None).context("Failed to verify signature")?; |
| 113 | + |
| 114 | + Ok(()) |
| 115 | + } |
| 116 | + Operation::CreateAccount(CreateAccountArgs { |
| 117 | + id, |
| 118 | + value, |
| 119 | + signature, |
| 120 | + service_id: _, |
| 121 | + challenge, |
| 122 | + }) => { |
| 123 | + if id.is_empty() { |
| 124 | + return Err( |
| 125 | + GeneralError::MissingArgumentError("id is empty".to_string()).into(), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + match challenge { |
| 130 | + ServiceChallengeInput::Signed(signature) => { |
| 131 | + if signature.is_empty() { |
| 132 | + return Err(GeneralError::MissingArgumentError( |
| 133 | + "challenge data is empty".to_string(), |
| 134 | + ) |
| 135 | + .into()); |
| 136 | + } |
| 137 | + // verify_signature(self, None).context("Failed to verify signature")?; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
83 | 145 | }
|
84 | 146 |
|
85 | 147 | impl Display for Operation {
|
|
0 commit comments