12 Commits

Author SHA1 Message Date
6c9fc3d973 updated github flow with asio for newer crowcpp 2024-07-14 16:15:26 +02:00
6a68f1cc45 Create dependabot.yml
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-05 09:01:28 +02:00
e3fd9cb576 fixes for warnings from code analysis 2023-04-20 22:56:34 +02:00
84cbb34391 added link to draftspec and to alternative implementation of RSA blind signatures using OpenSSL or BoringSSL
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-30 22:09:37 +01:00
a2747b6934 updated catch2 to v3.3.1
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-29 23:59:28 +01:00
ed525909bb splitted model.cpp into json_serialisation.cpp and model.cpp
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-29 12:20:54 +01:00
bdab59a507 corrected Link to OpenCoin Protocol 2023-01-29 12:20:08 +01:00
fa746dd925 extended README.md to doxygen mainpage and add to doxygen build
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-31 14:36:37 +01:00
0e4c094eed corrected name RequestCDD to RequestCDDC 2022-12-31 14:36:37 +01:00
87386e1b65 introduced and used bigint datatype
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-28 00:43:58 +01:00
0d3ffa0e5d Create FUNDING.yml 2022-12-19 23:10:01 +01:00
24adaebd07 updated github actions to cross-platform build and added action for github pages 2022-12-19 23:09:53 +01:00
15 changed files with 1024 additions and 672 deletions

13
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# These are supported funding model platforms
github: [gittiver] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

11
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

View File

@ -1,4 +1,4 @@
name: CMake
name: Build and test (cmake based build)
on:
push:
@ -19,34 +19,47 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest,
macos-latest,
windows-latest]
steps:
- uses: actions/checkout@v3
- name: Prepare dependencies
run: |
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get update && \
sudo apt-get install -yq \
libboost-system-dev \
libboost-date-time-dev \
cmake \
graphviz doxygen
elif [ "$RUNNER_OS" == "Windows" ]; then
choco install boost-msvc-14.3 graphviz doxygen.install
elif [ "$RUNNER_OS" == "macOS" ]; then
brew install boost graphviz doxygen
else
echo "$RUNNER_OS not supported"
exit 1
fi
shell: bash
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get update && \
sudo apt-get install -yq \
libasio-dev \
libssl-dev zlib1g-dev \
cmake graphviz doxygen
elif [ "$RUNNER_OS" == "Windows" ]; then
VCPKG_DEFAULT_TRIPLET=x64-windows vcpkg install
elif [ "$RUNNER_OS" == "macOS" ]; then
brew install asio openssl zlib doxygen graphviz
else
echo "$RUNNER_OS not supported"
exit 1
fi
shell: bash
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
cmake \
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake \
-B build
elif [ "$RUNNER_OS" == "macOS" ]; then
cmake \
-B build
else
cmake \
-B build
fi
shell: bash
- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
run: cmake --build build --config ${{env.BUILD_TYPE}}
shell: bash
- name: Test

View File

@ -1,4 +1,4 @@
name: Doxygen Github Pages Deploy action
name: Deploy Doxygen results to Github Pages
on:
push:
@ -15,7 +15,12 @@ jobs:
- name: Checkout 🛎️
uses: actions/checkout@v3
- name: Prepare dependencies
run: sudo apt-get update && sudo apt-get install -yq libboost-system-dev libboost-date-time-dev cmake graphviz doxygen
run: |
sudo apt-get update && \
sudo apt-get install -yq \
libasio-dev \
libssl-dev zlib1g-dev \
cmake graphviz doxygen
- name: configure
run: cmake -B build -S . -DEXPATPP_BUILD_DOCS=ON
- name: clean generated docs dir

View File

@ -1,20 +1,22 @@
cmake_minimum_required(VERSION 3.1.3)
cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR)
project(oc-issuer VERSION 0.0.2 LANGUAGES CXX)
enable_language(C)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
if (MSVC)
add_compile_options(/W4 /WX)
add_compile_options(/W4)
else()
add_compile_options(-Wall -Wextra -pedantic)
endif()
project(oc-mint VERSION 0.0.1 LANGUAGES CXX)
include(FetchContent)
@ -25,11 +27,12 @@ set(CROW_BUILD_EXAMPLES Off)
set(CROW_BUILD_TOOLS Off)
set(CROW_BUILD_TESTS Off)
set(CROW_BUILD_DOCS Off)
set(CROW_FEATURES "ssl;compression")
# add crow project to the build
FetchContent_Declare(crow
GIT_REPOSITORY https://github.com/CrowCpp/Crow.git
GIT_TAG v1.0+5
GIT_TAG v1.2.0
)
if(NOT crow_POPULATED)
@ -47,14 +50,14 @@ if(NOT expected_POPULATED)
endif(NOT expected_POPULATED)
include(CTest)
enable_testing()
enable_testing()
set(CATCH_INSTALL_DOCS Off)
set(CATCH_INSTALL_EXTRAS Off)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.2.0
GIT_TAG v3.6.0
)
FetchContent_MakeAvailable(Catch2)
@ -64,23 +67,27 @@ find_package(Doxygen
REQUIRED dot
OPTIONAL_COMPONENTS mscgen dia)
set(DOXYGEN_HAVE_DOT YES)
doxygen_add_docs(
doc
src
COMMENT "Generate documentation"
doxygen_add_docs( doc
README.md
src
COMMENT "Generate documentation"
)
# build common library
set(LIB_SOURCES src/model.cpp src/model.hpp)
set(LIB_SOURCES
src/model.cpp src/model.hpp
src/json_serialisation.cpp
src/big_int.hpp src/big_int.cpp )
add_library(oc-mint-lib ${LIB_SOURCES})
target_link_libraries(oc-mint-lib PUBLIC Crow::Crow)
target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src)
add_executable(oc-mint src/main.cpp)
target_link_libraries(oc-mint PRIVATE oc-mint-lib INTERFACE tl::expected::expected)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE oc-mint-lib INTERFACE tl::expected::expected)
## these are unittests that can be run on any platform
add_executable(tests test/test.cpp)
add_executable(tests test/test_big_int.cpp test/test.cpp)
target_link_libraries(tests
oc-mint-lib
Catch2::Catch2WithMain)

View File

@ -1,23 +1,47 @@
![build](https://github.com/OpenCoin/oc-mint-cpp/actions/workflows/cmake.yaml/badge.svg)
opencoin-issuer-cpp - a C++ OpenCoin Issuer REST-API implementation {#mainpage}
=============================================================
# oc mint sample
![build](https://github.com/OpenCoin/opencoin-issuer-cpp/actions/workflows/cmake.yaml/badge.svg)
[![Documentation](https://github.com/OpenCoin/opencoin-issuer-cpp/actions/workflows/doxygen-gh-pages.yml/badge.svg)](https://github.com/OpenCoin/opencoin-issuer-cpp/actions/workflows/doxygen-gh-pages.yml)
this is a C++ implementation of the opencoin protocol
as mapping to some kind of REST interface
# opencoin issuer
actually it is a work in progress.
this is a C++ implementation of the [opencoin protocol](https://opencoin.org/OpenCoin.html)
done as mapping to some kind of REST interface.
As all issuer related interactions of the protocol follows a request/response mechanism we are able to map all of them to Http-Requests.
We decided to use POST-Requests in all cases,
as the protocol uses JSON-formatted messages for requests.
The following table gives an overview of the mapping of requests to URLs:
| Request | URL | Response | C++ Interface Method |
|:------------------|:-------------|:------------------------------|:---------------------------------------------------|
| RequestCDDCSerial | /cddc/serial | ResponseCDDCSerial | cdd.cdd_serial of Model::get_current_cddc() |
| RequestCDDC | /cddc | ResponseCDDC | Model::get_current_cddc() |
| RequestMKCs | /mkcs | ResponseMKCs | Model::getMKCs |
| RequestMint | /mint | ResponseMint | Model::mint |
| RequestRenew | /renew | ResponseMint or ResponseDelay | ? |
| RequestResume | /resume | ResponseMint or ResponseDelay | ? |
| RequestRedeem | /redeem | ResponseRedeem | Model::redeem |
actually the implementation is a work in progress.
## TODO
- [ ] select crypto library
+ https://en.wikipedia.org/wiki/Comparison_of_cryptography_libraries
+ [ ] blinding utilizing crypto++
+ [ ] integrate session and login to make transactions account based
+ [ ] bookkeeping for accounts
## Protocol Questions
+ What is signed as cdd - only the content of the cdd item with curly braces
or also the key?
+ the weighted URL as array has a different js encoding as other elements
+ Clarify PSS usage (see https://crypto.stackexchange.com/questions/12707/usability-of-padding-scheme-in-blinded-rsa-signature)
## TODO
+ [ ] bigint type for big integers encoded as string
+ [ ] blinding utilizing crypto++
+ Clarify PSS usage (see https://crypto.stackexchange.com/questions/12707/usability-of-padding-scheme-in-blinded-rsa-signature)
### Blinding Notes
@ -27,3 +51,8 @@ https://crypto.stackexchange.com/questions/12707/usability-of-padding-scheme-in-
https://crypto.stackexchange.com/questions/54085/is-there-a-standard-padding-format-for-rsa-blind-signatures/60728#60728
https://crypto.stackexchange.com/questions/5626/rsa-blind-signatures-in-practice
<https://cfrg.github.io/draft-irtf-cfrg-blind-signatures/draft-irtf-cfrg-rsa-blind-signatures.html>
alternative implementation using openssl
https://github.com/jedisct1/blind-rsa-signatures

104
src/big_int.cpp Normal file
View File

@ -0,0 +1,104 @@
#include "big_int.hpp"
#include "tl/expected.hpp"
#include <charconv>
#pragma clang diagnostic push
#pragma ide diagnostic ignored "clion-misra-cpp2008-6-4-5"
inline uint8_t hex(char c) {
switch(c) {
case '0': return 0U;
case '1': return 1U;
case '2': return 2U;
case '3': return 3U;
case '4': return 4U;
case '5': return 5U;
case '6': return 6U;
case '7': return 7U;
case '8': return 8U;
case '9': return 9U;
case 'a': return 10U;
case 'b': return 11U;
case 'c': return 12U;
case 'd': return 13U;
case 'e': return 14U;
case 'f': return 15U;
case 'A': return 10U;
case 'B': return 11U;
case 'C': return 12U;
case 'D': return 13U;
case 'E': return 14U;
case 'F': return 15U;
default:
return 0xffU;
}
}
#pragma clang diagnostic pop
tl::expected<BigInt,BigInt::eError>
BigInt::from_string(const std::string& str) {
BigInt b;
uint8_t hval=0U;
uint8_t nibble;
size_t i = str.size()+1U;
for(auto c : str) {
nibble = hex(c);
if (nibble ==0xFFU) {
return tl::make_unexpected(eError::PARSE_ERROR);
}
if ( i%2U != 0U ) {
hval = nibble << 4U;
} else {
hval |= nibble;
b.data[256U - (i/2U)]= hval;
hval = 0U;
}
i--;
}
return b;
}
BigInt BigInt::from_int(uint64_t value)
{
BigInt b;
b.data[248U]=static_cast<uint8_t>(value >> 56 & 0xffU);
b.data[249U]=static_cast<uint8_t>(value >> 48 & 0xffU);
b.data[250U]=static_cast<uint8_t>(value >> 40U & 0xffU);
b.data[251U]=static_cast<uint8_t>(value >> 32U & 0xffU);
b.data[252U]=static_cast<uint8_t>(value >> 24U & 0xffU);
b.data[253U]=static_cast<uint8_t>(value >> 16U & 0xffU);
b.data[254U]=static_cast<uint8_t>(value >> 8U & 0xffU);
b.data[255U]=static_cast<uint8_t>(value & 0xffU);
return b;
}
constexpr char hex_char [] = "0123456789abcdef";
std::string BigInt::to_string() const
{
std::string s;
uint8_t b;
uint8_t first_digit = 0U;
for (size_t i = 0U; i<256U;i++) {
b = data[i];
if (first_digit==0U) {
if (b==0U) {
continue;
} else if (b>0xfU) {
s.push_back(hex_char[b >> 4]);
} else {
/* nothing to do here */
}
s.push_back(hex_char[b & 0xf]);
first_digit = b;
} else {
s.push_back(hex_char[b >> 4]);
s.push_back(hex_char[b & 0xf]);
}
}
return s;
}
bool operator == (const BigInt& rhs, const BigInt& lhs)
{ return rhs.data == lhs.data; }

27
src/big_int.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef OC_ISSUER_BIG_INT_HPP
#define OC_ISSUER_BIG_INT_HPP
#include <string>
#include <array>
#include "tl/expected.hpp"
struct BigInt {
BigInt() : data() {}
virtual ~BigInt() = default;
enum class eError : uint8_t { PARSE_ERROR };
static tl::expected<BigInt, eError> from_string(const std::string &str);
static BigInt from_int(uint64_t value);
[[nodiscard]] std::string to_string() const;
friend bool operator == (const BigInt& rhs, const BigInt& lhs);
private:
std::array<uint8_t,256U> data;
};
bool operator==(const BigInt &rhs, const BigInt &lhs);
#endif // #ifndef #ifndef OC_ISSUER_BIG_INT_HPP

474
src/json_serialisation.cpp Normal file
View File

@ -0,0 +1,474 @@
#include "model.hpp"
#include "crow/json.h"
#include "tl/expected.hpp"
#define TO_JSON(name) r[#name]=name
#define BIGINT_TO_JSON(name) r[#name]=name.to_string()
#define TO_JSON_JSON(name) r[#name]=name.to_json()
#define TO_JSON_ARRAY(name) r[#name]=list_to_json(name)
template <class T>
crow::json::wvalue list_to_json(const std::vector<T> &array) {
crow::json::wvalue::list l;
for (const auto& item : array) {
l.push_back(item.to_json());
}
return {l};
}
crow::json::wvalue list_to_json(const std::vector<unsigned int> &array) {
crow::json::wvalue::list l;
for (auto item : array) {
l.emplace_back(item);
}
return {l};
}
crow::json::wvalue PublicKey::to_json() const {
crow::json::wvalue r;
BIGINT_TO_JSON(modulus);
BIGINT_TO_JSON(public_exponent);
r["type"]="rsa public key";
return r;
}
crow::json::wvalue WeightedUrl::to_json() const {
crow::json::wvalue::list l;
crow::json::wvalue w(weight);
l.push_back(w);
l.emplace_back(url);
return l;
}
crow::json::wvalue CDD::to_json() const {
crow::json::wvalue r;
TO_JSON(additional_info);
TO_JSON(cdd_expiry_date);
TO_JSON(cdd_location);
TO_JSON(cdd_serial);
TO_JSON(cdd_signing_date);
TO_JSON(currency_divisor);
TO_JSON(currency_name);
TO_JSON_ARRAY(denominations);
BIGINT_TO_JSON(id);
TO_JSON_ARRAY(info_service);
TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key);
TO_JSON_ARRAY(mint_service);
TO_JSON(protocol_version);
TO_JSON_ARRAY(redeem_service);
TO_JSON_ARRAY(renew_service);
r["type"] = "cdd";
return r;
}
crow::json::wvalue CDDC::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(cdd);
TO_JSON(signature);
r["type"] = "cdd certificate";
return r;
}
crow::json::wvalue MintKey::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_serial);
TO_JSON(coins_expiry_date);
TO_JSON(denomination);
BIGINT_TO_JSON(id);
BIGINT_TO_JSON(issuer_id);
TO_JSON_JSON(public_mint_key);
TO_JSON(sign_coins_not_after);
TO_JSON(sign_coins_not_before);
r["type"] = "mint key";
return r;
}
crow::json::wvalue MintKeyCert::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(mint_key);
TO_JSON(signature);
r["type"] = "mint key certificate";
return r;
}
crow::json::wvalue Response::to_json() const {
crow::json::wvalue r;
TO_JSON(message_reference);
TO_JSON(status_code);
TO_JSON(status_description);
return r;
}
crow::json::wvalue ResponseCDDCSerial::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial);
r["type"] = "response cdd serial";
return r;
}
tl::expected<RequestCDDCSerial, eError>
RequestCDDCSerial::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!json.has("type") || !json.has("message_reference")) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "request cdd serial") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestCDDCSerial r;
r.message_reference = json["message_reference"].u();
return r;
}
}
crow::json::wvalue ResponseCDDC::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_JSON(cddc);
r["type"] = "response cdd serial";
return r;
}
tl::expected<RequestCDDC, eError>
RequestCDDC::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("cdd_serial"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request cddc") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestCDDC r;
r.cdd_serial = json["cdd_serial"].u();
r.message_reference = json["message_reference"].u();
return r;
}
}
tl::expected<RequestMKCs, eError>
RequestMKCs::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("denominations") && json.has("message_reference") &&
json.has("mint_key_ids") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request mint key certificates") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestMKCs r;
r.message_reference = json["message_reference"].u();
auto denominations = json["denominations"];
if (denominations.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
for (const auto& d : denominations.lo()) {
r.denominations.push_back(d.u());
}
}
auto mint_key_ids = json["mint_key_ids"];
if (mint_key_ids.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
for (const auto& k: mint_key_ids.lo()) {
auto kv = BigInt::from_string(k.s());
if (!kv.has_value()) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
r.mint_key_ids.push_back(*kv);
}
}
}
return r;
}
}
crow::json::wvalue ResponseMKCs::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(keys);
r["type"] = "response mint key certificates";
return r;
}
crow::json::wvalue Blind::to_json() const {
crow::json::wvalue r;
BIGINT_TO_JSON(blinded_payload_hash);
BIGINT_TO_JSON(mint_key_id);
TO_JSON(reference);
r["type"] = "blinded payload hash";
return r;
}
tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
if (!(json.has("type")
&& json.has("blinded_payload_hash")
&& json.has("mint_key_id")
&& json.has("reference"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "blinded payload hash") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
Blind r;
auto hash = BigInt::from_string(json["blinded_payload_hash"].s());
if (!hash) {
// std::cout << "invalid hash : " << json["blinded_payload_hash"].s()
// << std::endl;
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
}
r.blinded_payload_hash = hash.value();
auto key_id = BigInt::from_string(json["mint_key_id"].s());
if (!key_id) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
}
r.mint_key_id = key_id.value();
r.reference = json["reference"].s();
return r;
}
}
crow::json::wvalue BlindSignature::to_json() const {
crow::json::wvalue r;
BIGINT_TO_JSON(blind_signature);
TO_JSON(reference);
r["type"] = "blind signature";
return r;
}
tl::expected<RequestMint, eError>
RequestMint::from_string(const std::string &str) {
std::vector<Blind> blinds;
// "type": "request mint"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("transaction_reference") && json.has("blinds"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "request mint") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestMint r;
r.message_reference= json["message_reference"].u();
auto tr = BigInt::from_string(json["transaction_reference"].s());
if (!tr)
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
r.transaction_reference = *tr;
if (json["blinds"].t()!=crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
}
for (const auto& item : json["blinds"]) {
auto b = Blind::from_json(item);
if (!b.has_value()) {
return tl::make_unexpected(b.error());
} else {
r.blinds.push_back(b.value());
}
}
return r;
}
}
crow::json::wvalue ResponseMint::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(blind_signatures);
r["type"] = "response mint";
return r;
}
crow::json::wvalue Coin::Payload::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_location);
TO_JSON(denomination);
BIGINT_TO_JSON(issuer_id);
BIGINT_TO_JSON(mint_key_id);
TO_JSON(protocol_version);
BIGINT_TO_JSON(serial);
r["type"] = "payload";
return r;
}
crow::json::wvalue Coin::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(payload);
TO_JSON(signature);
r["type"] = "coin";
return r;
}
tl::expected<Coin::Payload, eError>
Coin::Payload::from_json(const crow::json::rvalue &json) {
if (!(json.has("cdd_location") && json.has("denomination") &&
json.has("issuer_id") && json.has("mint_key_id") &&
json.has("protocol_version") && json.has("serial") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "payload") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
Coin::Payload payload;
payload.cdd_location = json["cdd_location"].s();
payload.denomination = json["denomination"].u();
auto id = BigInt::from_string(json["issuer_id"].s());
if (!id)
tl::make_unexpected(eError::JSON_PARSE_ERROR);
payload.issuer_id = *id;
id = BigInt::from_string(json["mint_key_id"].s());
if (!id)
tl::make_unexpected(eError::JSON_PARSE_ERROR);
payload.mint_key_id = *id;
payload.protocol_version = json["protocol_version"].s();
id = BigInt::from_string(json["serial"].s());
if (!id)
tl::make_unexpected(eError::JSON_PARSE_ERROR);
payload.serial = *id;
return payload;
}
}
tl::expected<Coin, eError> Coin::from_json(const crow::json::rvalue &json) {
if (!(json.has("type") && json.has("payload") && json.has("signature"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "coin") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
auto pl = Payload::from_json(json["payload"]);
if (!pl.has_value()) {
return tl::make_unexpected(pl.error());
} else {
Coin c;
c.payload = pl.value();
c.signature = json["signature"].s();
return c;
}
}
}
crow::json::wvalue CoinStack::to_json() const {
crow::json::wvalue r;
TO_JSON_ARRAY(coins);
TO_JSON(subject);
r["type"] = "coinstack";
return r;
}
tl::expected<RequestRenew, eError>
RequestRenew::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("blinds") && json.has("coins") &&
json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request renew") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else if ((json["coins"].t() != crow::json::type::List) ||
(json["blinds"].t() != crow::json::type::List)) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} else {
RequestRenew r;
for (const auto& item : json["coins"]) {
auto coin = Coin::from_json(item);
if (!coin.has_value()) {
return tl::make_unexpected(coin.error());
} else {
r.coins.push_back(coin.value());
}
}
for (const auto& item : json["blinds"]) {
auto blind = Blind::from_json(item);
if (!blind.has_value()) {
return tl::make_unexpected(blind.error());
} else {
r.blinds.push_back(blind.value());
}
}
r.message_reference = json["message_reference"].u();
r.transaction_reference = json["transaction_reference"].s();
return r;
}
}
crow::json::wvalue ResponseDelay::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"] = "response delay";
return r;
}
tl::expected<RequestResume, eError>
RequestResume::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request resume") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestResume r;
r.message_reference = json["message_reference"].u();
auto tr = BigInt::from_string(json["transaction_reference"].s());
if (!tr)
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
r.transaction_reference = *tr;
return r;
}
}
tl::expected<RequestRedeem, eError>
RequestRedeem::from_string(const std::string &str) {
// "type":
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("coins") && json.has("message_reference") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request redeem") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestRedeem r;
r.message_reference = json["message_reference"].u();
if (json["coins"].t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
}
for (const auto& item : json["coins"]) {
auto coin = Coin::from_json(item);
if (!coin.has_value()) {
return tl::make_unexpected(coin.error());
} else {
r.coins.push_back(coin.value());
}
}
return r;
}
}
crow::json::wvalue ResponseRedeem::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"] = "response redeem";
return r;
}

View File

@ -1,22 +1,22 @@
#include "crow.h"
#include "crow/common.h"
#include "crow/http_parser_merged.h"
#include "crow/http_response.h"
#include "model.hpp"
int main() {
crow::SimpleApp app;
std::shared_ptr<Model> model = Model::getModel("simple");
std::shared_ptr<Model> model = Model::get_model("simple");
CROW_ROUTE(app, "/cddc")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &req) {
.methods(crow::HTTPMethod::POST)
([&model](const crow::request &req) {
auto req_cddc = RequestCDDC::from_string(req.body);
if (!req_cddc) {
return crow::response(crow::status::BAD_REQUEST);
} else {
ResponseCDDC res;
res.message_reference = req_cddc->message_reference;
auto cddc = model->getCDDC(req_cddc->cdd_serial);
auto cddc = model->get_cddc(req_cddc->cdd_serial);
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
@ -29,14 +29,14 @@ int main() {
CROW_ROUTE(app, "/cddc/serial")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestCDDSerial::from_string(request.body);
auto req = RequestCDDCSerial::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
ResponseCDDSerial res;
ResponseCDDCSerial res;
res.message_reference = req->message_reference;
auto cddc = model->getCurrentCDDC();
auto cddc = model->get_current_cddc();
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
@ -47,8 +47,8 @@ int main() {
}
});
CROW_ROUTE(app, "/mkcs")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
CROW_ROUTE(app, "/mkcs").methods(crow::HTTPMethod::POST)
([&model](const crow::request &request) {
auto req = RequestMKCs::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
@ -61,8 +61,8 @@ int main() {
}
});
CROW_ROUTE(app, "/mint")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::POST)
([&model](const crow::request &request) {
auto req = RequestMint::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
@ -70,7 +70,9 @@ int main() {
ResponseMint res;
res.message_reference = req->message_reference;
auto minted = model->mint(req->transaction_reference, req->blinds);
/// \todo change argument transaction_reference to bigint
auto minted =
model->mint(req->transaction_reference.to_string(), req->blinds);
res.blind_signatures = minted;
res.status_code = crow::status::OK;

View File

@ -1,484 +1,56 @@
#include "model.hpp"
#include "crow/json.h"
#define TO_JSON(name) r[#name] = name
#define TO_JSON_JSON(name) r[#name] = name.to_json()
#define TO_JSON_ARRAY(name) r[#name] = list_to_json(name)
template <class T>
crow::json::wvalue list_to_json(const std::vector<T> &array) {
crow::json::wvalue::list l;
for (auto item : array)
l.push_back(item.to_json());
return crow::json::wvalue(l);
}
crow::json::wvalue list_to_json(const std::vector<unsigned int> &array) {
crow::json::wvalue::list l;
for (auto item : array)
l.push_back(item);
return crow::json::wvalue(l);
}
crow::json::wvalue PublicKey::to_json() const {
crow::json::wvalue r;
TO_JSON(modulus);
TO_JSON(public_exponent);
r["type"] = "rsa public key";
return r;
}
crow::json::wvalue WeightedUrl::to_json() const {
crow::json::wvalue::list l;
crow::json::wvalue w(weight);
l.push_back(w);
l.push_back(url);
return l;
}
crow::json::wvalue CDD::to_json() const {
crow::json::wvalue r;
TO_JSON(additional_info);
TO_JSON(cdd_expiry_date);
TO_JSON(cdd_location);
TO_JSON(cdd_serial);
TO_JSON(cdd_signing_date);
TO_JSON(currency_divisor);
TO_JSON(currency_name);
TO_JSON_ARRAY(denominations);
TO_JSON(id);
TO_JSON_ARRAY(info_service);
TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key);
TO_JSON_ARRAY(mint_service);
TO_JSON(protocol_version);
TO_JSON_ARRAY(redeem_service);
TO_JSON_ARRAY(renew_service);
r["type"] = "cdd";
return r;
}
crow::json::wvalue CDDC::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(cdd);
TO_JSON(signature);
r["type"] = "cdd certificate";
return r;
}
crow::json::wvalue MintKey::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_serial);
TO_JSON(coins_expiry_date);
TO_JSON(denomination);
TO_JSON(id);
TO_JSON(issuer_id);
TO_JSON_JSON(public_mint_key);
TO_JSON(sign_coins_not_after);
TO_JSON(sign_coins_not_before);
r["type"] = "mint key";
return r;
}
crow::json::wvalue MintKeyCert::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(mint_key);
TO_JSON(signature);
r["type"] = "mint key certificate";
return r;
}
crow::json::wvalue Response::to_json() const {
crow::json::wvalue r;
TO_JSON(message_reference);
TO_JSON(status_code);
TO_JSON(status_description);
return r;
}
crow::json::wvalue ResponseCDDSerial::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial);
r["type"] = "response cdd serial";
return r;
}
tl::expected<RequestCDDSerial, eError>
RequestCDDSerial::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!json.has("type") || !json.has("message_reference")) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "request cdd serial") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestCDDSerial r;
r.message_reference = json["message_reference"].u();
return r;
}
}
crow::json::wvalue ResponseCDDC::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_JSON(cddc);
r["type"] = "response cdd serial";
return r;
}
tl::expected<RequestCDDC, eError>
RequestCDDC::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("cdd_serial"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request cddc") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestCDDC r;
r.cdd_serial = json["cdd_serial"].u();
r.message_reference = json["message_reference"].u();
return r;
}
}
tl::expected<RequestMKCs, eError>
RequestMKCs::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("denominations") && json.has("message_reference") &&
json.has("mint_key_ids") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request mint key certificates") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestMKCs r;
r.message_reference = json["message_reference"].u();
auto denominations = json["denominations"];
if (denominations.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
for (auto d : denominations.lo()) {
r.denominations.push_back(d.u());
}
}
auto mint_key_ids = json["mint_key_ids"];
if (mint_key_ids.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
for (auto k : mint_key_ids.lo()) {
r.mint_key_ids.push_back(k.u());
}
}
return r;
}
}
crow::json::wvalue ResponseMKCs::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(keys);
r["type"] = "response mint key certificates";
return r;
}
crow::json::wvalue Blind::to_json() const {
crow::json::wvalue r;
TO_JSON(blinded_payload_hash);
TO_JSON(mint_key_id);
TO_JSON(reference);
r["type"] = "blinded payload hash";
return r;
}
tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
if (!(json.has("type") && json.has("blinded_payload_hash") &&
json.has("mint_key_id") && json.has("reference"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "blinded payload hash") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
Blind r;
r.blinded_payload_hash = json["blinded_payload_hash"].s();
r.mint_key_id = json["mint_key_id"].s();
r.reference = json["reference"].s();
return r;
}
}
crow::json::wvalue BlindSignature::to_json() const {
crow::json::wvalue r;
TO_JSON(blind_signature);
TO_JSON(reference);
r["type"] = "blind signature";
return r;
}
tl::expected<RequestMint, eError>
RequestMint::from_string(const std::string &str) {
std::vector<Blind> blinds;
// "type": "request mint"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("transaction_reference") && json.has("blinds"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "request mint") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestMint r;
r.message_reference = json["message_reference"].u();
r.transaction_reference = json["transaction_reference"].s();
if (json["blinds"].t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
}
for (auto item : json["blinds"]) {
auto b = Blind::from_json(item);
if (!b.has_value()) {
return tl::make_unexpected(b.error());
} else {
r.blinds.push_back(b.value());
}
}
return r;
}
}
crow::json::wvalue ResponseMint::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(blind_signatures);
r["type"] = "response mint";
return r;
}
crow::json::wvalue Coin::Payload::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_location);
TO_JSON(denomination);
TO_JSON(issuer_id);
TO_JSON(mint_key_id);
TO_JSON(protocol_version);
TO_JSON(serial);
r["type"] = "payload";
return r;
}
crow::json::wvalue Coin::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(payload);
TO_JSON(signature);
r["type"] = "coin";
return r;
}
tl::expected<Coin::Payload, eError>
Coin::Payload::from_json(const crow::json::rvalue &json) {
if (!(json.has("cdd_location") && json.has("denomination") &&
json.has("issuer_id") && json.has("mint_key_id") &&
json.has("protocol_version") && json.has("serial") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "payload") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
Coin::Payload payload;
payload.cdd_location = json["cdd_location"].s();
payload.denomination = json["denomination"].u();
payload.issuer_id = json["issuer_id"].s();
payload.mint_key_id = json["mint_key_id"].s();
payload.protocol_version = json["protocol_version"].s();
payload.serial = json["serial"].s();
return payload;
}
}
tl::expected<Coin, eError> Coin::from_json(const crow::json::rvalue &json) {
if (!(json.has("type") && json.has("payload") && json.has("signature"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "coin") {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
auto pl = Payload::from_json(json["payload"]);
if (!pl.has_value()) {
return tl::make_unexpected(pl.error());
} else {
Coin c;
c.payload = pl.value();
c.signature = json["signature"].s();
return c;
}
}
}
crow::json::wvalue CoinStack::to_json() const {
crow::json::wvalue r;
TO_JSON_ARRAY(coins);
TO_JSON(subject);
r["type"] = "coinstack";
return r;
}
tl::expected<RequestRenew, eError>
RequestRenew::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("blinds") && json.has("coins") &&
json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request renew") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else if ((json["coins"].t() != crow::json::type::List) ||
(json["blinds"].t() != crow::json::type::List)) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} else {
RequestRenew r;
for (auto item : json["coins"]) {
auto coin = Coin::from_json(item);
if (!coin.has_value()) {
return tl::make_unexpected(coin.error());
} else {
r.coins.push_back(coin.value());
}
}
for (auto item : json["blinds"]) {
auto blind = Blind::from_json(item);
if (!blind.has_value()) {
return tl::make_unexpected(blind.error());
} else {
r.blinds.push_back(blind.value());
}
}
r.message_reference = json["message_reference"].u();
r.transaction_reference = json["transaction_reference"].s();
return r;
}
}
crow::json::wvalue ResponseDelay::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"] = "response delay";
return r;
}
tl::expected<RequestResume, eError>
RequestResume::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request resume") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestResume r;
r.message_reference = json["message_reference"].u();
r.transaction_reference = json["transaction_reference"].s();
return r;
}
}
tl::expected<RequestRedeem, eError>
RequestRedeem::from_string(const std::string &str) {
// "type":
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if (!(json.has("coins") && json.has("message_reference") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request redeem") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else {
RequestRedeem r;
r.message_reference = json["message_reference"].u();
if (json["coins"].t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
}
for (auto item : json["coins"]) {
auto coin = Coin::from_json(item);
if (!coin.has_value()) {
return tl::make_unexpected(coin.error());
} else {
r.coins.push_back(coin.value());
}
}
return r;
}
}
crow::json::wvalue ResponseRedeem::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"] = "response redeem";
return r;
}
#include "tl/expected.hpp"
using std::cout;
using std::endl;
#define UNUSED(s) /* s */
/** this is for now our sample model */
class DefaultModel : public Model {
public:
DefaultModel() {}
tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) override {
cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl;
return &m_cddc;
};
DefaultModel() = default;
tl::expected<CDDC *, bool> getCurrentCDDC() override {
cout << __FUNCTION__ << "()" << endl;
tl::expected<CDDC *, bool> get_cddc(unsigned int cdd_serial) override {
cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl; // NOLINT(clion-misra-cpp2008-5-2-12)
return &m_cddc;
};
return &m_cddc;
}
tl::expected<CDDC *, bool> get_current_cddc() override {
cout << __FUNCTION__ << "()" << endl;
std::vector<BlindSignature> mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) override {
std::vector<BlindSignature> res;
cout << __FUNCTION__ << "("
<< ")" << endl;
return &m_cddc;
}
return res;
}
std::vector<BlindSignature>
mint(const std::string & UNUSED(transaction_reference),
const std::vector<Blind> &UNUSED(blinds)) override {
std::vector<BlindSignature> res;
cout << __FUNCTION__ << "("
<< ")" << endl;
const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) override {
std::vector<MintKeyCert> res;
cout << __FUNCTION__ << endl;
return res;
}
bool redeem(const std::vector<Coin> &coins) override {
cout << __FUNCTION__ << endl;
return false;
}
return res;
}
std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> & UNUSED(denominations),
const std::vector<BigInt> & UNUSED(mint_key_ids)) override {
std::vector<MintKeyCert> res;
cout << __FUNCTION__ << endl;
return res;
}
bool redeem(const std::vector<Coin> &UNUSED(coins)) override {
cout << __FUNCTION__ << endl;
return false;
}
private:
CDDC m_cddc;
CDDC m_cddc;
};
std::unique_ptr<Model> Model::getModel(const std::string & /*backend_name*/) {
cout << __FUNCTION__ << endl;
//:wq
// if (backend_name=="default")
return std::make_unique<DefaultModel>();
std::unique_ptr<Model> Model::get_model(const std::string & /*backend_name*/) {
cout << __FUNCTION__ << endl;
//:wq
// if (backend_name=="default")
return std::make_unique<DefaultModel>();
}

View File

@ -1,5 +1,5 @@
#ifndef MODEL_HPP
#define MODEL_HPP
#ifndef OC_ISSUER_MODEL_HPP
#define OC_ISSUER_MODEL_HPP
#include <chrono>
#include <memory>
@ -9,76 +9,84 @@
#include "crow/json.h"
#include "tl/expected.hpp"
#include "big_int.hpp"
struct PublicKey {
std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
std::string public_exponent; //: 65537,
BigInt modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
BigInt public_exponent;//: 65537,
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct WeightedUrl {
uint32_t weight;
std::string url;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
/** currency description document */
/** @brief currency description document
*
* this structure describes a currency based on opencoin protocol.
* It can be converted to json to provide the specified currency
* description document.
*/
struct CDD {
std::string additional_info;
time_t cdd_expiry_date; //: 2023-07-22T15:45:53.164685
std::string cdd_location; //: https://opencent.org,
size_t cdd_serial; //: 1,
time_t cdd_signing_date; //: 2022-07-22T15:45:53.164685,
size_t currency_divisor; //: 100,
std::string currency_name; //: OpenCent,
std::vector<unsigned> denominations; //: [1, 2, 5],
std::string id; //: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
time_t cdd_expiry_date; /// expiry date of this document (e.g.
///2023-07-22T15:45:53.164685)
std::string cdd_location; /// URL of location of this document (e.g
///https://opencent.org)
size_t cdd_serial; /// serial number of currency description document
time_t cdd_signing_date; /// date of signing this document (e.g.
///2022-07-22T15:45:53.164685)
size_t currency_divisor; /// divisor used for coins of this currency
std::string currency_name; /// name of the currency (e.g. OpenCent)
std::vector<unsigned>
denominations; /// the available denominations of this currency
BigInt id; /// an identity for this currency
std::vector<WeightedUrl> info_service;
/* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82,
PublicKey
issuer_public_master_key; //: {
// modulus:
// daaa63ddda38c189b8c49020c8276adbe0a695685a...,
// public_exponent: 65537,
// type: rsa public key
//},
/* eCipherSuite*/
std::string issuer_cipher_suite; /// the cipher suite used for this currencey
/// (currently only RSA-SHA256-PSS-CHAUM82
/// is supported)
PublicKey issuer_public_master_key; /// the public key of this currency
std::vector<WeightedUrl> mint_service;
std::string protocol_version; //: https://opencoin.org/1.0,
std::string protocol_version; // e.g. https://opencoin.org/1.0
std::vector<WeightedUrl> redeem_service;
std::vector<WeightedUrl> renew_service;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct CDDC {
CDD cdd;
std::string signature;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct MintKey {
unsigned int cdd_serial;
std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685",
unsigned int denomination; //": 1,
std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96",
std::string issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2",
unsigned int denomination; //": 1,
BigInt id; // "1ceb977bb531c65f133ab8b0d60862b17369d96",
BigInt issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2",
PublicKey public_mint_key;
std::string sign_coins_not_after;
std::string sign_coins_not_before;
// "type": "mint key"
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct MintKeyCert {
MintKey mint_key;
std::string signature;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
enum class eError {
@ -95,20 +103,20 @@ struct Response {
unsigned int status_code;
std::string status_description;
virtual crow::json::wvalue to_json() const;
[[nodiscard]] virtual crow::json::wvalue to_json() const=0;
};
struct RequestCDDSerial {
struct RequestCDDCSerial {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
static tl::expected<RequestCDDSerial, eError>
static tl::expected<RequestCDDCSerial, eError>
from_string(const std::string &str);
};
struct ResponseCDDSerial : Response {
unsigned int cdd_serial;
struct ResponseCDDCSerial : Response {
unsigned int cdd_serial{0U};
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
struct RequestCDDC {
@ -121,14 +129,14 @@ struct RequestCDDC {
struct ResponseCDDC : Response {
CDDC cddc;
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
struct RequestMKCs {
std::vector<unsigned int> denominations;
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
std::vector<unsigned int> mint_key_ids;
std::vector<BigInt> mint_key_ids;
// "type": "request mint key certificates"
static tl::expected<RequestMKCs, eError> from_string(const std::string &str);
};
@ -136,27 +144,27 @@ struct RequestMKCs {
struct ResponseMKCs : Response {
std::vector<MintKeyCert> keys;
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
struct Blind {
std::string blinded_payload_hash; // bigint
std::string mint_key_id; // bigint
BigInt blinded_payload_hash; //bigint
BigInt mint_key_id; //bigint
std::string reference;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
static tl::expected<Blind, eError> from_json(const crow::json::rvalue &json);
};
struct BlindSignature {
std::string blind_signature;
BigInt blind_signature;
std::string reference;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct RequestMint {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
std::string transaction_reference;
BigInt transaction_reference;
std::vector<Blind> blinds;
// "type": "request mint"
static tl::expected<RequestMint, eError> from_string(const std::string &str);
@ -165,27 +173,26 @@ struct RequestMint {
struct ResponseMint : Response {
std::vector<BlindSignature> blind_signatures;
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
struct Coin {
struct Payload {
std::string cdd_location;
unsigned int denomination;
std::string issuer_id;
std::string mint_key_id;
BigInt issuer_id;
BigInt mint_key_id;
std::string protocol_version;
std::string serial;
BigInt serial;
crow::json::wvalue to_json() const;
static tl::expected<Payload, eError>
from_json(const crow::json::rvalue &json);
[[nodiscard]] crow::json::wvalue to_json() const;
static tl::expected<Payload,eError> from_json(const crow::json::rvalue& json);
};
Payload payload;
std::string signature;
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
static tl::expected<Coin, eError> from_json(const crow::json::rvalue &json);
};
@ -193,7 +200,7 @@ struct CoinStack {
std::vector<Coin> coins;
std::string subject;
// "type": "coinstack"
crow::json::wvalue to_json() const;
[[nodiscard]] crow::json::wvalue to_json() const;
};
struct RequestRenew {
@ -207,13 +214,13 @@ struct RequestRenew {
};
struct ResponseDelay : Response {
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
struct RequestResume {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
std::string transaction_reference;
BigInt transaction_reference;
// "type": "request resume"
static tl::expected<RequestResume, eError>
from_string(const std::string &str);
@ -229,28 +236,73 @@ struct RequestRedeem {
};
struct ResponseRedeem : Response {
crow::json::wvalue to_json() const override;
[[nodiscard]] crow::json::wvalue to_json() const override;
};
class Model {
public:
virtual ~Model(){};
virtual ~Model()=default;
virtual tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) = 0;
virtual tl::expected<CDDC *, bool> getCurrentCDDC() = 0;
/**
* return the CurrencyDocumentDescription certifikate for a specific
* serial version number of it.
* [see spec](https://opencoin.org/0.4/schemata.html#cddc)
* @return returns a pointer to the CDDC if successful, false otherwise
*/
virtual tl::expected<CDDC *, bool> get_cddc(unsigned int cdd_serial) = 0;
virtual const std::vector<MintKeyCert>
/**
* return the CurrencyDocumentDescription certifikate
* [see spec](https://opencoin.org/0.4/schemata.html#cddc)
* @return returns a pointer to the CDDC if successful, false otherwise
*/virtual tl::expected<CDDC *, bool> get_current_cddc() = 0;
/**
* return the MintKey certificates for a given list of denominations
* and mint key ids
*
* @param denominations
* @param mint_key_ids
*
* @return mint key certificates for given denominations and mint_key_ids
*/
virtual std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) = 0;
const std::vector<BigInt> &mint_key_ids) = 0;
/**
* returns the vector of blind signatures for a given vector of blinds
*
* @param transaction_reference reference to a transaction (send from client)
* @param blinds the vector of blinds to sign
*
* @return
*/
virtual std::vector<BlindSignature>
mint(const std::string &transaction_reference,
mint(std::string const& transaction_reference,
const std::vector<Blind> &blinds) = 0;
/**
* redeem valid coins into real money
*
* @param coins the coins to redeem
*
* @return true if successful, false on error
*/
virtual bool redeem(const std::vector<Coin> &coins) = 0;
static std::unique_ptr<Model> getModel(const std::string &backend_name);
/**
* factory function returning a concrete backend for Opencoin API handling.
* based on backend_name a concrete backend will be returned
* or in case of error null.
* @param backend_name
*
* @return pointer to backend instance or null on invalid backend name
*/
static std::unique_ptr<Model> get_model(const std::string &backend_name);
private:
};
#endif // #ifndef MODEL_HPP
#endif // #ifndef OC_ISSUER_MODEL_HPP

View File

@ -2,43 +2,45 @@
#include "model.hpp"
#include <catch2/catch_test_macros.hpp>
TEST_CASE("PublicKey::to_json", "[to_json]") {
PublicKey k{"daaa63ddda38c189b8c49020c8276adbe0a695685a...", "65537"};
TEST_CASE( "PublicKey::to_json", "[to_json]" ) {
PublicKey k //{ BigInt::from_string("daaa63ddda38c189b8c49020c8276adbe0a695685a...").value(),
// BigInt::from_int(65537UL) };
;
auto json = k.to_json();
REQUIRE(json["modulus"].dump() == "\"" + k.modulus + "\"");
REQUIRE(json["public_exponent"].dump() == "\"" + k.public_exponent + "\"");
REQUIRE(json["type"].dump() == "\"rsa public key\"");
REQUIRE(json.keys().size() == 3);
REQUIRE( json["modulus"].dump() == "\"" + k.modulus.to_string() + "\"" );
REQUIRE( json["public_exponent"].dump() == "\"" + k.public_exponent.to_string()+"\"" );
REQUIRE( json["type"].dump() == "\"rsa public key\"" );
REQUIRE( json.keys().size() == 3 );
}
TEST_CASE("RequestCDDSerial::from_string", "[from_string]") {
TEST_CASE("RequestCDDCSerial::from_string", "[from_string]") {
// good case
std::string good = "{"
"\"message_reference\": 100000,"
"\"type\": \"request cdd serial\""
"}";
auto res = RequestCDDSerial::from_string(good);
auto res = RequestCDDCSerial::from_string(good);
REQUIRE(res.has_value() == true);
REQUIRE(res->message_reference == 100000);
// bad cases
res = RequestCDDSerial::from_string("");
res = RequestCDDCSerial::from_string("");
REQUIRE(res.has_value() == false);
// invalid type
res = RequestCDDSerial::from_string("{"
"\"message_reference\": 100000,"
"\"type\": \"request something wrong\""
"}");
res = RequestCDDCSerial::from_string("{"
"\"message_reference\": 100000,"
"\"type\": \"request something wrong\""
"}");
REQUIRE(res.has_value() == false);
// invalid attribute name
res = RequestCDDSerial::from_string("{"
"\"x_message_reference\": 100000,"
"}");
res = RequestCDDCSerial::from_string("{"
"\"x_message_reference\": 100000,"
"}");
REQUIRE(res.has_value() == false);
}
@ -76,15 +78,14 @@ TEST_CASE("RequestCDDC::from_string", "[from_string]") {
TEST_CASE("RequestMKCs::from_string", "[from_string]") {
// good case
auto res =
RequestMKCs::from_string("{"
"\"denominations\": [1, 2, 5],"
"\"message_reference\": 100002,"
"\"mint_key_ids\": [],"
"\"type\": \"request mint key certificates\""
"}");
const std::vector<uint32_t> EXPECTED_DENOMINATIONS = {1, 2, 5};
const std::vector<uint32_t> EXPECTED_MINT_KEY_IDS = {};
auto res = RequestMKCs::from_string( "{"
"\"denominations\": [1, 2, 5],"
"\"message_reference\": 100002,"
"\"mint_key_ids\": [],"
"\"type\": \"request mint key certificates\""
"}");
const std::vector<uint32_t> EXPECTED_DENOMINATIONS = {1,2,5};
const std::vector<BigInt> EXPECTED_MINT_KEY_IDS = {};
REQUIRE(res.has_value() == true);
REQUIRE(res->denominations == EXPECTED_DENOMINATIONS);
@ -129,62 +130,59 @@ TEST_CASE("Blind::from_json", "[from_string]") {
auto good = crow::json::load(
"{"
"\"blinded_payload_hash\": "
"\"924edb672c3345492f38341ff86b57181da4c673ef...\","
"\"924edb672c3345492f38341ff86b57181da4c673efd5fd76c0f64e369974c678f89ffcb1c5f77bf04911a9a63831b52675b70c06548a4d945ec5dd1d825ab08\","
"\"mint_key_id\": \"1ceb977bb531c65f133ab8b0d60862b17369d96\","
"\"reference\": \"a0\","
"\"type\": \"blinded payload hash\""
"}");
auto res = Blind::from_json(good);
REQUIRE(res.has_value() == true);
REQUIRE(res->blinded_payload_hash ==
"924edb672c3345492f38341ff86b57181da4c673ef...");
REQUIRE(res->mint_key_id == "1ceb977bb531c65f133ab8b0d60862b17369d96");
REQUIRE(res->reference == "a0");
// bad cases
// wrong_type["blinded_payload_hash"]=
// "924edb672c3345492f38341ff86b57181da4c673ef..."; wrong_type["mint_key_id"]=
// "1ceb977bb531c65f133ab8b0d60862b17369d96"; wrong_type["reference"] = "a0";
REQUIRE(res.has_value()==true);
REQUIRE(res->blinded_payload_hash.to_string()==
"924edb672c3345492f38341ff86b57181da4c673efd5fd76c0f64e369974c678f89ffcb1c5f77bf04911a9a63831b52675b70c06548a4d945ec5dd1d825ab08");
REQUIRE(res->mint_key_id.to_string()=="1ceb977bb531c65f133ab8b0d60862b17369d96");
REQUIRE(res->reference=="a0");
// bad cases
// wrong_type["blinded_payload_hash"]= "924edb672c3345492f38341ff86b57181da4c673ef...";
// wrong_type["mint_key_id"]= "1ceb977bb531c65f133ab8b0d60862b17369d96";
// wrong_type["reference"] = "a0";
// wrong_type["type"]= "wrong type";
// res = Blind::from_json(wrong_type);
}
TEST_CASE("RequestMint::from_string", "[from_string]") {
// good case
auto res = RequestMint::from_string(
"{"
"\"blinds\": ["
"{"
"\"blinded_payload_hash\": "
"\"924edb672c3345492f38341ff86b57181da4c673ef...\","
"\"mint_key_id\": \"1ceb977bb531c65f133ab8b0d60862b17369d96\","
"\"reference\": \"a0\","
"\"type\": \"blinded payload hash\""
"},"
"{"
"\"blinded_payload_hash\": "
"\"95db92e1c46ebea5edec5e508a831263de6fb78b4c...\","
"\"mint_key_id\": \"f2864e5cd937dbaa4825e73a81062de162143682\","
"\"reference\": \"a1\","
"\"type\": \"blinded payload hash\""
"},"
"{"
"\"blinded_payload_hash\": "
"\"10afac98ac43eb40e996c621d5db4d2238348e3f74...\","
"\"mint_key_id\": \"897a16bf12bd9ba474ef7be0e3a53553a7b4ece8\","
"\"reference\": \"a2\","
"\"type\": \"blinded payload hash\""
"}"
"],"
"\"message_reference\": 100003,"
"\"transaction_reference\": \"b2221a58008a05a6c4647159c324c985\","
"\"type\": \"request mint\""
"}");
REQUIRE(res.has_value() == true);
REQUIRE(res->message_reference == 100003);
REQUIRE(res->transaction_reference == "b2221a58008a05a6c4647159c324c985");
// good case
auto res = RequestMint::from_string( "{"
"\"blinds\": ["
"{"
"\"blinded_payload_hash\": \"924edb672c3345492f38341ff86b57181da4c673efd5fd76c0f64e369974c678f89ffcb1c5f77bf04911a9a63831b52675b70c06548a4d945ec5dd1d825ab08\","
"\"mint_key_id\": \"1ceb977bb531c65f133ab8b0d60862b17369d96\","
"\"reference\": \"a0\","
"\"type\": \"blinded payload hash\""
"},"
"{"
"\"blinded_payload_hash\": \"95db92e1c46ebea5edec5e508a831263de6fb78b4cf9187593f6af815b51db9db35ad5eaf2c0c83bd7e13c999df4f0f1af65b367eb7c2b6addb9735dce156b5\","
"\"mint_key_id\": \"f2864e5cd937dbaa4825e73a81062de162143682\","
"\"reference\": \"a1\","
"\"type\": \"blinded payload hash\""
"},"
"{"
"\"blinded_payload_hash\": \"10afac98ac43eb40e996c621d5db4d2238348e3f74850856d940955da0fd24fa4d3aee79da1e9da24e85cf9cefd96feaca5b26a9353a3d9fcb4bd34145046ce8\","
"\"mint_key_id\": \"897a16bf12bd9ba474ef7be0e3a53553a7b4ece8\","
"\"reference\": \"a2\","
"\"type\": \"blinded payload hash\""
"}"
"],"
"\"message_reference\": 100003,"
"\"transaction_reference\": \"b2221a58008a05a6c4647159c324c985\","
"\"type\": \"request mint\""
"}");
REQUIRE(res.has_value()==true);
REQUIRE(res->message_reference==100003);
REQUIRE(res->transaction_reference.to_string() == "b2221a58008a05a6c4647159c324c985");
/// \todo check blinds
// bad cases
@ -228,7 +226,7 @@ TEST_CASE("RequestRenew::from_string", "[from_string]") {
" \"blinds\": ["
" {"
" \"blinded_payload_hash\": "
"\"7ed0cda1c1b36f544514b12848b8436974b7b9f6c7...\","
"\"7ed0cda1c1b36f544514b12848b8436974b7b9f6c7231ebcc566678e3478d3279db03872e4710413ad20a55d56ef12fb1800ac187195322535a626e178931cf9\","
" \"mint_key_id\": \"f2864e5cd937dbaa4825e73a81062de162143682\","
" \"reference\": \"b0\","
" \"type\": \"blinded payload hash\""
@ -280,17 +278,16 @@ TEST_CASE("RequestRenew::from_string", "[from_string]") {
TEST_CASE("RequestResume::from_string", "[from_string]") {
// good case
auto res = RequestResume::from_string(
"{"
"\"message_reference\": 100005,"
"\"transaction_reference\": \"ad45f23d3b1a11df587fd2803bab6c39\","
"\"type\": \"request resume\""
"}");
REQUIRE(res.has_value() == true);
REQUIRE(res->message_reference == 100005);
REQUIRE(res->transaction_reference == "ad45f23d3b1a11df587fd2803bab6c39");
auto res = RequestResume::from_string( "{"
"\"message_reference\": 100005,"
"\"transaction_reference\": \"ad45f23d3b1a11df587fd2803bab6c39\","
"\"type\": \"request resume\""
"}");
REQUIRE(res.has_value()==true);
REQUIRE(res->message_reference==100005);
REQUIRE(res->transaction_reference.to_string()=="ad45f23d3b1a11df587fd2803bab6c39");
// bad cases
res = RequestResume::from_string("");
REQUIRE(res.has_value() == false);

37
test/test_big_int.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "big_int.hpp"
#include <catch2/catch_test_macros.hpp>
TEST_CASE("BigInt::from_string", "[big_int]") {
const std::string VALID [] = {
"1",
"12",
"123",
"1234",
"12345",
"123456",
"120456",
"123056",
"120345",
"1234560abc",
"aabbcc",
"abcdef1"
};
for (const auto & i : VALID) {
auto b = BigInt::from_string(i);
REQUIRE(b.has_value());
REQUIRE(b->to_string() == i);
}
auto invalid_hex = "aabbcc..";
{
auto b = BigInt::from_string(invalid_hex);
REQUIRE(!b);
}
}
TEST_CASE("BigInt::from_int", "[big_int]") {
auto b = BigInt::from_int(0xaabbccdd);
REQUIRE(b.to_string()=="aabbccdd");
}

9
vcpkg.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "oc-mint-cpp",
"version-string": "master",
"dependencies": [
"asio",
"openssl",
"zlib"
]
}