Skip to content

Commit a97abbd

Browse files
committed
added a flag to auto suggest signer based on the name of it, and made it possible to get all accounts on emulator
1 parent 47ff62e commit a97abbd

File tree

6 files changed

+102
-23
lines changed

6 files changed

+102
-23
lines changed

interaction_builder.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ type OverflowInteractionBuilder struct {
8888
StopOnError *bool
8989

9090
Testing OverflowTestingAsssertions
91+
92+
AutoSigner bool
9193
}
9294

9395
type OverflowTestingAsssertions struct {
@@ -315,6 +317,13 @@ func WithSigner(signer string) OverflowInteractionOption {
315317
}
316318
}
317319

320+
// set payer, proposer authorizer as the signer
321+
func WithAutoSigner() OverflowInteractionOption {
322+
return func(oib *OverflowInteractionBuilder) {
323+
oib.AutoSigner = true
324+
}
325+
}
326+
318327
// set service account as payer, proposer, authorizer
319328
func WithSignerServiceAccount() OverflowInteractionOption {
320329
return func(oib *OverflowInteractionBuilder) {
@@ -492,15 +501,30 @@ func (oib OverflowInteractionBuilder) Send() *OverflowResult {
492501
return result
493502
}
494503

495-
if oib.Proposer == nil {
496-
result.Err = fmt.Errorf("%v You need to set the proposer signer", emoji.PileOfPoo)
497-
return result
498-
}
499-
500504
codeFileName := fmt.Sprintf("%s/%s.cdc", oib.BasePath, oib.FileName)
501505

502506
result.DeclarationInfo = *declarationInfo(oib.TransactionCode)
503507

508+
// if we have more then one should the following be payload signers?
509+
if oib.AutoSigner {
510+
if len(result.DeclarationInfo.Authorizers) != 1 {
511+
result.Err = errors.New("currently do not support more then 1 signer when using authSigner")
512+
return result
513+
}
514+
515+
account, err := oib.Overflow.AccountE(result.DeclarationInfo.Authorizers[0].Name)
516+
if err != nil {
517+
result.Err = err
518+
return result
519+
}
520+
oib.Payer = account
521+
oib.Proposer = account
522+
}
523+
524+
if oib.Proposer == nil {
525+
result.Err = fmt.Errorf("%v You need to set the proposer signer", emoji.PileOfPoo)
526+
return result
527+
}
504528
oib.Overflow.Log.Reset()
505529
/*
506530
❗ Special case: if an account is both the payer and either a proposer or authorizer, it is only required to sign the envelope.

npm_module.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ type OverflowSolution struct {
2828
Warnings []string `json:"warnings"`
2929
}
3030

31-
type OverflowAuthorizers [][]string
31+
type OverflowAuthorizers []OverflowAuthorizer
32+
33+
type OverflowAuthorizer struct {
34+
Name string
35+
Entitlements []string
36+
}
3237

3338
// a type containing information about parameter types and orders
3439
type OverflowDeclarationInfo struct {
@@ -192,7 +197,7 @@ func paramsAndAuthorizers(code []byte) (*ast.ParameterList, OverflowAuthorizers)
192197
prepareParams := txd.Prepare.FunctionDeclaration.ParameterList
193198
if prepareParams != nil {
194199
for _, parg := range txd.Prepare.FunctionDeclaration.ParameterList.ParametersByIdentifier() {
195-
// name := parg.Identifier.Identifier
200+
name := parg.Identifier.Identifier
196201
ta := parg.TypeAnnotation
197202
if ta != nil {
198203
rt, ok := ta.Type.(*ast.ReferenceType)
@@ -205,9 +210,12 @@ func paramsAndAuthorizers(code []byte) (*ast.ParameterList, OverflowAuthorizers)
205210
entitlements = append(entitlements, entitlement.Identifier.Identifier)
206211
}
207212
}
208-
authorizers = append(authorizers, entitlements)
213+
authorizers = append(authorizers, OverflowAuthorizer{
214+
Name: name,
215+
Entitlements: entitlements,
216+
})
209217
} else {
210-
authorizers = append(authorizers, []string{})
218+
authorizers = append(authorizers, OverflowAuthorizer{Name: name})
211219
}
212220
}
213221
}

state.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,26 @@ func (o *OverflowState) ServiceAccountName() string {
403403
return o.ServiceAccountSuffix
404404
}
405405

406+
// CreateAccountsE ensures that all accounts present in the deployment block for the given network is present
407+
func (o *OverflowState) GetEmulatorAccounts() map[string]string {
408+
p := o.State
409+
acct := *p.AccountsForNetwork(o.Network)
410+
411+
sort.SliceStable(acct, func(i, j int) bool {
412+
return strings.Compare(acct[i].Name, acct[j].Name) < 1
413+
})
414+
415+
accountMap := map[string]string{}
416+
for _, account := range acct {
417+
if account.Name == "emulator-account" {
418+
continue
419+
}
420+
421+
accountMap[strings.TrimPrefix(account.Name, "emulator-")] = account.Address.Hex()
422+
}
423+
return accountMap
424+
}
425+
406426
// CreateAccountsE ensures that all accounts present in the deployment block for the given network is present
407427
func (o *OverflowState) CreateAccountsE(ctx context.Context) (*OverflowState, error) {
408428
p := o.State
@@ -568,6 +588,7 @@ func (o *OverflowState) sendTx(ftb *OverflowInteractionBuilder) *OverflowResult
568588
po := *ftb.PrintOptions
569589
result.Print(po...)
570590
}
591+
571592
if o.StopOnError && result.Err != nil {
572593
result.PrintArguments(nil)
573594
panic(result.Err)

testdata/TestParseConfig/parse_and_filter.golden

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@
66
ParameterOrder: []string{},
77
},
88
"create_nft_collection": {
9-
Parameters: map[string]string{},
10-
Authorizers: overflow.OverflowAuthorizers{[]string{"Storage"}},
9+
Parameters: map[string]string{},
10+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
11+
Name: "acct",
12+
Entitlements: []string{
13+
"Storage",
14+
},
15+
}},
1116
ParameterOrder: []string{},
1217
},
1318
"emulatorFoo": {
14-
Parameters: map[string]string{"test": "String"},
15-
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
19+
Parameters: map[string]string{"test": "String"},
20+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
21+
Name: "acct",
22+
Entitlements: []string{"BorrowValue"},
23+
}},
1624
ParameterOrder: []string{"test"},
1725
},
1826
"mainnetFoo": {
19-
Parameters: map[string]string{"test": "String"},
20-
Authorizers: overflow.OverflowAuthorizers{[]string{}},
27+
Parameters: map[string]string{"test": "String"},
28+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
29+
Name: "acct",
30+
Entitlements: []string{},
31+
}},
2132
ParameterOrder: []string{"test"},
2233
},
2334
"mainnetaTransaction": {
@@ -35,7 +46,10 @@
3546
"amount": "UFix64",
3647
"recipient": "Address",
3748
},
38-
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
49+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
50+
Name: "signer",
51+
Entitlements: []string{"BorrowValue"},
52+
}},
3953
ParameterOrder: []string{
4054
"recipient",
4155
"amount",
@@ -46,7 +60,10 @@
4660
"amount": "UFix64",
4761
"to": "Address",
4862
},
49-
Authorizers: overflow.OverflowAuthorizers{[]string{"BorrowValue"}},
63+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
64+
Name: "signer",
65+
Entitlements: []string{"BorrowValue"},
66+
}},
5067
ParameterOrder: []string{
5168
"amount",
5269
"to",
@@ -55,14 +72,23 @@
5572
"signWithMultipleAccounts": {
5673
Parameters: map[string]string{"test": "String"},
5774
Authorizers: overflow.OverflowAuthorizers{
58-
[]string{},
59-
[]string{},
75+
overflow.OverflowAuthorizer{
76+
Name: "acct",
77+
Entitlements: []string{},
78+
},
79+
overflow.OverflowAuthorizer{
80+
Name: "account2",
81+
Entitlements: []string{},
82+
},
6083
},
6184
ParameterOrder: []string{"test"},
6285
},
6386
"testnetFoo": {
64-
Parameters: map[string]string{"test": "String"},
65-
Authorizers: overflow.OverflowAuthorizers{[]string{"Storage"}},
87+
Parameters: map[string]string{"test": "String"},
88+
Authorizers: overflow.OverflowAuthorizers{overflow.OverflowAuthorizer{
89+
Name: "acct",
90+
Entitlements: []string{"Storage"},
91+
}},
6692
ParameterOrder: []string{"test"},
6793
},
6894
"zTransaction": {

transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionRes
9898
authorizers = append(authorizers, auth)
9999
standardStakeholders[auth] = []string{"authorizer"}
100100
if len(argInfo.Authorizers) > i {
101-
authorizerTypes[auth] = argInfo.Authorizers[i]
101+
authorizerTypes[auth] = argInfo.Authorizers[i].Entitlements
102102
}
103103
}
104104

transaction_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func TestTransactionIntegration(t *testing.T) {
351351
WithSigner("first"),
352352
).AssertSuccess(t)
353353

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

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

0 commit comments

Comments
 (0)