Skip to content

Commit e6e580b

Browse files
committed
Address books
1 parent 1dec601 commit e6e580b

12 files changed

Lines changed: 265 additions & 131 deletions

File tree

state/state.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
)
1313

1414
type StateStruct struct {
15-
Senders []*common.Address
16-
Paymasters []*common.Address
15+
AddressBooks map[string]*AddressBook
1716
//Default Gas Costs?
1817
Signers []signer.Signer `json:"-"`
1918
SignersRaw []string
@@ -31,15 +30,50 @@ func InitState() {
3130
err := State.Load()
3231
if err != nil {
3332
fmt.Println(err)
33+
State.AddressBooks = make(map[string]*AddressBook)
3434

35-
State.Senders = []*common.Address{}
36-
State.Paymasters = make([]*common.Address, 1)
37-
State.Paymasters[0] = &common.Address{}
35+
State.AddressBooks[Sender] = &AddressBook{}
36+
State.AddressBooks[Paymaster] = &AddressBook{}
3837
State.Signers = []signer.Signer{}
3938
State.ABIs = make(map[string]string)
4039
}
4140
}
4241

42+
type AddressBook []*common.Address
43+
44+
const Sender = "Sender"
45+
const Paymaster = "Paymaster"
46+
47+
func GetAddressBook(label string) (*AddressBook, bool) {
48+
ab, ok := State.AddressBooks[label]
49+
if !ok {
50+
ab = &AddressBook{}
51+
State.AddressBooks[label] = ab
52+
}
53+
return ab, true
54+
}
55+
56+
func (ab *AddressBook) Add(addr *common.Address) {
57+
*ab = append(*ab, addr)
58+
State.Save()
59+
}
60+
61+
func (ab *AddressBook) Remove(addr *common.Address) {
62+
//Find the address index
63+
i := 0
64+
for i = 0; i < len(*ab); i++ {
65+
if (*ab)[i].Hex() == addr.Hex() {
66+
break
67+
}
68+
}
69+
if i == len(*ab) {
70+
return
71+
}
72+
//Remove the address
73+
*ab = append((*ab)[:i], (*ab)[i+1:]...)
74+
State.Save()
75+
}
76+
4377
var StateFile = "state.json"
4478

4579
func (st *StateStruct) Save() {

ui/abiui.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ func SelectMethodUI(contract string) {
258258
func SetParamUI(label string, input abi.Argument) (interface{}, error) {
259259
switch input.Type.T {
260260
case abi.AddressTy:
261-
return InputNewAddressUI(label)
261+
addr, ok := AddressFromBookUI(label)
262+
if ok {
263+
return addr, nil
264+
}
262265
case abi.UintTy:
263266
switch input.Type.Size {
264267
case 256, 160, 128:

ui/addressbookui.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package ui
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/manifoldco/promptui"
8+
"github.com/san-lab/go4337/state"
9+
)
10+
11+
var AddAddressItem = &Item{Label: "Add a new Address"}
12+
var RemoveAddressItem = &Item{Label: "Remove an Address"}
13+
14+
func AddressFromBookUI(label string) (*common.Address, bool) {
15+
selectToRemove := false
16+
abook, ok := state.GetAddressBook(label)
17+
if !ok {
18+
fmt.Println("Invalid address book: ", label)
19+
return nil, false
20+
}
21+
normalLabel := "Select a " + label
22+
removeLabel := "Select a " + label + " to remove"
23+
currentLabel := normalLabel
24+
for {
25+
items := []*Item{}
26+
for _, s := range *abook {
27+
items = append(items, &Item{Label: s.String(), Details: "Select this " + label, Value: s})
28+
29+
}
30+
if !selectToRemove {
31+
items = append(items, AddAddressItem, RemoveAddressItem)
32+
}
33+
items = append(items, Back)
34+
// Create a new select prompt
35+
prompt := promptui.Select{
36+
Label: currentLabel,
37+
Items: items,
38+
Templates: ItemTemplate,
39+
Size: 10,
40+
}
41+
_, sel, err := prompt.Run()
42+
if err != nil {
43+
fmt.Println(err)
44+
return nil, false
45+
}
46+
47+
switch sel {
48+
49+
case Back.Label:
50+
return nil, false
51+
case AddAddressItem.Label:
52+
53+
naddrs, err := InputNewAddressUI("Add a new " + label)
54+
if err != nil {
55+
fmt.Println(err)
56+
} else {
57+
abook.Add(naddrs)
58+
59+
continue
60+
}
61+
case RemoveAddressItem.Label:
62+
selectToRemove = true
63+
currentLabel = removeLabel
64+
default:
65+
val, ok := GetValue(sel, items)
66+
if !ok || val == nil {
67+
fmt.Println("Invalid selection: ", sel)
68+
return nil, false
69+
70+
}
71+
if selectToRemove {
72+
abook.Remove(val.(*common.Address))
73+
selectToRemove = false
74+
currentLabel = normalLabel
75+
} else {
76+
return val.(*common.Address), true
77+
}
78+
}
79+
}
80+
81+
}

ui/commoninputs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func InputNewAddressUI(label string) (*common.Address, error) {
164164
if err != nil {
165165
return nil, err
166166
}
167+
s = strings.Trim(s, " ")
167168
common.BytesToAddress(common.FromHex(s))
168169
addr := common.HexToAddress(s)
169170
return &addr, nil

ui/paymasterui.go

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,18 @@
11
package ui
22

33
import (
4-
"fmt"
5-
64
"github.com/ethereum/go-ethereum/common"
7-
"github.com/manifoldco/promptui"
85
"github.com/san-lab/go4337/state"
96
)
107

118
var AddPaymasterItem = &Item{Label: "Add Paymaster", Details: "Add a new paymaster address"}
129

13-
func PaymasterUI() (*common.Address, error) {
14-
for {
15-
items := []*Item{}
16-
for _, s := range state.State.Paymasters {
17-
items = append(items, &Item{Label: s.String(), Details: "Select this Paymaster", Value: s})
18-
19-
}
20-
items = append(items, AddPaymasterItem, Back)
21-
// Create a new select prompt
22-
prompt := promptui.Select{
23-
Label: "Select a Paymaster",
24-
Items: items,
25-
Templates: ItemTemplate,
26-
Size: 10,
27-
}
28-
_, sel, err := prompt.Run()
29-
if err != nil {
30-
return nil, err
31-
}
32-
switch sel {
33-
case Back.Label:
34-
return nil, fmt.Errorf("Back")
35-
case AddPaymasterItem.Label:
36-
pymas, err := InputNewAddressUI("Add Paymester")
37-
if err != nil {
38-
fmt.Println(err)
39-
} else {
40-
state.State.Paymasters = append(state.State.Paymasters, pymas)
41-
state.State.Save()
42-
}
43-
44-
default:
45-
val, ok := GetValue(sel, items)
46-
if !ok {
47-
return nil, fmt.Errorf("Invalid selection")
48-
}
49-
addr := val.(*common.Address)
10+
func PaymasterUI() (*common.Address, bool) {
5011

51-
PaymasterItem.Value = addr
52-
PaymasterItem.DisplayValue = addr.String()
53-
fmt.Println("Selected Paymaster:", addr.String())
54-
return addr, nil
55-
}
12+
addr, ok := AddressFromBookUI(state.Paymaster)
13+
if ok {
14+
PaymasterItem.Value = addr
15+
PaymasterItem.DisplayValue = addr.String()
5616
}
57-
return nil, nil
17+
return addr, ok
5818
}

ui/rootui.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func RootUI() {
3131
UserOpItem,
3232
SignerItem,
3333
AbisItem,
34+
ChainIDItem,
3435
EntryPointItem,
3536
Exit,
3637
}
@@ -60,6 +61,8 @@ func RootUI() {
6061
EntryPointUI()
6162
case Exit.Label:
6263
return
64+
case ChainIDItem.Label:
65+
InputUint(ChainIDItem, 64)
6366
default:
6467
fmt.Println("Not implemented yet:", sel)
6568
}

ui/senderui.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

ui/signerui.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ func SignerUI() {
3535
AddSignerUI()
3636
default:
3737
if i < len(state.State.Signers) {
38-
SignItem.Value = state.State.Signers[i]
39-
SignItem.DisplayValue = state.State.Signers[i].String()
38+
SignerItem.Value = state.State.Signers[i]
4039
SignerItem.DisplayValue = state.State.Signers[i].String()
41-
SignItem.Details = "Sign the user operation with " + state.State.Signers[i].String()
4240
return
4341
}
4442
fmt.Println("Unreachable reached:", sel)

0 commit comments

Comments
 (0)