Skip to content

Commit 0fe35b9

Browse files
karalabefjl
authored andcommitted
mobile: iOS naming and API fixes for generators and Swift (ethereum#3408)
* build: modify the iOS namespace to iGeth (gomobile limitation) * mobile: assign names to return types for ObjC wrapper * mobile: use more expanded names for iOS/Swift API
1 parent 3fc7c97 commit 0fe35b9

15 files changed

+151
-147
lines changed

build/ci.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ func doXCodeFramework(cmdline []string) {
800800
// Build the iOS XCode framework
801801
build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
802802
build.MustRun(gomobileTool("init"))
803-
bind := gomobileTool("bind", "--target", "ios", "--tags", "ios", "--prefix", "GE", "-v", "github.com/ethereum/go-ethereum/mobile")
803+
bind := gomobileTool("bind", "--target", "ios", "--tags", "ios", "--prefix", "i", "-v", "github.com/ethereum/go-ethereum/mobile")
804804

805805
if *local {
806806
// If we're building locally, use the build folder and stop afterwards

mobile/accounts.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *Accounts) Size() int {
5656
}
5757

5858
// Get returns the account at the given index from the slice.
59-
func (a *Accounts) Get(index int) (*Account, error) {
59+
func (a *Accounts) Get(index int) (account *Account, _ error) {
6060
if index < 0 || index >= len(a.accounts) {
6161
return nil, errors.New("index out of bounds")
6262
}
@@ -91,8 +91,8 @@ func NewAccountManager(keydir string, scryptN, scryptP int) *AccountManager {
9191
}
9292

9393
// HasAddress reports whether a key with the given address is present.
94-
func (am *AccountManager) HasAddress(addr *Address) bool {
95-
return am.manager.HasAddress(addr.address)
94+
func (am *AccountManager) HasAddress(address *Address) bool {
95+
return am.manager.HasAddress(address.address)
9696
}
9797

9898
// GetAccounts returns all key files present in the directory.
@@ -102,32 +102,32 @@ func (am *AccountManager) GetAccounts() *Accounts {
102102

103103
// DeleteAccount deletes the key matched by account if the passphrase is correct.
104104
// If a contains no filename, the address must match a unique key.
105-
func (am *AccountManager) DeleteAccount(a *Account, passphrase string) error {
105+
func (am *AccountManager) DeleteAccount(account *Account, passphrase string) error {
106106
return am.manager.DeleteAccount(accounts.Account{
107-
Address: a.account.Address,
108-
File: a.account.File,
107+
Address: account.account.Address,
108+
File: account.account.File,
109109
}, passphrase)
110110
}
111111

112112
// Sign signs hash with an unlocked private key matching the given address.
113-
func (am *AccountManager) Sign(addr *Address, hash []byte) ([]byte, error) {
114-
return am.manager.Sign(addr.address, hash)
113+
func (am *AccountManager) Sign(address *Address, hash []byte) (signature []byte, _ error) {
114+
return am.manager.Sign(address.address, hash)
115115
}
116116

117117
// SignWithPassphrase signs hash if the private key matching the given address can be
118118
// decrypted with the given passphrase.
119-
func (am *AccountManager) SignWithPassphrase(addr *Address, passphrase string, hash []byte) ([]byte, error) {
120-
return am.manager.SignWithPassphrase(addr.address, passphrase, hash)
119+
func (am *AccountManager) SignWithPassphrase(address *Address, passphrase string, hash []byte) (signature []byte, _ error) {
120+
return am.manager.SignWithPassphrase(address.address, passphrase, hash)
121121
}
122122

123123
// Unlock unlocks the given account indefinitely.
124-
func (am *AccountManager) Unlock(a *Account, passphrase string) error {
125-
return am.manager.TimedUnlock(a.account, passphrase, 0)
124+
func (am *AccountManager) Unlock(account *Account, passphrase string) error {
125+
return am.manager.TimedUnlock(account.account, passphrase, 0)
126126
}
127127

128128
// Lock removes the private key with the given address from memory.
129-
func (am *AccountManager) Lock(addr *Address) error {
130-
return am.manager.Lock(addr.address)
129+
func (am *AccountManager) Lock(address *Address) error {
130+
return am.manager.Lock(address.address)
131131
}
132132

133133
// TimedUnlock unlocks the given account with the passphrase. The account
@@ -152,27 +152,27 @@ func (am *AccountManager) NewAccount(passphrase string) (*Account, error) {
152152
}
153153

154154
// ExportKey exports as a JSON key, encrypted with newPassphrase.
155-
func (am *AccountManager) ExportKey(a *Account, passphrase, newPassphrase string) ([]byte, error) {
156-
return am.manager.Export(a.account, passphrase, newPassphrase)
155+
func (am *AccountManager) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) {
156+
return am.manager.Export(account.account, passphrase, newPassphrase)
157157
}
158158

159159
// ImportKey stores the given encrypted JSON key into the key directory.
160-
func (am *AccountManager) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (*Account, error) {
161-
account, err := am.manager.Import(keyJSON, passphrase, newPassphrase)
160+
func (am *AccountManager) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) {
161+
acc, err := am.manager.Import(keyJSON, passphrase, newPassphrase)
162162
if err != nil {
163163
return nil, err
164164
}
165-
return &Account{account}, nil
165+
return &Account{acc}, nil
166166
}
167167

168168
// Update changes the passphrase of an existing account.
169-
func (am *AccountManager) Update(a *Account, passphrase, newPassphrase string) error {
170-
return am.manager.Update(a.account, passphrase, newPassphrase)
169+
func (am *AccountManager) Update(account *Account, passphrase, newPassphrase string) error {
170+
return am.manager.Update(account.account, passphrase, newPassphrase)
171171
}
172172

173173
// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
174174
// a key file in the key directory. The key file is encrypted with the same passphrase.
175-
func (am *AccountManager) ImportPreSaleKey(keyJSON []byte, passphrase string) (*Account, error) {
175+
func (am *AccountManager) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) {
176176
account, err := am.manager.ImportPreSaleKey(keyJSON, passphrase)
177177
if err != nil {
178178
return nil, err

mobile/big.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (bi *BigInts) Size() int {
7878
}
7979

8080
// Get returns the bigint at the given index from the slice.
81-
func (bi *BigInts) Get(index int) (*BigInt, error) {
81+
func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) {
8282
if index < 0 || index >= len(bi.bigints) {
8383
return nil, errors.New("index out of bounds")
8484
}

mobile/bind.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import (
3131
// Signer is an interaface defining the callback when a contract requires a
3232
// method to sign the transaction before submission.
3333
type Signer interface {
34-
Sign(*Address, *Transaction) (*Transaction, error)
34+
Sign(*Address, *Transaction) (tx *Transaction, _ error)
3535
}
3636

3737
type signer struct {
3838
sign bind.SignerFn
3939
}
4040

41-
func (s *signer) Sign(addr *Address, tx *Transaction) (*Transaction, error) {
42-
sig, err := s.sign(types.HomesteadSigner{}, addr.address, tx.tx)
41+
func (s *signer) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
42+
sig, err := s.sign(types.HomesteadSigner{}, addr.address, unsignedTx.tx)
4343
if err != nil {
4444
return nil, err
4545
}
@@ -113,7 +113,7 @@ type BoundContract struct {
113113

114114
// DeployContract deploys a contract onto the Ethereum blockchain and binds the
115115
// deployment address with a wrapper.
116-
func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (*BoundContract, error) {
116+
func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) {
117117
// Convert all the deployment parameters to Go types
118118
params := make([]interface{}, len(args.objects))
119119
for i, obj := range args.objects {
@@ -137,7 +137,7 @@ func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client
137137

138138
// BindContract creates a low level contract interface through which calls and
139139
// transactions may be made through.
140-
func BindContract(address *Address, abiJSON string, client *EthereumClient) (*BoundContract, error) {
140+
func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
141141
parsed, err := abi.JSON(strings.NewReader(abiJSON))
142142
if err != nil {
143143
return nil, err
@@ -179,24 +179,24 @@ func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, arg
179179
}
180180

181181
// Transact invokes the (paid) contract method with params as input values.
182-
func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (*Transaction, error) {
182+
func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
183183
params := make([]interface{}, len(args.objects))
184184
for i, obj := range args.objects {
185185
params[i] = obj
186186
}
187-
tx, err := c.contract.Transact(&opts.opts, method, params)
187+
rawTx, err := c.contract.Transact(&opts.opts, method, params)
188188
if err != nil {
189189
return nil, err
190190
}
191-
return &Transaction{tx}, nil
191+
return &Transaction{rawTx}, nil
192192
}
193193

194194
// Transfer initiates a plain transaction to move funds to the contract, calling
195195
// its default method if one is available.
196-
func (c *BoundContract) Transfer(opts *TransactOpts) (*Transaction, error) {
197-
tx, err := c.contract.Transfer(&opts.opts)
196+
func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
197+
rawTx, err := c.contract.Transfer(&opts.opts)
198198
if err != nil {
199199
return nil, err
200200
}
201-
return &Transaction{tx}, nil
201+
return &Transaction{rawTx}, nil
202202
}

mobile/common.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ type Hash struct {
3333
}
3434

3535
// NewHashFromBytes converts a slice of bytes to a hash value.
36-
func NewHashFromBytes(hash []byte) (*Hash, error) {
36+
func NewHashFromBytes(binary []byte) (hash *Hash, _ error) {
3737
h := new(Hash)
38-
if err := h.SetBytes(hash); err != nil {
38+
if err := h.SetBytes(binary); err != nil {
3939
return nil, err
4040
}
4141
return h, nil
4242
}
4343

4444
// NewHashFromHex converts a hex string to a hash value.
45-
func NewHashFromHex(hash string) (*Hash, error) {
45+
func NewHashFromHex(hex string) (hash *Hash, _ error) {
4646
h := new(Hash)
47-
if err := h.SetHex(hash); err != nil {
47+
if err := h.SetHex(hex); err != nil {
4848
return nil, err
4949
}
5050
return h, nil
@@ -95,7 +95,7 @@ func (h *Hashes) Size() int {
9595
}
9696

9797
// Get returns the hash at the given index from the slice.
98-
func (h *Hashes) Get(index int) (*Hash, error) {
98+
func (h *Hashes) Get(index int) (hash *Hash, _ error) {
9999
if index < 0 || index >= len(h.hashes) {
100100
return nil, errors.New("index out of bounds")
101101
}
@@ -108,18 +108,18 @@ type Address struct {
108108
}
109109

110110
// NewAddressFromBytes converts a slice of bytes to a hash value.
111-
func NewAddressFromBytes(address []byte) (*Address, error) {
111+
func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
112112
a := new(Address)
113-
if err := a.SetBytes(address); err != nil {
113+
if err := a.SetBytes(binary); err != nil {
114114
return nil, err
115115
}
116116
return a, nil
117117
}
118118

119119
// NewAddressFromHex converts a hex string to a address value.
120-
func NewAddressFromHex(address string) (*Address, error) {
120+
func NewAddressFromHex(hex string) (address *Address, _ error) {
121121
a := new(Address)
122-
if err := a.SetHex(address); err != nil {
122+
if err := a.SetHex(hex); err != nil {
123123
return nil, err
124124
}
125125
return a, nil
@@ -170,7 +170,7 @@ func (a *Addresses) Size() int {
170170
}
171171

172172
// Get returns the address at the given index from the slice.
173-
func (a *Addresses) Get(index int) (*Address, error) {
173+
func (a *Addresses) Get(index int) (address *Address, _ error) {
174174
if index < 0 || index >= len(a.addresses) {
175175
return nil, errors.New("index out of bounds")
176176
}

mobile/discover.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Enode struct {
5353
// and UDP discovery port 30301.
5454
//
5555
// enode://<hex node id>@10.3.58.6:30303?discport=30301
56-
func NewEnode(rawurl string) (*Enode, error) {
56+
func NewEnode(rawurl string) (enode *Enode, _ error) {
5757
node, err := discv5.ParseNode(rawurl)
5858
if err != nil {
5959
return nil, err
@@ -82,7 +82,7 @@ func (e *Enodes) Size() int {
8282
}
8383

8484
// Get returns the enode at the given index from the slice.
85-
func (e *Enodes) Get(index int) (*Enode, error) {
85+
func (e *Enodes) Get(index int) (enode *Enode, _ error) {
8686
if index < 0 || index >= len(e.nodes) {
8787
return nil, errors.New("index out of bounds")
8888
}

mobile/doc.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
// should not be provided to limit the remote code complexity. Arrays should be
5252
// avoided as much as possible since they complicate bounds checking.
5353
//
54+
// If a method has multiple return values (e.g. some return + an error), those
55+
// are generated as output arguments in ObjC. To avoid weird generated names like
56+
// ret_0 for them, please always assign names to output variables if tuples.
57+
//
5458
// Note, a panic *cannot* cross over language boundaries, instead will result in
5559
// an undebuggable SEGFAULT in the process. For error handling only ever use error
5660
// returns, which may be the only or the second return.

0 commit comments

Comments
 (0)