github.com/koko1123/flow-go-1@v0.29.6/fvm/blueprints/contracts.go (about)

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