ConfigurationΒΆ

There are several ways to load the necessary configuration at startup. This tutorial will just hard-code all parameters. You could also parse it from command line flags or read a json/yaml file. The configuration parameters are highly dependent on the blockchain that you want to use go-perun with. Since this example only uses Ethereum it only needs Ethereum parameters.

We put all parameters in a config struct and create a global cfg variable that will hold the configuration:

type config struct {
	mnemonic string                   // Ethereum wallet mnemonic.
	chainURL string                   // Url of the Ethereum node.
	hosts    map[Role]string          // Hosts for incoming connections.
	addrs    map[Role]*wallet.Address // Wallet addresses of both roles.
}

// cfg saves the global configuration.
var cfg config

RoleAlice and RoleBob are constants with values 0 and 1 for easier access:

type Role int

const (
	RoleAlice Role = iota
	RoleBob
)

func (r Role) String() string {
	switch r {
	case RoleAlice:
		return "Alice"
	case RoleBob:
		return "Bob"
	}
	return fmt.Sprintf("%d", r)
}

Then we need an init function that will set all fields of the configuration on startup:

func init() {
	cfg.mnemonic = "pistol kiwi shrug future ozone ostrich match remove crucial oblige cream critic"
	cfg.chainURL = "ws://127.0.0.1:8545"
	// Set the Host that the go-perun Client will bind to.
	cfg.hosts = map[Role]string{
		RoleAlice: "0.0.0.0:8401",
		RoleBob:   "0.0.0.0:8402",
	}
	// Fix the on-chain addresses of Alice and Bob.
	cfg.addrs = map[Role]*wallet.Address{
		RoleAlice: wallet.AsWalletAddr(common.HexToAddress("0x2EE1ac154435f542ECEc55C5b0367650d8A5343B")),
		RoleBob:   wallet.AsWalletAddr(common.HexToAddress("0x70765701b79a4e973dAbb4b30A72f5a845f22F9E")),
	}
}

The on-chain addresses can be hard-coded here since we already know them from the ganache-cli setup.