├── images └── bitcoin.png ├── ZeroReserve_images.qrc ├── Router.cpp ├── RemotePaymentDialog.h ├── NewWallet.h ├── PeerAddressDialog.cpp ├── PeerAddressDialog.h ├── README.md ├── ZrDummyBitcoin.h ├── TmLocalCohorte.h ├── dbconfig.h ├── TraceRouter.cpp ├── TmLocalCoordinator.h ├── paymentdialog.h ├── frienddetailsdialog.h ├── RemotePaymentRequestDialog.h ├── TmRemoteCoordinator.h ├── TraceRouter.h ├── TmRemoteCohorte.h ├── ZeroReserveConfig.ui ├── dbconfig.cpp ├── Credit.h ├── ZrDummyBitcoin.cpp ├── BitcoinAddressList.h ├── NewWallet.cpp ├── ZeroReserveDialog.h ├── ZrSatoshiBitcoin.h ├── ZeroReservePlugin.h ├── RemotePaymentDialog.cpp ├── Credit.cpp ├── p3ZeroReserverRS.h ├── Router.h ├── TransactionManager.h ├── MyOrders.h ├── ZrLibBitcoin.h ├── paymentdialog.cpp ├── frienddetailsdialog.cpp ├── ZRBitcoin.h ├── ZeroReserve.pro ├── PeerAddressDialog.ui ├── ZrSatoshiBitcoin.cpp ├── NewWallet.ui ├── TmLocalCoordinator.cpp ├── BitcoinAddressList.cpp ├── RemotePaymentRequestDialog.ui ├── zrtypes.h ├── RemotePaymentDialog.ui ├── TmLocalCohorte.cpp ├── RemotePaymentRequestDialog.cpp ├── TmRemoteCoordinator.cpp ├── TransactionManager.cpp ├── Payment.h ├── Currency.h ├── zrdb.h ├── paymentdialog.ui ├── frienddetailsdialog.ui ├── OrderBook.h ├── Payment.cpp ├── ZeroReservePlugin.cpp ├── RSZRRemoteItems.h ├── RSZeroReserveItems.h ├── COPYING ├── TmRemoteCohorte.cpp ├── OrderBook.cpp └── p3ZeroReserveRS.cpp /images/bitcoin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/ZeroReserve/master/images/bitcoin.png -------------------------------------------------------------------------------- /ZeroReserve_images.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/bitcoin.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /Router.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "Router.h" 19 | #include "TraceRouter.h" 20 | 21 | Router * Router::instance = 0; 22 | 23 | 24 | 25 | Router * Router::Instance() 26 | { 27 | if( !Router::instance ) { 28 | Router::instance = new TraceRouter(); 29 | } 30 | return instance; 31 | } 32 | -------------------------------------------------------------------------------- /RemotePaymentDialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef REMOTEPAYMENTDIALOG_H 19 | #define REMOTEPAYMENTDIALOG_H 20 | 21 | #include 22 | 23 | namespace Ui { 24 | class RemotePaymentDialog; 25 | } 26 | 27 | class RemotePaymentDialog : public QDialog 28 | { 29 | Q_OBJECT 30 | 31 | public: 32 | explicit RemotePaymentDialog(QWidget *parent = 0); 33 | ~RemotePaymentDialog(); 34 | 35 | private slots: 36 | void payTo(); 37 | void loadPayment(QString address); 38 | 39 | private: 40 | Ui::RemotePaymentDialog *ui; 41 | }; 42 | 43 | #endif // REMOTEPAYMENTDIALOG_H 44 | -------------------------------------------------------------------------------- /NewWallet.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef NEWWALLET_H 20 | #define NEWWALLET_H 21 | 22 | #include "ZRBitcoin.h" 23 | 24 | #include 25 | 26 | namespace Ui { 27 | class NewWallet; 28 | } 29 | 30 | class NewWallet : public QDialog 31 | { 32 | Q_OBJECT 33 | 34 | public: 35 | 36 | explicit NewWallet(QWidget *parent = 0); 37 | ~NewWallet(); 38 | 39 | QString m_seed; 40 | ZR::MyWallet::WalletType m_walletType; 41 | 42 | private slots: 43 | void wallet(); 44 | void makeSeed( bool enabled ); 45 | 46 | 47 | private: 48 | Ui::NewWallet *ui; 49 | }; 50 | 51 | #endif // NEWWALLET_H 52 | -------------------------------------------------------------------------------- /PeerAddressDialog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "PeerAddressDialog.h" 19 | #include "ui_PeerAddressDialog.h" 20 | 21 | PeerAddressDialog::PeerAddressDialog(QWidget *parent) : 22 | QDialog(parent), 23 | ui(new Ui::PeerAddressDialog) 24 | { 25 | ui->setupUi(this); 26 | connect( ui->buttonBox, SIGNAL(accepted()), this, SLOT( setPeerAddress() ) ); 27 | } 28 | 29 | PeerAddressDialog::~PeerAddressDialog() 30 | { 31 | delete ui; 32 | } 33 | 34 | 35 | void PeerAddressDialog::setPeerAddress() 36 | { 37 | m_nick = ui->NickName->text().toStdString(); 38 | m_address = ui->peerAddress->text().toStdString(); 39 | } 40 | -------------------------------------------------------------------------------- /PeerAddressDialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef PEERADDRESSDIALOG_H 20 | #define PEERADDRESSDIALOG_H 21 | 22 | #include "zrtypes.h" 23 | 24 | #include 25 | 26 | namespace Ui { 27 | class PeerAddressDialog; 28 | } 29 | 30 | class PeerAddressDialog : public QDialog 31 | { 32 | Q_OBJECT 33 | 34 | public: 35 | explicit PeerAddressDialog(QWidget *parent = 0); 36 | ~PeerAddressDialog(); 37 | 38 | 39 | private slots: 40 | void setPeerAddress(); 41 | 42 | public: 43 | Ui::PeerAddressDialog *ui; 44 | 45 | ZR::BitcoinAddress m_address; 46 | std::string m_nick; 47 | }; 48 | 49 | #endif // PEERADDRESSDIALOG_H 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ZeroReserve 2 | =========== 3 | 4 | Friend 2 Friend Payment and Bitcoin exchange 5 | 6 | Prerequisite for building is a successful RetroShare build and sqlite3 and libnmcrpc 7 | RetroShare is available from http://retroshare.sourceforge.net/ 8 | sqlite3 is probably part of your Linux distribution. If you are on Windows, 9 | get it from http://www.sqlite.org/ 10 | 11 | Get libnmcrpc directly from git: 12 | ``` 13 | $ git clone git@gitorious.org:libnmcrpc/libnmcrpc.git 14 | ``` 15 | 16 | Then configure with : 17 | ``` 18 | $ autogen.sh 19 | $ ./configure 20 | ``` 21 | 22 | To build, checkout the sources to the plugins directory of Retroshare and build with: 23 | ``` 24 | $ qmake && make clean && make 25 | ``` 26 | 27 | To install on Windows, drop the resulting DLL into the 28 | %APPDATA%\Retroshare\extensions directory. 29 | 30 | To install on Linux or MacOS, drop the resulting shared object into 31 | ~/.retroshare/extensions 32 | 33 | Running ZeroReserve requires a running Satoshi Client first: 34 | ``` 35 | $ ./bitcoind 36 | or 37 | $ ./bitcoin-qt -server # that shows the GUI 38 | 39 | ``` 40 | This is an example bitcoin.conf with all entries required: 41 | ``` 42 | testnet=1 43 | rpcuser=anu 44 | rpcpassword=mysupersecretpassword 45 | rpcport=18332 46 | ``` 47 | 48 | 49 | This is experimental software. Use at your own risk. At this stage, leave TestNet 50 | on. Don't enable real currencies. 51 | 52 | Building and running on MacOS may or may not work at this point. 53 | -------------------------------------------------------------------------------- /ZrDummyBitcoin.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | 20 | #ifndef ZRDUMMYBITCOIN_H 21 | #define ZRDUMMYBITCOIN_H 22 | 23 | #include "zrtypes.h" 24 | #include "ZRBitcoin.h" 25 | 26 | 27 | class ZrDummyBitcoin : public ZR::Bitcoin 28 | { 29 | public: 30 | ZrDummyBitcoin(); 31 | 32 | virtual ZR::RetVal start(); 33 | virtual ZR::RetVal stop(); 34 | virtual ZR::RetVal commit(); 35 | virtual ZR::ZR_Number getBalance(); 36 | virtual ZR::MyWallet * mkWallet( ZR::MyWallet::WalletType wType ); 37 | virtual void send( const std::string & dest, const ZR::ZR_Number & amount ); 38 | virtual void loadWallets( std::vector< ZR::MyWallet *> & wallets ); 39 | }; 40 | 41 | #endif // ZRDUMMYBITCOIN_H 42 | -------------------------------------------------------------------------------- /TmLocalCohorte.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef TMLOCALCOHORTE_H 20 | #define TMLOCALCOHORTE_H 21 | 22 | #include "zrtypes.h" 23 | #include "TransactionManager.h" 24 | 25 | class RsZeroReserveInitTxItem; 26 | class Payment; 27 | 28 | class TmLocalCohorte : public TransactionManager 29 | { 30 | public: 31 | TmLocalCohorte( const ZR::TransactionId & txId ); 32 | virtual ~TmLocalCohorte(); 33 | 34 | virtual ZR::RetVal init(); 35 | 36 | protected: 37 | virtual ZR::RetVal processItem( RsZeroReserveTxItem * item ); 38 | virtual ZR::RetVal abortTx( RsZeroReserveTxItem * item ); 39 | 40 | virtual void rollback(); 41 | 42 | private: 43 | Payment * m_payment; 44 | }; 45 | 46 | #endif // TMLOCALCOHORTE_H 47 | -------------------------------------------------------------------------------- /dbconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef DBCONFIG_H 19 | #define DBCONFIG_H 20 | 21 | #include "ui_ZeroReserveConfig.h" 22 | 23 | #include "retroshare-gui/configpage.h" 24 | 25 | 26 | class DBConfig : public ConfigPage 27 | { 28 | Q_OBJECT 29 | public: 30 | DBConfig( QWidget * parent = 0, Qt::WFlags flags = 0 ); 31 | 32 | virtual bool save(QString &errmsg); 33 | virtual void load(); 34 | 35 | virtual QPixmap iconPixmap() const { return QPixmap(":/images/bitcoin.png") ; } 36 | virtual QString pageName() const { return "Zero Reserve"; } 37 | virtual QString helpText() const { return ""; } 38 | 39 | 40 | private slots: 41 | void editTxLog(); 42 | 43 | private: 44 | Ui::DBConfig ui; 45 | }; 46 | 47 | #endif // DBCONFIG_H 48 | -------------------------------------------------------------------------------- /TraceRouter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "TraceRouter.h" 19 | 20 | TraceRouter::TraceRouter() 21 | { 22 | } 23 | 24 | void TraceRouter::addRoute( const ZR::VirtualAddress &dest, const std::string & gateway ) 25 | { 26 | routingTable[ dest ] = gateway; 27 | } 28 | 29 | 30 | const std::string TraceRouter::nextHop( const ZR::VirtualAddress &dest ) 31 | { 32 | RoutingTable::const_iterator it = routingTable.find( dest ); 33 | if( it != routingTable.end() ){ 34 | return (*it).second; 35 | } 36 | 37 | return std::string(); 38 | } 39 | 40 | bool TraceRouter::hasRoute( const ZR::VirtualAddress & dest ) 41 | { 42 | if( routingTable.find( dest ) == routingTable.end() ){ 43 | return false; 44 | } 45 | return true; 46 | } 47 | -------------------------------------------------------------------------------- /TmLocalCoordinator.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef TMLOCALCOORDINATOR_H 19 | #define TMLOCALCOORDINATOR_H 20 | 21 | #include "TransactionManager.h" 22 | 23 | class Payment; 24 | 25 | class TmLocalCoordinator : public TransactionManager 26 | { 27 | public: 28 | TmLocalCoordinator( Payment * payment ); 29 | virtual ~TmLocalCoordinator(); 30 | 31 | virtual ZR::RetVal init(); 32 | 33 | protected: 34 | virtual ZR::RetVal processItem( RsZeroReserveTxItem * item ); 35 | virtual ZR::RetVal abortTx( RsZeroReserveTxItem * item ); 36 | 37 | virtual void rollback(); 38 | 39 | static const ZR::TransactionId mkId(); 40 | 41 | private: 42 | Payment * m_payment; 43 | 44 | static unsigned int sequence; 45 | }; 46 | 47 | #endif // TMLOCALCOORDINATOR_H 48 | -------------------------------------------------------------------------------- /paymentdialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef PAYMENTDIALOG_H 19 | #define PAYMENTDIALOG_H 20 | 21 | #include "zrtypes.h" 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | namespace Ui { 29 | class PaymentDialog; 30 | } 31 | 32 | class PaymentDialog : public QDialog 33 | { 34 | Q_OBJECT 35 | 36 | public: 37 | explicit PaymentDialog( const std::string & payee, QWidget *parent = 0 ); 38 | ~PaymentDialog(); 39 | 40 | private slots: 41 | void payTo(); 42 | void loadAvailableFunds( QString arg = "" ); 43 | 44 | private: 45 | ZR::ZR_Number availableFunds(); 46 | 47 | Ui::PaymentDialog *ui; 48 | const std::string m_payee; 49 | }; 50 | 51 | #endif // PAYMENTDIALOG_H 52 | -------------------------------------------------------------------------------- /frienddetailsdialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef FRIENDDETAILSDIALOG_H 19 | #define FRIENDDETAILSDIALOG_H 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace Ui { 26 | class FriendDetailsDialog; 27 | } 28 | 29 | class FriendDetailsDialog : public QDialog 30 | { 31 | Q_OBJECT 32 | 33 | public: 34 | explicit FriendDetailsDialog( const std::string & id, QWidget *parent = 0, const std::string & name = "Error. No id selected!" ); 35 | ~FriendDetailsDialog(); 36 | 37 | private: 38 | Ui::FriendDetailsDialog *ui; 39 | std::string m_id; 40 | 41 | protected slots: 42 | void editFriend(); 43 | void loadPeer( QString currency = "" ); 44 | }; 45 | 46 | #endif // FRIENDDETAILSDIALOG_H 47 | -------------------------------------------------------------------------------- /RemotePaymentRequestDialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef REMOTEPAYMENTREQUESTDIALOG_H 19 | #define REMOTEPAYMENTREQUESTDIALOG_H 20 | 21 | #include 22 | 23 | namespace Ui { 24 | class RemotePaymentRequestDialog; 25 | } 26 | 27 | class RemotePaymentRequestDialog : public QDialog 28 | { 29 | Q_OBJECT 30 | 31 | public: 32 | explicit RemotePaymentRequestDialog(QWidget *parent = 0); 33 | ~RemotePaymentRequestDialog(); 34 | 35 | private slots: 36 | void currencySelected( QString currency ); 37 | void amountEntered(QString amount ); 38 | void sendRequest(); 39 | 40 | private: 41 | QString getPayAddress( QString amount, QString currency ); 42 | 43 | private: 44 | Ui::RemotePaymentRequestDialog *ui; 45 | }; 46 | 47 | #endif // REMOTEPAYMENTREQUESTDIALOG_H 48 | -------------------------------------------------------------------------------- /TmRemoteCoordinator.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef TMREMOTECOORDINATOR_H 19 | #define TMREMOTECOORDINATOR_H 20 | 21 | #include "TransactionManager.h" 22 | #include "zrtypes.h" 23 | 24 | class Payment; 25 | 26 | 27 | class TmRemoteCoordinator : public TransactionManager 28 | { 29 | TmRemoteCoordinator(); 30 | public: 31 | TmRemoteCoordinator(const ZR::VirtualAddress &addr, Payment * payment, const std::string &myId); 32 | virtual ~TmRemoteCoordinator(); 33 | 34 | virtual ZR::RetVal init(); 35 | virtual ZR::RetVal processItem( RSZRRemoteTxItem * item ); 36 | virtual ZR::RetVal abortTx( RSZRRemoteTxItem * item ); 37 | 38 | virtual void rollback(); 39 | 40 | private: 41 | ZR::VirtualAddress m_Destination; 42 | Payment * m_Payment; 43 | std::string m_myId; 44 | }; 45 | 46 | #endif // TMREMOTECOORDINATOR_H 47 | -------------------------------------------------------------------------------- /TraceRouter.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef TRACEROUTER_H 19 | #define TRACEROUTER_H 20 | 21 | #include "Router.h" 22 | #include "zrtypes.h" 23 | 24 | #include 25 | #include 26 | 27 | /** 28 | * @brief Very simple router class, gathering routing information from items that propagate through the net 29 | */ 30 | 31 | class TraceRouter : public Router 32 | { 33 | public: 34 | TraceRouter(); 35 | 36 | typedef std::map< ZR::VirtualAddress, std::string > RoutingTable; 37 | 38 | virtual void addRoute( const ZR::VirtualAddress & dest, const std::string & gateway ); 39 | virtual const std::string nextHop( const ZR::VirtualAddress & dest ); 40 | virtual bool hasRoute(const ZR::VirtualAddress &dest ); 41 | 42 | protected: 43 | /** map destinions to gateways */ 44 | RoutingTable routingTable; 45 | }; 46 | 47 | #endif // TRACEROUTER_H 48 | -------------------------------------------------------------------------------- /TmRemoteCohorte.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef TMREMOTECOHORTE_H 20 | #define TMREMOTECOHORTE_H 21 | 22 | 23 | #include "TransactionManager.h" 24 | 25 | class RSZRRemoteTxInitItem; 26 | class Payment; 27 | 28 | class TmRemoteCohorte : public TransactionManager 29 | { 30 | public: 31 | TmRemoteCohorte( const ZR::TransactionId &txId ); 32 | virtual ZR::RetVal init(); 33 | 34 | protected: 35 | virtual ZR::RetVal processItem( RSZRRemoteTxItem *item ); 36 | virtual ZR::RetVal abortTx( RSZRRemoteTxItem * item ); 37 | 38 | virtual void rollback(); 39 | 40 | ZR::RetVal forwardItem( RSZRRemoteTxItem *item ); 41 | 42 | private: 43 | ZR::RetVal setup( RSZRRemoteTxInitItem *item ); 44 | ZR::RetVal isPayee( const ZR::VirtualAddress & addr ); 45 | 46 | bool m_IsHop; 47 | Payment * m_PaymentReceiver; 48 | Payment * m_PaymentSpender; 49 | }; 50 | 51 | #endif // TMREMOTECOHORTE_H 52 | -------------------------------------------------------------------------------- /ZeroReserveConfig.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | DBConfig 4 | 5 | 6 | 7 | 0 8 | 0 9 | 407 10 | 305 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | <html><head/><body><p>This should be located on a separate device, so if you lose your book, you can recreate it from a backup and this log.</p></body></html> 18 | 19 | 20 | 21 | 22 | 70 23 | 90 24 | 221 25 | 41 26 | 27 | 28 | 29 | <html><head/><body><p>It is advisable to place the transaction log (TX log) on a separate device - maybe a USB stick or a memory card. That way you can always re-create your book.</p></body></html> 30 | 31 | 32 | Set the transaction log path 33 | 34 | 35 | 36 | 37 | 38 | 104 39 | 130 40 | 131 41 | 31 42 | 43 | 44 | 45 | TX log path... 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /dbconfig.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "dbconfig.h" 19 | #include "zrdb.h" 20 | 21 | #include "QFileDialog" 22 | #include "QFileInfo" 23 | 24 | 25 | DBConfig::DBConfig( QWidget *parent, Qt::WFlags flags ) : 26 | ConfigPage(parent, flags) 27 | { 28 | ui.setupUi( this ); 29 | connect( ui.TxLogPathButton, SIGNAL(clicked()), this, SLOT(editTxLog()) ); 30 | } 31 | 32 | 33 | bool DBConfig::save(QString &errmsg) 34 | { 35 | return true; 36 | } 37 | 38 | void DBConfig::load() 39 | { 40 | 41 | } 42 | 43 | void DBConfig::editTxLog() 44 | { 45 | QString txLogPath = QString::fromStdString( ZrDB::Instance()->getConfig( ZrDB::TXLOGPATH ) ); 46 | QFileInfo fileInfo( txLogPath ); 47 | QString newPath = QFileDialog::getSaveFileName( 0, "Set the Transaction Log", fileInfo.absoluteDir().absolutePath(), "Transaction Log (*.tx)" ); 48 | if( newPath.isEmpty() ) 49 | return; 50 | 51 | ZrDB::Instance()->updateConfig( ZrDB::TXLOGPATH, newPath.toStdString() ); 52 | } 53 | -------------------------------------------------------------------------------- /Credit.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef CREDIT_H 19 | #define CREDIT_H 20 | 21 | #include "zrtypes.h" 22 | 23 | #include 24 | #include 25 | 26 | class Credit 27 | { 28 | Credit(); 29 | public: 30 | typedef std::list< Credit * > CreditList; 31 | 32 | 33 | Credit( const std::string & id, const std::string & currencySym ); 34 | 35 | void updateCredit(); 36 | void loadPeer(); 37 | void publish(); 38 | void updateOurCredit(); 39 | void updateBalance(); 40 | 41 | static void getCreditList( CreditList & outList, const std::string & id ); 42 | 43 | ZR::ZR_Number getPeerAvailable(){ return m_credit - m_balance; } 44 | ZR::ZR_Number getMyAvailable(){ return m_our_credit + m_balance; } 45 | 46 | public: 47 | std::string m_id; 48 | std::string m_currency; 49 | ZR::ZR_Number m_our_credit; // our credit with peer 50 | ZR::ZR_Number m_credit; // their credit with us 51 | ZR::ZR_Number m_balance; // negative means we owe them money 52 | }; 53 | 54 | #endif // CREDIT_H 55 | -------------------------------------------------------------------------------- /ZrDummyBitcoin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | 20 | 21 | #include "ZrDummyBitcoin.h" 22 | 23 | 24 | ZR::Bitcoin * ZR::Bitcoin::instance = NULL; 25 | 26 | 27 | ZR::Bitcoin * ZR::Bitcoin::Instance() 28 | { 29 | if( instance == NULL ){ 30 | instance = new ZrDummyBitcoin(); 31 | } 32 | return instance; 33 | } 34 | 35 | 36 | ZrDummyBitcoin::ZrDummyBitcoin() 37 | { 38 | } 39 | 40 | ZR::RetVal ZrDummyBitcoin::start() 41 | { 42 | return ZR::ZR_SUCCESS; 43 | } 44 | 45 | ZR::RetVal ZrDummyBitcoin::stop() 46 | { 47 | return ZR::ZR_SUCCESS; 48 | } 49 | 50 | 51 | ZR::RetVal ZrDummyBitcoin::commit() 52 | { 53 | return ZR::ZR_SUCCESS; 54 | } 55 | 56 | 57 | ZR::ZR_Number ZrDummyBitcoin::getBalance() 58 | { 59 | return 0; 60 | } 61 | 62 | ZR::MyWallet * ZrDummyBitcoin::mkWallet( ZR::MyWallet::WalletType wType ) 63 | { 64 | return NULL; 65 | } 66 | 67 | void ZrDummyBitcoin::send( const std::string & , const ZR::ZR_Number & ) 68 | {} 69 | 70 | 71 | void ZrDummyBitcoin::loadWallets( std::vector< ZR::MyWallet *> & wallets ) 72 | {} 73 | -------------------------------------------------------------------------------- /BitcoinAddressList.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef BITCOINADDRESSLIST_H 19 | #define BITCOINADDRESSLIST_H 20 | 21 | #include "ZRBitcoin.h" 22 | 23 | #include 24 | 25 | #include 26 | 27 | class BitcoinAddressList : public QAbstractItemModel 28 | { 29 | Q_OBJECT 30 | public: 31 | enum WalletType 32 | { 33 | MYWALLET, 34 | PEERWALLET 35 | }; 36 | 37 | explicit BitcoinAddressList(QObject *parent = 0); 38 | 39 | virtual QModelIndex index(int, int, const QModelIndex&) const; 40 | virtual QModelIndex parent(const QModelIndex&) const; 41 | virtual int rowCount(const QModelIndex&) const; 42 | virtual int columnCount(const QModelIndex&) const; 43 | virtual QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const; 44 | virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; 45 | 46 | void addWallet( ZR::Wallet * wallet ); 47 | void loadWallets(); 48 | 49 | 50 | signals: 51 | 52 | public slots: 53 | 54 | 55 | private: 56 | QList< ZR::Wallet* > m_walletList; 57 | WalletType m_walletType; 58 | }; 59 | 60 | #endif // BITCOINADDRESSLIST_H 61 | -------------------------------------------------------------------------------- /NewWallet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "NewWallet.h" 19 | #include "ui_NewWallet.h" 20 | 21 | #include "ZRBitcoin.h" 22 | 23 | NewWallet::NewWallet(QWidget *parent) : 24 | QDialog(parent), 25 | ui(new Ui::NewWallet) 26 | { 27 | ui->setupUi(this); 28 | connect( ui->buttonBox, SIGNAL(accepted()), this, SLOT( wallet() ) ); 29 | connect( ui->newElectrum, SIGNAL( toggled( bool ) ), this, SLOT(makeSeed( bool ) ) ); 30 | m_walletType = ZR::MyWallet::INVALID; 31 | } 32 | 33 | NewWallet::~NewWallet() 34 | { 35 | delete ui; 36 | } 37 | 38 | 39 | void NewWallet::makeSeed( bool enabled ) 40 | { 41 | if( !enabled ){ 42 | ui->seed->setText( "" ); 43 | return; 44 | } 45 | ZR::MyWallet * wallet = ZR::Bitcoin::Instance()->mkWallet( ZR::MyWallet::ELECTRUMSEED ); 46 | ZR::WalletSeed seed = wallet->seed(); 47 | ui->seed->setText( QString::fromStdString( seed ) ); 48 | } 49 | 50 | void NewWallet::wallet() 51 | { 52 | m_seed = ui->seed->text(); 53 | if( ui->brainWallet->isEnabled() ){ 54 | m_walletType = ZR::MyWallet::BRAINWALLET; 55 | } 56 | else if( ui->importElectrum->isEnabled() || ui->newElectrum->isEnabled() ) { 57 | m_walletType = ZR::MyWallet::ELECTRUMSEED; 58 | } 59 | else { 60 | m_walletType = ZR::MyWallet::INVALID; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ZeroReserveDialog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef ZERORESERVEDIALOG_H 20 | #define ZERORESERVEDIALOG_H 21 | 22 | 23 | #include "retroshare-gui/mainpage.h" 24 | #include "ui_ZeroReserveDialog.h" 25 | #include "OrderBook.h" 26 | 27 | #include 28 | #include 29 | 30 | 31 | class ZeroReserveDialog : public MainPage 32 | { 33 | Q_OBJECT 34 | 35 | public: 36 | ZeroReserveDialog( OrderBook * bids, OrderBook * asks, QWidget *parent = 0 ); 37 | void updateFriendList(); 38 | 39 | 40 | public slots: 41 | void addBid(); 42 | void addAsk(); 43 | 44 | private slots: 45 | void contextMenuFriendList(QPoint); 46 | void friendDetails(); 47 | void payTo(); 48 | void loadGrandTotal(); 49 | void cancelOrder(); 50 | void contextMenuMyOrders(const QPoint & ); 51 | void remoteRequest(); 52 | void remotePayment(); 53 | 54 | // Bitcoin Wallet 55 | void contextMenuMyAddresses( const QPoint & ); 56 | void newWallet(); 57 | void sendBTCTo(); 58 | 59 | void contextMenuPeerAddresses( const QPoint & ); 60 | void newPeerAddress(); 61 | void refreshWallet(); 62 | 63 | 64 | private: 65 | void doOrder(OrderBook * book, OrderBook::Order::OrderType type, ZR::ZR_Number price, ZR::ZR_Number amount ); 66 | void loadTxLog(); 67 | 68 | Ui::ZeroReserveDialog ui; 69 | 70 | }; 71 | 72 | #endif // ZERORESERVEDIALOG_H 73 | -------------------------------------------------------------------------------- /ZrSatoshiBitcoin.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ZRSATOSHIBITCOIN_H 19 | #define ZRSATOSHIBITCOIN_H 20 | 21 | #include "ZRBitcoin.h" 22 | 23 | #include "JsonRpc.hpp" 24 | 25 | 26 | class ZrSatoshiBitcoin : public ZR::Bitcoin 27 | { 28 | public: 29 | ZrSatoshiBitcoin(); 30 | 31 | virtual ZR::RetVal commit(); 32 | virtual ZR::RetVal start(); 33 | virtual ZR::RetVal stop(); 34 | virtual ZR::ZR_Number getBalance(); 35 | 36 | virtual ZR::MyWallet * mkWallet( ZR::MyWallet::WalletType wType ); 37 | virtual void loadWallets( std::vector< ZR::MyWallet *> & wallets ); 38 | 39 | virtual void send( const std::string & dest, const ZR::ZR_Number & amount ); 40 | 41 | public: 42 | nmcrpc::RpcSettings m_settings; 43 | }; 44 | 45 | 46 | 47 | class SatoshiWallet : public ZR::MyWallet 48 | { 49 | public: 50 | SatoshiWallet( const ZR::BitcoinAddress & address, const ZR::ZR_Number & balance ) : 51 | ZR::MyWallet( WIFIMPORT ), 52 | m_Address( address ), 53 | m_Balance( balance ) 54 | {} 55 | virtual ZR::BitcoinAddress getAddress(){ return m_Address; } 56 | virtual ZR::ZR_Number getBalance(){ return m_Balance; } 57 | virtual ZR::RetVal persist(){ return ZR::ZR_SUCCESS; } 58 | virtual ZR::WalletSeed seed(){ return ""; } 59 | virtual void setSeed( const ZR::WalletSeed & ){} 60 | virtual ZR::RetVal getSecret( ZR::WalletSecret & ){ return ZR::ZR_SUCCESS; } 61 | 62 | 63 | private: 64 | ZR::BitcoinAddress m_Address; 65 | ZR::ZR_Number m_Balance; 66 | }; 67 | 68 | 69 | #endif // ZRSATOSHIBITCOIN_H 70 | -------------------------------------------------------------------------------- /ZeroReservePlugin.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ZERORESERVEPLUGIN_H 19 | #define ZERORESERVEPLUGIN_H 20 | 21 | #include 22 | #include 23 | #include "util/rsthreads.h" 24 | 25 | 26 | class OrderBook; 27 | class p3ZeroReserveRS; 28 | class ConfigPage; 29 | 30 | class ZeroReservePlugin: public RsPlugin 31 | { 32 | public: 33 | ZeroReservePlugin() ; 34 | virtual ~ZeroReservePlugin() {} 35 | 36 | virtual MainPage *qt_page() const ; 37 | virtual QIcon *qt_icon() const ; 38 | virtual QTranslator *qt_translator(QApplication *app, const QString& languageCode, const QString& externalDir) const; 39 | virtual QDialog * qt_about_page() const; 40 | virtual ConfigPage *qt_config_page() const ; 41 | virtual void stop(); 42 | 43 | virtual void getPluginVersion(int& major,int& minor,int& svn_rev) const ; 44 | virtual void setPlugInHandler(RsPluginHandler *pgHandler); 45 | 46 | virtual std::string configurationFileName() const { return "zeroreserve.cfg" ; } 47 | 48 | virtual std::string getShortPluginDescription() const ; 49 | virtual std::string getPluginName() const; 50 | virtual RsPQIService * rs_pqi_service() const; 51 | virtual void setInterfaces(RsPlugInInterfaces& interfaces); 52 | private: 53 | mutable RsPluginHandler *mPlugInHandler; 54 | mutable MainPage* mainpage ; 55 | mutable QIcon* mIcon ; 56 | mutable RsPeers* m_peers; 57 | OrderBook * m_asks; 58 | OrderBook * m_bids; 59 | mutable p3ZeroReserveRS * m_ZeroReserve; 60 | 61 | static RsMutex widget_creation_mutex; 62 | }; 63 | 64 | extern ZeroReservePlugin * g_ZeroReservePlugin; 65 | 66 | 67 | #endif 68 | 69 | -------------------------------------------------------------------------------- /RemotePaymentDialog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "RemotePaymentDialog.h" 19 | #include "ui_RemotePaymentDialog.h" 20 | 21 | #include "Payment.h" 22 | #include "TmRemoteCoordinator.h" 23 | #include "Router.h" 24 | 25 | #include "QMessageBox" 26 | 27 | RemotePaymentDialog::RemotePaymentDialog(QWidget *parent) : 28 | QDialog(parent), 29 | ui(new Ui::RemotePaymentDialog) 30 | { 31 | ui->setupUi(this); 32 | connect( ui->destination, SIGNAL(textEdited(QString)), this, SLOT( loadPayment( QString ) ) ); 33 | connect( ui->buttonBox, SIGNAL( accepted() ), this, SLOT( payTo() ) ); 34 | } 35 | 36 | RemotePaymentDialog::~RemotePaymentDialog() 37 | { 38 | delete ui; 39 | } 40 | 41 | 42 | void RemotePaymentDialog::payTo() 43 | { 44 | Currency::CurrencySymbols sym = Currency::getCurrencyByName( ui->currency->text().toStdString() ); 45 | const std::string & nextHop = Router::Instance()->nextHop( ui->destination->text().toStdString() ); 46 | if( nextHop.empty() ){ 47 | QMessageBox::critical(0, "Router Error", "No route to destination" ); 48 | return; 49 | } 50 | Payment * payment = new PaymentSpender( nextHop, ZR::ZR_Number::fromDecimalString( ui->amount->text() ), Currency::currencySymbols[ sym ], Payment::PAYMENT ); 51 | TmRemoteCoordinator * tm = new TmRemoteCoordinator( ui->destination->text().toStdString(), payment, "" ); 52 | if( ! tm->init() ) delete tm; 53 | } 54 | 55 | void RemotePaymentDialog::loadPayment( QString address ) 56 | { 57 | Payment::Request req = Payment::getRequest( address.toStdString() ); 58 | ui->amount->setText( req.m_Amount.toDecimalQString() ); 59 | ui->currency->setText( Currency::currencyNames[ req.m_Currency ] ); 60 | } 61 | -------------------------------------------------------------------------------- /Credit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "Credit.h" 20 | 21 | #include "zrdb.h" 22 | #include "Currency.h" 23 | #include "ZeroReservePlugin.h" 24 | #include "p3ZeroReserverRS.h" 25 | 26 | #include 27 | 28 | 29 | void Credit::getCreditList( CreditList & outList, const std::string & id ) 30 | { 31 | ZrDB::Instance()->loadPeer( id, outList ); 32 | } 33 | 34 | 35 | 36 | Credit::Credit( const std::string & id, const std::string & currencySym ) : 37 | m_id( id ), 38 | m_currency( currencySym ) 39 | { 40 | if( Currency::getCurrencyBySymbol( currencySym) == Currency::INVALID ){ 41 | throw std::runtime_error( "Credit::Credit(): Invalid currency symbol" ); 42 | } 43 | } 44 | 45 | 46 | // TODO: Check for SQL injection 47 | 48 | void Credit::updateCredit() 49 | { 50 | ZrDB * db = ZrDB::Instance(); 51 | if( !db->peerExists( *this )){ 52 | db->createPeerRecord( *this ); 53 | } 54 | db->updatePeerCredit( *this, "credit", m_credit ); 55 | } 56 | 57 | void Credit::updateOurCredit() 58 | { 59 | ZrDB * db = ZrDB::Instance(); 60 | if( !db->peerExists( *this )){ 61 | db->createPeerRecord( *this ); 62 | } 63 | db->updatePeerCredit( *this, "our_credit", m_our_credit ); 64 | } 65 | 66 | void Credit::updateBalance() 67 | { 68 | ZrDB * db = ZrDB::Instance(); 69 | if( !db->peerExists( *this )){ 70 | db->createPeerRecord( *this ); 71 | } 72 | db->updatePeerCredit( *this, "balance", m_balance ); 73 | } 74 | 75 | void Credit::loadPeer() 76 | { 77 | ZrDB::Instance()->loadPeer( *this ); 78 | } 79 | 80 | void Credit::publish() 81 | { 82 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 83 | p3zr->sendCredit( this ); 84 | } 85 | -------------------------------------------------------------------------------- /p3ZeroReserverRS.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef P3ZERORESERVERRS_H 19 | #define P3ZERORESERVERRS_H 20 | 21 | 22 | #include "RSZeroReserveItems.h" 23 | 24 | #include "retroshare/rspeers.h" 25 | #include "plugins/rspqiservice.h" 26 | #include "pqi/pqimonitor.h" 27 | #include "RSZRRemoteItems.h" 28 | 29 | 30 | 31 | 32 | class RsPluginHandler; 33 | class OrderBook; 34 | class RsPeers; 35 | 36 | class p3ZeroReserveRS : public RsPQIService, public pqiMonitor 37 | { 38 | public: 39 | p3ZeroReserveRS(RsPluginHandler *pgHandler, OrderBook * bids, OrderBook * asks, RsPeers* peers); 40 | 41 | virtual int tick(); 42 | 43 | /** send a message to buy to a buyer */ 44 | ZR::RetVal sendBuyMsg( const ZR::VirtualAddress & ourAddress, const ZR::VirtualAddress & theirAddress, const ZR::ZR_Number & amount ); 45 | bool sendOrder( const std::string& peer_id, OrderBook::Order * order ); 46 | bool sendCredit( Credit * credit ); 47 | void publishOrder( OrderBook::Order * order ); 48 | std::string getOwnId(){ return m_peers->getOwnId(); } 49 | virtual void statusChange(const std::list &plist); 50 | 51 | // remote stuff 52 | void sendRemote( const ZR::VirtualAddress & address, ZR::ZR_Number amount, const std::string & currency ); 53 | 54 | private: 55 | 56 | void processIncoming(); 57 | void janitor(); 58 | void sendPackets(); 59 | void handleOrder( RsZeroReserveOrderBookItem *item ); 60 | void handleCredit( RsZeroReserveCreditItem *item ); 61 | void handleMessage( RsZeroReserveMsgItem *item ); 62 | void handlePaymentRequest( RSZRPayRequestItem * item ); 63 | void handleBuyRequest( RSZRBuyRequestItem * item ); 64 | 65 | 66 | 67 | /** help our friends to bootstrap the order book */ 68 | void sendOrderBook(const std::string &uid); 69 | 70 | private: 71 | OrderBook * m_bids; 72 | OrderBook * m_asks; 73 | RsPeers * m_peers; 74 | int m_initialized; 75 | }; 76 | 77 | #endif // P3ZERORESERVERRS_H 78 | -------------------------------------------------------------------------------- /Router.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ROUTER_H 19 | #define ROUTER_H 20 | 21 | #include "zrtypes.h" 22 | 23 | #include 24 | #include 25 | 26 | 27 | /** 28 | * @brief Interface class for routers 29 | * 30 | * A route is a possible tunnel, pointing to the source of some data, for example the origin of 31 | * an order. Each node on the network knows only the next hop of that route. 32 | * 33 | * A tunnel is a route that is currently in use. As it is 2-directional, each node knows the next 34 | * hop in both direction. Tunnels exist only as long as 2 distant nodes, i.e. nodes that are not 35 | * friends, need to communicate. 36 | */ 37 | 38 | class Router 39 | { 40 | public: 41 | typedef std::map< ZR::VirtualAddress, std::pair< std::string, std::string > > TunnelList; 42 | enum TunnelDirection { 43 | SERVER = 0, 44 | CLIENT 45 | }; 46 | 47 | virtual void addRoute( const ZR::VirtualAddress & dest, const std::string & gateway ) = 0; 48 | 49 | /** Query next hop of a route */ 50 | virtual const std::string nextHop( const ZR::VirtualAddress & dest ) = 0; 51 | virtual bool hasRoute( const ZR::VirtualAddress & dest ) = 0; 52 | 53 | /** 54 | * adds a virtual tunnel 55 | * @arg dest: the virtual address 56 | * @arg gateways: a pair of the IDs of the hop forward and the hop backward 57 | */ 58 | virtual void addTunnel( const ZR::VirtualAddress & dest, std::pair< ZR::PeerAddress, ZR::PeerAddress > & gateways ){ m_Tunnels[ dest ] = gateways; } 59 | virtual ZR::RetVal getTunnel( const ZR::VirtualAddress & dest, std::pair< ZR::PeerAddress, ZR::PeerAddress > & gateways ) 60 | { 61 | TunnelList::iterator it = m_Tunnels.find( dest ); 62 | if( it == m_Tunnels.end() ) 63 | return ZR::ZR_FAILURE; 64 | gateways = (*it).second; 65 | return ZR::ZR_SUCCESS; 66 | } 67 | 68 | static Router * Instance(); 69 | 70 | 71 | protected: 72 | TunnelList m_Tunnels; 73 | 74 | static Router * instance; 75 | }; 76 | 77 | #endif // ROUTER_H 78 | -------------------------------------------------------------------------------- /TransactionManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef TRANSACTIONMANAGER_H 20 | #define TRANSACTIONMANAGER_H 21 | 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | class RsZeroReserveTxItem; 29 | class RsZeroReserveInitTxItem; 30 | class RSZRRemoteTxItem; 31 | 32 | /** 33 | Manage multi hop transaction. The payer is the coordiantor, all in between 34 | nodes and the payee are cohorts. Cancel TX if an in-between node tries to 35 | cheat or if an in-between node goes away during the TX 36 | */ 37 | 38 | class TransactionManager 39 | { 40 | TransactionManager( const TransactionManager &); 41 | TransactionManager(); 42 | public: 43 | typedef std::map< std::string, TransactionManager *> TxManagers; 44 | enum TxPhase { 45 | INIT = 0, 46 | QUERY, 47 | VOTE_YES, 48 | VOTE_NO, 49 | COMMIT, 50 | ACK_COMMIT, 51 | ABORT, 52 | PHASE_NUMBER 53 | }; 54 | enum Role { 55 | Coordinator = 0, 56 | Payee, 57 | Hop 58 | }; 59 | 60 | TransactionManager( const ZR::TransactionId & txId ); 61 | virtual ~TransactionManager(); 62 | 63 | virtual ZR::RetVal init() = 0; 64 | 65 | static int handleTxItem( RsZeroReserveTxItem * item ); 66 | static int handleTxItem( RSZRRemoteTxItem *item ); 67 | 68 | static void timeout(); 69 | 70 | protected: 71 | 72 | virtual ZR::RetVal processItem( RsZeroReserveTxItem * item ){ return ZR::ZR_FAILURE; } 73 | virtual ZR::RetVal processItem( RSZRRemoteTxItem * item ){ return ZR::ZR_FAILURE; } 74 | virtual ZR::RetVal abortTx( RsZeroReserveTxItem * item ){ return ZR::ZR_FAILURE; } 75 | virtual ZR::RetVal abortTx( RSZRRemoteTxItem * item ){ return ZR::ZR_FAILURE; } 76 | 77 | virtual void rollback() = 0; 78 | virtual bool isTimedOut(); 79 | 80 | 81 | const ZR::TransactionId m_TxId; 82 | TxPhase m_Phase; 83 | qint64 m_startOfPhase; 84 | /** maximum time of each phase */ 85 | qint64 m_maxTime[ PHASE_NUMBER ]; 86 | 87 | static TxManagers currentTX; 88 | }; 89 | 90 | #endif // TRANSACTIONMANAGER_H 91 | -------------------------------------------------------------------------------- /MyOrders.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef MYORDERS_H 20 | #define MYORDERS_H 21 | 22 | #include "OrderBook.h" 23 | #include "Payment.h" 24 | 25 | #include 26 | 27 | /** 28 | * @brief Holds pointers to all orders from myself. 29 | * Matches oders and controls execution 30 | */ 31 | 32 | class MyOrders : public OrderBook 33 | { 34 | Q_OBJECT 35 | MyOrders(); 36 | 37 | public: 38 | MyOrders(OrderBook *bids, OrderBook *asks); 39 | virtual int columnCount(const QModelIndex&) const; 40 | virtual QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const; 41 | virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; 42 | 43 | 44 | /** Seller side: start executing the deal - initiate Bitcoin payment */ 45 | int startExecute(Payment *payment); 46 | 47 | /** Seller side: remove Order from the book, if partly filled, publish 48 | * a new order, finish Bitcoin payment 49 | */ 50 | int finishExecute( Payment *payment ); 51 | ZR::RetVal updateOrders( Payment * payment , const ZR::VirtualAddress &txId ); 52 | void rollback( PaymentReceiver *payment ); 53 | void rollback( PaymentSpender *payment, const ZR::VirtualAddress & txId ); 54 | 55 | void cancelOrder( int index ); 56 | 57 | static MyOrders * Instance(); 58 | 59 | protected: 60 | /** Matches our new order with all others */ 61 | virtual ZR::RetVal match(Order *order); 62 | virtual ZR::RetVal matchAsk(Order *order); 63 | 64 | /** Matches incoming new order with ours */ 65 | virtual ZR::RetVal matchOther( Order * other ); 66 | 67 | 68 | /** Buyer side: start buying Bitcoins */ 69 | void buy( Order * order, ZR::ZR_Number amount , const Order::ID &myId ); 70 | 71 | void filterBids( OrderList & filteredOrders, const Currency::CurrencySymbols currencySym ); 72 | static bool reverseCompareOrder( const Order * left, const Order * right ); 73 | 74 | private: 75 | OrderBook * m_bids; 76 | OrderBook * m_asks; 77 | 78 | std::map< ZR::TransactionId, std::pair< Order, Order > > m_CurrentTxOrders; 79 | 80 | private: 81 | 82 | static MyOrders * me; 83 | }; 84 | 85 | #endif // MYORDERS_H 86 | -------------------------------------------------------------------------------- /ZrLibBitcoin.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ZRLIBBITCOIN_H 19 | #define ZRLIBBITCOIN_H 20 | 21 | #include "ZRBitcoin.h" 22 | #include "zrtypes.h" 23 | 24 | #include 25 | 26 | #include 27 | 28 | class ZrLibBitcoin : public ZR::Bitcoin 29 | { 30 | public: 31 | ZrLibBitcoin(); 32 | virtual ~ZrLibBitcoin(){} 33 | 34 | virtual ZR::RetVal start(); 35 | virtual ZR::RetVal stop(); 36 | virtual ZR::RetVal commit(); 37 | virtual ZR::ZR_Number getBalance(); 38 | 39 | virtual ZR::MyWallet *mkWallet( ZR::MyWallet::WalletType wType ); 40 | virtual void loadWallets( std::vector< ZR::MyWallet *> & wallets ); 41 | 42 | 43 | private: 44 | ZR::RetVal initChain( const std::string & pathname ); 45 | void connection_started(const std::error_code& ec, bc::channel_ptr node); 46 | void recv_tx( const std::error_code& ec, const bc::transaction_type & tx, bc::channel_ptr node ); 47 | void new_unconfirm_valid_tx( const std::error_code & ec, const bc::index_list & unconfirmed, const bc::transaction_type & tx ); 48 | void handle_start(const std::error_code& ec); 49 | 50 | bc::threadpool m_netPool; 51 | bc::threadpool m_diskPool; 52 | bc::threadpool m_memPool; 53 | 54 | bc::hosts m_hosts; 55 | bc::handshake m_handshake; 56 | bc::network m_network; 57 | bc::protocol m_protocol; 58 | 59 | bc::poller m_poller; 60 | bc::transaction_pool m_txpool; 61 | bc::transaction_indexer m_txidx; 62 | 63 | bc::session m_session; 64 | 65 | bc::leveldb_blockchain m_blockChain; 66 | 67 | bool started; 68 | }; 69 | 70 | class LibBitcoinWallet : public ZR::MyWallet 71 | { 72 | friend ZrLibBitcoin; 73 | LibBitcoinWallet( MyWallet::WalletType wType ); 74 | public: 75 | 76 | virtual ZR::WalletSeed seed(); 77 | virtual void setSeed( const ZR::WalletSeed & seed ); 78 | virtual ZR::BitcoinAddress getAddress(); 79 | virtual ZR::RetVal persist(); 80 | virtual ZR::ZR_Number getBalance(); 81 | 82 | 83 | virtual ZR::RetVal getSecret( ZR::WalletSecret &secret_out ); 84 | 85 | private: 86 | bc::deterministic_wallet wallet; 87 | bc::elliptic_curve_key privkey; 88 | }; 89 | 90 | #endif // ZRLIBBITCOIN_H 91 | -------------------------------------------------------------------------------- /paymentdialog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "paymentdialog.h" 19 | #include "ui_paymentdialog.h" 20 | #include "Currency.h" 21 | #include "TmLocalCoordinator.h" 22 | #include "Credit.h" 23 | #include "zrtypes.h" 24 | #include "Payment.h" 25 | 26 | #include "retroshare/rspeers.h" 27 | 28 | #include 29 | 30 | PaymentDialog::PaymentDialog( const std::string & payee, QWidget *parent ) : 31 | QDialog( parent ), 32 | ui( new Ui::PaymentDialog ), 33 | m_payee( payee ) 34 | { 35 | const std::string peername = rsPeers->getPeerName( payee ); 36 | 37 | ui->setupUi(this); 38 | ui->label->setText( QString::fromUtf8( peername.c_str() ) ); 39 | int index = 0; 40 | while(Currency::currencyNames[ index ]){ 41 | ui->currencySelector->addItem( Currency::currencyNames[ index ] ); 42 | index++; 43 | } 44 | connect( ui->buttonBox, SIGNAL(accepted()), this, SLOT( payTo() ) ); 45 | connect( ui->currencySelector, SIGNAL(currentIndexChanged(QString)), this, SLOT(loadAvailableFunds( QString ) ) ); 46 | loadAvailableFunds(); 47 | } 48 | 49 | PaymentDialog::~PaymentDialog() 50 | { 51 | delete ui; 52 | } 53 | 54 | void PaymentDialog::payTo() 55 | { 56 | Currency::CurrencySymbols sym = Currency::getCurrencyByName( ui->currencySelector->currentText().toStdString() ); 57 | Payment * payment = new PaymentSpender( m_payee, ZR::ZR_Number::fromDecimalString( ui->amount->text() ), Currency::currencySymbols[ sym ], Payment::PAYMENT ); 58 | TmLocalCoordinator * tm = new TmLocalCoordinator( payment ); 59 | if( ! tm->init() ) delete tm; 60 | } 61 | 62 | void PaymentDialog::loadAvailableFunds(QString arg) 63 | { 64 | ui->lcdAvailableFunds->display( availableFunds().toDouble() ); 65 | } 66 | 67 | 68 | ZR::ZR_Number PaymentDialog::availableFunds() 69 | { 70 | Currency::CurrencySymbols sym = Currency::getCurrencyByName( ui->currencySelector->currentText().toStdString() ); 71 | Credit peerCredit( m_payee, Currency::currencySymbols[ sym ] ); 72 | try { 73 | peerCredit.loadPeer(); 74 | } 75 | catch( std::exception e ) { 76 | QMessageBox::critical(0, "Error reading credit", e.what() ); 77 | } 78 | 79 | return peerCredit.getMyAvailable(); 80 | } 81 | -------------------------------------------------------------------------------- /frienddetailsdialog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "frienddetailsdialog.h" 20 | #include "ui_frienddetailsdialog.h" 21 | #include "Credit.h" 22 | #include "Currency.h" 23 | 24 | #include 25 | #include 26 | 27 | FriendDetailsDialog::FriendDetailsDialog( const std::string & uid, QWidget *parent, const std::string & name ) : 28 | QDialog(parent), 29 | ui(new Ui::FriendDetailsDialog), 30 | m_id(uid) 31 | { 32 | ui->setupUi(this); 33 | 34 | int index = 0; 35 | while(Currency::currencyNames[ index ]){ 36 | ui->currencySelector->addItem( Currency::currencyNames[ index ] ); 37 | index++; 38 | } 39 | 40 | ui->label->setText( QString::fromUtf8( name.c_str() ) ); 41 | 42 | connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(editFriend())); 43 | connect(ui->currencySelector, SIGNAL(currentIndexChanged(QString)), this, SLOT(loadPeer(QString) ) ); 44 | 45 | loadPeer(); 46 | } 47 | 48 | void FriendDetailsDialog::loadPeer( QString ) 49 | { 50 | Currency::CurrencySymbols sym = Currency::getCurrencyByName( ui->currencySelector->currentText().toStdString() ); 51 | Credit peerCredit( m_id, Currency::currencySymbols[ sym ] ); 52 | try { 53 | peerCredit.loadPeer(); 54 | } 55 | catch( std::exception e ) { 56 | QMessageBox::critical(0, "Error reading credit", e.what() ); 57 | } 58 | ui->creditSpinBox->setValue( peerCredit.m_credit.toDouble() ); 59 | ui->yourCredit->display( peerCredit.m_our_credit.toDouble() ); 60 | ui->balance->display( peerCredit.m_balance.toDouble() ); 61 | } 62 | 63 | FriendDetailsDialog::~FriendDetailsDialog() 64 | { 65 | delete ui; 66 | } 67 | 68 | 69 | void FriendDetailsDialog::editFriend() 70 | { 71 | Currency::CurrencySymbols sym = Currency::getCurrencyByName( ui->currencySelector->currentText().toStdString() ); 72 | Credit peerCredit( m_id, Currency::currencySymbols[ sym ] ); 73 | peerCredit.loadPeer(); 74 | peerCredit.m_credit = ZR::ZR_Number::fromDecimalString( ui->creditSpinBox->text() ); 75 | try { 76 | peerCredit.updateCredit(); 77 | peerCredit.publish(); 78 | } 79 | catch( std::exception e ) { 80 | QMessageBox::critical( 0, "Error inserting credit", e.what() ); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /ZRBitcoin.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ZRBITCOIN_H 19 | #define ZRBITCOIN_H 20 | 21 | #include "zrtypes.h" 22 | #include "zrdb.h" 23 | 24 | namespace ZR 25 | { 26 | 27 | 28 | class Wallet 29 | { 30 | public: 31 | Wallet() {} 32 | 33 | virtual ZR::BitcoinAddress getAddress() = 0; 34 | virtual ZR::ZR_Number getBalance() = 0; 35 | virtual ZR::RetVal persist() = 0; 36 | 37 | const std::string & getNick(){ return m_nick; } 38 | void setNick( const std::string & nick ){ m_nick = nick; } 39 | 40 | protected: 41 | std::string m_nick; 42 | }; 43 | 44 | 45 | class MyWallet : public Wallet 46 | { 47 | public: 48 | enum WalletType { 49 | BRAINWALLET, 50 | ELECTRUMSEED, 51 | WIFIMPORT, 52 | CASASCIUS, 53 | INVALID 54 | }; 55 | 56 | MyWallet( WalletType wType ) : m_walletType( wType ) 57 | {} 58 | 59 | virtual ZR::WalletSeed seed() = 0; 60 | virtual void setSeed( const ZR::WalletSeed & seed ) = 0; 61 | 62 | virtual ZR::RetVal getSecret( ZR::WalletSecret & secret_out ) = 0; 63 | 64 | protected: 65 | WalletType m_walletType; 66 | }; 67 | 68 | class PeerWallet : public Wallet 69 | { 70 | PeerWallet(); 71 | public: 72 | PeerWallet( const ZR::BitcoinAddress & address ) : 73 | m_Address( address ) 74 | {} 75 | virtual ZR::BitcoinAddress getAddress(){ return m_Address; } 76 | virtual ZR::RetVal persist(){ 77 | return ZrDB::Instance()->addPeerWallet( m_Address, m_nick ); 78 | } 79 | 80 | virtual ZR::ZR_Number getBalance(){ 81 | return 0; 82 | } 83 | 84 | private: 85 | ZR::BitcoinAddress m_Address; 86 | }; 87 | 88 | 89 | class Bitcoin 90 | { 91 | public: 92 | virtual ZR::RetVal commit() = 0; 93 | virtual ZR::RetVal start() = 0; 94 | virtual ZR::RetVal stop() = 0; 95 | virtual ZR::ZR_Number getBalance() = 0; 96 | 97 | virtual MyWallet * mkWallet( MyWallet::WalletType wType ) = 0; 98 | virtual void loadWallets( std::vector< ZR::MyWallet *> & wallets ) = 0; 99 | 100 | virtual void send( const std::string & dest, const ZR::ZR_Number & amount ) = 0; 101 | 102 | static Bitcoin * Instance(); 103 | 104 | protected: 105 | 106 | static Bitcoin * instance; 107 | }; 108 | 109 | 110 | } 111 | 112 | #endif // ZRBITCOIN_H 113 | -------------------------------------------------------------------------------- /ZeroReserve.pro: -------------------------------------------------------------------------------- 1 | !include("../Common/retroshare_plugin.pri"):error("Could not include file ../Common/retroshare_plugin.pri") 2 | 3 | # Use bogus currency. If Testnet is a compile time option of the Bitcoin code, set it there, too 4 | DEFINES += ZR_TESTNET 5 | 6 | isEmpty(ZR_BITCOIN){ 7 | # One of those must be defined 8 | # ZR_BITCOIN = ZR_LIBBITCOIN 9 | # ZR_BITCOIN = ZR_DUMMYBITCOIN 10 | ZR_BITCOIN = ZR_SATOSHI 11 | } 12 | 13 | 14 | CONFIG += qt \ 15 | uic \ 16 | qrc \ 17 | resources 18 | 19 | SOURCES = ZeroReserveDialog.cpp \ 20 | ZeroReservePlugin.cpp \ 21 | frienddetailsdialog.cpp \ 22 | paymentdialog.cpp \ 23 | OrderBook.cpp \ 24 | Currency.cpp \ 25 | p3ZeroReserveRS.cpp \ 26 | RSZeroReserveItems.cpp \ 27 | Payment.cpp \ 28 | TransactionManager.cpp \ 29 | zrdb.cpp \ 30 | MyOrders.cpp \ 31 | Credit.cpp \ 32 | dbconfig.cpp \ 33 | Router.cpp \ 34 | TraceRouter.cpp \ 35 | RemotePaymentDialog.cpp \ 36 | RemotePaymentRequestDialog.cpp \ 37 | RSZRRemoteItems.cpp \ 38 | TmRemoteCoordinator.cpp \ 39 | TmLocalCoordinator.cpp \ 40 | TmLocalCohorte.cpp \ 41 | TmRemoteCohorte.cpp \ 42 | NewWallet.cpp \ 43 | BitcoinAddressList.cpp \ 44 | PeerAddressDialog.cpp 45 | 46 | LIBS += -lsqlite3 47 | 48 | win32 { 49 | INCLUDEPATH += ../../../libsqlite ../../../boost 50 | } 51 | 52 | 53 | HEADERS = ZeroReserveDialog.h \ 54 | ZeroReservePlugin.h \ 55 | frienddetailsdialog.h \ 56 | paymentdialog.h \ 57 | OrderBook.h \ 58 | Currency.h \ 59 | p3ZeroReserverRS.h \ 60 | RSZeroReserveItems.h \ 61 | Payment.h \ 62 | TransactionManager.h \ 63 | zrdb.h \ 64 | zrtypes.h \ 65 | MyOrders.h \ 66 | Credit.h \ 67 | dbconfig.h \ 68 | Router.h \ 69 | TraceRouter.h \ 70 | RemotePaymentDialog.h \ 71 | RemotePaymentRequestDialog.h \ 72 | RSZRRemoteItems.h \ 73 | TmRemoteCoordinator.h \ 74 | TmLocalCoordinator.h \ 75 | TmLocalCohorte.h \ 76 | TmRemoteCohorte.h \ 77 | ZRBitcoin.h \ 78 | ZrLibBitcoin.h \ 79 | NewWallet.h \ 80 | BitcoinAddressList.h \ 81 | PeerAddressDialog.h 82 | 83 | FORMS = ZeroReserveDialog.ui \ 84 | frienddetailsdialog.ui \ 85 | paymentdialog.ui \ 86 | ZeroReserveConfig.ui \ 87 | RemotePaymentDialog.ui \ 88 | RemotePaymentRequestDialog.ui \ 89 | NewWallet.ui \ 90 | PeerAddressDialog.ui 91 | 92 | TARGET = ZeroReserve 93 | 94 | RESOURCES = ZeroReserve_images.qrc 95 | 96 | 97 | contains(ZR_BITCOIN, ZR_LIBBITCOIN) { 98 | HEADERS += ZrLibBitcoin.h 99 | SOURCES += ZrLibBitcoin.cpp 100 | QMAKE_CXXFLAGS += -std=c++11 101 | LIBS += -lbitcoin -lleveldb -lcurl -lboost_thread -lboost_system -lboost_regex -lboost_filesystem 102 | DEFINES += LEVELDB_ENABLED 103 | } 104 | 105 | contains(ZR_BITCOIN, ZR_DUMMYBITCOIN) { 106 | HEADERS += ZrDummyBitcoin.h 107 | SOURCES += ZrDummyBitcoin.cpp 108 | } 109 | 110 | contains(ZR_BITCOIN, ZR_SATOSHI) { 111 | HEADERS += ZrSatoshiBitcoin.h 112 | SOURCES += ZrSatoshiBitcoin.cpp 113 | QMAKE_CXXFLAGS += -std=c++11 114 | LIBS += -L$(HOME)/lib -lnmcrpc -lcurl -ljsoncpp 115 | INCLUDEPATH += $(HOME)/lib 116 | } 117 | 118 | -------------------------------------------------------------------------------- /PeerAddressDialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | PeerAddressDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 428 10 | 282 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 120 20 | 210 21 | 261 22 | 41 23 | 24 | 25 | 26 | Qt::Horizontal 27 | 28 | 29 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 30 | 31 | 32 | 33 | 34 | 35 | 30 36 | 66 37 | 121 38 | 20 39 | 40 | 41 | 42 | Nickname 43 | 44 | 45 | 46 | 47 | 48 | 30 49 | 90 50 | 351 51 | 31 52 | 53 | 54 | 55 | 56 | 57 | 58 | 30 59 | 140 60 | 121 61 | 16 62 | 63 | 64 | 65 | Bitcoin Address 66 | 67 | 68 | 69 | 70 | 71 | 30 72 | 160 73 | 351 74 | 31 75 | 76 | 77 | 78 | 79 | 80 | 81 | 30 82 | 20 83 | 341 84 | 31 85 | 86 | 87 | 88 | 89 | 12 90 | 91 | 92 | 93 | Peer Bitcoin Address 94 | 95 | 96 | 97 | 98 | 99 | 100 | buttonBox 101 | accepted() 102 | PeerAddressDialog 103 | accept() 104 | 105 | 106 | 248 107 | 254 108 | 109 | 110 | 157 111 | 274 112 | 113 | 114 | 115 | 116 | buttonBox 117 | rejected() 118 | PeerAddressDialog 119 | reject() 120 | 121 | 122 | 316 123 | 260 124 | 125 | 126 | 286 127 | 274 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /ZrSatoshiBitcoin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "ZrSatoshiBitcoin.h" 19 | 20 | 21 | using namespace nmcrpc; 22 | 23 | ZrSatoshiBitcoin::ZrSatoshiBitcoin() 24 | { 25 | std::string home = getenv ("HOME"); 26 | m_settings.readConfig( home + "/.bitcoin/bitcoin.conf" ); 27 | } 28 | 29 | 30 | ZR::RetVal ZrSatoshiBitcoin::commit() 31 | { 32 | return ZR::ZR_SUCCESS; 33 | } 34 | 35 | ZR::RetVal ZrSatoshiBitcoin::start() 36 | { 37 | return ZR::ZR_SUCCESS; 38 | } 39 | 40 | ZR::RetVal ZrSatoshiBitcoin::stop() 41 | { 42 | return ZR::ZR_SUCCESS; 43 | } 44 | 45 | ZR::ZR_Number ZrSatoshiBitcoin::getBalance() 46 | { 47 | 48 | JsonRpc rpc( m_settings ); 49 | JsonRpc::JsonData res = rpc.executeRpc ( "getinfo" ); 50 | ZR::ZR_Number balance = res["balance"].asDouble(); 51 | 52 | return balance; 53 | } 54 | 55 | 56 | ZR::MyWallet * ZrSatoshiBitcoin::mkWallet( ZR::MyWallet::WalletType wType ) 57 | { 58 | return NULL; 59 | } 60 | 61 | void ZrSatoshiBitcoin::loadWallets( std::vector< ZR::MyWallet *> & wallets ) 62 | { 63 | JsonRpc rpc( m_settings ); 64 | JsonRpc::JsonData res = rpc.executeRpc ("listaddressgroupings"); 65 | // assert (res.isArray()); 66 | for( nmcrpc::JsonRpc::JsonData::iterator it1 = res.begin(); it1 != res.end(); it1++ ){ 67 | JsonRpc::JsonData res0 = *it1; 68 | // assert (res0.isArray()); 69 | for( nmcrpc::JsonRpc::JsonData::iterator it2 = res0.begin(); it2 != res0.end(); it2++ ){ 70 | JsonRpc::JsonData res1 = *it2; 71 | // assert (res1.isArray()); 72 | 73 | JsonRpc::JsonData res2 = res1[ 0u ]; 74 | // assert (res2.isString()); 75 | ZR::BitcoinAddress address = res2.asString(); 76 | 77 | JsonRpc::JsonData res21 = res1[ 1u ]; 78 | // assert ( res21.isDouble() ); 79 | ZR::ZR_Number balance = res21.asDouble(); 80 | ZR::MyWallet * wallet = new SatoshiWallet( address, balance ); 81 | wallets.push_back( wallet ); 82 | } 83 | } 84 | 85 | return; 86 | } 87 | 88 | 89 | void ZrSatoshiBitcoin::send( const std::string & dest, const ZR::ZR_Number & amount ) 90 | { 91 | JsonRpc rpc( m_settings ); 92 | std::vector params; 93 | params.push_back( dest ); 94 | params.push_back( amount.toDouble() ); 95 | JsonRpc::JsonData res = rpc.executeRpcList ("sendtoaddress", params ); 96 | } 97 | 98 | 99 | ///////////////////////////////////////////////////////////////////// 100 | 101 | ZR::Bitcoin * ZR::Bitcoin::instance = NULL; 102 | 103 | 104 | ZR::Bitcoin * ZR::Bitcoin::Instance() 105 | { 106 | if( instance == NULL ){ 107 | instance = new ZrSatoshiBitcoin(); 108 | } 109 | return instance; 110 | } 111 | -------------------------------------------------------------------------------- /NewWallet.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | NewWallet 4 | 5 | 6 | 7 | 0 8 | 0 9 | 571 10 | 247 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 40 20 | 120 21 | 100 22 | 21 23 | 24 | 25 | 26 | compatible to www.brainwallet.org - a simple sha256 hash 27 | 28 | 29 | Brain Wallet 30 | 31 | 32 | 33 | 34 | 35 | 40 36 | 160 37 | 191 38 | 21 39 | 40 | 41 | 42 | Import Electrum Seed 43 | 44 | 45 | 46 | 47 | 48 | 40 49 | 200 50 | 171 51 | 21 52 | 53 | 54 | 55 | New Electrum Seed 56 | 57 | 58 | 59 | 60 | 61 | 330 62 | 200 63 | 160 64 | 25 65 | 66 | 67 | 68 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 69 | 70 | 71 | 72 | 73 | 74 | 30 75 | 70 76 | 511 77 | 31 78 | 79 | 80 | 81 | 82 | 83 | 84 | 40 85 | 30 86 | 331 87 | 21 88 | 89 | 90 | 91 | 92 | 12 93 | 94 | 95 | 96 | Enter a seed or Pass Phrase 97 | 98 | 99 | 100 | 101 | 102 | 103 | buttonBox 104 | accepted() 105 | NewWallet 106 | accept() 107 | 108 | 109 | 409 110 | 212 111 | 112 | 113 | 285 114 | 123 115 | 116 | 117 | 118 | 119 | buttonBox 120 | rejected() 121 | NewWallet 122 | reject() 123 | 124 | 125 | 409 126 | 212 127 | 128 | 129 | 285 130 | 123 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /TmLocalCoordinator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "TmLocalCoordinator.h" 20 | #include "p3ZeroReserverRS.h" 21 | #include "ZeroReservePlugin.h" 22 | #include "Payment.h" 23 | 24 | 25 | unsigned int TmLocalCoordinator::sequence = 1; 26 | 27 | const ZR::TransactionId TmLocalCoordinator::mkId() 28 | { 29 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 30 | 31 | std::ostringstream txId; 32 | txId << p3zr->getOwnId() << ":" << ++sequence; 33 | return txId.str(); 34 | } 35 | 36 | TmLocalCoordinator::TmLocalCoordinator( Payment *payment ) : 37 | TransactionManager( mkId() ), 38 | m_payment( payment ) 39 | { 40 | } 41 | 42 | TmLocalCoordinator::~TmLocalCoordinator() 43 | { 44 | delete m_payment; 45 | } 46 | 47 | 48 | 49 | void TmLocalCoordinator::rollback() 50 | { 51 | 52 | } 53 | 54 | 55 | 56 | ZR::RetVal TmLocalCoordinator::init() 57 | { 58 | std::cerr << "Zero Reserve: Setting TX manager up as coordinator. ID: " << m_TxId << std::endl; 59 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 60 | if ( m_payment->init() == ZR::ZR_FAILURE ){ 61 | std::cerr << "Zero Reserve: Error, not enough Credit " << std::endl; 62 | return ZR::ZR_FAILURE; 63 | } 64 | RsZeroReserveInitTxItem * initItem = new RsZeroReserveInitTxItem( m_payment ); 65 | initItem->setTxId( m_TxId ); 66 | p3zr->sendItem( initItem ); 67 | return ZR::ZR_SUCCESS; 68 | } 69 | 70 | 71 | ZR::RetVal TmLocalCoordinator::processItem( RsZeroReserveTxItem * item ) 72 | { 73 | RsZeroReserveTxItem * reply; 74 | p3ZeroReserveRS * p3zr; 75 | 76 | // TODO: Timeout 77 | switch( item->getTxPhase() ) 78 | { 79 | case VOTE_YES: 80 | std::cerr << "Zero Reserve: TX Coordinator: Received Vote: YES" << std::endl; 81 | reply = new RsZeroReserveTxItem( COMMIT ); 82 | reply->PeerId( m_payment->getCounterparty() ); 83 | reply->setTxId( m_TxId ); 84 | p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 85 | p3zr->sendItem( reply ); 86 | return ZR::ZR_SUCCESS; 87 | case VOTE_NO: 88 | return abortTx( item ); 89 | case ACK_COMMIT: 90 | std::cerr << "Zero Reserve: TX Coordinator: Received Acknowledgement, Committing" << std::endl; 91 | m_payment->commit( m_TxId ); 92 | return ZR::ZR_FINISH; 93 | case ABORT: 94 | return abortTx( item ); 95 | default: 96 | throw std::runtime_error( "Unknown Transaction Phase"); 97 | } 98 | return ZR::ZR_SUCCESS; 99 | } 100 | 101 | ZR::RetVal TmLocalCoordinator::abortTx( RsZeroReserveTxItem * item ) 102 | { 103 | std::cerr << "Zero Reserve: TX Manger:Error happened. Aborting." << std::endl; 104 | return ZR::ZR_FAILURE; 105 | } 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /BitcoinAddressList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "BitcoinAddressList.h" 20 | 21 | #include 22 | 23 | BitcoinAddressList::BitcoinAddressList(QObject *parent) : 24 | QAbstractItemModel(parent) 25 | { 26 | } 27 | 28 | 29 | QModelIndex BitcoinAddressList::index(int x, int y, const QModelIndex&) const 30 | { 31 | return createIndex(x, y); 32 | } 33 | 34 | QModelIndex BitcoinAddressList::parent(const QModelIndex&) const 35 | { 36 | return QModelIndex(); 37 | } 38 | 39 | QVariant BitcoinAddressList::headerData(int section, Qt::Orientation orientation, int role) const 40 | { 41 | if (role == Qt::DisplayRole) 42 | { 43 | if (orientation == Qt::Horizontal) { 44 | switch (section) 45 | { 46 | case 0: 47 | return QString("Address"); 48 | case 1: 49 | return QString("Nick"); 50 | case 2: 51 | return QString("Balance"); 52 | } 53 | } 54 | } 55 | return QVariant(); 56 | } 57 | 58 | 59 | int BitcoinAddressList::columnCount(const QModelIndex&) const 60 | { 61 | return 3; 62 | } 63 | 64 | int BitcoinAddressList::rowCount(const QModelIndex&) const 65 | { 66 | return m_walletList.size(); 67 | } 68 | 69 | 70 | QVariant BitcoinAddressList::data( const QModelIndex& index, int role ) const 71 | { 72 | if (role == Qt::DisplayRole && index.row() < m_walletList.size()){ 73 | ZR::Wallet * wallet = m_walletList[index.row()]; 74 | switch(index.column()){ 75 | case 0: 76 | return QVariant( QString::fromStdString( wallet->getAddress() ) ); 77 | case 1: 78 | return QVariant( QString::fromStdString( wallet->getNick() ) ); 79 | case 2: 80 | return QVariant( wallet->getBalance().toDecimalQString() ); 81 | default: 82 | return QVariant(); 83 | } 84 | 85 | } 86 | return QVariant(); 87 | } 88 | 89 | void BitcoinAddressList::addWallet( ZR::Wallet * wallet ) 90 | { 91 | beginResetModel(); 92 | m_walletList.push_back( wallet ); 93 | endResetModel(); 94 | } 95 | 96 | 97 | void BitcoinAddressList::loadWallets() 98 | { 99 | std::vector< ZR::MyWallet *> wallets; 100 | while( true ){ 101 | try{ 102 | ZR::Bitcoin::Instance()->loadWallets( wallets ); 103 | break; 104 | } 105 | catch( ... ){ 106 | QMessageBox::StandardButton pressed = QMessageBox::critical( NULL, "JSON Error", "Can't connect to Satoshi Client", QMessageBox::Retry | QMessageBox::Abort ); 107 | if ( pressed == QMessageBox::Abort ) 108 | return; 109 | } 110 | } 111 | beginResetModel(); 112 | for( std::vector< ZR::MyWallet *>::const_iterator it = wallets.begin(); it != wallets.end(); it++ ){ 113 | m_walletList.push_back( *it ); 114 | } 115 | endResetModel(); 116 | } 117 | 118 | 119 | -------------------------------------------------------------------------------- /RemotePaymentRequestDialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | RemotePaymentRequestDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 584 10 | 389 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 70 20 | 330 21 | 160 22 | 25 23 | 24 | 25 | 26 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 27 | 28 | 29 | 30 | 31 | 32 | 100 33 | 230 34 | 411 35 | 31 36 | 37 | 38 | 39 | false 40 | 41 | 42 | <html><head/><body><p>copy this to the clipboard and send a message to the recipient</p></body></html> 43 | 44 | 45 | true 46 | 47 | 48 | 49 | 50 | 51 | 100 52 | 160 53 | 191 54 | 31 55 | 56 | 57 | 58 | 59 | 60 | 61 | 26 62 | 170 63 | 61 64 | 20 65 | 66 | 67 | 68 | Amount 69 | 70 | 71 | 72 | 73 | 74 | 310 75 | 160 76 | 201 77 | 31 78 | 79 | 80 | 81 | 82 | 83 | 84 | 30 85 | 30 86 | 501 87 | 51 88 | 89 | 90 | 91 | 92 | 12 93 | 94 | 95 | 96 | Enter Amount and Currency. An Address is then generated 97 | which must be sent to the recipient by email or other means 98 | 99 | 100 | 101 | 102 | 103 | 104 | buttonBox 105 | accepted() 106 | RemotePaymentRequestDialog 107 | accept() 108 | 109 | 110 | 149 111 | 342 112 | 113 | 114 | 291 115 | 194 116 | 117 | 118 | 119 | 120 | buttonBox 121 | rejected() 122 | RemotePaymentRequestDialog 123 | reject() 124 | 125 | 126 | 149 127 | 342 128 | 129 | 130 | 291 131 | 194 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /zrtypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef ZRTYPES_H 20 | #define ZRTYPES_H 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace ZR { 30 | 31 | class ZR_Number : public boost::rational< int64_t > 32 | { 33 | public: 34 | ZR_Number() : boost::rational< int64_t >::rational( 0 ){} 35 | 36 | ZR_Number( int64_t numerator, int64_t denumerator = 1): 37 | boost::rational< int64_t >::rational( numerator, denumerator){} 38 | 39 | ZR_Number( const boost::rational< int64_t > & a ) : 40 | boost::rational< int64_t >::rational( a.numerator(), a.denominator() ){} 41 | 42 | double toDouble() const 43 | { 44 | return boost::rational_cast( *this ); 45 | } 46 | 47 | static ZR_Number fromFractionString( const std::string & s_num ) 48 | { 49 | boost::rational< int64_t > num; 50 | std::istringstream sNum( s_num ); 51 | sNum >> num; 52 | return num; 53 | } 54 | 55 | static ZR_Number fromDecimalString( const std::string & s_num ) 56 | { 57 | const char delim = ( s_num.find( ',' ) != std::string::npos ) ? ',' : '.'; 58 | std::istringstream iss( s_num ); 59 | std::string sIntPart; 60 | std::string sFracPart; 61 | std::getline(iss, sIntPart, delim); 62 | std::getline(iss, sFracPart, delim); 63 | int factor = (int)pow(10, sFracPart.length() ); 64 | int intPart = strtol( sIntPart.c_str(), NULL, 10 ); 65 | int fracPart = strtol( sFracPart.c_str(), NULL, 10 ); 66 | if( intPart < 0 ) fracPart = -fracPart; 67 | ZR_Number zrnum( intPart * factor + fracPart, factor ); 68 | return zrnum; 69 | } 70 | static ZR_Number fromDecimalString( QString s_num ) 71 | { 72 | return fromDecimalString( s_num.toStdString() ); 73 | } 74 | 75 | std::string toStdString() const 76 | { 77 | std::ostringstream o; 78 | o << *this; 79 | return o.str(); 80 | } 81 | int length() 82 | { 83 | return toStdString().length(); 84 | } 85 | QString toQString() 86 | { 87 | return QString::fromStdString( toStdString() ); 88 | } 89 | 90 | std::string toDecimalStdString() const 91 | { 92 | std::ostringstream o; 93 | o << toDouble(); 94 | return o.str(); 95 | } 96 | QString toDecimalQString() const 97 | { 98 | return QString::fromStdString( toDecimalStdString() ); 99 | } 100 | }; 101 | 102 | enum RetVal { 103 | ZR_FAILURE = 0, 104 | ZR_SUCCESS, 105 | ZR_FINISH 106 | }; 107 | 108 | /** 109 | * @brief serves as the virtual address of a Turtle style tunnel and also as a TX id for remote payments 110 | */ 111 | typedef std::string VirtualAddress; 112 | /** 113 | * @brief Transaction ID for local payments 114 | */ 115 | typedef std::string TransactionId; 116 | 117 | typedef std::string PeerAddress; 118 | 119 | typedef std::string WalletSeed; 120 | typedef std::string WalletSecret; 121 | typedef std::string BitcoinAddress; 122 | 123 | } 124 | 125 | #endif // ZRTYPES_H 126 | -------------------------------------------------------------------------------- /RemotePaymentDialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | RemotePaymentDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 664 10 | 300 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 370 20 | 210 21 | 161 22 | 31 23 | 24 | 25 | 26 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 27 | 28 | 29 | 30 | 31 | 32 | 150 33 | 110 34 | 431 35 | 31 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | 44 | 45 | 46 | 47 | 48 | 30 49 | 163 50 | 91 51 | 21 52 | 53 | 54 | 55 | Amount 56 | 57 | 58 | 59 | 60 | 61 | 26 62 | 114 63 | 91 64 | 21 65 | 66 | 67 | 68 | Destination 69 | 70 | 71 | 72 | 73 | 74 | 30 75 | 20 76 | 581 77 | 31 78 | 79 | 80 | 81 | 82 | 12 83 | 84 | 85 | 86 | Paste the destination address. The amount will appear automatically. 87 | 88 | 89 | 90 | 91 | 92 | 350 93 | 160 94 | 111 95 | 31 96 | 97 | 98 | 99 | 100 | 10 101 | 102 | 103 | 104 | - 105 | 106 | 107 | 108 | 109 | 110 | 150 111 | 160 112 | 191 113 | 31 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | buttonBox 122 | accepted() 123 | RemotePaymentDialog 124 | accept() 125 | 126 | 127 | 450 128 | 225 129 | 130 | 131 | 331 132 | 149 133 | 134 | 135 | 136 | 137 | buttonBox 138 | rejected() 139 | RemotePaymentDialog 140 | reject() 141 | 142 | 143 | 450 144 | 225 145 | 146 | 147 | 331 148 | 149 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /TmLocalCohorte.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "TmLocalCohorte.h" 20 | #include "RSZeroReserveItems.h" 21 | #include "Payment.h" 22 | #include "p3ZeroReserverRS.h" 23 | #include "ZeroReservePlugin.h" 24 | 25 | 26 | TmLocalCohorte::TmLocalCohorte( const ZR::TransactionId & txId ) : 27 | TransactionManager( txId ) 28 | { 29 | } 30 | 31 | TmLocalCohorte::~TmLocalCohorte() 32 | { 33 | delete m_payment; 34 | } 35 | 36 | 37 | void TmLocalCohorte::rollback() 38 | { 39 | 40 | } 41 | 42 | 43 | 44 | 45 | ZR::RetVal TmLocalCohorte::init() 46 | { 47 | 48 | std::cerr << "Zero Reserve: TX Manager: Payment request for " << m_payment->getAmount() << " " 49 | << m_payment->getCurrency() 50 | << " received - Setting TX manager up as cohorte" << std::endl; 51 | 52 | RsZeroReserveTxItem * reply; 53 | ZR::RetVal retval; 54 | 55 | if ( m_payment->init() == ZR::ZR_FAILURE ){ 56 | std::cerr << "Zero Reserve: initCohort(): Insufficient Credit - voting no" << std::endl; 57 | reply = new RsZeroReserveTxItem( VOTE_NO ); 58 | retval = ZR::ZR_FAILURE; 59 | } 60 | else { 61 | reply = new RsZeroReserveTxItem( VOTE_YES ); 62 | retval = ZR::ZR_SUCCESS; 63 | } 64 | 65 | reply->PeerId( m_payment->getCounterparty() ); 66 | reply->setTxId( m_TxId ); 67 | 68 | p3ZeroReserveRS * p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 69 | p3zs->sendItem( reply ); // TODO: error handling 70 | return retval; 71 | } 72 | 73 | 74 | 75 | 76 | 77 | ZR::RetVal TmLocalCohorte::processItem( RsZeroReserveTxItem * item ) 78 | { 79 | RsZeroReserveTxItem * reply; 80 | p3ZeroReserveRS * p3zs; 81 | 82 | // TODO: Timeout 83 | switch( item->getTxPhase() ) 84 | { 85 | case QUERY: 86 | { 87 | RsZeroReserveInitTxItem * initItem = dynamic_cast< RsZeroReserveInitTxItem *> ( item ); 88 | if( m_Phase != INIT || initItem == NULL ) 89 | return abortTx( item ); 90 | m_Phase = QUERY; 91 | m_payment = initItem->getPayment(); 92 | return init(); 93 | } 94 | case COMMIT: 95 | std::cerr << "Zero Reserve: TX Cohorte: Received Command: COMMIT" << std::endl; 96 | if( m_Phase != QUERY ) 97 | return abortTx( item ); 98 | m_Phase = COMMIT; 99 | reply = new RsZeroReserveTxItem( ACK_COMMIT ); 100 | reply->PeerId( m_payment->getCounterparty() ); 101 | reply->setTxId( m_TxId ); 102 | p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 103 | p3zs->sendItem( reply ); 104 | m_payment->commit( m_TxId ); 105 | return ZR::ZR_FINISH; 106 | case ABORT: 107 | return ZR::ZR_FINISH; 108 | default: 109 | throw std::runtime_error( "Unknown Transaction Phase"); 110 | } 111 | return ZR::ZR_SUCCESS; 112 | } 113 | 114 | ZR::RetVal TmLocalCohorte::abortTx( RsZeroReserveTxItem * item ) 115 | { 116 | std::cerr << "Zero Reserve: TX Manger:Error happened. Aborting." << std::endl; 117 | RsZeroReserveTxItem * reply = new RsZeroReserveTxItem( ABORT ); 118 | p3ZeroReserveRS * p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 119 | reply->PeerId( m_payment->getCounterparty() ); 120 | reply->setTxId( m_TxId ); 121 | p3zs->sendItem( reply ); 122 | return ZR::ZR_FAILURE; 123 | } 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /RemotePaymentRequestDialog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "RemotePaymentRequestDialog.h" 20 | #include "ui_RemotePaymentRequestDialog.h" 21 | 22 | #include "Currency.h" 23 | #include "p3ZeroReserverRS.h" 24 | #include "ZeroReservePlugin.h" 25 | #include "p3ZeroReserverRS.h" 26 | #include "ZeroReservePlugin.h" 27 | #include "Payment.h" 28 | 29 | #include "util/radix64.h" 30 | 31 | #include 32 | 33 | 34 | RemotePaymentRequestDialog::RemotePaymentRequestDialog(QWidget *parent) : 35 | QDialog(parent), 36 | ui(new Ui::RemotePaymentRequestDialog) 37 | { 38 | ui->setupUi(this); 39 | int index = 0; 40 | while(Currency::currencyNames[ index ]){ 41 | ui->currencySelector->addItem( Currency::currencyNames[ index ] ); 42 | index++; 43 | } 44 | 45 | ui->amount->setValidator( new QDoubleValidator( 1E-8, 1E12, 0) ); 46 | 47 | connect( ui->currencySelector, SIGNAL( currentIndexChanged( QString ) ), this, SLOT( currencySelected( QString ) ) ); 48 | connect( ui->amount, SIGNAL( textEdited( QString ) ), this, SLOT( amountEntered( QString ) ) ); 49 | connect( ui->buttonBox, SIGNAL( accepted() ), this, SLOT( sendRequest() ) ); 50 | } 51 | 52 | 53 | RemotePaymentRequestDialog::~RemotePaymentRequestDialog() 54 | { 55 | delete ui; 56 | } 57 | 58 | 59 | void RemotePaymentRequestDialog::currencySelected( QString currency ) 60 | { 61 | QString amount = ui->amount->text(); 62 | if( !amount.isEmpty() ){ 63 | ui->payAddress->setText( getPayAddress( currency, amount ) ); 64 | } 65 | } 66 | 67 | void RemotePaymentRequestDialog::amountEntered( QString amount ) 68 | { 69 | QString currency = QString::fromStdString( Currency::currencySymbols[ Currency::getCurrencyByName( ui->currencySelector->currentText().toStdString() ) ] ); 70 | ui->payAddress->setText( getPayAddress( currency, amount ) ); 71 | } 72 | 73 | 74 | QString RemotePaymentRequestDialog::getPayAddress( QString amount, QString currency ) 75 | { 76 | unsigned char md[ SHA256_DIGEST_LENGTH ]; 77 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 78 | // FIXME: the getOwnId() must be replaced by a secret - else the originator of the order can be calculated 79 | // FIXME: by friends and friends of friends 80 | QString data = QString::fromStdString( p3zr->getOwnId() ) + amount + currency + QString::number( rand() ); 81 | int length = data.length(); 82 | unsigned char * buff = new unsigned char[ length ]; 83 | memcpy( buff, data.toAscii(), length ); 84 | SHA256( buff, length, md); 85 | std::string payAddress; 86 | Radix64::encode( (const char*)md, SHA256_DIGEST_LENGTH, payAddress ); 87 | delete [] buff; 88 | return QString::fromStdString( payAddress ); 89 | } 90 | 91 | 92 | void RemotePaymentRequestDialog::sendRequest() 93 | { 94 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 95 | ZR::VirtualAddress address = ui->payAddress->text().toStdString(); 96 | ZR::ZR_Number amount = ZR::ZR_Number::fromDecimalString( ui->amount->text() ); 97 | Currency::CurrencySymbols currencySym = Currency::getCurrencyByName(ui->currencySelector->currentText().toStdString() ); 98 | std::string currency = Currency::currencySymbols[ currencySym ]; 99 | p3zr->sendRemote( address, amount, currency ); 100 | Payment::addMyRequest( address, Payment::Request( amount, currencySym )); 101 | } 102 | -------------------------------------------------------------------------------- /TmRemoteCoordinator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "TmRemoteCoordinator.h" 19 | #include "RSZRRemoteItems.h" 20 | #include "Router.h" 21 | #include "ZeroReservePlugin.h" 22 | #include "p3ZeroReserverRS.h" 23 | #include "Payment.h" 24 | 25 | TmRemoteCoordinator::TmRemoteCoordinator(const ZR::VirtualAddress & addr , Payment *payment, const std::string & myId ) : 26 | TransactionManager( addr + ':' + myId ), 27 | m_Destination( addr ), 28 | m_Payment( payment ), 29 | m_myId( myId ) 30 | { 31 | } 32 | 33 | TmRemoteCoordinator::~TmRemoteCoordinator() 34 | { 35 | delete m_Payment; 36 | } 37 | 38 | 39 | void TmRemoteCoordinator::rollback() 40 | { 41 | 42 | } 43 | 44 | 45 | ZR::RetVal TmRemoteCoordinator::init() 46 | { 47 | std::cerr << "Zero Reserve: Setting TX manager up as coordinator" << std::endl; 48 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 49 | RSZRRemoteTxInitItem * item = new RSZRRemoteTxInitItem( m_Destination, QUERY, Router::SERVER, m_Payment, m_myId ); 50 | ZR::PeerAddress addr = Router::Instance()->nextHop( m_Destination ); 51 | if( addr.empty() ) 52 | return ZR::ZR_FAILURE; 53 | item->PeerId( addr ); 54 | p3zr->sendItem( item ); 55 | return ZR::ZR_SUCCESS; 56 | } 57 | 58 | 59 | ZR::RetVal TmRemoteCoordinator::processItem( RSZRRemoteTxItem * item ) 60 | { 61 | RSZRRemoteTxItem * reply; 62 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 63 | 64 | switch( item->getTxPhase() ) 65 | { 66 | case VOTE_YES: 67 | { 68 | std::cerr << "Zero Reserve: TX Coordinator: Received Vote: YES" << std::endl; 69 | RSZRRemoteTxInitItem * initItem = dynamic_cast< RSZRRemoteTxInitItem * >( item ); 70 | if( !initItem )return abortTx( item ); 71 | 72 | reply = new RSZRRemoteTxItem( m_Destination, COMMIT, Router::SERVER, item->getPayerId() ); 73 | reply->PeerId( m_Payment->getCounterparty() ); 74 | ZR::ZR_Number receivedAmount = initItem->getPayment()->getAmount(); 75 | if( m_Payment->getAmount() != receivedAmount ){ 76 | if( m_Payment->getAmount() < receivedAmount){ 77 | std::cerr << "Zero Reserve: ERROR: reveived payment request higher than original" << std::endl; 78 | return abortTx( item ); // someone attempting fraud? 79 | } 80 | m_Payment->setAmount( receivedAmount ); 81 | } 82 | p3zr->sendItem( reply ); 83 | return ZR::ZR_SUCCESS; 84 | } 85 | case VOTE_NO: 86 | std::cerr << "Zero Reserve: TX Coordinator: Received Vote: NO" << std::endl; 87 | return abortTx( item ); 88 | case ACK_COMMIT: 89 | std::cerr << "Zero Reserve: TX Coordinator: Received Acknowledgement, Committing" << std::endl; 90 | m_Payment->commit( m_TxId ); 91 | return ZR::ZR_FINISH; 92 | case ABORT: 93 | return abortTx( item ); 94 | default: 95 | throw std::runtime_error( "Unknown Transaction Phase"); 96 | } 97 | return ZR::ZR_SUCCESS; 98 | } 99 | 100 | ZR::RetVal TmRemoteCoordinator::abortTx( RSZRRemoteTxItem *item ) 101 | { 102 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 103 | RSZRRemoteTxItem * abortItem = new RSZRRemoteTxItem( m_Destination, ABORT, Router::SERVER, item->getPayerId() ); 104 | abortItem->PeerId( m_Payment->getCounterparty() ); 105 | p3zr->sendItem( abortItem ); 106 | return ZR::ZR_FINISH; 107 | } 108 | -------------------------------------------------------------------------------- /TransactionManager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "TransactionManager.h" 19 | #include "TmLocalCohorte.h" 20 | #include "TmRemoteCohorte.h" 21 | #include "RSZeroReserveItems.h" 22 | #include "RSZRRemoteItems.h" 23 | #include "ZeroReservePlugin.h" 24 | #include "p3ZeroReserverRS.h" 25 | #include "Payment.h" 26 | #include "zrtypes.h" 27 | #include "Router.h" 28 | 29 | #include 30 | #include 31 | 32 | 33 | 34 | TransactionManager::TxManagers TransactionManager::currentTX; 35 | 36 | 37 | /** 38 | * @brief Handle remote Transaction Items 39 | * @param item 40 | * @return 41 | */ 42 | 43 | int TransactionManager::handleTxItem( RSZRRemoteTxItem * item ) 44 | { 45 | ZR::RetVal retVal; 46 | std::cerr << "Zero Reserve: TX Manger handling incoming item - Destination: " << item->getAddress() << std::endl; 47 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 48 | ZR::TransactionId txId = item->getAddress() + ":" + item->getPayerId(); 49 | std::cerr << "\n\nXXXXXXXXXXXXXXXX " << txId << std::endl; 50 | TransactionManager * tm; 51 | TxManagers::iterator it = currentTX.find( txId ); 52 | if( it == currentTX.end() ){ 53 | tm = new TmRemoteCohorte( txId ); 54 | } 55 | else { 56 | tm = (*it).second; 57 | } 58 | try{ 59 | retVal = tm->processItem( item ); 60 | } 61 | catch( std::runtime_error e){ 62 | std::cerr << "Zero Reserve: Exception caught: " << e.what() << std::endl; 63 | return ZR::ZR_FAILURE; 64 | } 65 | 66 | if( retVal != ZR::ZR_SUCCESS ){ 67 | delete tm; 68 | } 69 | return retVal; 70 | } 71 | 72 | 73 | /** 74 | * @brief Handle local Transaction Items 75 | * @param item 76 | * @return success or failure 77 | */ 78 | 79 | int TransactionManager::handleTxItem( RsZeroReserveTxItem * item ) 80 | { 81 | std::cerr << "Zero Reserve: TX Manger handling incoming item id = " << item->getTxId() << std::endl; 82 | ZR::TransactionId txId = item->getTxId(); 83 | TransactionManager * tm; 84 | TxManagers::iterator it = currentTX.find( txId ); 85 | if( it == currentTX.end() ){ 86 | tm = new TmLocalCohorte( txId ); 87 | } 88 | else { 89 | tm = (*it).second; 90 | } 91 | ZR::RetVal retVal = tm->processItem( item ); 92 | if( retVal != ZR::ZR_SUCCESS ){ 93 | delete tm; 94 | } 95 | return retVal; 96 | } 97 | 98 | 99 | void TransactionManager::timeout() 100 | { 101 | for( TxManagers::iterator it = currentTX.begin(); it != currentTX.end(); it++ ){ 102 | TransactionManager * tm = (*it).second; 103 | if( tm->isTimedOut() ){ 104 | tm->rollback(); 105 | delete tm; 106 | } 107 | } 108 | } 109 | 110 | 111 | TransactionManager::TransactionManager( const ZR::TransactionId & txId ) : 112 | m_TxId( txId ), 113 | m_Phase( INIT ), 114 | m_startOfPhase( QDateTime::currentMSecsSinceEpoch() ) 115 | { 116 | m_maxTime[ INIT ] = 5000; 117 | m_maxTime[ QUERY ] = 5000; 118 | m_maxTime[ VOTE_YES ] = 5000; 119 | m_maxTime[ VOTE_NO ] = 5000; 120 | m_maxTime[ COMMIT ] = 5000; 121 | m_maxTime[ ACK_COMMIT ] = 5000; 122 | m_maxTime[ ABORT ] = 5000; 123 | 124 | currentTX[ txId ] = this; 125 | } 126 | 127 | TransactionManager::~TransactionManager() 128 | { 129 | std::cerr << "Zero Reserve: TX Manager: Cleaning up." << std::endl; 130 | currentTX.erase( m_TxId ); 131 | } 132 | 133 | bool TransactionManager::isTimedOut() 134 | { 135 | return ( QDateTime::currentMSecsSinceEpoch() - m_startOfPhase > m_maxTime[ m_Phase ] ); 136 | } 137 | -------------------------------------------------------------------------------- /Payment.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef PAYMENT_H 19 | #define PAYMENT_H 20 | 21 | #include "zrtypes.h" 22 | #include "Credit.h" 23 | #include "Currency.h" 24 | 25 | #include 26 | #include 27 | 28 | class QListWidget; 29 | 30 | /** 31 | * Take care of the actual payment on behalf of the @TransactionManager 32 | */ 33 | 34 | class Payment 35 | { 36 | Payment(); 37 | public: 38 | enum Category { 39 | PAYMENT, // generic payment where the other leg of the deal is outside the system 40 | DEBT_CANCEL, // triangle payments with the aim to cancel out debt 41 | BITCOIN 42 | }; 43 | 44 | class Request { 45 | public: 46 | Request() : m_Amount(0), m_Currency( Currency::INVALID ){} 47 | 48 | Request( const ZR::ZR_Number & amount, const Currency::CurrencySymbols & currency) : 49 | m_Amount( amount ), m_Currency( currency ){} 50 | 51 | bool isValid(){ return (m_Currency != Currency::INVALID ); } 52 | 53 | ZR::ZR_Number m_Amount; 54 | Currency::CurrencySymbols m_Currency; 55 | }; 56 | 57 | typedef std::map< ZR::VirtualAddress, Request > Requests; 58 | 59 | Payment(const std::string & counterparty, const ZR::ZR_Number &amount, const std::string & currency, Category category); 60 | virtual ~Payment(){} 61 | 62 | virtual ZR::ZR_Number newBalance() const = 0; 63 | virtual int init() = 0; 64 | virtual int commit( const ZR::TransactionId &txId ) = 0; 65 | 66 | const std::string & getCounterparty(){ return m_credit.m_id; } 67 | void setCounterparty( const std::string & counterparty ); 68 | const std::string & getCurrency(){ return m_credit.m_currency; } 69 | ZR::ZR_Number getAmount(){ return m_amount; } 70 | Category getCategory(){ return m_category; } 71 | void referrerId( const std::string & referrer ){ m_referrer = referrer; } 72 | const ZR::VirtualAddress & referrerId(){ return m_referrer; } 73 | 74 | void setAmount( const ZR::ZR_Number & amount ){ m_amount = amount; } 75 | 76 | static void addRequest( const ZR::VirtualAddress & addr, const Request & req ) 77 | { 78 | requestList.insert( std::pair< ZR::VirtualAddress, Request >( addr, req ) ); 79 | } 80 | static void addMyRequest( const ZR::VirtualAddress & addr, const Request & req ) 81 | { 82 | myRequests.insert( std::pair< ZR::VirtualAddress, Request >( addr, req ) ); 83 | } 84 | static const Request getRequest( const ZR::VirtualAddress & addr ); 85 | static const Request getMyRequest( const ZR::VirtualAddress & addr ); 86 | 87 | protected: 88 | Credit m_credit; 89 | ZR::ZR_Number m_amount; 90 | Category m_category; 91 | ZR::VirtualAddress m_referrer; 92 | 93 | public: 94 | static QListWidget * txLogView; 95 | 96 | private: 97 | static Requests requestList; 98 | static Requests myRequests; 99 | }; 100 | 101 | 102 | class PaymentReceiver : public Payment 103 | { 104 | public: 105 | PaymentReceiver(const std::string & counterparty, const ZR::ZR_Number &amount, const std::string & currency, Category category); 106 | virtual ~PaymentReceiver(){} 107 | 108 | virtual ZR::ZR_Number newBalance() const; 109 | virtual int init(); 110 | virtual int commit( const ZR::TransactionId &txId ); 111 | }; 112 | 113 | 114 | class PaymentSpender : public Payment 115 | { 116 | public: 117 | PaymentSpender(const std::string & counterparty, ZR::ZR_Number amount, const std::string & currency, Category category); 118 | virtual ~PaymentSpender(){} 119 | 120 | virtual ZR::ZR_Number newBalance() const; 121 | virtual int init(); 122 | virtual int commit( const ZR::TransactionId &txId ); 123 | }; 124 | 125 | 126 | 127 | #endif // PAYMENT_H 128 | -------------------------------------------------------------------------------- /Currency.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef CURRENCY_H 19 | #define CURRENCY_H 20 | 21 | #include 22 | 23 | class Currency 24 | { 25 | public: 26 | enum CurrencySymbols 27 | { 28 | #ifdef ZR_TESTNET 29 | FAU = 0, 30 | GPM, 31 | TBC, 32 | ZWD, 33 | #else 34 | AED = 0, 35 | AFN, 36 | ALL, 37 | AMD, 38 | ANG, 39 | AOA, 40 | ARS, 41 | AUD, 42 | AWG, 43 | AZN, 44 | BAM, 45 | BBD, 46 | BDT, 47 | BGN, 48 | BHD, 49 | BIF, 50 | BMD, 51 | BND, 52 | BOB, 53 | BRL, 54 | BSD, 55 | BTC, 56 | BTN, 57 | BWP, 58 | BYR, 59 | BZD, 60 | CAD, 61 | CDF, 62 | CHF, 63 | CLP, 64 | CNY, 65 | COP, 66 | CRC, 67 | CUC, 68 | CUP, 69 | CVE, 70 | CZK, 71 | DJF, 72 | DKK, 73 | DOP, 74 | DZD, 75 | EGP, 76 | ERN, 77 | ETB, 78 | EUR, 79 | FJD, 80 | FKP, 81 | GBP, 82 | GEL, 83 | GGP, 84 | GHS, 85 | GIP, 86 | GMD, 87 | XAU, 88 | GNF, 89 | GTQ, 90 | GYD, 91 | HKD, 92 | HNL, 93 | HRK, 94 | HTG, 95 | HUF, 96 | IDR, 97 | ILS, 98 | IMP, 99 | INR, 100 | IQD, 101 | IRR, 102 | ISK, 103 | JEP, 104 | JMD, 105 | JOD, 106 | JPY, 107 | KES, 108 | XKW, 109 | KGS, 110 | KHR, 111 | KMF, 112 | KPW, 113 | KRW, 114 | KWD, 115 | KYD, 116 | KZT, 117 | LAK, 118 | LBP, 119 | LKR, 120 | LRD, 121 | LSL, 122 | LTC, 123 | LTL, 124 | LVL, 125 | LYD, 126 | MAD, 127 | MDL, 128 | MGA, 129 | MKD, 130 | MMK, 131 | MNT, 132 | MOP, 133 | MRO, 134 | MUR, 135 | MVR, 136 | MWK, 137 | MXN, 138 | MYR, 139 | MZN, 140 | NAD, 141 | NGN, 142 | NIO, 143 | NMC, 144 | NOK, 145 | NPR, 146 | NZD, 147 | OMR, 148 | PAB, 149 | PEN, 150 | PGK, 151 | PHP, 152 | PKR, 153 | PLN, 154 | PYG, 155 | QAR, 156 | RON, 157 | RSD, 158 | RUB, 159 | RWF, 160 | SAR, 161 | SBD, 162 | SCR, 163 | SDG, 164 | SEK, 165 | SGD, 166 | SHP, 167 | SLL, 168 | XAG, 169 | SOS, 170 | SPL, 171 | SRD, 172 | STD, 173 | SVC, 174 | SYP, 175 | SZL, 176 | THB, 177 | TJS, 178 | TMT, 179 | TND, 180 | TOP, 181 | TRY, 182 | TTD, 183 | TVD, 184 | TWD, 185 | TZS, 186 | UAH, 187 | UGX, 188 | USD, 189 | UYU, 190 | UZS, 191 | VEF, 192 | VND, 193 | VUV, 194 | WST, 195 | XAF, 196 | XCD, 197 | XDR, 198 | XOF, 199 | XPF, 200 | YER, 201 | ZAR, 202 | ZMW, 203 | #endif // ZR_TESTNET 204 | INVALID 205 | }; 206 | 207 | static const char * const currencyNames[]; 208 | static const char * const currencySymbols[]; 209 | 210 | public: 211 | Currency(); 212 | ~Currency(); 213 | static CurrencySymbols getCurrencyByName( const std::string & currency ); 214 | static CurrencySymbols getCurrencyBySymbol( const std::string & currency ); 215 | 216 | }; 217 | 218 | #endif // CURRENCY_H 219 | -------------------------------------------------------------------------------- /zrdb.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ZRDB_H 19 | #define ZRDB_H 20 | 21 | #include "zrtypes.h" 22 | #include "Credit.h" 23 | #include "OrderBook.h" 24 | 25 | #include "util/rsthreads.h" 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | 36 | /** 37 | Database class to save and load friend data and payment info. Uses sqlite3 38 | */ 39 | 40 | class ZrDB 41 | { 42 | ZrDB(); 43 | void init(); 44 | public: 45 | 46 | typedef struct { 47 | std::string currency; 48 | ZR::ZR_Number our_credit; // credit with all peers 49 | ZR::ZR_Number credit; // their credit with us 50 | ZR::ZR_Number outstanding; // their debt with us 51 | ZR::ZR_Number debt; // our debt with them 52 | ZR::ZR_Number balance; 53 | } GrandTotal; 54 | 55 | typedef struct { 56 | QString id; 57 | QString currency; 58 | ZR::ZR_Number m_amount; 59 | QDateTime timestamp; 60 | } TxLogItem; 61 | 62 | typedef struct { 63 | ZR::WalletSecret secret; 64 | std::string nick; 65 | int type; 66 | } MyWallet; 67 | 68 | static ZrDB * Instance(); 69 | void createPeerRecord( const Credit & peer_in ); 70 | void updatePeerCredit(const Credit & peer_in, const std::string & column, ZR::ZR_Number &value ); 71 | void loadPeer( Credit & peer_out ); 72 | void loadPeer( const std::string & id, Credit::CreditList & peer_out ); 73 | bool peerExists( const Credit & peer_in ); 74 | 75 | GrandTotal &loadGrandTotal( const std::string & currency ); 76 | 77 | std::string getConfig( const std::string & key ); 78 | void updateConfig( const std::string & key, const std::string & value ); 79 | 80 | void addOrder( OrderBook::Order * order ); 81 | void loadOrders(OrderBook::OrderList *orders_out ); 82 | void updateOrder( OrderBook::Order * order ); 83 | void deleteOrder( OrderBook::Order * order ); 84 | void addToOrderList( OrderBook::Order * order ); 85 | 86 | void close(); 87 | 88 | void peerRecordExists(){ m_peer_record_exists = true; } 89 | void setPeerCredit( const std::string & credit, const std::string & our_credit, const std::string & balance ); 90 | void addPeerCredit( Credit * credit ); 91 | void setConfigValue( const std::string & val ) { m_config_value = val; } 92 | void addToGrandTotal( char ** cols ); 93 | void addToTxList( const TxLogItem & item ); 94 | 95 | void openTxLog(); 96 | void closeTxLog(); 97 | void appendTx(const std::string & id, const std::string ¤cy, ZR::ZR_Number amount ); 98 | void loadTxLog(std::list< TxLogItem > & txList ); 99 | 100 | 101 | // TODO void logPayment() const; 102 | // TODO: void replayPaymentLog(); 103 | // TODO: void backup() const; 104 | // TODO: void restore() const; 105 | 106 | ////////// Bitcoin Wallet ////////////// 107 | ZR::RetVal storeMyWallet( const ZR::WalletSecret &secret, unsigned int type, const std::string &nick ); 108 | ZR::RetVal addPeerWallet( const ZR::BitcoinAddress & address, const std::string & nick ); 109 | void loadMyWallets( std::vector< MyWallet > & wallets ); 110 | void addMyWallet( MyWallet & wallet ); 111 | 112 | private: 113 | void setConfig( const std::string & key, const std::string & value ); 114 | void runQuery( const std::string & sql ); 115 | 116 | 117 | private: 118 | RsMutex m_peer_mutex; 119 | RsMutex m_config_mutex; 120 | RsMutex m_tx_mutex; 121 | 122 | sqlite3 *m_db; 123 | sqlite3 *m_txLog; 124 | 125 | // buffers for the callbacks 126 | bool m_peer_record_exists; 127 | std::string m_config_value; 128 | GrandTotal grandTotal; 129 | Credit * m_credit; 130 | Credit::CreditList * m_creditList; 131 | std::list< TxLogItem > * m_txList; 132 | OrderBook::OrderList * m_orderList; 133 | std::vector< MyWallet > * m_wallets; 134 | 135 | static ZrDB * instance; 136 | static RsMutex creation_mutex; 137 | 138 | public: // config parameters 139 | static const char * const TXLOGPATH; 140 | static const char * const DB_VERSION; // integer 141 | }; 142 | 143 | #endif // ZRDB_H 144 | -------------------------------------------------------------------------------- /paymentdialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | PaymentDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 442 10 | 351 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 30 20 | 285 21 | 341 22 | 32 23 | 24 | 25 | 26 | Qt::Horizontal 27 | 28 | 29 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 30 | 31 | 32 | 33 | 34 | 35 | 30 36 | 60 37 | 341 38 | 31 39 | 40 | 41 | 42 | 43 | 12 44 | 45 | 46 | 47 | Error, no friend selected 48 | 49 | 50 | 51 | 52 | 53 | 215 54 | 236 55 | 151 56 | 20 57 | 58 | 59 | 60 | 61 | 62 | 63 | 215 64 | 126 65 | 151 66 | 31 67 | 68 | 69 | 70 | 71 | 72 | 73 | 215 74 | 186 75 | 151 76 | 23 77 | 78 | 79 | 80 | 81 | 82 | 83 | 60 84 | 235 85 | 131 86 | 20 87 | 88 | 89 | 90 | 91 | 11 92 | 93 | 94 | 95 | Amount 96 | 97 | 98 | 99 | 100 | 101 | 60 102 | 186 103 | 131 104 | 20 105 | 106 | 107 | 108 | 109 | 11 110 | 111 | 112 | 113 | Your credit + balance. 114 | 115 | 116 | Available Funds 117 | 118 | 119 | 120 | 121 | 122 | 60 123 | 130 124 | 141 125 | 21 126 | 127 | 128 | 129 | 130 | 11 131 | 132 | 133 | 134 | Currency 135 | 136 | 137 | 138 | 139 | 140 | 30 141 | 30 142 | 271 143 | 31 144 | 145 | 146 | 147 | 148 | 12 149 | 150 | 151 | 152 | Pay To: 153 | 154 | 155 | 156 | 157 | 158 | 159 | buttonBox 160 | accepted() 161 | PaymentDialog 162 | accept() 163 | 164 | 165 | 248 166 | 254 167 | 168 | 169 | 157 170 | 274 171 | 172 | 173 | 174 | 175 | buttonBox 176 | rejected() 177 | PaymentDialog 178 | reject() 179 | 180 | 181 | 316 182 | 260 183 | 184 | 185 | 286 186 | 274 187 | 188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /frienddetailsdialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | FriendDetailsDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 405 10 | 364 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 40 20 | 300 21 | 341 22 | 32 23 | 24 | 25 | 26 | Qt::Horizontal 27 | 28 | 29 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 30 | 31 | 32 | 33 | 34 | 35 | 20 36 | 20 37 | 361 38 | 31 39 | 40 | 41 | 42 | 43 | 14 44 | 45 | 46 | 47 | Error: No id 48 | 49 | 50 | 51 | 52 | 53 | 190 54 | 61 55 | 191 56 | 31 57 | 58 | 59 | 60 | 61 | 62 | 63 | 40 64 | 75 65 | 131 66 | 20 67 | 68 | 69 | 70 | 71 | 11 72 | 73 | 74 | 75 | Currency 76 | 77 | 78 | 79 | 80 | 81 | 190 82 | 110 83 | 191 84 | 31 85 | 86 | 87 | 88 | 2 89 | 90 | 91 | 1000000.000000000000000 92 | 93 | 94 | 95 | 96 | 97 | 190 98 | 160 99 | 191 100 | 31 101 | 102 | 103 | 104 | 105 | 106 | 107 | 40 108 | 170 109 | 91 110 | 21 111 | 112 | 113 | 114 | 115 | 11 116 | 117 | 118 | 119 | Balance 120 | 121 | 122 | 123 | 124 | 125 | 40 126 | 120 127 | 131 128 | 21 129 | 130 | 131 | 132 | 133 | 11 134 | 135 | 136 | 137 | Credit 138 | 139 | 140 | 141 | 142 | 143 | 190 144 | 220 145 | 191 146 | 31 147 | 148 | 149 | 150 | 151 | 152 | 153 | 40 154 | 220 155 | 131 156 | 31 157 | 158 | 159 | 160 | 161 | 11 162 | 163 | 164 | 165 | Your Credit 166 | 167 | 168 | 169 | 170 | 171 | 172 | buttonBox 173 | accepted() 174 | FriendDetailsDialog 175 | accept() 176 | 177 | 178 | 248 179 | 254 180 | 181 | 182 | 157 183 | 274 184 | 185 | 186 | 187 | 188 | buttonBox 189 | rejected() 190 | FriendDetailsDialog 191 | reject() 192 | 193 | 194 | 316 195 | 260 196 | 197 | 198 | 286 199 | 274 200 | 201 | 202 | 203 | 204 | 205 | -------------------------------------------------------------------------------- /OrderBook.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #ifndef ORDERBOOK_H 19 | #define ORDERBOOK_H 20 | 21 | #include "zrtypes.h" 22 | #include "Currency.h" 23 | 24 | #include "util/rsthreads.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | 31 | 32 | /** 33 | An object of this class is either the bid or the ask side of the complete order book. 34 | */ 35 | 36 | class OrderBook : public QAbstractItemModel 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | /** 42 | A single order 43 | */ 44 | class Order 45 | { 46 | public: 47 | typedef std::string ID; 48 | enum OrderType { BID = 0, ASK }; 49 | enum Purpose { NEW = 0, // new order 50 | CANCEL, // tell everyone I changed my mind 51 | FILLED, // tell everyone this order is gone 52 | PARTLY_FILLED, // everybody update the order book 53 | SELL // tell the buyer through a tunnel that there is a match 54 | // an ask market order will not be published 55 | // bid market orders go to execution right away. 56 | }; 57 | Order( bool isMyOrder = false ) : m_isMyOrder( isMyOrder), m_commitment( 0 ) 58 | {} 59 | 60 | ID m_order_id; // hashed from order attributes, a secret and randomness 61 | bool m_isMyOrder; 62 | OrderType m_orderType; 63 | ZR::ZR_Number m_amount; 64 | ZR::ZR_Number m_price; // the amount as number for sorting 65 | Currency::CurrencySymbols m_currency; 66 | qint64 m_timeStamp; // no more than 1 order / millisecond 67 | OrderBook::Order::Purpose m_purpose; 68 | ZR::ZR_Number m_commitment; // the amount which is currently processed by active TX 69 | // m_commitment <= m_amount 70 | 71 | bool operator == (const Order & other); 72 | bool operator < ( const Order & other) const; 73 | void setOrderId(); 74 | 75 | static const qint64 timeout; 76 | }; 77 | 78 | public: 79 | typedef QList::iterator OrderIterator; 80 | typedef QList OrderList; 81 | 82 | explicit OrderBook(); 83 | virtual ~OrderBook(); 84 | 85 | virtual QModelIndex index(int, int, const QModelIndex&) const; 86 | virtual QModelIndex parent(const QModelIndex&) const; 87 | virtual int rowCount(const QModelIndex&) const; 88 | virtual int columnCount(const QModelIndex&) const; 89 | virtual QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const; 90 | virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; 91 | 92 | OrderIterator begin(){ return m_orders.begin(); } 93 | OrderIterator end() { return m_orders.end(); } 94 | 95 | void setMyOrders( OrderBook * myOrders ){ m_myOrders = myOrders; } 96 | 97 | /** @return ZR::ZR_FINISH if this order has been completed */ 98 | virtual ZR::RetVal processOrder( Order* order ); 99 | virtual ZR::RetVal processMyOrder( Order* order ); 100 | void timeoutOrders(); 101 | 102 | void filterOrders(OrderList & filteredOrders , const Currency::CurrencySymbols currencySym); 103 | 104 | /** remove an order from the book 105 | * @param order Template for the order to match. Relevant fields: Id, timestamp and currency 106 | * @return a pointer to the removed order */ 107 | virtual Order * remove( Order * order ); 108 | virtual Order * remove( const std::string & order_id ); 109 | OrderIterator find( const std::string & order_id ); 110 | 111 | void beginReset(){ beginResetModel(); } 112 | void endReset(){ endResetModel(); } 113 | 114 | virtual int addOrder( Order* order ); 115 | 116 | 117 | protected: 118 | mutable RsMutex m_order_mutex; 119 | 120 | OrderList m_orders; 121 | OrderList m_filteredOrders; 122 | Currency::CurrencySymbols m_currency; 123 | OrderBook * m_myOrders; 124 | 125 | protected: 126 | /** Matches our new order with all others */ 127 | virtual ZR::RetVal match( Order * ){ return ZR::ZR_FAILURE; } 128 | virtual ZR::RetVal matchAsk( Order * ){ return ZR::ZR_FAILURE; } 129 | /** Matches incoming new order with ours */ 130 | virtual ZR::RetVal matchOther( Order * ){ return ZR::ZR_FAILURE; } 131 | signals: 132 | 133 | public slots: 134 | void setCurrency( const QString & currency ); 135 | 136 | private: 137 | static bool compareOrder( const Order * left, const Order * right ); 138 | }; 139 | 140 | #endif // ORDERBOOK_H 141 | -------------------------------------------------------------------------------- /Payment.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "Payment.h" 19 | #include "MyOrders.h" 20 | #include "zrtypes.h" 21 | #include "zrdb.h" 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | 30 | 31 | QListWidget * Payment::txLogView = NULL; 32 | 33 | Payment::Requests Payment::requestList; 34 | Payment::Requests Payment::myRequests; 35 | 36 | 37 | Payment::Payment( const std::string & counterparty, const ZR::ZR_Number & amount, const std::string & currency, Category category) : 38 | m_credit( counterparty, currency ), 39 | m_amount( amount ), 40 | m_category( category ) 41 | { 42 | m_credit.loadPeer(); 43 | } 44 | 45 | void Payment::setCounterparty( const std::string & counterparty ) 46 | { 47 | m_credit.m_id = counterparty; 48 | m_credit.loadPeer(); 49 | } 50 | 51 | 52 | const Payment::Request Payment::getRequest( const ZR::VirtualAddress & addr ) 53 | { 54 | Requests::iterator it = requestList.find( addr ); 55 | if( it == requestList.end() ) 56 | return Request( 0, Currency::INVALID ); 57 | return (*it).second; 58 | } 59 | 60 | const Payment::Request Payment::getMyRequest( const ZR::VirtualAddress & addr ) 61 | { 62 | Requests::iterator it = myRequests.find( addr ); 63 | if( it == myRequests.end() ) 64 | return Request( 0, Currency::INVALID ); 65 | return (*it).second; 66 | } 67 | 68 | /////// PaymentReceiver 69 | 70 | PaymentReceiver::PaymentReceiver( const std::string & counterparty, const ZR::ZR_Number & amount, const std::string & currency, Category category) : 71 | Payment( counterparty, amount, currency, category) 72 | {} 73 | 74 | 75 | ZR::ZR_Number PaymentReceiver::newBalance() const 76 | { 77 | return m_credit.m_balance + m_amount; 78 | } 79 | 80 | 81 | int PaymentReceiver::init() 82 | { 83 | switch( m_category ) 84 | { 85 | case BITCOIN: 86 | if( m_credit.getPeerAvailable() <= 0 ) 87 | return ZR::ZR_FAILURE; 88 | if( m_amount > m_credit.getPeerAvailable() ){ 89 | m_amount = m_credit.getPeerAvailable(); 90 | } 91 | return MyOrders::Instance()->startExecute( this ); 92 | case PAYMENT: 93 | if( m_credit.getPeerAvailable() < m_amount ) return ZR::ZR_FAILURE; 94 | return ZR::ZR_SUCCESS; 95 | default: 96 | return ZR::ZR_FAILURE; 97 | } 98 | } 99 | 100 | int PaymentReceiver::commit( const ZR::TransactionId &txId ) 101 | { 102 | m_credit.loadPeer(); 103 | m_credit.m_balance = newBalance(); 104 | // TODO: make atomic !!!! 105 | ZrDB::Instance()->updatePeerCredit( m_credit, "balance", m_credit.m_balance ); 106 | ZrDB::Instance()->appendTx( m_credit.m_id, m_credit.m_currency, m_amount ); 107 | 108 | if( txLogView ){ 109 | txLogView->insertItem( 0, QDateTime::currentDateTime().toString() + " : " + m_credit.m_currency.c_str() + " : +" + m_amount.toDecimalQString() ); 110 | txLogView->setCurrentRow( 0 ); // make the view emit currentItemChanged() 111 | } 112 | 113 | switch( m_category ) 114 | { 115 | case BITCOIN: 116 | return MyOrders::Instance()->finishExecute( this ); 117 | case PAYMENT: 118 | return ZR::ZR_SUCCESS; 119 | default: 120 | return ZR::ZR_FAILURE; 121 | } 122 | } 123 | 124 | 125 | /////// PaymentSpender 126 | 127 | PaymentSpender::PaymentSpender(const std::string & counterparty, ZR::ZR_Number amount, const std::string & currency, Category category) : 128 | Payment( counterparty, amount, currency, category) 129 | {} 130 | 131 | ZR::ZR_Number PaymentSpender::newBalance() const 132 | { 133 | return m_credit.m_balance - m_amount; 134 | } 135 | 136 | int PaymentSpender::init() 137 | { 138 | if( m_credit.getMyAvailable() < m_amount ){ 139 | return ZR::ZR_FAILURE; 140 | } 141 | return ZR::ZR_SUCCESS; 142 | } 143 | 144 | int PaymentSpender::commit( const ZR::TransactionId & txId ) 145 | { 146 | m_credit.loadPeer(); 147 | m_credit.m_balance = newBalance(); 148 | // TODO: make atomic !!!! 149 | ZrDB::Instance()->updatePeerCredit( m_credit, "balance", m_credit.m_balance ); 150 | ZrDB::Instance()->appendTx( m_credit.m_id, m_credit.m_currency, -m_amount ); 151 | 152 | if( txLogView ){ 153 | txLogView->insertItem( 0, QDateTime::currentDateTime().toString() + " : " + m_credit.m_currency.c_str() + " : -" + m_amount.toDecimalQString() ); 154 | txLogView->setCurrentRow( 0 ); // make the view emit currentItemChanged() 155 | } 156 | 157 | switch( m_category ) 158 | { 159 | case BITCOIN: 160 | return MyOrders::Instance()->updateOrders( this, txId ); 161 | case PAYMENT: 162 | return ZR::ZR_SUCCESS; 163 | default: 164 | return ZR::ZR_FAILURE; 165 | } 166 | return ZR::ZR_SUCCESS; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /ZeroReservePlugin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #include "ZeroReservePlugin.h" 20 | #include "ZeroReserveDialog.h" 21 | #include "OrderBook.h" 22 | #include "p3ZeroReserverRS.h" 23 | #include "zrdb.h" 24 | #include "dbconfig.h" 25 | #include "ZRBitcoin.h" 26 | #include "util/rsversion.h" 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | ZeroReservePlugin * g_ZeroReservePlugin; 35 | RsMutex ZeroReservePlugin::widget_creation_mutex("widget_creation_mutex"); 36 | 37 | 38 | extern "C" { 39 | void *RETROSHARE_PLUGIN_provide() 40 | { 41 | g_ZeroReservePlugin = new ZeroReservePlugin() ; 42 | return (void*) g_ZeroReservePlugin; 43 | } 44 | // This symbol contains the svn revision number grabbed from the executable. 45 | // It will be tested by RS to load the plugin automatically, since it is safe to load plugins 46 | // with same revision numbers, assuming that the revision numbers are up-to-date. 47 | // 48 | uint32_t RETROSHARE_PLUGIN_revision = SVN_REVISION_NUMBER; 49 | 50 | // This symbol contains the svn revision number grabbed from the executable. 51 | // It will be tested by RS to load the plugin automatically, since it is safe to load plugins 52 | // with same revision numbers, assuming that the revision numbers are up-to-date. 53 | // 54 | uint32_t RETROSHARE_PLUGIN_api = RS_PLUGIN_API_VERSION ; 55 | } 56 | 57 | #define IMAGE_LINKS ":/images/bitcoin.png" 58 | 59 | void ZeroReservePlugin::getPluginVersion(int& major,int& minor,int& svn_rev) const 60 | { 61 | major = 5 ; 62 | minor = 4 ; 63 | svn_rev = SVN_REVISION_NUMBER; 64 | } 65 | 66 | ZeroReservePlugin::ZeroReservePlugin() 67 | { 68 | mainpage = NULL ; 69 | mIcon = NULL ; 70 | mPlugInHandler = NULL; 71 | m_ZeroReserve = NULL; 72 | m_peers = NULL; 73 | 74 | m_asks = new OrderBook(); 75 | m_bids = new OrderBook(); 76 | } 77 | 78 | void ZeroReservePlugin::setInterfaces(RsPlugInInterfaces &interfaces) 79 | { 80 | m_peers = interfaces.mPeers; 81 | } 82 | 83 | MainPage *ZeroReservePlugin::qt_page() const 84 | { 85 | RsStackMutex widgetCreationMutex( widget_creation_mutex ); 86 | if(mainpage == NULL){ 87 | mainpage = new ZeroReserveDialog( m_bids, m_asks ); 88 | } 89 | 90 | return mainpage ; 91 | } 92 | 93 | 94 | void ZeroReservePlugin::setPlugInHandler(RsPluginHandler *pgHandler) 95 | { 96 | mPlugInHandler = pgHandler; 97 | } 98 | 99 | QIcon *ZeroReservePlugin::qt_icon() const 100 | { 101 | if(mIcon == NULL) 102 | { 103 | Q_INIT_RESOURCE(ZeroReserve_images) ; 104 | 105 | mIcon = new QIcon(IMAGE_LINKS) ; 106 | } 107 | 108 | return mIcon ; 109 | } 110 | 111 | RsPQIService * ZeroReservePlugin::rs_pqi_service() const 112 | { 113 | if(m_ZeroReserve == NULL){ 114 | m_ZeroReserve = new p3ZeroReserveRS(mPlugInHandler, m_bids, m_asks, m_peers ); 115 | ZR::Bitcoin::Instance()->start(); 116 | } 117 | 118 | return m_ZeroReserve ; 119 | } 120 | 121 | std::string ZeroReservePlugin::getShortPluginDescription() const 122 | { 123 | return QApplication::translate("ZeroReservePlugin", "This plugin implements a distributed Bitcoin exchange.").toUtf8().constData(); 124 | } 125 | 126 | std::string ZeroReservePlugin::getPluginName() const 127 | { 128 | return QApplication::translate("ZeroReservePlugin", "Zero Reserve").toUtf8().constData(); 129 | } 130 | 131 | QTranslator* ZeroReservePlugin::qt_translator(QApplication */*app*/, const QString& languageCode, const QString& externalDir) const 132 | { 133 | if (languageCode == "en") { 134 | return NULL; 135 | } 136 | 137 | QTranslator* translator = new QTranslator(); 138 | 139 | if (translator->load(externalDir + "/ZeroReserve_" + languageCode + ".qm")) { 140 | return translator; 141 | } else if (translator->load(":/lang/ZeroReserve_" + languageCode + ".qm")) { 142 | return translator; 143 | } 144 | 145 | delete(translator); 146 | return NULL; 147 | } 148 | 149 | ConfigPage * ZeroReservePlugin::qt_config_page() const 150 | { 151 | return new DBConfig() ; 152 | } 153 | 154 | QDialog * ZeroReservePlugin::qt_about_page() const 155 | { 156 | static QMessageBox *about_dialog = NULL ; 157 | 158 | if(about_dialog == NULL) 159 | { 160 | about_dialog = new QMessageBox() ; 161 | 162 | QString text ; 163 | text += QObject::tr("

RetroShare Zero Reserve plugin


* Author: Rüdiger Koch
" ) ; 164 | text += QObject::tr("
Zero Reserve implements a distributed Bitcoin Exchange based on the Ripple idea.") ; 165 | text += QObject::tr("Your friend needs to run the plugin to trade with you, of course.") ; 166 | text += QObject::tr("

This is experimental software. Use at your own risk. Don't hesitate to send comments and suggestion to anu at zerostate dot net.") ; 167 | 168 | about_dialog->setText(text) ; 169 | about_dialog->setStandardButtons(QMessageBox::Ok) ; 170 | } 171 | 172 | return about_dialog ; 173 | } 174 | 175 | void ZeroReservePlugin::stop() 176 | { 177 | std::cerr << "Zero Reserve: Closing Database" << std::endl; 178 | // TODO: Stop taking TX, try to finish all ongoing TX and send out errors to all 179 | // TODO: TX controllers who aren't done within 30 seconds. 180 | // TODO: Display a progress bar. 181 | ZrDB::Instance()->close(); 182 | 183 | ZR::Bitcoin::Instance()->stop(); 184 | } 185 | -------------------------------------------------------------------------------- /RSZRRemoteItems.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef RSZRREMOTEITEMS_H 20 | #define RSZRREMOTEITEMS_H 21 | 22 | #include "zrtypes.h" 23 | #include "RSZeroReserveItems.h" 24 | #include "Router.h" 25 | #include "Payment.h" 26 | 27 | #include 28 | 29 | /** 30 | * @brief base class of all items which go beyond friends. 31 | * 32 | * There are 2 types of remote items, one that propagates routing information to a target 33 | * address and one that is intended for the target address. Both go here. 34 | */ 35 | class RSZRRemoteItem : public RsZeroReserveItem 36 | { 37 | RSZRRemoteItem(); 38 | public: 39 | 40 | RSZRRemoteItem( const ZR::VirtualAddress & addr, uint8_t zeroreserve_subtype ) : 41 | RsZeroReserveItem( zeroreserve_subtype ), 42 | m_Address( addr ) 43 | {} 44 | 45 | RSZRRemoteItem(void *data,uint32_t size, uint8_t zeroreserve_subtype); 46 | 47 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ; 48 | 49 | virtual bool serialise(void *data,uint32_t& size); 50 | virtual uint32_t serial_size() const; 51 | 52 | const ZR::VirtualAddress & getAddress(){ return m_Address; } 53 | 54 | protected: 55 | // depending on the item type, this can be a target address or a propagation address 56 | // to be added to the router 57 | ZR::VirtualAddress m_Address; 58 | }; 59 | 60 | 61 | /** 62 | * @brief propagate a payment request through the network. 63 | * @see RsZeroReserveOrderBookItem 64 | * 65 | */ 66 | 67 | class RSZRPayRequestItem : public RSZRRemoteItem 68 | { 69 | RSZRPayRequestItem(); 70 | public: 71 | 72 | RSZRPayRequestItem(void *data,uint32_t size); 73 | RSZRPayRequestItem(const ZR::VirtualAddress & addr, const ZR::ZR_Number &amount, const std::string & currencySymbol); 74 | 75 | virtual bool serialise(void *data,uint32_t& size) ; 76 | virtual uint32_t serial_size() const ; 77 | virtual std::ostream & print(std::ostream &out, uint16_t indent = 0); 78 | 79 | const std::string & getCurrency(){ return m_Currency; } 80 | const ZR::ZR_Number & getAmount(){ return m_Amount; } 81 | 82 | private: 83 | ZR::ZR_Number m_Amount; 84 | std::string m_Currency; 85 | }; 86 | 87 | /** 88 | * @brief propagate Bitcoin bid and ask orders through the network 89 | * @see RSZRPayRequestItem 90 | */ 91 | 92 | class RsZeroReserveOrderBookItem: public RSZRRemoteItem 93 | { 94 | RsZeroReserveOrderBookItem(); 95 | public: 96 | RsZeroReserveOrderBookItem(void *data,uint32_t size) ; 97 | RsZeroReserveOrderBookItem( OrderBook::Order * order) ; 98 | 99 | virtual bool serialise(void *data,uint32_t& size) ; 100 | virtual uint32_t serial_size() const ; 101 | 102 | virtual ~RsZeroReserveOrderBookItem() {} 103 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); 104 | OrderBook::Order * getOrder(){ return m_order; } 105 | 106 | private: 107 | OrderBook::Order * m_order; 108 | uint32_t m_data_size ; 109 | }; 110 | 111 | /** 112 | * @brief follows a route from the coordinator of a transaction (payer) to the payee and back. 113 | * @see RSZRRemoteTxInitItem 114 | */ 115 | 116 | class RSZRRemoteTxItem : public RSZRRemoteItem 117 | { 118 | RSZRRemoteTxItem(); 119 | public: 120 | 121 | RSZRRemoteTxItem( void *data, uint32_t size, uint8_t itemType = ZR_REMOTE_TX_ITEM ); 122 | RSZRRemoteTxItem(const ZR::VirtualAddress & addr, TransactionManager::TxPhase txPhase, 123 | Router::TunnelDirection direction, 124 | const OrderBook::Order::ID & payerId, uint8_t itemType = ZR_REMOTE_TX_ITEM ); 125 | 126 | virtual bool serialise(void *data,uint32_t& size) ; 127 | virtual uint32_t serial_size() const ; 128 | virtual std::ostream & print(std::ostream &out, uint16_t indent = 0); 129 | 130 | TransactionManager::TxPhase getTxPhase() { return m_TxPhase; } 131 | Router::TunnelDirection getDirection() { return m_Direction; } 132 | const OrderBook::Order::ID & getPayerId(){ return m_PayerId; } 133 | 134 | protected: 135 | TransactionManager::TxPhase m_TxPhase; 136 | Router::TunnelDirection m_Direction; 137 | OrderBook::Order::ID m_PayerId; 138 | }; 139 | 140 | /** 141 | * @brief follows a route from the coordinator of a transaction (payer) to the payee and back. 142 | * @see RSZRRemoteTxItem 143 | * This item sets up the transaction. It carries payment information of the TX which the hops 144 | * need to VOTE on. 145 | */ 146 | 147 | class RSZRRemoteTxInitItem : public RSZRRemoteTxItem 148 | { 149 | RSZRRemoteTxInitItem(); 150 | public: 151 | 152 | RSZRRemoteTxInitItem( void *data, uint32_t size ); 153 | RSZRRemoteTxInitItem(const ZR::VirtualAddress & addr, TransactionManager::TxPhase txPhase, Router::TunnelDirection direction, Payment * payment, const OrderBook::Order::ID & payerId ); 154 | 155 | virtual bool serialise(void *data,uint32_t& size) ; 156 | virtual uint32_t serial_size() const ; 157 | virtual std::ostream & print(std::ostream &out, uint16_t indent = 0); 158 | 159 | Payment * getPayment(){ 160 | m_Payment->setCounterparty( PeerId() ); 161 | return m_Payment; 162 | } 163 | 164 | protected: 165 | Payment * m_Payment; 166 | }; 167 | 168 | /** 169 | * @brief route a buy request along an existing route to an order 170 | * 171 | */ 172 | 173 | class RSZRBuyRequestItem : public RSZRRemoteItem 174 | { 175 | RSZRBuyRequestItem(); 176 | public: 177 | 178 | RSZRBuyRequestItem(void *data,uint32_t size); 179 | /** 180 | * @brief RSZRBuyRequestItem 181 | * @param sellerAddr our order ID and also the route to be set pointing to us 182 | * @param buyerAddr their order ID / buyer address 183 | * @param amount 184 | */ 185 | RSZRBuyRequestItem( const ZR::VirtualAddress & sellerAddr, const ZR::VirtualAddress & buyerAddr, const ZR::ZR_Number &amount ); 186 | 187 | virtual bool serialise(void *data,uint32_t& size) ; 188 | virtual uint32_t serial_size() const ; 189 | virtual std::ostream & print(std::ostream &out, uint16_t indent = 0); 190 | 191 | const ZR::VirtualAddress & getOurAddress(){ return m_SellerAddress; } 192 | const ZR::ZR_Number & getAmount(){ return m_Amount; } 193 | 194 | private: 195 | ZR::ZR_Number m_Amount; 196 | ZR::VirtualAddress m_SellerAddress; 197 | }; 198 | 199 | #endif // RSZRREMOTEITEMS_H 200 | -------------------------------------------------------------------------------- /RSZeroReserveItems.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | 19 | #ifndef RSZERORESERVEITEMS_H 20 | #define RSZERORESERVEITEMS_H 21 | 22 | #include "serialiser/rsserial.h" 23 | #include "OrderBook.h" 24 | #include "Credit.h" 25 | #include "TransactionManager.h" 26 | #include "RSZeroReserveItems.h" 27 | 28 | class Payment; 29 | 30 | const uint8_t QOS_PRIORITY_RS_ZERORESERVE = 3; 31 | extern const uint16_t RS_SERVICE_TYPE_ZERORESERVE_PLUGIN; 32 | extern const uint32_t CONFIG_TYPE_ZERORESERVE_PLUGIN; 33 | 34 | 35 | /** Base class of all ZeroReserve Items, i.e. all classes related to sending stuff 36 | * over the net and receiving from the net using RetroShare. 37 | * TODO: This will have a protocol_version attribute once we reach version 1, and 38 | * logic to handle newer / older protocol versions. Protocol version 1 will 39 | * be the first version that handles multi-hop fully encrypted. Crypto will 40 | * happen here, as will plausibility checking and checking for attacks like 41 | * SQL injection so the inner classes can rely on the integrity of the data. 42 | */ 43 | 44 | class RsZeroReserveItem : public RsItem 45 | { 46 | public: 47 | RsZeroReserveItem(void *data, uint32_t &size, uint8_t zeroreserve_subtype ); 48 | RsZeroReserveItem( uint8_t zeroreserve_subtype ) : 49 | RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_ZERORESERVE_PLUGIN, zeroreserve_subtype) 50 | { 51 | setPriorityLevel(QOS_PRIORITY_RS_ZERORESERVE); 52 | } 53 | 54 | enum RS_PKT_SUBTYPE { 55 | ZERORESERVE_ORDERBOOK_ITEM = 0x01, 56 | ZERORESERVE_TX_ITEM, 57 | ZERORESERVE_TX_INIT_ITEM, 58 | ZERORESERVE_CREDIT_ITEM, 59 | ZERORESERVE_MSG_ITEM, 60 | 61 | ZR_REMOTE_PAYREQUEST_ITEM, 62 | ZR_REMOTE_BUYREQUEST_ITEM, 63 | ZR_REMOTE_TX_ITEM, 64 | ZR_REMOTE_TX_INIT_ITEM 65 | }; 66 | 67 | virtual ~RsZeroReserveItem() {}; 68 | virtual void clear() {}; 69 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ; 70 | 71 | virtual bool serialise(void *data, uint32_t & /*size */ ); 72 | virtual uint32_t serial_size() const { return headersOffset + 1; } 73 | 74 | static const uint8_t PROTOCOL_VERSION; 75 | static const uint8_t headersOffset; 76 | static const int CURRENCY_STRLEN; 77 | static const int HOLLERITH_LEN_SPEC; 78 | 79 | protected: 80 | uint32_t m_Offset; 81 | }; 82 | 83 | 84 | class RsZeroReserveMsgItem: public RsZeroReserveItem 85 | { 86 | public: 87 | enum MsgType { 88 | REQUEST_ORDERBOOK, 89 | SENT_ORDERBOOK, 90 | INVALID 91 | }; 92 | 93 | RsZeroReserveMsgItem() :RsZeroReserveItem( ZERORESERVE_MSG_ITEM ) {} 94 | RsZeroReserveMsgItem(void *data,uint32_t size) ; 95 | RsZeroReserveMsgItem(MsgType msgType, const std::string & msg ); 96 | 97 | virtual bool serialise(void *data,uint32_t& size) ; 98 | virtual uint32_t serial_size() const ; 99 | 100 | virtual ~RsZeroReserveMsgItem() {} 101 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); 102 | 103 | MsgType getType(){ return m_msgType; } 104 | const std::string & getMessage(){ return m_msg; } 105 | 106 | private: 107 | MsgType m_msgType; 108 | std::string m_msg; 109 | }; 110 | 111 | 112 | class RsZeroReserveTxItem: public RsZeroReserveItem 113 | { 114 | RsZeroReserveTxItem(); 115 | public: 116 | RsZeroReserveTxItem( RS_PKT_SUBTYPE zeroreserve_subtype = ZERORESERVE_TX_ITEM) : 117 | RsZeroReserveItem( (uint8_t)zeroreserve_subtype ) {} 118 | 119 | RsZeroReserveTxItem(void *data,uint32_t size, RS_PKT_SUBTYPE zeroreserve_subtype = ZERORESERVE_TX_ITEM ); 120 | RsZeroReserveTxItem( TransactionManager::TxPhase phase, RS_PKT_SUBTYPE subtype = ZERORESERVE_TX_ITEM ); 121 | void setTxId( const std::string & id ){ m_txId = id; } 122 | const std::string & getTxId(){ return m_txId; } 123 | 124 | virtual bool serialise(void *data,uint32_t& size) ; 125 | virtual uint32_t serial_size() const ; 126 | 127 | virtual ~RsZeroReserveTxItem() {} 128 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); 129 | 130 | TransactionManager::TxPhase getTxPhase() { return m_TxPhase; } 131 | 132 | protected: 133 | TransactionManager::TxPhase m_TxPhase; 134 | std::string m_txId; 135 | }; 136 | 137 | 138 | class RsZeroReserveInitTxItem: public RsZeroReserveTxItem 139 | { 140 | RsZeroReserveInitTxItem(); 141 | public: 142 | RsZeroReserveInitTxItem(void *data,uint32_t size) ; 143 | RsZeroReserveInitTxItem( Payment * payment ); 144 | 145 | virtual bool serialise(void *data,uint32_t& size) ; 146 | virtual uint32_t serial_size() const ; 147 | 148 | virtual ~RsZeroReserveInitTxItem(); 149 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); 150 | 151 | Payment * getPayment(); 152 | TransactionManager::Role getRole() { return m_Role; } 153 | 154 | private: 155 | TransactionManager::Role m_Role; 156 | Payment * m_payment; 157 | }; 158 | 159 | 160 | 161 | class RsZeroReserveCreditItem: public RsZeroReserveItem 162 | { 163 | public: 164 | RsZeroReserveCreditItem() :RsZeroReserveItem( ZERORESERVE_CREDIT_ITEM ) {} 165 | RsZeroReserveCreditItem(void *data,uint32_t size) ; 166 | RsZeroReserveCreditItem( Credit * credit) ; 167 | 168 | virtual bool serialise(void *data,uint32_t& size) ; 169 | virtual uint32_t serial_size() const ; 170 | 171 | virtual ~RsZeroReserveCreditItem(); 172 | virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); 173 | Credit * getCredit(){ return m_credit; } 174 | 175 | private: 176 | Credit * m_credit; 177 | }; 178 | 179 | 180 | class RsZeroReserveSerialiser: public RsSerialType 181 | { 182 | public: 183 | RsZeroReserveSerialiser() 184 | :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_ZERORESERVE_PLUGIN) 185 | { 186 | } 187 | virtual ~RsZeroReserveSerialiser() {} 188 | 189 | virtual uint32_t size (RsItem *item) 190 | { 191 | return dynamic_cast(item)->serial_size() ; 192 | } 193 | 194 | virtual bool serialise (RsItem *item, void *data, uint32_t *size) 195 | { 196 | return dynamic_cast(item)->serialise(data,*size) ; 197 | } 198 | virtual RsItem *deserialise(void *data, uint32_t *size); 199 | }; 200 | 201 | #endif // RSZERORESERVEITEMS_H 202 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /TmRemoteCohorte.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "TmRemoteCohorte.h" 19 | #include "p3ZeroReserverRS.h" 20 | #include "ZeroReservePlugin.h" 21 | #include "Router.h" 22 | #include "Payment.h" 23 | #include "MyOrders.h" 24 | 25 | 26 | TmRemoteCohorte::TmRemoteCohorte( const ZR::TransactionId & txId ) : 27 | TransactionManager( txId ), 28 | m_PaymentReceiver( 0 ), 29 | m_PaymentSpender( 0 ) 30 | { 31 | } 32 | 33 | void TmRemoteCohorte::rollback() 34 | { 35 | if( !m_IsHop ) 36 | MyOrders::Instance()->rollback( dynamic_cast < PaymentReceiver *> ( m_PaymentReceiver ) ); 37 | } 38 | 39 | 40 | ZR::RetVal TmRemoteCohorte::init() 41 | { 42 | std::cerr << "Zero Reserve: TX Cohorte: Initializing... checking available funds" << std::endl; 43 | return ZR::ZR_SUCCESS; 44 | } 45 | 46 | 47 | ZR::RetVal TmRemoteCohorte::setup( RSZRRemoteTxInitItem * item ) 48 | { 49 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 50 | m_PaymentReceiver = item->getPayment(); 51 | m_Phase = QUERY; 52 | if( m_PaymentReceiver->getAmount() < ZR::ZR_Number(1, 1000000)){ 53 | return abortTx( item ); // minimum is 1E-6 in any currency, in particular, this checks if amount < 0 54 | } 55 | 56 | Payment::Request req = Payment::getMyRequest( item->getAddress() ); 57 | if( isPayee( item->getAddress() ) == ZR::ZR_SUCCESS ){ // we are the payee 58 | std::cerr << "Zero Reserve: TX Cohorte: Initializing payee role :: Amount: " 59 | << m_PaymentReceiver->getAmount() << " " << m_PaymentReceiver->getCurrency() << std::endl; 60 | TxPhase vote = ( m_PaymentReceiver->init() == ZR::ZR_FAILURE ) ? VOTE_NO : VOTE_YES; 61 | RSZRRemoteTxInitItem * resendItem = new RSZRRemoteTxInitItem( item->getAddress(), vote, Router::CLIENT, m_PaymentReceiver, item->getPayerId() ); 62 | m_IsHop = false; 63 | resendItem->PeerId( item->PeerId() ); 64 | p3zr->sendItem( resendItem ); 65 | } 66 | else{ // we are a hop 67 | m_IsHop = true; 68 | m_PaymentSpender = new PaymentSpender( Router::Instance()->nextHop( item->getAddress() ), m_PaymentReceiver->getAmount(), m_PaymentReceiver->getCurrency(), m_PaymentReceiver->getCategory() ); 69 | forwardItem( item ); 70 | } 71 | 72 | return ZR::ZR_SUCCESS; 73 | } 74 | 75 | ZR::RetVal TmRemoteCohorte::isPayee( const ZR::VirtualAddress & addr ) 76 | { 77 | Payment::Request req = Payment::getMyRequest( addr ); 78 | if( req.isValid() ) 79 | return ZR::ZR_SUCCESS; 80 | 81 | if( MyOrders::Instance()->find( addr ) != MyOrders::Instance()->end() ){ 82 | return ZR::ZR_SUCCESS; 83 | } 84 | return ZR::ZR_FAILURE; 85 | } 86 | 87 | ZR::RetVal TmRemoteCohorte::forwardItem( RSZRRemoteTxItem * item ) 88 | { 89 | std::cerr << "Zero Reserve: TX Cohorte: Passing on query" << std::endl; 90 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 91 | RSZRRemoteTxInitItem * resendItem = new RSZRRemoteTxInitItem( item->getAddress() , item->getTxPhase(), item->getDirection(), m_PaymentReceiver, item->getPayerId() ); 92 | ZR::PeerAddress nextAddr = Router::Instance()->nextHop( item->getAddress() ); 93 | ZR::PeerAddress prevAddr = item->PeerId(); 94 | std::pair< ZR::PeerAddress, ZR::PeerAddress > route( prevAddr, nextAddr ); 95 | Router::Instance()->addTunnel( item->getAddress(), route ); 96 | resendItem->PeerId( nextAddr ); 97 | p3zr->sendItem( resendItem ); 98 | return ZR::ZR_SUCCESS; 99 | } 100 | 101 | 102 | ZR::RetVal TmRemoteCohorte::processItem( RSZRRemoteTxItem * item ) 103 | { 104 | RSZRRemoteTxItem * reply; 105 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 106 | 107 | // TODO: Timeout 108 | switch( item->getTxPhase() ) 109 | { 110 | case QUERY: 111 | { 112 | RSZRRemoteTxInitItem * initItem = dynamic_cast< RSZRRemoteTxInitItem *> ( item ); 113 | if( m_Phase != INIT || initItem == NULL ) 114 | return abortTx( item ); 115 | if( item->getDirection() == Router::SERVER ) 116 | return setup( initItem ); 117 | 118 | return ZR::ZR_FAILURE; 119 | } 120 | case VOTE_NO: 121 | case VOTE_YES: 122 | { 123 | std::cerr << "Zero Reserve: TX Cohorte: Received VOTE" << std::endl; 124 | RSZRRemoteTxInitItem * resendItem = new RSZRRemoteTxInitItem( item->getAddress(), item->getTxPhase(), item->getDirection(), m_PaymentReceiver, item->getPayerId() ); 125 | std::pair< ZR::PeerAddress, ZR::PeerAddress > route; 126 | if( Router::Instance()->getTunnel( item->getAddress(), route ) == ZR::ZR_FAILURE ) 127 | return ZR::ZR_FAILURE; 128 | resendItem->PeerId( route.first ); 129 | p3zr->sendItem( resendItem ); 130 | return ZR::ZR_SUCCESS; 131 | } 132 | case COMMIT: 133 | std::cerr << "Zero Reserve: TX Cohorte: Received Command: COMMIT" << std::endl; 134 | if( m_Phase != QUERY ) 135 | return abortTx( item ); 136 | m_Phase = COMMIT; 137 | if( m_IsHop ){ 138 | return forwardItem( item ); 139 | } 140 | else { 141 | std::cerr << "Zero Reserve: TX Cohorte: Payee committing" << std::endl; 142 | m_PaymentReceiver->commit( m_TxId ); 143 | reply = new RSZRRemoteTxItem( item->getAddress(), ACK_COMMIT, Router::CLIENT, item->getPayerId() ); 144 | reply->PeerId( item->PeerId() ); 145 | p3zr->sendItem( reply ); 146 | return ZR::ZR_FINISH; 147 | } 148 | break; 149 | case ACK_COMMIT: 150 | { 151 | std::cerr << "Zero Reserve: TX Cohorte: Received ACK_COMMIT" << std::endl; 152 | if( m_Phase != COMMIT ) 153 | return abortTx( item ); 154 | reply = new RSZRRemoteTxItem( item->getAddress(), item->getTxPhase(), item->getDirection(), item->getPayerId() ); 155 | std::pair< ZR::PeerAddress, ZR::PeerAddress > route; 156 | if( Router::Instance()->getTunnel( item->getAddress(), route ) == ZR::ZR_FAILURE ) 157 | return ZR::ZR_FAILURE; 158 | reply->PeerId( route.first ); 159 | m_PaymentReceiver->commit( m_TxId ); 160 | m_PaymentSpender->commit( m_TxId ); 161 | p3zr->sendItem( reply ); 162 | return ZR::ZR_FINISH; 163 | } 164 | case ABORT: 165 | { 166 | std::cerr << "Zero Reserve: TX Cohorte: Received ABORT" << std::endl; 167 | if( item->getDirection() == Router::SERVER ){ 168 | forwardItem( item ); 169 | rollback(); 170 | return ZR::ZR_FINISH; // can die after passing on the message 171 | } 172 | else { 173 | return abortTx( item ); 174 | } 175 | } 176 | default: 177 | throw std::runtime_error( "Unknown Transaction Phase"); 178 | } 179 | } 180 | 181 | ZR::RetVal TmRemoteCohorte::abortTx(RSZRRemoteTxItem *item ) 182 | { 183 | std::cerr << "Zero Reserve: TX Cohorte: Sending ABORT" << std::endl; 184 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 185 | RSZRRemoteTxItem * abortItem = new RSZRRemoteTxItem( item->getAddress(), ABORT, Router::CLIENT, item->getPayerId() ); 186 | std::pair< ZR::PeerAddress, ZR::PeerAddress > route; 187 | if( Router::Instance()->getTunnel( item->getAddress(), route ) == ZR::ZR_FAILURE ) 188 | return ZR::ZR_FAILURE; 189 | abortItem->PeerId( route.first ); 190 | p3zr->sendItem( abortItem ); 191 | return ZR::ZR_SUCCESS; // only requesting ABORT from coordinator here. Need to stay alive 192 | } 193 | -------------------------------------------------------------------------------- /OrderBook.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "OrderBook.h" 19 | #include "ZeroReservePlugin.h" 20 | #include "p3ZeroReserverRS.h" 21 | #include "zrdb.h" 22 | 23 | #include "util/radix64.h" 24 | 25 | #include 26 | #include 27 | 28 | #ifdef ZR_TESTNET 29 | const qint64 OrderBook::Order::timeout = 1800000; // 30 minutes 30 | #else 31 | const qint64 OrderBook::Order::timeout = 86400000; // one day 32 | #endif 33 | 34 | OrderBook::OrderBook() : 35 | m_order_mutex("order_mutex") 36 | { 37 | m_myOrders = NULL; 38 | } 39 | 40 | OrderBook::~OrderBook() 41 | { 42 | for( OrderIterator it = m_orders.begin(); it != m_orders.end(); it++) delete *it; 43 | } 44 | 45 | QModelIndex OrderBook::index(int x, int y, const QModelIndex&) const 46 | { 47 | return createIndex(x, y); 48 | } 49 | 50 | QModelIndex OrderBook::parent(const QModelIndex&) const 51 | { 52 | return QModelIndex(); 53 | } 54 | 55 | int OrderBook::rowCount(const QModelIndex&) const 56 | { 57 | RsStackMutex orderMutex( m_order_mutex ); 58 | return m_filteredOrders.size(); 59 | } 60 | 61 | int OrderBook::columnCount(const QModelIndex&) const 62 | { 63 | return 2; 64 | } 65 | 66 | 67 | QVariant OrderBook::data( const QModelIndex& index, int role ) const 68 | { 69 | RsStackMutex orderMutex( m_order_mutex ); 70 | 71 | if (role == Qt::DisplayRole && index.row() < m_filteredOrders.size()){ 72 | Order * order = m_filteredOrders[index.row()]; 73 | switch(index.column()){ 74 | case 0: 75 | return QVariant( order->m_amount.toDouble() ); 76 | case 1: 77 | return QVariant( order->m_price.toDouble() ); 78 | default: 79 | return QVariant(); 80 | } 81 | 82 | } 83 | return QVariant(); 84 | } 85 | 86 | QVariant OrderBook::headerData(int section, Qt::Orientation orientation, int role) const 87 | { 88 | if (role == Qt::DisplayRole) 89 | { 90 | if (orientation == Qt::Horizontal) { 91 | switch (section) 92 | { 93 | case 0: 94 | return QString("Volume"); 95 | case 1: 96 | return QString("Price"); 97 | } 98 | } 99 | } 100 | return QVariant(); 101 | } 102 | 103 | void OrderBook::setCurrency( const QString & currency ) 104 | { 105 | m_currency = Currency::getCurrencyByName( currency.toStdString() ); 106 | beginResetModel(); 107 | filterOrders( m_filteredOrders, m_currency ); 108 | endResetModel(); 109 | } 110 | 111 | void OrderBook::filterOrders( OrderList & filteredOrders, const Currency::CurrencySymbols currencySym ) 112 | { 113 | RsStackMutex orderMutex( m_order_mutex ); 114 | filteredOrders.clear(); 115 | for(OrderIterator it = m_orders.begin(); it != m_orders.end(); it++){ 116 | if( (*it)->m_currency == currencySym ) 117 | filteredOrders.append( *it ); 118 | } 119 | qSort( filteredOrders.begin(), filteredOrders.end(), compareOrder ); 120 | } 121 | 122 | ZR::RetVal OrderBook::processMyOrder( Order* order ) 123 | { 124 | ZR::RetVal retval; 125 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 126 | 127 | try { 128 | ZrDB::Instance()->addOrder( order ); 129 | } 130 | catch( std::runtime_error & e ){ 131 | std::cerr << "Zero Reserve: ERROR:" << e.what() << std::endl; 132 | return ZR::ZR_FAILURE; 133 | } 134 | 135 | m_myOrders->addOrder( order ); 136 | addOrder( order ); 137 | 138 | if( order->m_orderType == Order::BID ){ 139 | retval = m_myOrders->match( order ); 140 | } 141 | else { 142 | // TODO: Re-enable this: 143 | // m_myOrders->matchAsk( order ); 144 | // addOrder( order ); 145 | // TODO: Add a publisher queue for the case that counterparty doesn't respond 146 | retval = ZR::ZR_SUCCESS; 147 | } 148 | if( ZR::ZR_SUCCESS == retval ){ 149 | p3zr->publishOrder( order ); 150 | } 151 | return ZR::ZR_SUCCESS; 152 | } 153 | 154 | ZR::RetVal OrderBook::processOrder( Order* order ) 155 | { 156 | if( m_myOrders->find( order->m_order_id ) != m_myOrders->end() ) 157 | return ZR::ZR_FAILURE; // this is my own order 158 | 159 | qint64 currentTime = QDateTime::currentMSecsSinceEpoch(); 160 | if( currentTime - order->m_timeStamp > Order::timeout 161 | || order->m_timeStamp - currentTime > 3600000){ // or more than an hour in the future 162 | return ZR::ZR_FAILURE; 163 | } 164 | 165 | if( Order::FILLED == order->m_purpose || Order::CANCEL == order->m_purpose ){ 166 | Order * oldOrder = remove( order ); 167 | if( oldOrder ){ 168 | delete oldOrder; 169 | return ZR::ZR_SUCCESS; 170 | } 171 | return ZR::ZR_FINISH; 172 | } 173 | 174 | if( Order::PARTLY_FILLED == order->m_purpose ){ 175 | for(OrderIterator it = m_orders.begin(); it != m_orders.end(); it++){ 176 | if( *order == *(*it) ){ 177 | if( order->m_amount == (*it)->m_amount ) 178 | return ZR::ZR_FINISH; // we have it already - do nothing 179 | } 180 | } 181 | remove( order ); // remove so it gets reinserted with the updates values below. 182 | addOrder( order ); 183 | return ZR::ZR_SUCCESS; 184 | } 185 | 186 | if( find( order->m_order_id ) != end() ) 187 | return ZR::ZR_FINISH; // order already in book 188 | 189 | addOrder( order ); 190 | return m_myOrders->matchOther( order ); 191 | } 192 | 193 | 194 | 195 | int OrderBook::addOrder( Order * order ) 196 | { 197 | std::cerr << "Zero Reserve: Inserting Type: " << (int)order->m_orderType << 198 | " Currency: " << order->m_currency << std::endl; 199 | { 200 | RsStackMutex orderMutex( m_order_mutex ); 201 | m_orders.append(order); 202 | } 203 | if( order->m_currency != m_currency ) return ZR::ZR_SUCCESS; 204 | 205 | beginInsertRows( QModelIndex(), m_filteredOrders.size(), m_filteredOrders.size()); 206 | filterOrders( m_filteredOrders, m_currency ); 207 | endInsertRows(); 208 | return ZR::ZR_SUCCESS; 209 | } 210 | 211 | 212 | 213 | OrderBook::Order * OrderBook::remove( Order * order ) 214 | { 215 | 216 | OrderIterator it = find( order->m_order_id ); 217 | if( it != m_orders.end() ){ 218 | { 219 | RsStackMutex orderMutex( m_order_mutex ); 220 | m_orders.erase( it ); 221 | } 222 | ZrDB::Instance()->deleteOrder( order); 223 | beginResetModel(); 224 | filterOrders( m_filteredOrders, m_currency ); 225 | endResetModel(); 226 | return *it; 227 | } 228 | return NULL; 229 | } 230 | 231 | OrderBook::Order * OrderBook::remove( const std::string & order_id ) 232 | { 233 | // FIXME: Boilerplate 234 | OrderIterator it = find( order_id ); 235 | if( it != m_orders.end() ){ 236 | { 237 | RsStackMutex orderMutex( m_order_mutex ); 238 | m_orders.erase( it ); 239 | } 240 | beginResetModel(); 241 | filterOrders( m_filteredOrders, m_currency ); 242 | endResetModel(); 243 | return *it; 244 | } 245 | return NULL; 246 | } 247 | 248 | OrderBook::OrderIterator OrderBook::find( const std::string & order_id ) 249 | { 250 | RsStackMutex orderMutex( m_order_mutex ); 251 | for( OrderIterator it = m_orders.begin(); it != m_orders.end(); it++ ){ 252 | if( order_id == (*it)->m_order_id ){ 253 | return it; 254 | } 255 | } 256 | return m_orders.end(); 257 | } 258 | 259 | bool OrderBook::compareOrder( const Order * left, const Order * right ){ 260 | return ( left->m_orderType == Order::BID ) ? left->m_price > right->m_price : left->m_price < right->m_price; 261 | } 262 | 263 | 264 | // Order implementation 265 | 266 | void OrderBook::Order::setOrderId() 267 | { 268 | unsigned char md[ SHA256_DIGEST_LENGTH ]; 269 | p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() ); 270 | std::ostringstream dataStream; 271 | // FIXME: the getOwnId() must be replaced by a secret - else the originator of the order can be calculated 272 | // FIXME: by friends and friends of friends 273 | dataStream << p3zr->getOwnId() << m_timeStamp << m_currency << m_orderType << m_amount << m_price; 274 | std::string data = dataStream.str(); 275 | int length = data.length(); 276 | unsigned char * buff = new unsigned char[ length ]; 277 | memcpy( buff, data.c_str(), length ); 278 | SHA256( buff, length, md); 279 | Radix64::encode( (const char*)md, SHA256_DIGEST_LENGTH, m_order_id ); 280 | delete [] buff; 281 | } 282 | 283 | 284 | bool OrderBook::Order::operator == ( const OrderBook::Order & other ) 285 | { 286 | if( m_order_id == other.m_order_id ) 287 | return true; 288 | else 289 | return false; 290 | } 291 | 292 | 293 | bool OrderBook::Order::operator < ( const Order & other ) const 294 | { 295 | return ( m_order_id.compare( other.m_order_id ) < 0 ); 296 | } 297 | 298 | 299 | void OrderBook::timeoutOrders() 300 | { 301 | qint64 currentTime = QDateTime::currentMSecsSinceEpoch(); 302 | OrderList ordersCopy( m_orders ); 303 | for( OrderIterator it = ordersCopy.begin(); it != ordersCopy.end(); it++ ){ 304 | Order * order = *it; 305 | if( currentTime - order->m_timeStamp > Order::timeout ){ 306 | if( order->m_commitment == 0 ){ 307 | remove( order ); 308 | if( order->m_isMyOrder ){ 309 | m_myOrders->remove( order ); 310 | } 311 | delete order; 312 | } 313 | } 314 | } 315 | } 316 | -------------------------------------------------------------------------------- /p3ZeroReserveRS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Zero Reserve Plugin for Retroshare. 3 | 4 | Zero Reserve is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Zero Reserve is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with Zero Reserve. If not, see . 16 | */ 17 | 18 | #include "p3ZeroReserverRS.h" 19 | #include "Credit.h" 20 | #include "Payment.h" 21 | #include "zrtypes.h" 22 | #include "Router.h" 23 | #include "ZeroReservePlugin.h" 24 | #include "ZeroReserveDialog.h" 25 | #include "MyOrders.h" 26 | 27 | #include "pqi/p3linkmgr.h" 28 | 29 | #include 30 | #include 31 | 32 | // after getting data from 3 peers, we believe we're complete 33 | static const int INIT_THRESHOLD = 3; 34 | 35 | p3ZeroReserveRS::p3ZeroReserveRS( RsPluginHandler *pgHandler, OrderBook * bids, OrderBook * asks, RsPeers* peers ) : 36 | RsPQIService( RS_SERVICE_TYPE_ZERORESERVE_PLUGIN, CONFIG_TYPE_ZERORESERVE_PLUGIN, 0, pgHandler ), 37 | m_bids(bids), 38 | m_asks(asks), 39 | m_peers(peers), 40 | m_initialized( 0 ) 41 | { 42 | addSerialType(new RsZeroReserveSerialiser()); 43 | pgHandler->getLinkMgr()->addMonitor( this ); 44 | } 45 | 46 | 47 | void p3ZeroReserveRS::statusChange(const std::list< pqipeer > &plist) 48 | { 49 | std::cerr << "Zero Reserve: Status changed:" << std::endl; 50 | if( m_initialized < INIT_THRESHOLD ){ 51 | for (std::list< pqipeer >::const_iterator it = plist.begin(); it != plist.end(); it++ ){ 52 | if( RS_PEER_CONNECTED & (*it).actions ){ 53 | RsZeroReserveMsgItem * item = new RsZeroReserveMsgItem( RsZeroReserveMsgItem::REQUEST_ORDERBOOK, "" ); 54 | item->PeerId( (*it).id ); 55 | sendItem( item ); 56 | } 57 | } 58 | } 59 | // give any newly connected peer updated credit info - we might have changed it. 60 | for (std::list< pqipeer >::const_iterator peerIt = plist.begin(); peerIt != plist.end(); peerIt++ ){ 61 | if( RS_PEER_CONNECTED & (*peerIt).actions ){ 62 | Credit::CreditList cl; 63 | Credit::getCreditList( cl, (*peerIt).id ); 64 | for( Credit::CreditList::const_iterator creditIt = cl.begin(); creditIt != cl.end(); creditIt++){ 65 | sendCredit( *creditIt ); 66 | } 67 | } 68 | } 69 | ZeroReserveDialog * mainWin = static_cast< ZeroReserveDialog * >( g_ZeroReservePlugin->qt_page() ); 70 | mainWin->updateFriendList(); 71 | } 72 | 73 | 74 | 75 | int p3ZeroReserveRS::tick() 76 | { 77 | processIncoming(); 78 | janitor(); 79 | return 0; 80 | } 81 | 82 | void p3ZeroReserveRS::janitor() 83 | { 84 | static time_t lastCleanup = 0; 85 | time_t t = time( 0 ); 86 | if( t - lastCleanup < 10 ) return; // cleanup once in 10 sec 87 | lastCleanup = t; 88 | 89 | m_asks->timeoutOrders(); 90 | m_bids->timeoutOrders(); 91 | 92 | TransactionManager::timeout(); 93 | } 94 | 95 | void p3ZeroReserveRS::processIncoming() 96 | { 97 | RsItem *item = NULL; 98 | while(NULL != (item = recvItem())){ 99 | switch( item->PacketSubType() ) 100 | { 101 | case RsZeroReserveItem::ZERORESERVE_ORDERBOOK_ITEM: 102 | handleOrder( dynamic_cast( item ) ); 103 | break; 104 | case RsZeroReserveItem::ZERORESERVE_TX_INIT_ITEM: 105 | case RsZeroReserveItem::ZERORESERVE_TX_ITEM: 106 | TransactionManager::handleTxItem( dynamic_cast( item ) ); 107 | break; 108 | case RsZeroReserveItem::ZERORESERVE_CREDIT_ITEM: 109 | handleCredit( dynamic_cast( item ) ); 110 | break; 111 | case RsZeroReserveItem::ZERORESERVE_MSG_ITEM: 112 | handleMessage( dynamic_cast( item ) ); 113 | break; 114 | case RsZeroReserveItem::ZR_REMOTE_PAYREQUEST_ITEM: 115 | handlePaymentRequest( dynamic_cast( item ) ); 116 | break; 117 | case RsZeroReserveItem::ZR_REMOTE_BUYREQUEST_ITEM: 118 | handleBuyRequest( dynamic_cast < RSZRBuyRequestItem *>( item ) ); 119 | break; 120 | case RsZeroReserveItem::ZR_REMOTE_TX_ITEM: 121 | case RsZeroReserveItem::ZR_REMOTE_TX_INIT_ITEM: 122 | TransactionManager::handleTxItem( dynamic_cast( item ) ); 123 | break; 124 | default: 125 | std::cerr << "Zero Reserve: Received Item unknown" << std::endl; 126 | } 127 | delete item; 128 | } 129 | } 130 | 131 | 132 | void p3ZeroReserveRS::handleBuyRequest( RSZRBuyRequestItem * item ) 133 | { 134 | #warning "IMPLEMENT" 135 | } 136 | 137 | 138 | 139 | ZR::RetVal p3ZeroReserveRS::sendBuyMsg( const ZR::VirtualAddress & ourAddress, const ZR::VirtualAddress & theirAddress, const ZR::ZR_Number & amount ) 140 | { 141 | #warning "IMPLEMENT" 142 | } 143 | 144 | 145 | void p3ZeroReserveRS::sendOrderBook( const std::string & uid ) 146 | { 147 | for( OrderBook::OrderIterator it = m_asks->begin(); it != m_asks->end(); it++ ){ 148 | sendOrder( uid, *it ); 149 | } 150 | for( OrderBook::OrderIterator it = m_bids->begin(); it != m_bids->end(); it++ ){ 151 | sendOrder( uid, *it ); 152 | } 153 | sendItem( new RsZeroReserveMsgItem( RsZeroReserveMsgItem::SENT_ORDERBOOK, "" ) ); 154 | } 155 | 156 | 157 | void p3ZeroReserveRS::handleMessage( RsZeroReserveMsgItem *item ) 158 | { 159 | std::cerr << "Zero Reserve: Received Message Item" << std::endl; 160 | RsZeroReserveMsgItem::MsgType msgType = item->getType(); 161 | switch( msgType ){ 162 | case RsZeroReserveMsgItem::REQUEST_ORDERBOOK: 163 | sendOrderBook( item->PeerId() ); 164 | break; 165 | case RsZeroReserveMsgItem::SENT_ORDERBOOK: 166 | m_initialized++; 167 | break; 168 | default: 169 | std::cerr << "Zero Reserve: Received unknown message" << std::endl; 170 | } 171 | } 172 | 173 | 174 | void p3ZeroReserveRS::handleOrder(RsZeroReserveOrderBookItem *item) 175 | { 176 | std::cerr << "Zero Reserve: Received Orderbook Item" << std::endl; 177 | ZR::RetVal result; 178 | item->print( std::cerr ); 179 | OrderBook::Order * order = item->getOrder(); 180 | 181 | if( !Router::Instance()->hasRoute( order->m_order_id ) ){ 182 | Router::Instance()->addRoute( order->m_order_id, item->PeerId() ); 183 | } 184 | 185 | if( order->m_orderType == OrderBook::Order::ASK ){ 186 | result = m_asks->processOrder( order ); 187 | } 188 | else{ 189 | result = m_bids->processOrder( order ); 190 | } 191 | 192 | if( ZR::ZR_SUCCESS == result ){ 193 | publishOrder( order ); 194 | } 195 | } 196 | 197 | void p3ZeroReserveRS::handleCredit(RsZeroReserveCreditItem *item) 198 | { 199 | std::cerr << "Zero Reserve: Received Credit Item" << std::endl; 200 | Credit * otherCredit = item->getCredit(); 201 | otherCredit->m_id = item->PeerId(); 202 | Credit ourCredit( otherCredit->m_id, otherCredit->m_currency ); 203 | if( ourCredit.m_our_credit != otherCredit->m_our_credit ){ 204 | otherCredit->updateOurCredit(); 205 | } 206 | if( ourCredit.m_balance != otherCredit->m_balance ){ 207 | std::cerr << "Zero Reserve: " << "Different balance: " << otherCredit->m_id << " has " << otherCredit->m_balance 208 | << " we have " << ourCredit.m_balance << std::endl; 209 | } 210 | } 211 | 212 | bool p3ZeroReserveRS::sendCredit( Credit * credit ) 213 | { 214 | std::cerr << "Zero Reserve: Sending Credit item to " << credit->m_id << std::endl; 215 | RsZeroReserveCreditItem * item = new RsZeroReserveCreditItem( credit ); 216 | if(!item){ 217 | std::cerr << "Cannot allocate RsZeroReserveCreditItem !" << std::endl; 218 | return false ; 219 | } 220 | sendItem( item ); 221 | item->print( std::cerr, 16 ); 222 | return true; 223 | } 224 | 225 | bool p3ZeroReserveRS::sendOrder( const std::string& peer_id, OrderBook::Order * order ) 226 | { 227 | std::cerr << "Zero Reserve: Sending order to " << peer_id << std::endl; 228 | RsZeroReserveOrderBookItem * item = new RsZeroReserveOrderBookItem( order ); 229 | if(!item){ 230 | std::cerr << "Cannot allocate RsZeroReserveOrderBookItem !" << std::endl; 231 | return false ; 232 | } 233 | item->PeerId( peer_id ); 234 | sendItem( item ); 235 | return true; 236 | } 237 | 238 | 239 | void p3ZeroReserveRS::publishOrder( OrderBook::Order * order ) 240 | { 241 | std::list< std::string > sendList; 242 | m_peers->getOnlineList(sendList); 243 | for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){ 244 | if( (*it) == getOwnId() ) continue; 245 | sendOrder( *it, order ); 246 | } 247 | } 248 | 249 | 250 | void p3ZeroReserveRS::sendRemote( const ZR::VirtualAddress & address, ZR::ZR_Number amount, const std::string & currency ) 251 | { 252 | std::list< std::string > sendList; 253 | m_peers->getOnlineList(sendList); 254 | for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){ 255 | if( (*it) == getOwnId() ) continue; 256 | RSZRPayRequestItem * item = new RSZRPayRequestItem( address, amount, currency ); 257 | item->PeerId( *it ); 258 | sendItem( item ); 259 | } 260 | } 261 | 262 | 263 | void p3ZeroReserveRS::handlePaymentRequest( RSZRPayRequestItem * item ) 264 | { 265 | if( Router::Instance()->hasRoute( item->getAddress() ) ){ 266 | return; 267 | } 268 | Credit credit( item->PeerId(), item->getCurrency() ); 269 | credit.loadPeer(); 270 | if( credit.getMyAvailable() == 0 ) return; 271 | 272 | std::cerr << "Zero Reserve: Adding address " << item->getAddress() << " to the routing table" << std::endl; 273 | Currency::CurrencySymbols currency = Currency::getCurrencyBySymbol( item->getCurrency() ); 274 | Router::Instance()->addRoute( item->getAddress(), item->PeerId() ); 275 | Payment::addRequest( item->getAddress(), Payment::Request( item->getAmount(), currency ) ); 276 | std::list< std::string > sendList; 277 | m_peers->getOnlineList(sendList); 278 | for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){ 279 | if( (*it) == getOwnId() || (*it) == item->PeerId() ) continue; 280 | RSZRPayRequestItem * republishItem = new RSZRPayRequestItem( *item ); 281 | republishItem->PeerId( *it ); 282 | sendItem( republishItem ); 283 | } 284 | } 285 | --------------------------------------------------------------------------------