github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/blueprints/contracts.go (about)

     1  package blueprints
     2  
     3  import (
     4  	_ "embed"
     5  
     6  	"github.com/onflow/cadence"
     7  	jsoncdc "github.com/onflow/cadence/encoding/json"
     8  	"github.com/onflow/cadence/runtime/common"
     9  
    10  	"github.com/onflow/flow-go/model/flow"
    11  )
    12  
    13  var ContractDeploymentAuthorizedAddressesPath = cadence.Path{
    14  	Domain:     common.PathDomainStorage,
    15  	Identifier: "authorizedAddressesToDeployContracts",
    16  }
    17  var ContractRemovalAuthorizedAddressesPath = cadence.Path{
    18  	Domain:     common.PathDomainStorage,
    19  	Identifier: "authorizedAddressesToRemoveContracts",
    20  }
    21  var IsContractDeploymentRestrictedPath = cadence.Path{
    22  	Domain:     common.PathDomainStorage,
    23  	Identifier: "isContractDeploymentRestricted",
    24  }
    25  
    26  //go:embed scripts/setContractOperationAuthorizersTransactionTemplate.cdc
    27  var setContractOperationAuthorizersTransactionTemplate string
    28  
    29  //go:embed scripts/setIsContractDeploymentRestrictedTransactionTemplate.cdc
    30  var setIsContractDeploymentRestrictedTransactionTemplate string
    31  
    32  //go:embed scripts/deployContractTransactionTemplate.cdc
    33  var DeployContractTransactionTemplate []byte
    34  
    35  // SetContractDeploymentAuthorizersTransaction returns a transaction for updating list of authorized accounts allowed to deploy/update contracts
    36  func SetContractDeploymentAuthorizersTransaction(serviceAccount flow.Address, authorized []flow.Address) (*flow.TransactionBody, error) {
    37  	return setContractAuthorizersTransaction(ContractDeploymentAuthorizedAddressesPath, serviceAccount, authorized)
    38  }
    39  
    40  // SetContractRemovalAuthorizersTransaction returns a transaction for updating list of authorized accounts allowed to remove contracts
    41  func SetContractRemovalAuthorizersTransaction(serviceAccount flow.Address, authorized []flow.Address) (*flow.TransactionBody, error) {
    42  	return setContractAuthorizersTransaction(ContractRemovalAuthorizedAddressesPath, serviceAccount, authorized)
    43  }
    44  
    45  func setContractAuthorizersTransaction(
    46  	path cadence.Path,
    47  	serviceAccount flow.Address,
    48  	authorized []flow.Address,
    49  ) (*flow.TransactionBody, error) {
    50  	addressValues := make([]cadence.Value, 0, len(authorized))
    51  	for _, address := range authorized {
    52  		addressValues = append(
    53  			addressValues,
    54  			cadence.BytesToAddress(address.Bytes()))
    55  	}
    56  
    57  	addressesArg, err := jsoncdc.Encode(cadence.NewArray(addressValues))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	pathArg, err := jsoncdc.Encode(path)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return flow.NewTransactionBody().
    68  		SetScript([]byte(setContractOperationAuthorizersTransactionTemplate)).
    69  		AddAuthorizer(serviceAccount).
    70  		AddArgument(addressesArg).
    71  		AddArgument(pathArg), nil
    72  }
    73  
    74  // SetIsContractDeploymentRestrictedTransaction sets the restricted flag for contract deployment
    75  func SetIsContractDeploymentRestrictedTransaction(serviceAccount flow.Address, restricted bool) (*flow.TransactionBody, error) {
    76  	argRestricted, err := jsoncdc.Encode(cadence.Bool(restricted))
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	argPath, err := jsoncdc.Encode(IsContractDeploymentRestrictedPath)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return flow.NewTransactionBody().
    87  		SetScript([]byte(setIsContractDeploymentRestrictedTransactionTemplate)).
    88  		AddAuthorizer(serviceAccount).
    89  		AddArgument(argRestricted).
    90  		AddArgument(argPath), nil
    91  }
    92  
    93  // TODO (ramtin) get rid of authorizers
    94  func DeployContractTransaction(address flow.Address, contract []byte, contractName string) *flow.TransactionBody {
    95  	return flow.NewTransactionBody().
    96  		SetScript(DeployContractTransactionTemplate).
    97  		AddArgument(jsoncdc.MustEncode(cadence.String(contractName))).
    98  		AddArgument(jsoncdc.MustEncode(cadence.String(contract))).
    99  		AddAuthorizer(address)
   100  }