summaryrefslogtreecommitdiff
path: root/simulation/main.go
blob: 0a0b8b65c32299ec1f1e1ac4c6a0a19c296d86f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"fmt"
	"os"
)

const DISABLE_DELAYS = false

const C2EC_BASE_URL = "http://localhost:8082"
const C2EC_BANK_BASE_URL = C2EC_BASE_URL + "/c2ec"
const C2EC_BANK_CONFIG_URL = C2EC_BANK_BASE_URL + "/config"
const C2EC_BANK_WITHDRAWAL_STATUS_URL = C2EC_BANK_BASE_URL + "/withdrawal-operation/:wopid"
const C2EC_BANK_WITHDRAWAL_REGISTRATION_URL = C2EC_BANK_BASE_URL + "/withdrawal-operation/:wopid"
const C2EC_BANK_WITHDRAWAL_PAYMENT_URL = C2EC_BANK_BASE_URL + "/withdrawal-operation/:wopid/payment"

// simulates the terminal talking to its backend system and executing the payment.
const PROVIDER_BACKEND_PAYMENT_DELAY_MS = 1000

// simulates the provider client fetching attestation at the providers backend.
const PROVIDER_BACKEND_ATTESTATION_DELAY_MS = 1000

// simulates the user presenting his card to the terminal
const TERMINAL_ACCEPT_CARD_DELAY_MS = 5000

// simulates the user scanning the QR code presented at the terminal
const WALLET_SCAN_QR_CODE_DELAY_MS = 5000

// https://docs.taler.net/core/api-exchange.html#tsref-type-CurrencySpecification
type CurrencySpecification struct {
	Name                            string `json:"name"`
	Currency                        string `json:"currency"`
	NumFractionalInputDigits        int    `json:"num_fractional_input_digits"`
	NumFractionalNormalDigits       int    `json:"num_fractional_normal_digits"`
	NumFractionalTrailingZeroDigits int    `json:"num_fractional_trailing_zero_digits"`
	AltUnitNames                    string `json:"alt_unit_names"`
}

// https://docs.taler.net/core/api-bank-integration.html#tsref-type-BankIntegrationConfig
type BankIntegrationConfig struct {
	Name                  string                `json:"name"`
	Version               string                `json:"version"`
	Implementation        string                `json:"implementation"`
	Currency              string                `json:"currency"`
	CurrencySpecification CurrencySpecification `json:"currency_specification"`
}

type SimulatedPhysicalInteraction struct {
	Msg string
}

func main() {

	if !c2ecAlive() {
		fmt.Println("start c2ec first.")
		return
	}

	kill := make(chan error)
	toTerminal := make(chan *SimulatedPhysicalInteraction, 10)
	toWallet := make(chan *SimulatedPhysicalInteraction, 10)

	// start simulated terminal
	go Terminal(toTerminal, toWallet, kill)

	// start simulated wallet
	go Wallet(toWallet, toTerminal, kill)

	for err := range kill {
		if err == nil {
			fmt.Print("simulation successful.")
			os.Exit(0)
		}
		fmt.Println("simulation error: ", err.Error())
		os.Exit(1)
	}
}

func c2ecAlive() bool {

	cfg, status, err := HttpGet(C2EC_BANK_CONFIG_URL, map[string]string{}, NewJsonCodec[BankIntegrationConfig]())
	if err != nil || status != 200 {
		return false
	}

	fmt.Println("C2EC-Config:", cfg.Name, cfg.Version, cfg.Currency, cfg.CurrencySpecification.AltUnitNames)
	return true
}