tesseract_protocol_substrate/
lib.rs

1//===------------------- lib.rs --------------------------------------------===//
2//  Copyright 2021, Tesseract Systems, Inc.
3//
4//  Licensed under the Apache License, Version 2.0 (the "License");
5//  you may not use this file except in compliance with the License.
6//  You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10//  Unless required by applicable law or agreed to in writing, software
11//  distributed under the License is distributed on an "AS IS" BASIS,
12//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//  See the License for the specific language governing permissions and
14//  limitations under the License.
15//===----------------------------------------------------------------------===//
16
17#![feature(async_closure)]
18
19#[cfg(feature = "client")]
20pub mod client;
21
22#[cfg(feature = "service")]
23pub mod service;
24
25use async_trait::async_trait;
26use std::sync::Arc;
27
28use serde::{Deserialize, Serialize};
29
30use tesseract_one::error::Result;
31use tesseract_one::Protocol;
32
33#[derive(Clone, Copy)]
34pub enum Substrate {
35    Protocol,
36}
37
38impl Default for Substrate {
39    fn default() -> Self {
40        Self::Protocol
41    }
42}
43
44impl Protocol for Substrate {
45    fn id(&self) -> String {
46        "substrate-v1".to_owned()
47    }
48}
49
50#[repr(u8)]
51#[derive(Serialize, Deserialize, Clone, Copy)]
52pub enum AccountType {
53    Ed25519 = 1,
54    Sr25519 = 2,
55    Ecdsa = 3,
56}
57
58pub mod method_names {
59    pub const GET_ACCOUNT: &str = "get_account";
60    pub const SIGN_TRANSACTION: &str = "sign_transaction";
61}
62
63#[derive(Serialize, Deserialize, Clone, Copy)]
64pub struct GetAccountRequest {
65    pub account_type: AccountType, // Type of a needed account
66}
67
68#[derive(Serialize, Deserialize, Clone)]
69pub struct GetAccountResponse {
70    pub public_key: Vec<u8>, // Public key of the account. 32/33 bytes depending of the AccountType
71    pub path: String,        // Derivation path or id of the account.
72}
73
74#[derive(Serialize, Deserialize, Clone)]
75pub struct SignTransactionRequest {
76    pub account_type: AccountType,   // Type of the signing account.
77    pub account_path: String, // Derivation path or id of the signing account returned from the wallet.
78    pub extrinsic_data: Vec<u8>, // SCALE serialized extrinsic (with Extra)
79    pub extrinsic_metadata: Vec<u8>, // SCALE serialized extrinsic metadata (Metadata V14) with type set to call type.
80    pub extrinsic_types: Vec<u8>, // SCALE serialized PortableRegistry with all used types (Metadata V14)
81}
82
83#[derive(Serialize, Deserialize, Clone)]
84pub struct SignTransactionResponse {
85    pub signature: Vec<u8>, // Signature. 64/65 bytes depending of the AccountType
86}
87
88#[async_trait]
89pub trait SubstrateService {
90    async fn get_account(self: Arc<Self>, account_type: AccountType) -> Result<GetAccountResponse>;
91    async fn sign_transaction(
92        self: Arc<Self>,
93        account_type: AccountType,
94        account_path: &str,
95        extrinsic_data: &[u8],
96        extrinsic_metadata: &[u8],
97        extrinsic_types: &[u8],
98    ) -> Result<Vec<u8>>;
99}