Skip to content

Feat/encryptionv38e2e #60

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
},
"cSpell.words": [
"ecdh"
]
}
5 changes: 5 additions & 0 deletions chain-common/proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "stored-key.proto";
import "transaction.proto";
import "validation.proto";
import "persona.proto";
import "post-encryption.proto";

message MWRequest {
oneof request {
Expand Down Expand Up @@ -36,6 +37,8 @@ message MWRequest {
GenerateMnemonicParam param_generate_mnemonic = 25;

PersonaGenerationParam param_generate_persona = 26;

PostEncryptionParam param_post_encryption = 27;
}
}

Expand Down Expand Up @@ -64,6 +67,8 @@ message MWResponse {
GenerateMnemonicResp resp_generate_mnemonic = 24;

PersonaGenerationResp resp_generate_persona = 25;

PostEncryptedResp resp_post_encryption = 26;
}
}

Expand Down
29 changes: 29 additions & 0 deletions chain-common/proto/base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,39 @@ message StoredKeyAccountInfo {
string extendedPublicKey = 5;
}

enum Curve {
Secp256k1 = 0;
Ed25519 = 1;
}

message EncryptOption {
enum Version {
V37 = 0;
V38 = 1;
}
Version version = 1;
}

message JWK {
string crv = 1;
bool ext = 3;
string x = 4;
string y = 5;
repeated string key_ops = 6;
string kty = 7;
optional string d = 8;
}

message AesJWK {
string alg = 1;
bool ext = 2;
string k = 3;
repeated string key_ops = 4;
string kty = 5;
}

message E2EEncryptParam {
bytes localKeyData = 1;
map<string, bytes> target = 2;
bytes authorPrivateKey = 3;
}
27 changes: 3 additions & 24 deletions chain-common/proto/persona.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,15 @@ message PersonaGenerationParam {
string mnemonic = 1;
string password = 2;
string path = 3;
enum Curve {
Secp256k1 = 0;
Ed25519 = 1;
}
Curve curve = 4;
EncryptOption option = 5;
}

message PersonaGenerationResp {
string identifier = 1;
JWKResp privateKey = 2;
JWKResp publicKey = 3;
optional AesJWKResp localKey = 4;
JWK privateKey = 2;
JWK publicKey = 3;
optional AesJWK localKey = 4;
EncryptOption option = 5;
}

message JWKResp {
string crv = 1;
bool ext = 3;
string x = 4;
string y = 5;
repeated string key_ops = 6;
string kty = 7;
optional string d = 8;
}

message AesJWKResp {
string alg = 1;
bool ext = 2;
string k = 3;
repeated string key_ops = 4;
string kty = 5;
}
35 changes: 35 additions & 0 deletions chain-common/proto/post-encryption.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";

package api;

import "base.proto";

enum PublicKeyAlgorithm {
Ed25519Algr = 0;
Secp256p1Algr = 1;
Secp256k1Algr = 2;
}

message PostEncryptionParam {
EncryptOption.Version version = 1;
bool isPlublic = 2;
string content = 3;
string network = 4;
optional bytes authorPublicKeyData = 5;
optional string authorUserId = 6;
optional PublicKeyAlgorithm authorPublicKeyAlgr = 7;
optional E2EEncryptParam param = 8;
}

message E2EEncryptionResult {
optional bytes iv = 1;
bytes encryptedPostKeyData = 2;
optional bytes ephemeralPublicKeyData = 3;
}

message PostEncryptedResp {
string content = 1;
string postIdentifier = 2;
bytes postKey = 3;
map<string, E2EEncryptionResult> results = 4;
}
18 changes: 16 additions & 2 deletions chain-common/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::convert::{From, TryFrom};
use std::str::FromStr;

use crate::generated::api::{
encrypt_option::Version, mw_response::Response, persona_generation_param::Curve, MwResponse,
MwResponseError,
encrypt_option::Version, mw_response::Response, Curve, MwResponse, MwResponseError,
};
use crypto::Error as CryptoError;

Expand All @@ -16,6 +15,13 @@ impl From<CryptoError> for MwResponseError {
}
}

impl From<CryptoError> for MwResponse {
fn from(err: CryptoError) -> Self {
let resp_error: MwResponseError = err.into();
resp_error.into()
}
}

impl From<crypto::jwk::BIP32Error> for MwResponseError {
fn from(err: crypto::jwk::BIP32Error) -> Self {
Self {
Expand Down Expand Up @@ -46,6 +52,14 @@ impl From<Result<Response, MwResponseError>> for MwResponse {
}
}

impl From<Response> for MwResponse {
fn from(response: Response) -> Self {
Self {
response: Some(response),
}
}
}

impl FromStr for Curve {
type Err = MwResponseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
138 changes: 97 additions & 41 deletions chain-common/src/generated/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ pub mod encrypt_option {
V38 = 1,
}
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Jwk {
#[prost(string, tag="1")]
pub crv: ::prost::alloc::string::String,
#[prost(bool, tag="3")]
pub ext: bool,
#[prost(string, tag="4")]
pub x: ::prost::alloc::string::String,
#[prost(string, tag="5")]
pub y: ::prost::alloc::string::String,
#[prost(string, repeated, tag="6")]
pub key_ops: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
#[prost(string, tag="7")]
pub kty: ::prost::alloc::string::String,
#[prost(string, optional, tag="8")]
pub d: ::core::option::Option<::prost::alloc::string::String>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AesJwk {
#[prost(string, tag="1")]
pub alg: ::prost::alloc::string::String,
#[prost(bool, tag="2")]
pub ext: bool,
#[prost(string, tag="3")]
pub k: ::prost::alloc::string::String,
#[prost(string, repeated, tag="4")]
pub key_ops: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
#[prost(string, tag="5")]
pub kty: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct E2eEncryptParam {
#[prost(bytes="vec", tag="1")]
pub local_key_data: ::prost::alloc::vec::Vec<u8>,
#[prost(map="string, bytes", tag="2")]
pub target: ::std::collections::HashMap<::prost::alloc::string::String, ::prost::alloc::vec::Vec<u8>>,
#[prost(bytes="vec", tag="3")]
pub author_private_key: ::prost::alloc::vec::Vec<u8>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum Coin {
Expand Down Expand Up @@ -75,6 +114,12 @@ pub enum StoredKeyExportType {
MnemonicExportType = 1,
KeyStoreJsonExportType = 2,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum Curve {
Secp256k1 = 0,
Ed25519 = 1,
}
/// Create a new account to the StoredKey at specific derivation path. Fail if the StoredKey is not a Hd StoredKey
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CreateStoredKeyNewAccountAtPathParam {
Expand Down Expand Up @@ -378,66 +423,73 @@ pub struct PersonaGenerationParam {
pub password: ::prost::alloc::string::String,
#[prost(string, tag="3")]
pub path: ::prost::alloc::string::String,
#[prost(enumeration="persona_generation_param::Curve", tag="4")]
#[prost(enumeration="Curve", tag="4")]
pub curve: i32,
#[prost(message, optional, tag="5")]
pub option: ::core::option::Option<EncryptOption>,
}
/// Nested message and enum types in `PersonaGenerationParam`.
pub mod persona_generation_param {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum Curve {
Secp256k1 = 0,
Ed25519 = 1,
}
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PersonaGenerationResp {
#[prost(string, tag="1")]
pub identifier: ::prost::alloc::string::String,
#[prost(message, optional, tag="2")]
pub private_key: ::core::option::Option<JwkResp>,
pub private_key: ::core::option::Option<Jwk>,
#[prost(message, optional, tag="3")]
pub public_key: ::core::option::Option<JwkResp>,
pub public_key: ::core::option::Option<Jwk>,
#[prost(message, optional, tag="4")]
pub local_key: ::core::option::Option<AesJwkResp>,
pub local_key: ::core::option::Option<AesJwk>,
#[prost(message, optional, tag="5")]
pub option: ::core::option::Option<EncryptOption>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct JwkResp {
#[prost(string, tag="1")]
pub crv: ::prost::alloc::string::String,
#[prost(bool, tag="3")]
pub ext: bool,
#[prost(string, tag="4")]
pub x: ::prost::alloc::string::String,
#[prost(string, tag="5")]
pub y: ::prost::alloc::string::String,
#[prost(string, repeated, tag="6")]
pub key_ops: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
#[prost(string, tag="7")]
pub kty: ::prost::alloc::string::String,
#[prost(string, optional, tag="8")]
pub d: ::core::option::Option<::prost::alloc::string::String>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AesJwkResp {
#[prost(string, tag="1")]
pub alg: ::prost::alloc::string::String,
pub struct PostEncryptionParam {
#[prost(enumeration="encrypt_option::Version", tag="1")]
pub version: i32,
#[prost(bool, tag="2")]
pub ext: bool,
pub is_plublic: bool,
#[prost(string, tag="3")]
pub k: ::prost::alloc::string::String,
#[prost(string, repeated, tag="4")]
pub key_ops: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
#[prost(string, tag="5")]
pub kty: ::prost::alloc::string::String,
pub content: ::prost::alloc::string::String,
#[prost(string, tag="4")]
pub network: ::prost::alloc::string::String,
#[prost(bytes="vec", optional, tag="5")]
pub author_public_key_data: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
#[prost(string, optional, tag="6")]
pub author_user_id: ::core::option::Option<::prost::alloc::string::String>,
#[prost(enumeration="PublicKeyAlgorithm", optional, tag="7")]
pub author_public_key_algr: ::core::option::Option<i32>,
#[prost(message, optional, tag="8")]
pub param: ::core::option::Option<E2eEncryptParam>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct E2eEncryptionResult {
#[prost(bytes="vec", optional, tag="1")]
pub iv: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
#[prost(bytes="vec", tag="2")]
pub encrypted_post_key_data: ::prost::alloc::vec::Vec<u8>,
#[prost(bytes="vec", optional, tag="3")]
pub ephemeral_public_key_data: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PostEncryptedResp {
#[prost(string, tag="1")]
pub content: ::prost::alloc::string::String,
#[prost(string, tag="2")]
pub post_identifier: ::prost::alloc::string::String,
#[prost(bytes="vec", tag="3")]
pub post_key: ::prost::alloc::vec::Vec<u8>,
#[prost(map="string, message", tag="4")]
pub results: ::std::collections::HashMap<::prost::alloc::string::String, E2eEncryptionResult>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum PublicKeyAlgorithm {
Ed25519Algr = 0,
Secp256p1Algr = 1,
Secp256k1Algr = 2,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MwRequest {
#[prost(oneof="mw_request::Request", tags="1, 2, 3, 4, 5, 10, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26")]
#[prost(oneof="mw_request::Request", tags="1, 2, 3, 4, 5, 10, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27")]
pub request: ::core::option::Option<mw_request::Request>,
}
/// Nested message and enum types in `MWRequest`.
Expand Down Expand Up @@ -482,11 +534,13 @@ pub mod mw_request {
ParamGenerateMnemonic(super::GenerateMnemonicParam),
#[prost(message, tag="26")]
ParamGeneratePersona(super::PersonaGenerationParam),
#[prost(message, tag="27")]
ParamPostEncryption(super::PostEncryptionParam),
}
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MwResponse {
#[prost(oneof="mw_response::Response", tags="1, 2, 3, 4, 5, 6, 11, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25")]
#[prost(oneof="mw_response::Response", tags="1, 2, 3, 4, 5, 6, 11, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26")]
pub response: ::core::option::Option<mw_response::Response>,
}
/// Nested message and enum types in `MWResponse`.
Expand Down Expand Up @@ -529,6 +583,8 @@ pub mod mw_response {
RespGenerateMnemonic(super::GenerateMnemonicResp),
#[prost(message, tag="25")]
RespGeneratePersona(super::PersonaGenerationResp),
#[prost(message, tag="26")]
RespPostEncryption(super::PostEncryptedResp),
}
}
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
Loading