-
Notifications
You must be signed in to change notification settings - Fork 174
/
contracts.go
61 lines (51 loc) · 1.91 KB
/
contracts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package blueprints
import (
"encoding/hex"
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-go/fvm/utils"
"github.com/onflow/flow-go/model/flow"
)
const ContractDeploymentAuthorizedAddressesPathDomain = "storage"
const ContractDeploymentAuthorizedAddressesPathIdentifier = "authorizedAddressesToDeployContracts"
const setContractDeploymentAuthorizersTransactionTemplate = `
transaction(addresses: [Address], path: StoragePath) {
prepare(signer: AuthAccount) {
signer.load<[Address]>(from: path)
signer.save(addresses, to: path)
}
}
`
// SetContractDeploymentAuthorizersTransaction returns a transaction for updating list of authroized accounts allowed to deploy/update contracts
func SetContractDeploymentAuthorizersTransaction(serviceAccount flow.Address, authorized []flow.Address) (*flow.TransactionBody, error) {
arg1, err := jsoncdc.Encode(utils.AddressSliceToCadenceValue(utils.FlowAddressSliceToCadenceAddressSlice(authorized)))
if err != nil {
return nil, err
}
arg2, err := jsoncdc.Encode(cadence.Path{
Domain: ContractDeploymentAuthorizedAddressesPathDomain,
Identifier: ContractDeploymentAuthorizedAddressesPathIdentifier,
})
if err != nil {
return nil, err
}
return flow.NewTransactionBody().
SetScript([]byte(setContractDeploymentAuthorizersTransactionTemplate)).
AddAuthorizer(serviceAccount).
AddArgument(arg1).
AddArgument(arg2), nil
}
const deployContractTransactionTemplate = `
transaction {
prepare(signer: AuthAccount) {
signer.contracts.add(name: "%s", code: "%s".decodeHex())
}
}
`
// TODO (ramtin) get rid of authorizers
func DeployContractTransaction(address flow.Address, contract []byte, contractName string) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(fmt.Sprintf(deployContractTransactionTemplate, contractName, hex.EncodeToString(contract)))).
AddAuthorizer(address)
}