auth.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. package bind
  17. import (
  18. "crypto/ecdsa"
  19. "errors"
  20. "io"
  21. "io/ioutil"
  22. "github.com/ethereum/go-ethereum/accounts"
  23. "github.com/ethereum/go-ethereum/accounts/external"
  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. // NewTransactor is a utility method to easily create a transaction signer from
  30. // an encrypted json key stream and the associated passphrase.
  31. func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
  32. json, err := ioutil.ReadAll(keyin)
  33. if err != nil {
  34. return nil, err
  35. }
  36. key, err := keystore.DecryptKey(json, passphrase)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return NewKeyedTransactor(key.PrivateKey), nil
  41. }
  42. // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
  43. // an decrypted key from a keystore
  44. func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
  45. return &TransactOpts{
  46. From: account.Address,
  47. Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  48. if address != account.Address {
  49. return nil, errors.New("not authorized to sign this account")
  50. }
  51. signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
  52. if err != nil {
  53. return nil, err
  54. }
  55. return tx.WithSignature(signer, signature)
  56. },
  57. }, nil
  58. }
  59. // NewKeyedTransactor is a utility method to easily create a transaction signer
  60. // from a single private key.
  61. func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
  62. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  63. return &TransactOpts{
  64. From: keyAddr,
  65. Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  66. if address != keyAddr {
  67. return nil, errors.New("not authorized to sign this account")
  68. }
  69. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return tx.WithSignature(signer, signature)
  74. },
  75. }
  76. }
  77. // NewClefTransactor is a utility method to easily create a transaction signer
  78. // with a clef backend.
  79. func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
  80. return &TransactOpts{
  81. From: account.Address,
  82. Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
  83. if address != account.Address {
  84. return nil, errors.New("not authorized to sign this account")
  85. }
  86. return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
  87. },
  88. }
  89. }