bind.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains all the wrappers from the bind package.
  17. package geth
  18. import (
  19. "errors"
  20. "math/big"
  21. "strings"
  22. "github.com/ethereum/go-ethereum/accounts/abi"
  23. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  24. "github.com/ethereum/go-ethereum/accounts/keystore"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. )
  29. // Signer is an interface defining the callback when a contract requires a
  30. // method to sign the transaction before submission.
  31. type Signer interface {
  32. Sign(*Address, *Transaction) (tx *Transaction, _ error)
  33. }
  34. type MobileSigner struct {
  35. sign bind.SignerFn
  36. }
  37. func (s *MobileSigner) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
  38. sig, err := s.sign(types.EIP155Signer{}, addr.address, unsignedTx.tx)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &Transaction{sig}, nil
  43. }
  44. // CallOpts is the collection of options to fine tune a contract call request.
  45. type CallOpts struct {
  46. opts bind.CallOpts
  47. }
  48. // NewCallOpts creates a new option set for contract calls.
  49. func NewCallOpts() *CallOpts {
  50. return new(CallOpts)
  51. }
  52. func (opts *CallOpts) IsPending() bool { return opts.opts.Pending }
  53. func (opts *CallOpts) GetGasLimit() int64 { return 0 /* TODO(karalabe) */ }
  54. // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  55. // Even then it's awkward to unpack the subtleties of a Go context out to Java.
  56. // func (opts *CallOpts) GetContext() *Context { return &Context{opts.opts.Context} }
  57. func (opts *CallOpts) SetPending(pending bool) { opts.opts.Pending = pending }
  58. func (opts *CallOpts) SetGasLimit(limit int64) { /* TODO(karalabe) */ }
  59. func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context }
  60. // TransactOpts is the collection of authorization data required to create a
  61. // valid Ethereum transaction.
  62. type TransactOpts struct {
  63. opts bind.TransactOpts
  64. }
  65. // NewTransactOpts creates a new option set for contract transaction.
  66. func NewTransactOpts() *TransactOpts {
  67. return new(TransactOpts)
  68. }
  69. // NewKeyedTransactor is a utility method to easily create a transaction signer
  70. // from a single private key.
  71. func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
  72. key, err := keystore.DecryptKey(keyJson, passphrase)
  73. if err != nil {
  74. return nil, err
  75. }
  76. keyAddr := crypto.PubkeyToAddress(key.PrivateKey.PublicKey)
  77. opts := bind.TransactOpts{
  78. From: keyAddr,
  79. Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  80. if address != keyAddr {
  81. return nil, errors.New("not authorized to sign this account")
  82. }
  83. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key.PrivateKey)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return tx.WithSignature(signer, signature)
  88. },
  89. }
  90. return &TransactOpts{opts}, nil
  91. }
  92. func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
  93. func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() }
  94. func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} }
  95. func (opts *TransactOpts) GetGasPrice() *BigInt { return &BigInt{opts.opts.GasPrice} }
  96. func (opts *TransactOpts) GetGasLimit() int64 { return int64(opts.opts.GasLimit) }
  97. // GetSigner cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  98. // func (opts *TransactOpts) GetSigner() Signer { return &signer{opts.opts.Signer} }
  99. // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  100. // Even then it's awkward to unpack the subtleties of a Go context out to Java.
  101. //func (opts *TransactOpts) GetContext() *Context { return &Context{opts.opts.Context} }
  102. func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address }
  103. func (opts *TransactOpts) SetNonce(nonce int64) { opts.opts.Nonce = big.NewInt(nonce) }
  104. func (opts *TransactOpts) SetSigner(s Signer) {
  105. opts.opts.Signer = func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
  106. sig, err := s.Sign(&Address{addr}, &Transaction{tx})
  107. if err != nil {
  108. return nil, err
  109. }
  110. return sig.tx, nil
  111. }
  112. }
  113. func (opts *TransactOpts) SetValue(value *BigInt) { opts.opts.Value = value.bigint }
  114. func (opts *TransactOpts) SetGasPrice(price *BigInt) { opts.opts.GasPrice = price.bigint }
  115. func (opts *TransactOpts) SetGasLimit(limit int64) { opts.opts.GasLimit = uint64(limit) }
  116. func (opts *TransactOpts) SetContext(context *Context) { opts.opts.Context = context.context }
  117. // BoundContract is the base wrapper object that reflects a contract on the
  118. // Ethereum network. It contains a collection of methods that are used by the
  119. // higher level contract bindings to operate.
  120. type BoundContract struct {
  121. contract *bind.BoundContract
  122. address common.Address
  123. deployer *types.Transaction
  124. }
  125. // DeployContract deploys a contract onto the Ethereum blockchain and binds the
  126. // deployment address with a wrapper.
  127. func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) {
  128. // Deploy the contract to the network
  129. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  130. if err != nil {
  131. return nil, err
  132. }
  133. addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &BoundContract{
  138. contract: bound,
  139. address: addr,
  140. deployer: tx,
  141. }, nil
  142. }
  143. // BindContract creates a low level contract interface through which calls and
  144. // transactions may be made through.
  145. func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
  146. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  147. if err != nil {
  148. return nil, err
  149. }
  150. return &BoundContract{
  151. contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client),
  152. address: address.address,
  153. }, nil
  154. }
  155. func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
  156. func (c *BoundContract) GetDeployer() *Transaction {
  157. if c.deployer == nil {
  158. return nil
  159. }
  160. return &Transaction{c.deployer}
  161. }
  162. // Call invokes the (constant) contract method with params as input values and
  163. // sets the output to result.
  164. func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
  165. if len(out.objects) == 1 {
  166. result := out.objects[0]
  167. if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil {
  168. return err
  169. }
  170. out.objects[0] = result
  171. } else {
  172. results := make([]interface{}, len(out.objects))
  173. copy(results, out.objects)
  174. if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
  175. return err
  176. }
  177. copy(out.objects, results)
  178. }
  179. return nil
  180. }
  181. // Transact invokes the (paid) contract method with params as input values.
  182. func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
  183. rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return &Transaction{rawTx}, nil
  188. }
  189. // Transfer initiates a plain transaction to move funds to the contract, calling
  190. // its default method if one is available.
  191. func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
  192. rawTx, err := c.contract.Transfer(&opts.opts)
  193. if err != nil {
  194. return nil, err
  195. }
  196. return &Transaction{rawTx}, nil
  197. }