Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy committed Dec 6, 2023
2 parents 04b853b + 6ca308d commit 4e7f738
Show file tree
Hide file tree
Showing 55 changed files with 2,584 additions and 566 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
sudo apt update
sudo apt install libwayland-dev libx11-dev libx11-xcb-dev libxkbcommon-x11-dev libgles2-mesa-dev libegl1-mesa-dev libffi-dev libxcursor-dev libvulkan-dev
- name: Copy needed files
run: |
cp "./libwallet/instantswap/instant_example.json" "./libwallet/instantswap/instant.json"
- name: Cache (dependencies)
uses: actions/cache@v1
id: cache
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Dockerfile
makefile

vendor
bin
bin
libwallet/instantswap/instant.json
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ A cross-platform desktop wallet built with [gio](https://gioui.org/).

## Building

* Set up instant exchange private key: copy file `./libwallet/instantswap/instant_example.json`
into `./libwallet/instantswap/instant.json`. Then replace with your own key. For now,
the supported instant exchange are: [trocador](https://trocador.app/),
[changenow](https://changenow.io/), [godex](https://godex.io/) and
[flypme](https://flyp.me/) (flypme does not require a private key)

For taking the api keys. Please go to the instant exchange websites

Note: You need to have [Go 1.19](https://golang.org/dl/) or above to build.

Then `go build`.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.3
github.com/btcsuite/btcwallet/walletdb v1.4.0
github.com/btcsuite/btcwallet/wtxmgr v1.5.0
github.com/crypto-power/instantswap v0.0.0-20230619161136-95ecf47d5ebf
github.com/crypto-power/instantswap v0.0.0-20231205171529-1a958b193aa4
github.com/dcrlabs/neutrino-ltc v0.0.0-20221031001456-55ef06cefead
github.com/decred/dcrd/addrmgr/v2 v2.0.2
github.com/decred/dcrd/blockchain/stake/v5 v5.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-power/instantswap v0.0.0-20230619161136-95ecf47d5ebf h1:8dd0iV9EMNiMOaZD2U3UjmMNmYTNbXz8L58NLWlFjWk=
github.com/crypto-power/instantswap v0.0.0-20230619161136-95ecf47d5ebf/go.mod h1:Yey9HyCagUlBLZfnUV4zTixvNrLvowj89BV5wVDVVXE=
github.com/crypto-power/instantswap v0.0.0-20231205171529-1a958b193aa4 h1:rILnjlNzcN1d3I3+9NZaAHQ8mb0sIrpef3MPTxnCyoA=
github.com/crypto-power/instantswap v0.0.0-20231205171529-1a958b193aa4/go.mod h1:Yey9HyCagUlBLZfnUV4zTixvNrLvowj89BV5wVDVVXE=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
58 changes: 58 additions & 0 deletions how-to-build-mobile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Building Cryptopower Wallet for Mobile

This readme assumes you have a working Android or iOS environment.

## 1. Building for Android

Note: To build Cryptopower for Android, you need to have;

1. gogio
2. [Android SDK with NDK bundle](https://developer.android.com/tools)
3. The ANDROID_SDK_ROOT point to the SDK root directory e.g `export ANDROID_SDK_ROOT=$HOME/.local/share/Android/Sdk`

Proceed to the [gioui Android doc](https://gioui.org/doc/install/android) for more info regarding gogio and other dependencies you will be needing.

execute the command below in a terminal window to install gogio:

`go install gioui.org/cmd/gogio@latest`

cd to the cryptopower root directory and execute the command below to generate a .apk file:

#### mainnet
`gogio -target android .`

#### testnet
`gogio -target android . --network=testnet`

there should now be a cryptopower.apk file in the cryptopower root directory. You can send this file to your android device and install it.

or to send it to your device automatically, execute the command below:

`adb install cryptopower.apk`

## 2. Building for iOS

Note: To build Cryptopower for iOS, you need to have;

1. gogio
2. [Xcode](https://developer.apple.com/xcode/)

Proceed to the [gioui iOS doc](https://gioui.org/doc/install/ios) for more info regarding gogio and other dependencies you will be needing.

execute the command below in a terminal window to install gogio:

`go install gioui.org/cmd/gogio@latest`

cd to the cryptopower root directory and execute the command below to generate a .app file:

#### mainnet
`gogio -o cryptopower.app -target ios .`

#### testnet
`gogio -o cryptopower.app -target ios . --network=testnet`

there should now be a cryptopower.app file in the cryptopower root directory. You can send this file to your iOS simulator.

or to send it to your simulator automatically, execute the command below:

`xcrun simctl install booted cryptopower.app`
23 changes: 20 additions & 3 deletions libwallet/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,23 @@ const (
DefaultConfirmations = 1
// DefaultRateRequestAmount is the amount used to perform the rate request query.
DefaultRateRequestAmount = 1
DefaultRateRequestBTC = 0.01
DefaultRateRequestLTC = 1
DefaultRateRequestDCR = 10
)

func DefaultRateRequestAmt(fromCurrency string) float64 {
switch fromCurrency {
case utils.BTCWalletAsset.String():
return DefaultRateRequestBTC
case utils.LTCWalletAsset.String():
return DefaultRateRequestLTC
case utils.DCRWalletAsset.String():
return DefaultRateRequestDCR
}
return DefaultRateRequestAmount
}

// StartScheduler starts the automatic order scheduler.
func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap.SchedulerParams) error {
const op errors.Op = "mgr.StartScheduler"
Expand Down Expand Up @@ -92,9 +107,11 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
fromCur := params.Order.FromCurrency
toCur := params.Order.ToCurrency
rateRequestParams := api.ExchangeRateRequest{
From: fromCur,
To: toCur,
Amount: DefaultRateRequestAmount, // amount needs to be greater than 0 to get the exchange rate
From: fromCur,
To: toCur,
Amount: DefaultRateRequestAmt(fromCur), // amount needs to be greater than 0 to get the exchange rate
FromNetwork: params.Order.FromNetwork,
ToNetwork: params.Order.ToNetwork,
}
log.Info("Order Scheduler: getting exchange rate info")
res, err := mgr.InstantSwap.GetExchangeRateInfo(exchangeObject, rateRequestParams)
Expand Down
5 changes: 5 additions & 0 deletions libwallet/instantswap/instant_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trocador": "your_trocador_api_key",
"changenow": "your_changenow_api_key",
"godex": "your_godex_api_key"
}
80 changes: 52 additions & 28 deletions libwallet/instantswap/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package instantswap

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"sync"
"time"
Expand All @@ -11,23 +13,51 @@ import (
"github.com/asdine/storm/q"
"github.com/crypto-power/instantswap/instantswap"

// Initialize exchange servers.
// load instantswap exchange packages
_ "github.com/crypto-power/instantswap/instantswap/exchange/changelly"
_ "github.com/crypto-power/instantswap/instantswap/exchange/changenow"
_ "github.com/crypto-power/instantswap/instantswap/exchange/coinswitch"
_ "github.com/crypto-power/instantswap/instantswap/exchange/flypme"
_ "github.com/crypto-power/instantswap/instantswap/exchange/godex"
_ "github.com/crypto-power/instantswap/instantswap/exchange/simpleswap"
_ "github.com/crypto-power/instantswap/instantswap/exchange/swapzone"
_ "github.com/crypto-power/instantswap/instantswap/exchange/trocador"
)

const (
// API_KEY_CHANGENOW is the changenow API key.
API_KEY_CHANGENOW = "249665653f1bbc620a70b4a6d25d0f8be126552e30c253df87685b880183be93" //nolint:revive
// API_KEY_GODEX is the godex API key.
API_KEY_GODEX = "lPM1O83kxGXJn9CpMhVRc8Yx22Z3h2/1EWyZ3lDoqtqEPYJqimHxysLKm7" + //nolint:revive
"RN5HO3QyH9PMXZy7n3CUQhF40cYWY2zg==a44e77479feb30c28481c020bce2a3b3"
)
//go:embed instant.json
var instants []byte
var privKeyMap = map[Server]string{
Trocador: "",
ChangeNow: "",
GoDex: "",
}

func init() {
// init private key map and ensure only supported exchange is filled
var newPrivKeyMap = make(map[Server]string)
err := json.Unmarshal(instants, &newPrivKeyMap)
if err != nil {
panic(err)
}
// assign available key to privKeyMap
for key := range privKeyMap {
if val, ok := newPrivKeyMap[key]; ok {
privKeyMap[key] = val
}
}
// delete instant exchange with no key
for key, val := range privKeyMap {
if val == "" {
delete(privKeyMap, key)
}
}
// add flypme to privKeyMap because it does not requires private key to access
privKeyMap[FlypMe] = ""
}

func GetInstantExchangePrivKey(server Server) (string, bool) {
key, ok := privKeyMap[server]
return key, ok
}

func NewInstantSwap(db *storm.DB) (*InstantSwap, error) {
if err := db.Init(&Order{}); err != nil {
Expand Down Expand Up @@ -154,19 +184,22 @@ func (instantSwap *InstantSwap) GetOrderByIDRaw(orderID int) (*Order, error) {
}

func (instantSwap *InstantSwap) CreateOrder(exchangeObject instantswap.IDExchange, params Order) (*Order, error) {
const op errors.Op = "instantSwap.CreateOrder"

data := instantswap.CreateOrder{
RefundAddress: params.RefundAddress, // if the trading fails, the exchange will refund coins here
Destination: params.DestinationAddress, // your exchanged coins will be sent here
FromCurrency: params.FromCurrency,
ToCurrency: params.ToCurrency,
InvoicedAmount: params.InvoicedAmount, // use InvoicedAmount or InvoicedAmount
FromNetwork: params.FromNetwork,
ToNetwork: params.ToNetwork,
Signature: params.Signature,
Provider: params.Provider,
}

res, err := exchangeObject.CreateOrder(data)
if err != nil {
return nil, errors.E(op, err)
return nil, err
}

order := &Order{
Expand Down Expand Up @@ -236,29 +269,20 @@ func (instantSwap *InstantSwap) GetExchangeRateInfo(exchangeObject instantswap.I
if err != nil {
return nil, errors.E(op, err)
}

return &res, nil
}

func (instantSwap *InstantSwap) ExchangeServers() []ExchangeServer {
return []ExchangeServer{
{
ChangeNow,
ExchangeConfig{
APIKey: API_KEY_CHANGENOW,
},
},
{
FlypMe,
ExchangeConfig{},
},
{
GoDex,
ExchangeConfig{
APIKey: API_KEY_GODEX,
var exchanges []ExchangeServer
for exchangeName, privKey := range privKeyMap {
exchanges = append(exchanges, ExchangeServer{
Server: exchangeName,
Config: ExchangeConfig{
APIKey: privKey,
},
},
})
}
return exchanges
}

// DeleteOrders deletes all orders saved to the DB.
Expand Down
18 changes: 4 additions & 14 deletions libwallet/instantswap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,10 @@ func (instantSwap *InstantSwap) checkForUpdates(exchangeObject instantswap.IDExc
// attempting to open legacy orders
nilExchangeServer := ExchangeServer{}
if order.ExchangeServer == nilExchangeServer {
switch order.Server {
case ChangeNow:
order.ExchangeServer.Server = order.Server
order.ExchangeServer.Config = ExchangeConfig{
APIKey: API_KEY_CHANGENOW,
}
case GoDex:
order.ExchangeServer.Server = order.Server
order.ExchangeServer.Config = ExchangeConfig{
APIKey: API_KEY_GODEX,
}
default:
order.ExchangeServer.Server = order.Server
order.ExchangeServer.Config = ExchangeConfig{}
privKey := privKeyMap[order.Server]
order.ExchangeServer.Server = order.Server
order.ExchangeServer.Config = ExchangeConfig{
APIKey: privKey,
}

err = instantSwap.updateOrder(order)
Expand Down
4 changes: 4 additions & 0 deletions libwallet/instantswap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
GoDex Server = "godex"
SimpleSwap Server = "simpleswap"
SwapZone Server = "swapzone"
Trocador Server = "trocador"
)

func (es Server) ToString() string {
Expand Down Expand Up @@ -88,6 +89,9 @@ type Order struct {

FromCurrency string `json:"fromCurrency"`
ToCurrency string `json:"toCurrency"`
FromNetwork string `json:"fromNetwork"`
ToNetwork string `json:"toNetwork"`
Provider string `json:"provider"`

DepositAddress string `json:"depositAddress"` // Address where funds that need to be exchanged should be sent to
RefundAddress string `json:"refundAddress"` // Address where funds are returned to if the exchange fails
Expand Down
2 changes: 2 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/modal"
"github.com/crypto-power/cryptopower/ui/page"
account "github.com/crypto-power/cryptopower/ui/page/accounts"
"github.com/crypto-power/cryptopower/ui/page/components"
"github.com/crypto-power/cryptopower/ui/page/dcrdex"
"github.com/crypto-power/cryptopower/ui/page/exchange"
Expand Down Expand Up @@ -145,6 +146,7 @@ func init() {
spv.UseLogger(dcrSpv)
instantswap.UseLogger(sharedWLog)
dcrdex.UseLogger(winLog)
account.UseLogger(winLog)

logger.New(subsystemSLoggers, subsystemBLoggers)
// Neutrino loglevel will always be set to error to control excessive logging.
Expand Down
2 changes: 1 addition & 1 deletion os.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package main

// OS-specific variables for non-android OS.

var isAndroidOS = false
const isAndroidOS = false
2 changes: 1 addition & 1 deletion os_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package main

// OS-specific variables for android OS.

var isAndroidOS = true
const isAndroidOS = true
Binary file removed ui/assets/decredicons/addIcon.png
Binary file not shown.
Binary file added ui/assets/decredicons/add_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/decredicons/trade_exchange_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/decredicons/trocador.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4e7f738

Please sign in to comment.