github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/sign.go (about) 1 // Copyright 2018 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 17 package feed 18 19 import ( 20 "crypto/ecdsa" 21 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/crypto" 24 ) 25 26 const signatureLength = 65 27 28 // Signature is an alias for a static byte array with the size of a signature 29 type Signature [signatureLength]byte 30 31 // Signer signs feed update payloads 32 type Signer interface { 33 Sign(common.Hash) (Signature, error) 34 Address() common.Address 35 } 36 37 // GenericSigner implements the Signer interface 38 // It is the vanilla signer that probably should be used in most cases 39 type GenericSigner struct { 40 PrivKey *ecdsa.PrivateKey 41 address common.Address 42 } 43 44 // NewGenericSigner builds a signer that will sign everything with the provided private key 45 func NewGenericSigner(privKey *ecdsa.PrivateKey) *GenericSigner { 46 return &GenericSigner{ 47 PrivKey: privKey, 48 address: crypto.PubkeyToAddress(privKey.PublicKey), 49 } 50 } 51 52 // Sign signs the supplied data 53 // It wraps the ethereum crypto.Sign() method 54 func (s *GenericSigner) Sign(data common.Hash) (signature Signature, err error) { 55 signaturebytes, err := crypto.Sign(data.Bytes(), s.PrivKey) 56 if err != nil { 57 return 58 } 59 copy(signature[:], signaturebytes) 60 return 61 } 62 63 // Address returns the public key of the signer's private key 64 func (s *GenericSigner) Address() common.Address { 65 return s.address 66 } 67 68 // getUserAddr extracts the address of the feed update signer 69 func getUserAddr(digest common.Hash, signature Signature) (common.Address, error) { 70 pub, err := crypto.SigToPub(digest.Bytes(), signature[:]) 71 if err != nil { 72 return common.Address{}, err 73 } 74 return crypto.PubkeyToAddress(*pub), nil 75 }