planetmint-go/x/machine/keeper/msg_server_attest_machine.go
Julian Strobl 4ba12f7b03
[config] Add planetmint section to app.toml
This patch extends `app.toml` and adds the following section with these
default values:

```
[planetmint]
watchmen-endpoint = "localhost"
watchmen-port = 7401
```

A global singleton `plmntConfig` is introduced to save and access the
values similar to how cosmos does it (see
vendor/github.com/cosmos/cosmos-sdk/types/config.go).

Different environments can be managed by changing the values in
`app.toml` and restarting the daemon.

// Closes #53

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-08-04 10:53:38 +02:00

60 lines
1.5 KiB
Go

package keeper
import (
"context"
"errors"
config "planetmint-go/config"
"planetmint-go/x/machine/types"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/crgimenes/go-osc"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMachine) (*types.MsgAttestMachineResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
isValidIssuerLiquid := validateIssuerLiquid(msg.Machine.IssuerLiquid)
if !isValidIssuerLiquid {
return nil, errors.New("invalid liquid key")
}
if msg.Machine.Reissue {
err := k.reissueMachine(msg.Machine)
if err != nil {
return nil, errors.New("an error occured while reissuning the machine")
}
}
k.StoreMachine(ctx, *msg.Machine)
k.StoreMachineIndex(ctx, *msg.Machine)
return &types.MsgAttestMachineResponse{}, nil
}
func validateIssuerLiquid(issuerLiquid string) bool {
xpubKeyLiquid, err := hdkeychain.NewKeyFromString(issuerLiquid)
if err != nil {
return false
}
isValidLiquidKey := xpubKeyLiquid.IsForNet(&chaincfg.MainNetParams)
return isValidLiquidKey
}
func (k msgServer) reissueMachine(machine *types.Machine) error {
conf := config.GetConfig()
client := osc.NewClient(conf.WatchmenConfig.Endpoint, conf.WatchmenConfig.Port)
msg := osc.NewMessage("/rddl/*")
msg.Append(machine.Name)
msg.Append(machine.Ticker)
msg.Append(machine.Domain)
msg.Append(int32(machine.Amount))
msg.Append("1")
msg.Append(int32(machine.Precision))
err := client.Send(msg)
return err
}