Skip to content

Commit

Permalink
added a flag to auto suggest signer based on the name of it, and made…
Browse files Browse the repository at this point in the history
… it possible to get all accounts on emulator
  • Loading branch information
bjartek committed Jan 28, 2025
1 parent 47ff62e commit a97abbd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 23 deletions.
34 changes: 29 additions & 5 deletions interaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type OverflowInteractionBuilder struct {
StopOnError *bool

Testing OverflowTestingAsssertions

AutoSigner bool
}

type OverflowTestingAsssertions struct {
Expand Down Expand Up @@ -315,6 +317,13 @@ func WithSigner(signer string) OverflowInteractionOption {
}
}

// set payer, proposer authorizer as the signer
func WithAutoSigner() OverflowInteractionOption {
return func(oib *OverflowInteractionBuilder) {
oib.AutoSigner = true
}
}

// set service account as payer, proposer, authorizer
func WithSignerServiceAccount() OverflowInteractionOption {
return func(oib *OverflowInteractionBuilder) {
Expand Down Expand Up @@ -492,15 +501,30 @@ func (oib OverflowInteractionBuilder) Send() *OverflowResult {
return result
}

if oib.Proposer == nil {
result.Err = fmt.Errorf("%v You need to set the proposer signer", emoji.PileOfPoo)
return result
}

codeFileName := fmt.Sprintf("%s/%s.cdc", oib.BasePath, oib.FileName)

result.DeclarationInfo = *declarationInfo(oib.TransactionCode)

// if we have more then one should the following be payload signers?
if oib.AutoSigner {
if len(result.DeclarationInfo.Authorizers) != 1 {
result.Err = errors.New("currently do not support more then 1 signer when using authSigner")
return result
}

account, err := oib.Overflow.AccountE(result.DeclarationInfo.Authorizers[0].Name)
if err != nil {
result.Err = err
return result
}
oib.Payer = account
oib.Proposer = account
}

if oib.Proposer == nil {
result.Err = fmt.Errorf("%v You need to set the proposer signer", emoji.PileOfPoo)
return result
}
oib.Overflow.Log.Reset()
/*
❗ Special case: if an account is both the payer and either a proposer or authorizer, it is only required to sign the envelope.
Expand Down
16 changes: 12 additions & 4 deletions npm_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ type OverflowSolution struct {
Warnings []string `json:"warnings"`
}

type OverflowAuthorizers [][]string
type OverflowAuthorizers []OverflowAuthorizer

type OverflowAuthorizer struct {
Name string
Entitlements []string
}

// a type containing information about parameter types and orders
type OverflowDeclarationInfo struct {
Expand Down Expand Up @@ -192,7 +197,7 @@ func paramsAndAuthorizers(code []byte) (*ast.ParameterList, OverflowAuthorizers)
prepareParams := txd.Prepare.FunctionDeclaration.ParameterList
if prepareParams != nil {
for _, parg := range txd.Prepare.FunctionDeclaration.ParameterList.ParametersByIdentifier() {
// name := parg.Identifier.Identifier
name := parg.Identifier.Identifier
ta := parg.TypeAnnotation
if ta != nil {
rt, ok := ta.Type.(*ast.ReferenceType)
Expand All @@ -205,9 +210,12 @@ func paramsAndAuthorizers(code []byte) (*ast.ParameterList, OverflowAuthorizers)
entitlements = append(entitlements, entitlement.Identifier.Identifier)
}
}
authorizers = append(authorizers, entitlements)
authorizers = append(authorizers, OverflowAuthorizer{
Name: name,
Entitlements: entitlements,
})
} else {
authorizers = append(authorizers, []string{})
authorizers = append(authorizers, OverflowAuthorizer{Name: name})
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,26 @@ func (o *OverflowState) ServiceAccountName() string {
return o.ServiceAccountSuffix
}

// CreateAccountsE ensures that all accounts present in the deployment block for the given network is present
func (o *OverflowState) GetEmulatorAccounts() map[string]string {
p := o.State
acct := *p.AccountsForNetwork(o.Network)

sort.SliceStable(acct, func(i, j int) bool {
return strings.Compare(acct[i].Name, acct[j].Name) < 1
})

accountMap := map[string]string{}
for _, account := range acct {
if account.Name == "emulator-account" {
continue
}

accountMap[strings.TrimPrefix(account.Name, "emulator-")] = account.Address.Hex()
}
return accountMap
}

// CreateAccountsE ensures that all accounts present in the deployment block for the given network is present
func (o *OverflowState) CreateAccountsE(ctx context.Context) (*OverflowState, error) {
p := o.State
Expand Down Expand Up @@ -568,6 +588,7 @@ func (o *OverflowState) sendTx(ftb *OverflowInteractionBuilder) *OverflowResult
po := *ftb.PrintOptions
result.Print(po...)
}

if o.StopOnError && result.Err != nil {
result.PrintArguments(nil)
panic(result.Err)
Expand Down
50 changes: 38 additions & 12 deletions testdata/TestParseConfig/parse_and_filter.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
ParameterOrder: []string{},
},
"create_nft_collection": {
Parameters: map[string]string{},
Authorizers: overflow.OverflowAuthorizers{[]string{"Storage"}},
Parameters: map[string]string{},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "acct",
Entitlements: []string{
"Storage",
},
}},
ParameterOrder: []string{},
},
"emulatorFoo": {
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "acct",
Entitlements: []string{"BorrowValue"},
}},
ParameterOrder: []string{"test"},
},
"mainnetFoo": {
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{[]string{}},
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "acct",
Entitlements: []string{},
}},
ParameterOrder: []string{"test"},
},
"mainnetaTransaction": {
Expand All @@ -35,7 +46,10 @@
"amount": "UFix64",
"recipient": "Address",
},
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "signer",
Entitlements: []string{"BorrowValue"},
}},
ParameterOrder: []string{
"recipient",
"amount",
Expand All @@ -46,7 +60,10 @@
"amount": "UFix64",
"to": "Address",
},
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "signer",
Entitlements: []string{"BorrowValue"},
}},
ParameterOrder: []string{
"amount",
"to",
Expand All @@ -55,14 +72,23 @@
"signWithMultipleAccounts": {
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{
[]string{},
[]string{},
overflow.OverflowAuthorizer{
Name: "acct",
Entitlements: []string{},
},
overflow.OverflowAuthorizer{
Name: "account2",
Entitlements: []string{},
},
},
ParameterOrder: []string{"test"},
},
"testnetFoo": {
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{[]string{"Storage"}},
Parameters: map[string]string{"test": "String"},
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
Name: "acct",
Entitlements: []string{"Storage"},
}},
ParameterOrder: []string{"test"},
},
"zTransaction": {
Expand Down
2 changes: 1 addition & 1 deletion transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionRes
authorizers = append(authorizers, auth)
standardStakeholders[auth] = []string{"authorizer"}
if len(argInfo.Authorizers) > i {
authorizerTypes[auth] = argInfo.Authorizers[i]
authorizerTypes[auth] = argInfo.Authorizers[i].Entitlements
}
}

Expand Down
2 changes: 1 addition & 1 deletion transaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func TestTransactionIntegration(t *testing.T) {
WithSigner("first"),
).AssertSuccess(t)

assert.Equal(t, []string{"BorrowValue", "SaveValue"}, res.DeclarationInfo.Authorizers[0])
assert.Equal(t, []string{"BorrowValue", "SaveValue"}, res.DeclarationInfo.Authorizers[0].Entitlements)
})

t.Run("send flow", func(t *testing.T) {
Expand Down

0 comments on commit a97abbd

Please sign in to comment.