From 77fd2eb5a90a9531e7af7c14b97583ea1d336662 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 1 Dec 2023 13:27:32 -0600 Subject: [PATCH] pull structs in separate header --- .gitignore | 4 + flutter_libsparkmobile.iml | 29 ------ src/flutter_libsparkmobile.cpp | 3 +- src/flutter_libsparkmobile.h | 148 +----------------------------- src/structs.h | 160 +++++++++++++++++++++++++++++++++ src/utils.cpp | 8 +- src/utils.h | 6 +- 7 files changed, 174 insertions(+), 184 deletions(-) delete mode 100644 flutter_libsparkmobile.iml create mode 100644 src/structs.h diff --git a/.gitignore b/.gitignore index 379c1db..928cced 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,10 @@ doc/api/ .flutter-plugins .flutter-plugins-dependencies +# IntelliJ related +*.iml +*.ipr +*.iws .idea/ example/ios/Podfile.lock diff --git a/flutter_libsparkmobile.iml b/flutter_libsparkmobile.iml deleted file mode 100644 index 777a11b..0000000 --- a/flutter_libsparkmobile.iml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/flutter_libsparkmobile.cpp b/src/flutter_libsparkmobile.cpp index 883eca0..92a3346 100644 --- a/src/flutter_libsparkmobile.cpp +++ b/src/flutter_libsparkmobile.cpp @@ -1,9 +1,10 @@ #include "flutter_libsparkmobile.h" #include "utils.h" #include "deps/sparkmobile/include/spark.h" +#include "deps/sparkmobile/bitcoin/uint256.h" +#include "structs.h" #include -#include "deps/sparkmobile/bitcoin/uint256.h" #include // Just for printing. using namespace spark; diff --git a/src/flutter_libsparkmobile.h b/src/flutter_libsparkmobile.h index bd02eb1..b26dd87 100644 --- a/src/flutter_libsparkmobile.h +++ b/src/flutter_libsparkmobile.h @@ -2,6 +2,7 @@ #define ORG_FIRO_SPARK_DART_INTERFACE_H #include +#include "structs.h" #ifndef FFI_PLUGIN_EXPORT #ifdef __cplusplus @@ -25,38 +26,6 @@ FFI_PLUGIN_EXPORT const char* createIncomingViewKey(const char* keyData, int index); */ -/* - * FFI-friendly wrapper for a spark::Coin. - * - * Coin: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/coin.h#L66 - */ -struct CCoin { - char type; - const unsigned char* k; - int kLength; - const char* address; - uint64_t v; - const unsigned char* memo; - int memoLength; - const unsigned char* serial_context; - int serial_contextLength; -}; - -/* - * FFI-friendly wrapper for a spark::IdentifiedCoinData. - * - * IdentifiedCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/coin.h#L19 - */ -struct CIdentifiedCoinData { - uint64_t i; - const unsigned char* d; - int dLength; - uint64_t v; - const unsigned char* k; - int kLength; - const char* memo; - int memoLength; -}; /* * FFI-friendly wrapper for spark::identifyCoin. @@ -66,33 +35,6 @@ struct CIdentifiedCoinData { FFI_PLUGIN_EXPORT struct CIdentifiedCoinData identifyCoin(struct CCoin c_struct, const char* keyDataHex, int index); -/* - * FFI-friendly wrapper for a spark::CRecipient. - * - * CRecipient: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/include/spark.h#L27 - */ -struct CCRecipient { - const unsigned char* pubKey; - int pubKeyLength; - uint64_t cAmount; - int subtractFee; -}; - -/* - * FFI-friendly wrapper for a spark::MintedCoinData. - * - * MintedCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/mint_transaction.h#L12 - */ -struct CMintedCoinData { - const char* address; - uint64_t value; - const char* memo; -}; - -struct PubKeyScript { - unsigned char* bytes; - int length; -}; /* * FFI-friendly wrapper for spark::createSparkMintRecipients. @@ -107,94 +49,6 @@ struct CCRecipient* createSparkMintRecipients( int serial_contextLength, int generate); -/* - * FFI-friendly wrapper for a std::pair. - * - * Note this is an ambiguation of a spark::CRecipient. This CRecip(ient) is just a wrapper for a - * CAmount and bool pair, and is not the same as the spark::CRecipient struct above, which gets - * wrapped for us as a CCRecipient and is unrelated to this struct. - * - * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L190 - */ -struct CRecip { - uint64_t amount; - int subtractFee; -}; - -/* - * FFI-friendly wrapper for a spark::OutputCoinData. - * - * OutputCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/spend_transaction.h#L33 - */ -struct COutputCoinData { - const char* address; - uint64_t value; - const char* memo; -}; - -/* - * FFI-friendly wrapper for a . - * - * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L195 - */ -struct COutputRecipient { - struct COutputCoinData output; - int subtractFee; -}; - -/* - * FFI-friendly wrapper for a spark::CSparkMintMeta. - * - * CSparkMintMeta: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/primitives.h#L9 - */ -struct CCSparkMintMeta { - uint64_t height; - const char* id; - int isUsed; - const char* txid; - uint64_t i; // Diversifier. - const unsigned char* d; // Encrypted diversifier. - int dLength; - uint64_t v; // Value. - const unsigned char* k; // Nonce. - int kLength; - const char* memo; - int memoLength; - unsigned char* serial_context; - int serial_contextLength; - char type; - CDataStream coin; - - CCSparkMintMeta(uint64_t height, const char* id, int isUsed, const char* txid, uint64_t i, const unsigned char* d, int dLength, uint64_t v, const unsigned char* k, int kLength, const char* memo, int memoLength, unsigned char* serial_context, int serial_contextLength, char type, const CDataStream& coinData); - ~CCSparkMintMeta(); -}; - -/* - * FFI-friendly wrapper for a spark::CoverSetData. - * - * CoverSetData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/spend_transaction.h#L28 - */ -struct CCoverSetData { - CDataStream** cover_set; // vs. struct CCoin* cover_set; - int cover_setLength; - const unsigned char* cover_set_representation; - int cover_set_representationLength; -}; - -/* - * FFI-friendly wrapper for a std::unordered_map. - * - * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L197 - */ -struct CCoverSets { - struct CCoverSetData* cover_sets; - int cover_setsLength; -}; - -struct OutputScript { - unsigned char* bytes; - int length; -}; /* * FFI-friendly wrapper for spark::createSparkSpendTransaction. diff --git a/src/structs.h b/src/structs.h new file mode 100644 index 0000000..4647575 --- /dev/null +++ b/src/structs.h @@ -0,0 +1,160 @@ +#ifndef ORG_FIRO_SPARK_DART_STRUCTS_H +#define ORG_FIRO_SPARK_DART_STRUCTS_H + +#include +#include "deps/sparkmobile/include/spark.h" + +/* + * FFI-friendly wrapper for a spark::Coin. + * + * Coin: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/coin.h#L66 + */ +struct CCoin { + char type; + const unsigned char* k; + int kLength; + const char* address; + uint64_t v; + const unsigned char* memo; + int memoLength; + const unsigned char* serial_context; + int serial_contextLength; +}; + +/* + * FFI-friendly wrapper for a spark::IdentifiedCoinData. + * + * IdentifiedCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/coin.h#L19 + */ +struct CIdentifiedCoinData { + uint64_t i; + const unsigned char* d; + int dLength; + uint64_t v; + const unsigned char* k; + int kLength; + const char* memo; + int memoLength; +}; + +/* + * FFI-friendly wrapper for a spark::CRecipient. + * + * CRecipient: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/include/spark.h#L27 + */ +struct CCRecipient { + const unsigned char* pubKey; + int pubKeyLength; + uint64_t cAmount; + int subtractFee; +}; + +/* + * FFI-friendly wrapper for a spark::MintedCoinData. + * + * MintedCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/mint_transaction.h#L12 + */ +struct CMintedCoinData { + const char* address; + uint64_t value; + const char* memo; +}; + +struct PubKeyScript { + unsigned char* bytes; + int length; +}; + + + +/* + * FFI-friendly wrapper for a std::pair. + * + * Note this is an ambiguation of a spark::CRecipient. This CRecip(ient) is just a wrapper for a + * CAmount and bool pair, and is not the same as the spark::CRecipient struct above, which gets + * wrapped for us as a CCRecipient and is unrelated to this struct. + * + * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L190 + */ +struct CRecip { + uint64_t amount; + int subtractFee; +}; + +/* + * FFI-friendly wrapper for a spark::OutputCoinData. + * + * OutputCoinData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/spend_transaction.h#L33 + */ +struct COutputCoinData { + const char* address; + uint64_t value; + const char* memo; +}; + +/* + * FFI-friendly wrapper for a . + * + * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L195 + */ +struct COutputRecipient { + struct COutputCoinData output; + int subtractFee; +}; + +/* + * FFI-friendly wrapper for a spark::CSparkMintMeta. + * + * CSparkMintMeta: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/primitives.h#L9 + */ +struct CCSparkMintMeta { + uint64_t height; + const char* id; + int isUsed; + const char* txid; + uint64_t i; // Diversifier. + const unsigned char* d; // Encrypted diversifier. + int dLength; + uint64_t v; // Value. + const unsigned char* k; // Nonce. + int kLength; + const char* memo; + int memoLength; + unsigned char* serial_context; + int serial_contextLength; + char type; + CDataStream coin; + + CCSparkMintMeta(uint64_t height, const char* id, int isUsed, const char* txid, uint64_t i, const unsigned char* d, int dLength, uint64_t v, const unsigned char* k, int kLength, const char* memo, int memoLength, unsigned char* serial_context, int serial_contextLength, char type, const CDataStream& coinData); + ~CCSparkMintMeta(); +}; + +/* + * FFI-friendly wrapper for a spark::CoverSetData. + * + * CoverSetData: https://github.com/firoorg/sparkmobile/blob/8bf17cd3deba6c3b0d10e89282e02936d7e71cdd/src/spend_transaction.h#L28 + */ +struct CCoverSetData { + CDataStream** cover_set; // vs. struct CCoin* cover_set; + int cover_setLength; + const unsigned char* cover_set_representation; + int cover_set_representationLength; +}; + +/* + * FFI-friendly wrapper for a std::unordered_map. + * + * See https://github.com/firoorg/sparkmobile/blob/23099b0d9010a970ad75b9cfe05d568d634088f3/src/spark.cpp#L197 + */ +struct CCoverSets { + struct CCoverSetData* cover_sets; + int cover_setsLength; +}; + +struct OutputScript { + unsigned char* bytes; + int length; +}; + + +#endif //ORG_FIRO_SPARK_DART_STRUCTS_H \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index 4ccb937..243b333 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -6,10 +6,10 @@ #include #include -#include "flutter_libsparkmobile.h" +#include "structs.h" #include "deps/sparkmobile/src/coin.h" #include "deps/sparkmobile/src/keys.h" -#include "deps/sparkmobile//bitcoin/script.h" // For CScript. +#include "deps/sparkmobile/bitcoin/script.h" // For CScript. /* * Utility function to generate an address from keyData, index, and a diversifier. @@ -57,7 +57,7 @@ spark::SpendKey createSpendKeyFromData(const char *keyData, int index) { * * TODO manage the memory allocated by this function. */ -struct CCoin createCCoin(char type, const unsigned char* k, int kLength, const char* address, uint64_t v, const unsigned char* memo, int memoLength, const unsigned char* serial_context, int serial_contextLength) { +CCoin createCCoin(char type, const unsigned char* k, int kLength, const char* address, uint64_t v, const unsigned char* memo, int memoLength, const unsigned char* serial_context, int serial_contextLength) { CCoin coin; coin.type = type; coin.k = copyBytes(k, kLength); @@ -179,7 +179,7 @@ CRecipient fromFFI(const CCRecipient& c_struct) { * * TODO manage the memory allocated by this function. */ -struct CCRecipient createCCRecipient(const unsigned char* pubKey, uint64_t amount, int subtractFee) { +CCRecipient createCCRecipient(const unsigned char* pubKey, uint64_t amount, int subtractFee) { CCRecipient recipient; recipient.pubKey = copyBytes(pubKey, 32); recipient.cAmount = amount; diff --git a/src/utils.h b/src/utils.h index f6bbdcc..6827fee 100644 --- a/src/utils.h +++ b/src/utils.h @@ -6,7 +6,7 @@ #include #include -#include "flutter_libsparkmobile.h" +#include "structs.h" #include "deps/sparkmobile/include/spark.h" #include "deps/sparkmobile/bitcoin/streams.h" // For CDataStream. @@ -18,7 +18,7 @@ spark::Coin fromFFI(const CCoin& c_struct); spark::Coin fromFFI(CDataStream& coinStream); -struct CCoin createCCoin(char type, const unsigned char* k, int kLength, const char* address, uint64_t v, const unsigned char* memo, int memoLength, const unsigned char* serial_context, int serial_contextLength); +CCoin createCCoin(char type, const unsigned char* k, int kLength, const char* address, uint64_t v, const unsigned char* memo, int memoLength, const unsigned char* serial_context, int serial_contextLength); CDataStream toFFI(const spark::Coin& cpp_struct); @@ -34,7 +34,7 @@ CRecipient createCRecipient(const CScript& script, CAmount amount, bool subtract CRecipient fromFFI(const CCRecipient& c_struct); -struct CCRecipient createCCRecipient(const unsigned char* pubKey, uint64_t amount, int subtractFee); +CCRecipient createCCRecipient(const unsigned char* pubKey, uint64_t amount, int subtractFee); CCRecipient toFFI(const CRecipient& cpp_struct);