mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 13:07:17 +00:00
remove ethutil helpers (refactored), and keypair (key management under ethcrypto package)
This commit is contained in:
parent
d87857ffdb
commit
e1ea41ee9c
@ -1,64 +0,0 @@
|
||||
package ethutil
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.crypto/ripemd160"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"github.com/obscuren/sha3"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Uitoa(i uint32) string {
|
||||
return strconv.FormatUint(uint64(i), 10)
|
||||
}
|
||||
|
||||
func Sha256Bin(data []byte) []byte {
|
||||
hash := sha256.Sum256(data)
|
||||
|
||||
return hash[:]
|
||||
}
|
||||
|
||||
func Ripemd160(data []byte) []byte {
|
||||
ripemd := ripemd160.New()
|
||||
ripemd.Write(data)
|
||||
|
||||
return ripemd.Sum(nil)
|
||||
}
|
||||
|
||||
func Sha3Bin(data []byte) []byte {
|
||||
d := sha3.NewKeccak256()
|
||||
d.Write(data)
|
||||
|
||||
return d.Sum(nil)
|
||||
}
|
||||
|
||||
// Helper function for comparing slices
|
||||
func CompareIntSlice(a, b []int) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i, v := range a {
|
||||
if v != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Returns the amount of nibbles that match each other from 0 ...
|
||||
func MatchingNibbleLength(a, b []int) int {
|
||||
i := 0
|
||||
for CompareIntSlice(a[:i+1], b[:i+1]) && i < len(b) {
|
||||
i += 1
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func Hex(d []byte) string {
|
||||
return hex.EncodeToString(d)
|
||||
}
|
||||
func FromHex(str string) []byte {
|
||||
h, _ := hex.DecodeString(str)
|
||||
return h
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
package ethutil
|
||||
|
||||
import (
|
||||
"github.com/obscuren/secp256k1-go"
|
||||
)
|
||||
|
||||
type KeyPair struct {
|
||||
PrivateKey []byte
|
||||
PublicKey []byte
|
||||
|
||||
// The associated account
|
||||
account *StateObject
|
||||
}
|
||||
|
||||
func GenerateNewKeyPair() (*KeyPair, error) {
|
||||
_, prv := secp256k1.GenerateKeyPair()
|
||||
|
||||
return NewKeyPairFromSec(prv)
|
||||
}
|
||||
|
||||
func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) {
|
||||
pubkey, err := secp256k1.GeneratePubKey(seckey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &KeyPair{PrivateKey: seckey, PublicKey: pubkey}, nil
|
||||
}
|
||||
|
||||
func NewKeyPairFromValue(val *Value) *KeyPair {
|
||||
v, _ := NewKeyPairFromSec(val.Bytes())
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (k *KeyPair) Address() []byte {
|
||||
return Sha3Bin(k.PublicKey[1:])[12:]
|
||||
}
|
||||
|
||||
func (k *KeyPair) RlpEncode() []byte {
|
||||
return k.RlpValue().Encode()
|
||||
}
|
||||
|
||||
func (k *KeyPair) RlpValue() *Value {
|
||||
return NewValue(k.PrivateKey)
|
||||
}
|
||||
|
||||
type KeyRing struct {
|
||||
keys []*KeyPair
|
||||
}
|
||||
|
||||
func (k *KeyRing) Add(pair *KeyPair) {
|
||||
k.keys = append(k.keys, pair)
|
||||
}
|
||||
|
||||
func (k *KeyRing) Get(i int) *KeyPair {
|
||||
if len(k.keys) > i {
|
||||
return k.keys[i]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KeyRing) Len() int {
|
||||
return len(k.keys)
|
||||
}
|
||||
|
||||
func (k *KeyRing) NewKeyPair(sec []byte) (*KeyPair, error) {
|
||||
keyPair, err := NewKeyPairFromSec(sec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.Add(keyPair)
|
||||
Config.Db.Put([]byte("KeyRing"), k.RlpValue().Encode())
|
||||
|
||||
return keyPair, nil
|
||||
}
|
||||
|
||||
func (k *KeyRing) Reset() {
|
||||
Config.Db.Put([]byte("KeyRing"), nil)
|
||||
k.keys = nil
|
||||
}
|
||||
|
||||
func (k *KeyRing) RlpValue() *Value {
|
||||
v := EmptyValue()
|
||||
for _, keyPair := range k.keys {
|
||||
v.Append(keyPair.RlpValue())
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// The public "singleton" keyring
|
||||
var keyRing *KeyRing
|
||||
|
||||
func GetKeyRing() *KeyRing {
|
||||
if keyRing == nil {
|
||||
keyRing = &KeyRing{}
|
||||
|
||||
data, _ := Config.Db.Get([]byte("KeyRing"))
|
||||
it := NewValueFromBytes(data).NewIterator()
|
||||
for it.Next() {
|
||||
v := it.Value()
|
||||
|
||||
key, err := NewKeyPairFromSec(v.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
keyRing.Add(key)
|
||||
}
|
||||
}
|
||||
|
||||
return keyRing
|
||||
}
|
Loading…
Reference in New Issue
Block a user