github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/api/organisation.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package api
    10  
    11  import (
    12  	"fmt"
    13  	"math/big"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/ethereum/go-ethereum/common"
    18  	"github.com/ethereum/go-ethereum/ethclient"
    19  	"github.com/sirupsen/logrus"
    20  	"github.com/vchain-us/vcn/internal/blockchain"
    21  	"github.com/vchain-us/vcn/pkg/meta"
    22  )
    23  
    24  // BlockchainOrganisation represents the organization data stored onto the blockchain.
    25  type BlockchainOrganisation struct {
    26  	Owner     common.Address   `json:"owner"`
    27  	Members   []common.Address `json:"members"`
    28  	Hash      string           `json:"hash"`
    29  	Timestamp time.Time        `json:"timestamp"`
    30  }
    31  
    32  // OwnerID returns org owner's public address as string for o, if any, otherwise an empty string
    33  func (o *BlockchainOrganisation) OwnerID() string {
    34  	if o != nil && o.Owner != common.BigToAddress(big.NewInt(0)) {
    35  		return strings.ToLower(o.Owner.Hex())
    36  	}
    37  	return ""
    38  }
    39  
    40  // MembersIDs returns org members' IDs (SignerIDs) as slice of strings for o, if any, otherwise a zero-len slice
    41  func (o *BlockchainOrganisation) MembersIDs() []string {
    42  	if o != nil && o.Owner != common.BigToAddress(big.NewInt(0)) {
    43  		keys := make([]string, len(o.Members))
    44  		for i, el := range o.Members {
    45  			keys[i] = strings.ToLower(el.Hex())
    46  		}
    47  		return keys
    48  	}
    49  	return []string{}
    50  }
    51  
    52  // GetBlockChainOrganisation returns a BlockchainOrganisation for the organization name, if any.
    53  // It returns a nil value and an error if the organization is not found.
    54  func GetBlockChainOrganisation(name string) (*BlockchainOrganisation, error) {
    55  	logger().WithFields(logrus.Fields{
    56  		"name": name,
    57  	}).Trace("GetBlockChainOrganisation")
    58  
    59  	// Connect and get organisation data
    60  	client, err := ethclient.Dial(meta.MainNet())
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	contractAddress := common.HexToAddress(meta.OrganisationsRelayContractAddress())
    65  	instance, err := blockchain.NewOrganisationsRelay(contractAddress, client)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	owner, memberAddresses, hash, timestamp, err := instance.GetOrganisation(nil, name)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	if owner != common.BigToAddress(big.NewInt(0)) {
    75  		org := &BlockchainOrganisation{
    76  			Owner:     owner,
    77  			Members:   memberAddresses,
    78  			Hash:      hash,
    79  			Timestamp: time.Unix(timestamp.Int64(), 0),
    80  		}
    81  		logger().
    82  			WithField("organisation", org).
    83  			Trace("Blockchain organisation found")
    84  		return org, nil
    85  	}
    86  
    87  	return nil, makeError(fmt.Sprintf(`organisation "%s" not found`, name), logrus.Fields{
    88  		"name": name,
    89  	})
    90  }