github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/bind/auth.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:30</date> 10 //</624450059613769728> 11 12 13 package bind 14 15 import ( 16 "crypto/ecdsa" 17 "errors" 18 "io" 19 "io/ioutil" 20 21 "github.com/ethereum/go-ethereum/accounts/keystore" 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/core/types" 24 "github.com/ethereum/go-ethereum/crypto" 25 ) 26 27 //newTransactior是一种实用方法,可以轻松地从中创建事务签名者 28 //加密的JSON密钥流和相关的密码短语。 29 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 30 json, err := ioutil.ReadAll(keyin) 31 if err != nil { 32 return nil, err 33 } 34 key, err := keystore.DecryptKey(json, passphrase) 35 if err != nil { 36 return nil, err 37 } 38 return NewKeyedTransactor(key.PrivateKey), nil 39 } 40 41 //NewKeyedTransactor是一种实用方法,可以轻松创建事务签名者 42 //从单个私钥。 43 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 44 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 45 return &TransactOpts{ 46 From: keyAddr, 47 Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { 48 if address != keyAddr { 49 return nil, errors.New("not authorized to sign this account") 50 } 51 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 52 if err != nil { 53 return nil, err 54 } 55 return tx.WithSignature(signer, signature) 56 }, 57 } 58 } 59