-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from tablelandnetwork/dtb/signer-lib
feat: signing pkg for programmatic access
- Loading branch information
Showing
5 changed files
with
383 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package signing | ||
|
||
import ( | ||
"bufio" | ||
"crypto/ecdsa" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"golang.org/x/crypto/sha3" | ||
) | ||
|
||
// Signer allows you to sign a big stream of bytes by calling Sum multiple times, then Sign. | ||
type Signer struct { | ||
state crypto.KeccakState | ||
privateKey *ecdsa.PrivateKey | ||
} | ||
|
||
// HexToECDSA parses a hex-encoded secp256k1 private key string to an ECDSA | ||
// private key. | ||
func HexToECDSA(hexKey string) (*ecdsa.PrivateKey, error) { | ||
return crypto.HexToECDSA(hexKey) | ||
} | ||
|
||
// FileToECDSA parses a file path to a hex-encoded secp256k1 private key to an | ||
// ECDSA private key. | ||
func FileToECDSA(hexPath string) (*ecdsa.PrivateKey, error) { | ||
return crypto.LoadECDSA(hexPath) | ||
} | ||
|
||
// NewSigner creates a new signer. | ||
func NewSigner(pk *ecdsa.PrivateKey) *Signer { | ||
return &Signer{ | ||
state: sha3.NewLegacyKeccak256().(crypto.KeccakState), | ||
privateKey: pk, | ||
} | ||
} | ||
|
||
// Sum updates the hash state with a new chunk. | ||
func (s *Signer) Sum(chunk []byte) { | ||
s.state.Write(chunk) | ||
} | ||
|
||
// Sign signs the internal state. | ||
func (s *Signer) Sign() ([]byte, error) { | ||
var h common.Hash | ||
_, _ = s.state.Read(h[:]) | ||
signature, err := crypto.Sign(h.Bytes(), s.privateKey) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("sign: %s", err) | ||
} | ||
|
||
return signature, nil | ||
} | ||
|
||
// SignFile signs an entire file, returning the signature as a byte slice. | ||
func (s *Signer) SignFile(filename string) ([]byte, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("error reading [file=%v]: %v", filename, err.Error()) | ||
} | ||
defer func() { | ||
_ = f.Close() | ||
}() | ||
|
||
// Check if the file is empty and return an error if it is | ||
info, err := f.Stat() | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to get file info: %s", err.Error()) | ||
} | ||
if info.Size() == 0 { | ||
return []byte{}, fmt.Errorf("error with file: content is empty") | ||
} | ||
|
||
nBytes, nChunks := int64(0), int64(0) | ||
r := bufio.NewReader(f) | ||
buf := make([]byte, 0, 4*1024) // 4KB buffer | ||
for { | ||
n, err := r.Read(buf[:cap(buf)]) | ||
buf = buf[:n] | ||
if n == 0 { | ||
if err == nil { | ||
continue | ||
} | ||
if err == io.EOF { | ||
break | ||
} | ||
return []byte{}, fmt.Errorf("unexpected error reading file: %s", err.Error()) | ||
} | ||
nChunks++ | ||
nBytes += int64(len(buf)) | ||
|
||
s.Sum(buf) | ||
|
||
if err != nil && err != io.EOF { | ||
return []byte{}, fmt.Errorf("error in buffer: %s", err.Error()) | ||
} | ||
} | ||
|
||
signature, err := s.Sign() | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to sign [file=%v]: %s", filename, err.Error()) | ||
} | ||
|
||
return signature, nil | ||
} | ||
|
||
// SignBytes signs the provided bytes, returning the signature as a byte slice. | ||
func (s *Signer) SignBytes(data []byte) ([]byte, error) { | ||
if len(data) == 0 { | ||
return []byte{}, fmt.Errorf("error with data: content is empty") | ||
} | ||
|
||
s.Sum(data) | ||
|
||
signature, err := s.Sign() | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to sign data: %s", err.Error()) | ||
} | ||
|
||
return signature, nil | ||
} |
Oops, something went wrong.