1 Commits

12 changed files with 183 additions and 480 deletions

13
.github/FUNDING.yml vendored
View File

@ -1,13 +0,0 @@
# 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']

View File

@ -1,4 +1,4 @@
name: Build and test (cmake based build) name: CMake
on: on:
push: push:
@ -42,11 +42,11 @@ jobs:
fi fi
shell: bash shell: bash
- name: Configure CMake - name: Configure CMake
run: cmake -B build run: cmake -B ${{github.workspace}}/build -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
shell: bash shell: bash
- name: Build - name: Build
# Build your program with the given configuration # Build your program with the given configuration
run: cmake --build build --config ${{env.BUILD_TYPE}} run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
shell: bash shell: bash
- name: Test - name: Test

View File

@ -1,4 +1,4 @@
name: Deploy Doxygen results to Github Pages name: Doxygen Github Pages Deploy action
on: on:
push: push:

View File

@ -9,12 +9,12 @@ if(POLICY CMP0077)
endif() endif()
if (MSVC) if (MSVC)
add_compile_options(/W4) add_compile_options(/W4 /WX)
else() else()
add_compile_options(-Wall -Wextra -pedantic) add_compile_options(-Wall -Wextra -pedantic)
endif() endif()
project(oc-issuer VERSION 0.0.2 LANGUAGES CXX) project(oc-mint VERSION 0.0.1 LANGUAGES CXX)
include(FetchContent) include(FetchContent)
@ -64,23 +64,23 @@ find_package(Doxygen
REQUIRED dot REQUIRED dot
OPTIONAL_COMPONENTS mscgen dia) OPTIONAL_COMPONENTS mscgen dia)
set(DOXYGEN_HAVE_DOT YES) set(DOXYGEN_HAVE_DOT YES)
doxygen_add_docs( doc doxygen_add_docs(
README.md doc
src src
COMMENT "Generate documentation" COMMENT "Generate documentation"
) )
# build common library # build common library
set(LIB_SOURCES src/model.cpp src/model.hpp src/big_int.hpp src/big_int.cpp) set(LIB_SOURCES src/model.cpp src/model.hpp)
add_library(oc-mint-lib ${LIB_SOURCES}) add_library(oc-mint-lib ${LIB_SOURCES})
target_link_libraries(oc-mint-lib PUBLIC Crow::Crow) target_link_libraries(oc-mint-lib PUBLIC Crow::Crow)
target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src) target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src)
add_executable(${PROJECT_NAME} src/main.cpp) add_executable(oc-mint src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE oc-mint-lib INTERFACE tl::expected::expected) target_link_libraries(oc-mint PRIVATE oc-mint-lib INTERFACE tl::expected::expected)
## these are unittests that can be run on any platform ## these are unittests that can be run on any platform
add_executable(tests test/test_big_int.cpp test/test.cpp) add_executable(tests test/test.cpp)
target_link_libraries(tests target_link_libraries(tests
oc-mint-lib oc-mint-lib
Catch2::Catch2WithMain) Catch2::Catch2WithMain)

View File

@ -1,40 +1,11 @@
opencoin-issuer-cpp - a C++ OpenCoin Issuer REST-API implementation {#mainpage} ![build](https://github.com/OpenCoin/oc-mint-cpp/actions/workflows/cmake.yaml/badge.svg)
=============================================================
![build](https://github.com/OpenCoin/opencoin-issuer-cpp/actions/workflows/cmake.yaml/badge.svg) # oc mint sample
[![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)
# opencoin issuer this is a C++ implementation of the opencoin protocol
as mapping to some kind of REST interface
this is a C++ implementation of the [opencoin protocol](https://opencoin.org/0.4/OpenCoin.html) actually it is a work in progress.
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::getCurrentCDDC() |
| RequestCDDC | /cddc | ResponseCDDC | Model::getCurrentCDDC() |
| 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 ## Protocol Questions
@ -43,6 +14,11 @@ or also the key?
+ the weighted URL as array has a different js encoding as other elements + 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) + 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++
### Blinding Notes ### Blinding Notes
https://stackoverflow.com/questions/47860570/how-to-create-and-verify-blind-rsa-signatures-with-crypto https://stackoverflow.com/questions/47860570/how-to-create-and-verify-blind-rsa-signatures-with-crypto

View File

@ -1,108 +0,0 @@
#include "big_int.hpp"
#include "tl/expected.hpp"
#include <iostream>
#include <charconv>
#include <vector>
inline uint8_t hex(char c) {
switch(c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': return 10;
case 'b': return 11;
case 'c': return 12;
case 'd': return 13;
case 'e': return 14;
case 'f': return 15;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
default:
return 0xff;
}
}
tl::expected<BigInt,BigInt::eError>
BigInt::from_string(const std::string& str) {
BigInt b;
// std::cout << str << std::endl;
uint8_t hval=0;
uint8_t nibble;
size_t i = str.size()+1;
for(auto c : str) {
nibble = hex(c);
if (nibble ==0xFF) {
return tl::make_unexpected(eError::PARSE_ERROR);
}
if (i%2) {
hval = nibble << 4;
} else {
hval |= nibble;
b.data[256 - (i/2)]= hval;
hval = 0;
}
i--;
}
// std::cout << std::hex;
// for (auto b: b.data)
// {
// std::cout << (int)b;
// }
// std::cout << std::dec << std::endl;
return b;
}
BigInt BigInt::from_int(uint64_t value)
{
BigInt b;
b.data[248]=(value >> 56 & 0xff);
b.data[249]=(value >> 48 & 0xff);
b.data[250]=(value >> 40 & 0xff);
b.data[251]=(value >> 32 & 0xff);
b.data[252]=(value >> 24 & 0xff);
b.data[253]=(value >> 16 & 0xff);
b.data[254]=(value >> 8 & 0xff);
b.data[255]=(value & 0xff);
return b;
}
constexpr char hex_char [] = "0123456789abcdef";
std::string BigInt::to_string() const
{
std::string s;
uint8_t b;
uint8_t first_digit = 0;
for (size_t i = 0; i<256;i++) {
b = data[i];
if (first_digit==0) {
if (b==0) {
continue;
} else if (b>0xf) {
s.push_back(hex_char[b >> 4]);
}
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; }

View File

@ -1,27 +0,0 @@
#ifndef BIG_INT_HPP
#define BIG_INT_HPP
#include <string>
#include <array>
#include "tl/expected.hpp"
struct BigInt {
BigInt() : data() {}
virtual ~BigInt() {}
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);
std::string to_string() const;
friend bool operator == (const BigInt& rhs, const BigInt& lhs);
private:
std::array<uint8_t,256> data;
};
bool operator==(const BigInt &rhs, const BigInt &lhs);
#endif // #ifndef #ifndef BIG_INT_HPP

View File

@ -29,11 +29,11 @@ int main() {
CROW_ROUTE(app, "/cddc/serial") CROW_ROUTE(app, "/cddc/serial")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) { .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestCDDCSerial::from_string(request.body); auto req = RequestCDDSerial::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
ResponseCDDCSerial res; ResponseCDDSerial res;
res.message_reference = req->message_reference; res.message_reference = req->message_reference;
auto cddc = model->getCurrentCDDC(); auto cddc = model->getCurrentCDDC();
@ -70,8 +70,7 @@ int main() {
ResponseMint res; ResponseMint res;
res.message_reference = req->message_reference; res.message_reference = req->message_reference;
/// \todo change argument transaction_reference to bigint auto minted = model->mint(req->transaction_reference, req->blinds);
auto minted = model->mint(req->transaction_reference.to_string(), req->blinds);
res.blind_signatures = minted; res.blind_signatures = minted;
res.status_code = crow::status::OK; res.status_code = crow::status::OK;

View File

@ -1,11 +1,9 @@
#include "model.hpp" #include "model.hpp"
#include "crow/json.h" #include "crow/json.h"
#include "tl/expected.hpp"
#define TO_JSON(name) r[#name]=name #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_JSON(name) r[#name]=name.to_json() #define TO_JSON_ARRAY(name) r[#name] = list_to_json(name)
#define TO_JSON_ARRAY(name) r[#name]=list_to_json(name)
template <class T> template <class T>
crow::json::wvalue list_to_json(const std::vector<T> &array) { crow::json::wvalue list_to_json(const std::vector<T> &array) {
@ -24,9 +22,9 @@ crow::json::wvalue list_to_json(const std::vector<unsigned int> &array) {
crow::json::wvalue PublicKey::to_json() const { crow::json::wvalue PublicKey::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
BIGINT_TO_JSON(modulus); TO_JSON(modulus);
BIGINT_TO_JSON(public_exponent); TO_JSON(public_exponent);
r["type"]="rsa public key"; r["type"] = "rsa public key";
return r; return r;
} }
@ -50,7 +48,7 @@ crow::json::wvalue CDD::to_json() const {
TO_JSON(currency_divisor); TO_JSON(currency_divisor);
TO_JSON(currency_name); TO_JSON(currency_name);
TO_JSON_ARRAY(denominations); TO_JSON_ARRAY(denominations);
BIGINT_TO_JSON(id); TO_JSON(id);
TO_JSON_ARRAY(info_service); TO_JSON_ARRAY(info_service);
TO_JSON(issuer_cipher_suite); TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key); TO_JSON_JSON(issuer_public_master_key);
@ -77,8 +75,8 @@ crow::json::wvalue MintKey::to_json() const {
TO_JSON(cdd_serial); TO_JSON(cdd_serial);
TO_JSON(coins_expiry_date); TO_JSON(coins_expiry_date);
TO_JSON(denomination); TO_JSON(denomination);
BIGINT_TO_JSON(id); TO_JSON(id);
BIGINT_TO_JSON(issuer_id); TO_JSON(issuer_id);
TO_JSON_JSON(public_mint_key); TO_JSON_JSON(public_mint_key);
TO_JSON(sign_coins_not_after); TO_JSON(sign_coins_not_after);
@ -104,7 +102,7 @@ crow::json::wvalue Response::to_json() const {
return r; return r;
} }
crow::json::wvalue ResponseCDDCSerial::to_json() const { crow::json::wvalue ResponseCDDSerial::to_json() const {
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial); TO_JSON(cdd_serial);
@ -112,8 +110,8 @@ crow::json::wvalue ResponseCDDCSerial::to_json() const {
return r; return r;
} }
tl::expected<RequestCDDCSerial, eError> tl::expected<RequestCDDSerial, eError>
RequestCDDCSerial::from_string(const std::string &str) { RequestCDDSerial::from_string(const std::string &str) {
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
@ -122,7 +120,7 @@ RequestCDDCSerial::from_string(const std::string &str) {
} else if (json["type"] != "request cdd serial") { } else if (json["type"] != "request cdd serial") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
RequestCDDCSerial r; RequestCDDSerial r;
r.message_reference = json["message_reference"].u(); r.message_reference = json["message_reference"].u();
return r; return r;
} }
@ -179,13 +177,8 @@ RequestMKCs::from_string(const std::string &str) {
if (mint_key_ids.t() != crow::json::type::List) { if (mint_key_ids.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
for (auto k: mint_key_ids.lo()) { for (auto k : mint_key_ids.lo()) {
auto kv = BigInt::from_string(k.s()); r.mint_key_ids.push_back(k.u());
if (!kv.has_value()) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
r.mint_key_ids.push_back(*kv);
}
} }
} }
return r; return r;
@ -201,37 +194,23 @@ crow::json::wvalue ResponseMKCs::to_json() const {
crow::json::wvalue Blind::to_json() const { crow::json::wvalue Blind::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
BIGINT_TO_JSON(blinded_payload_hash); TO_JSON(blinded_payload_hash);
BIGINT_TO_JSON(mint_key_id); TO_JSON(mint_key_id);
TO_JSON(reference); TO_JSON(reference);
r["type"] = "blinded payload hash"; r["type"] = "blinded payload hash";
return r; return r;
} }
tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) { tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
if (!(json.has("type") if (!(json.has("type") && json.has("blinded_payload_hash") &&
&& json.has("blinded_payload_hash") json.has("mint_key_id") && json.has("reference"))) {
&& json.has("mint_key_id")
&& json.has("reference"))) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "blinded payload hash") { } else if (json["type"] != "blinded payload hash") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
Blind r; Blind r;
r.blinded_payload_hash = json["blinded_payload_hash"].s();
auto hash = BigInt::from_string(json["blinded_payload_hash"].s()); r.mint_key_id = json["mint_key_id"].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(); r.reference = json["reference"].s();
return r; return r;
} }
@ -239,7 +218,7 @@ tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
crow::json::wvalue BlindSignature::to_json() const { crow::json::wvalue BlindSignature::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
BIGINT_TO_JSON(blind_signature); TO_JSON(blind_signature);
TO_JSON(reference); TO_JSON(reference);
r["type"] = "blind signature"; r["type"] = "blind signature";
return r; return r;
@ -259,13 +238,9 @@ RequestMint::from_string(const std::string &str) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
RequestMint r; RequestMint r;
r.message_reference= json["message_reference"].u(); r.message_reference = json["message_reference"].u();
auto tr = BigInt::from_string(json["transaction_reference"].s()); r.transaction_reference = json["transaction_reference"].s();
if (!tr) if (json["blinds"].t() != crow::json::type::List) {
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); return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} }
@ -292,10 +267,10 @@ crow::json::wvalue Coin::Payload::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON(cdd_location); TO_JSON(cdd_location);
TO_JSON(denomination); TO_JSON(denomination);
BIGINT_TO_JSON(issuer_id); TO_JSON(issuer_id);
BIGINT_TO_JSON(mint_key_id); TO_JSON(mint_key_id);
TO_JSON(protocol_version); TO_JSON(protocol_version);
BIGINT_TO_JSON(serial); TO_JSON(serial);
r["type"] = "payload"; r["type"] = "payload";
return r; return r;
@ -322,20 +297,10 @@ Coin::Payload::from_json(const crow::json::rvalue &json) {
Coin::Payload payload; Coin::Payload payload;
payload.cdd_location = json["cdd_location"].s(); payload.cdd_location = json["cdd_location"].s();
payload.denomination = json["denomination"].u(); payload.denomination = json["denomination"].u();
auto id = BigInt::from_string(json["issuer_id"].s()); payload.issuer_id = json["issuer_id"].s();
if (!id) payload.mint_key_id = json["mint_key_id"].s();
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(); payload.protocol_version = json["protocol_version"].s();
id = BigInt::from_string(json["serial"].s()); payload.serial = json["serial"].s();
if (!id)
tl::make_unexpected(eError::JSON_PARSE_ERROR);
payload.serial = *id;
return payload; return payload;
} }
} }
@ -427,10 +392,7 @@ RequestResume::from_string(const std::string &str) {
} else { } else {
RequestResume r; RequestResume r;
r.message_reference = json["message_reference"].u(); r.message_reference = json["message_reference"].u();
auto tr = BigInt::from_string(json["transaction_reference"].s()); r.transaction_reference = json["transaction_reference"].s();
if (!tr)
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
r.transaction_reference = *tr;
return r; return r;
} }
} }
@ -489,7 +451,7 @@ public:
return &m_cddc; return &m_cddc;
} }
std::vector<BlindSignature> mint(const std::string& transaction_reference, std::vector<BlindSignature> mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) override { const std::vector<Blind> &blinds) override {
std::vector<BlindSignature> res; std::vector<BlindSignature> res;
cout << __FUNCTION__ << "(" cout << __FUNCTION__ << "("
@ -500,7 +462,7 @@ public:
const std::vector<MintKeyCert> const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations, getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<BigInt> &mint_key_ids) override { const std::vector<unsigned int> &mint_key_ids) override {
std::vector<MintKeyCert> res; std::vector<MintKeyCert> res;
cout << __FUNCTION__ << endl; cout << __FUNCTION__ << endl;
return res; return res;

View File

@ -9,11 +9,10 @@
#include "crow/json.h" #include "crow/json.h"
#include "tl/expected.hpp" #include "tl/expected.hpp"
#include "big_int.hpp"
struct PublicKey { struct PublicKey {
BigInt modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...", std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
BigInt public_exponent;//: 65537, std::string public_exponent; //: 65537,
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
@ -25,36 +24,29 @@ struct WeightedUrl {
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
/** @brief currency description document /** 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 { struct CDD {
std::string additional_info; std::string additional_info;
time_t cdd_expiry_date; /// expiry date of this document (e.g. time_t cdd_expiry_date; //: 2023-07-22T15:45:53.164685
///2023-07-22T15:45:53.164685) std::string cdd_location; //: https://opencent.org,
std::string cdd_location; /// URL of location of this document (e.g size_t cdd_serial; //: 1,
///https://opencent.org) time_t cdd_signing_date; //: 2022-07-22T15:45:53.164685,
size_t cdd_serial; /// serial number of currency description document size_t currency_divisor; //: 100,
time_t cdd_signing_date; /// date of signing this document (e.g. std::string currency_name; //: OpenCent,
///2022-07-22T15:45:53.164685) std::vector<unsigned> denominations; //: [1, 2, 5],
size_t currency_divisor; /// divisor used for coins of this currency std::string id; //: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
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; std::vector<WeightedUrl> info_service;
/* eCipherSuite*/ /* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82,
std::string issuer_cipher_suite; /// the cipher suite used for this currencey PublicKey
/// (currently only RSA-SHA256-PSS-CHAUM82 issuer_public_master_key; //: {
/// is supported) // modulus:
PublicKey issuer_public_master_key; /// the public key of this currency // daaa63ddda38c189b8c49020c8276adbe0a695685a...,
// public_exponent: 65537,
// type: rsa public key
//},
std::vector<WeightedUrl> mint_service; std::vector<WeightedUrl> mint_service;
std::string protocol_version; // e.g. https://opencoin.org/1.0 std::string protocol_version; //: https://opencoin.org/1.0,
std::vector<WeightedUrl> redeem_service; std::vector<WeightedUrl> redeem_service;
std::vector<WeightedUrl> renew_service; std::vector<WeightedUrl> renew_service;
@ -71,9 +63,9 @@ struct CDDC {
struct MintKey { struct MintKey {
unsigned int cdd_serial; unsigned int cdd_serial;
std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685", std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685",
unsigned int denomination; //": 1, unsigned int denomination; //": 1,
BigInt id; // "1ceb977bb531c65f133ab8b0d60862b17369d96", std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96",
BigInt issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2", std::string issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2",
PublicKey public_mint_key; PublicKey public_mint_key;
std::string sign_coins_not_after; std::string sign_coins_not_after;
@ -106,14 +98,14 @@ struct Response {
virtual crow::json::wvalue to_json() const; virtual crow::json::wvalue to_json() const;
}; };
struct RequestCDDCSerial { struct RequestCDDSerial {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
static tl::expected<RequestCDDCSerial, eError> static tl::expected<RequestCDDSerial, eError>
from_string(const std::string &str); from_string(const std::string &str);
}; };
struct ResponseCDDCSerial : Response { struct ResponseCDDSerial : Response {
unsigned int cdd_serial; unsigned int cdd_serial;
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
@ -136,7 +128,7 @@ struct RequestMKCs {
std::vector<unsigned int> denominations; std::vector<unsigned int> denominations;
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
std::vector<BigInt> mint_key_ids; std::vector<unsigned int> mint_key_ids;
// "type": "request mint key certificates" // "type": "request mint key certificates"
static tl::expected<RequestMKCs, eError> from_string(const std::string &str); static tl::expected<RequestMKCs, eError> from_string(const std::string &str);
}; };
@ -148,15 +140,15 @@ struct ResponseMKCs : Response {
}; };
struct Blind { struct Blind {
BigInt blinded_payload_hash; //bigint std::string blinded_payload_hash; // bigint
BigInt mint_key_id; //bigint std::string mint_key_id; // bigint
std::string reference; std::string reference;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
static tl::expected<Blind, eError> from_json(const crow::json::rvalue &json); static tl::expected<Blind, eError> from_json(const crow::json::rvalue &json);
}; };
struct BlindSignature { struct BlindSignature {
BigInt blind_signature; std::string blind_signature;
std::string reference; std::string reference;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
@ -164,7 +156,7 @@ struct BlindSignature {
struct RequestMint { struct RequestMint {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
BigInt transaction_reference; std::string transaction_reference;
std::vector<Blind> blinds; std::vector<Blind> blinds;
// "type": "request mint" // "type": "request mint"
static tl::expected<RequestMint, eError> from_string(const std::string &str); static tl::expected<RequestMint, eError> from_string(const std::string &str);
@ -180,13 +172,14 @@ struct Coin {
struct Payload { struct Payload {
std::string cdd_location; std::string cdd_location;
unsigned int denomination; unsigned int denomination;
BigInt issuer_id; std::string issuer_id;
BigInt mint_key_id; std::string mint_key_id;
std::string protocol_version; std::string protocol_version;
BigInt serial; std::string serial;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
static tl::expected<Payload,eError> from_json(const crow::json::rvalue& json); static tl::expected<Payload, eError>
from_json(const crow::json::rvalue &json);
}; };
Payload payload; Payload payload;
@ -220,7 +213,7 @@ struct ResponseDelay : Response {
struct RequestResume { struct RequestResume {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
BigInt transaction_reference; std::string transaction_reference;
// "type": "request resume" // "type": "request resume"
static tl::expected<RequestResume, eError> static tl::expected<RequestResume, eError>
from_string(const std::string &str); from_string(const std::string &str);
@ -243,63 +236,18 @@ class Model {
public: public:
virtual ~Model(){}; virtual ~Model(){};
/**
* 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> getCDDC(unsigned int cdd_serial) = 0; virtual tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) = 0;
virtual tl::expected<CDDC *, bool> getCurrentCDDC() = 0;
/**
* 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> getCurrentCDDC() = 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 const std::vector<MintKeyCert> virtual const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations, getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<BigInt> &mint_key_ids) = 0; const std::vector<unsigned int> &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> virtual std::vector<BlindSignature>
mint(std::string const& transaction_reference, mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) = 0; 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; virtual bool redeem(const std::vector<Coin> &coins) = 0;
/**
* 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> getModel(const std::string &backend_name); static std::unique_ptr<Model> getModel(const std::string &backend_name);
private: private:

View File

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

View File

@ -1,37 +0,0 @@
#include "big_int.hpp"
#include <catch2/catch_test_macros.hpp>
TEST_CASE("BigInt::from_string", "[big_int]") {
std::string VALID [] = {
"1",
"12",
"123",
"1234",
"12345",
"123456",
"120456",
"123056",
"120345",
"1234560abc",
"aabbcc",
"abcdef1"
};
for (size_t i= 0; i<sizeof(VALID)/sizeof(VALID[0]);i++) {
auto b = BigInt::from_string(VALID[i]);
REQUIRE(b->to_string() == VALID[i]);
REQUIRE(b.has_value());
}
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");
}