fetch.go 997 Bytes
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1 2 3 4 5 6 7 8 9
package sign

import (
	"io/ioutil"

	"dfss/dfssc/common"
	"dfss/dfssc/security"
	"dfss/dfssp/api"
	"dfss/net"
10
	"github.com/spf13/viper"
Loïck Bonniot's avatar
Loïck Bonniot committed
11 12 13 14
	"golang.org/x/net/context"
)

// FetchContract tries to download contract metadata from specified uuid, and stores the resulting json at path
ElyKar's avatar
ElyKar committed
15 16
func FetchContract(passphrase, uuid, path string) error {
	auth := security.NewAuthContainer(passphrase)
Loïck Bonniot's avatar
Loïck Bonniot committed
17 18 19 20 21
	ca, cert, key, err := auth.LoadFiles()
	if err != nil {
		return err
	}

22
	conn, err := net.Connect(viper.GetString("platform_addrport"), cert, key, ca, nil)
Loïck Bonniot's avatar
Loïck Bonniot committed
23 24 25 26 27 28 29 30
	if err != nil {
		return err
	}

	request := &api.GetContractRequest{
		Uuid: uuid,
	}
	client := api.NewPlatformClient(conn)
31
	ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
Loïck Bonniot's avatar
Loïck Bonniot committed
32 33 34 35 36 37 38 39 40 41 42 43 44
	defer cancel()
	response, err := client.GetContract(ctx, request)
	if err != nil {
		return err
	}

	err = common.EvaluateErrorCodeResponse(response.ErrorCode)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(path, response.Json, 0600)
}