ethereum

package
v1.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2023 License: AGPL-3.0 Imports: 17 Imported by: 18

Documentation

Overview

Package ethereum provides cryptographic operations used in go-dvote related to ethereum.

Index

Constants

View Source
const (
	// SignatureLength is the size of an ECDSA signature in hexString format
	SignatureLength = ethcrypto.SignatureLength
	// PubKeyLengthBytes is the size of a Public Key
	PubKeyLengthBytes = 33
	// PubKeyLengthBytesUncompressed is the size of a uncompressed Public Key
	PubKeyLengthBytesUncompressed = 65
	// SigningPrefix is the prefix added when hashing
	SigningPrefix = "\u0019Ethereum Signed Message:\n"
	// DefaultSIKPayload conatains the default seed to sing during
	// SIK generation
	DefaultSIKPayload = "vocdoni-sik-payload"
)

Variables

This section is empty.

Functions

func AddrFromBytes added in v1.7.0

func AddrFromBytes(addr []byte) ethcommon.Address

AddrFromBytes returns the Ethereum address from a byte array

func AddrFromPublicKey

func AddrFromPublicKey(pub []byte) (ethcommon.Address, error)

AddrFromPublicKey standaolone function to obtain the Ethereum address from a ECDSA public key

func AddrFromSignature

func AddrFromSignature(message, signature []byte) (ethcommon.Address, error)

AddrFromSignature recovers the Ethereum address that created the signature of a message

func BuildVocdoniMessage added in v1.3.0

func BuildVocdoniMessage(message []byte) []byte

BuildVocdoniMessage builds the payload of a vocdoni message ready to be signed

func BuildVocdoniProtoTxMessage added in v1.10.0

func BuildVocdoniProtoTxMessage(tx *models.Tx, chainID string, hash []byte) ([]byte, error)

BuildVocdoniProtoTxMessage builds the message to be signed for a vocdoni transaction. It takes an optional transaction hash, if it is not provided it will be computed. It returns the payload that needs to be signed.

func BuildVocdoniTransaction added in v1.3.0

func BuildVocdoniTransaction(marshaledTx []byte, chainID string) ([]byte, *models.Tx, error)

BuildVocdoniTransaction builds the payload for a Vocdoni transaction. It returns the payload that needs to be signed and the unmarshaled transaction struct.

func CompressPubKey

func CompressPubKey(pubHexDec string) (string, error)

CompressPubKey returns the compressed public key in hexString format

func DecompressPubKey

func DecompressPubKey(pubComp types.HexBytes) (types.HexBytes, error)

DecompressPubKey takes a compressed public key and returns it descompressed. If already decompressed, returns the same key.

func Hash

func Hash(data []byte) []byte

Hash data adding Ethereum prefix

func HashRaw

func HashRaw(data []byte) []byte

HashRaw hashes data with no prefix

func PubKeyFromPrivateKey

func PubKeyFromPrivateKey(privHex string) (string, error)

PubKeyFromPrivateKey returns the hex public key given a hex private key

func PubKeyFromSignature

func PubKeyFromSignature(message, signature []byte) ([]byte, error)

PubKeyFromSignature recovers the ECDSA public key that created the signature of a message public key is hex encoded

Types

type Address added in v1.7.0

type Address ethcommon.Address

Address represents an Ethereum like address

type SignKeys

type SignKeys struct {
	Public     ecdsa.PublicKey
	Private    ecdsa.PrivateKey
	Authorized map[ethcommon.Address]bool
	Lock       sync.RWMutex
}

SignKeys represents an ECDSA pair of keys for signing. Authorized addresses is a list of Ethereum like addresses which are checked on Verify

func NewSignKeys

func NewSignKeys() *SignKeys

NewSignKeys creates an ECDSA pair of keys for signing and initializes the map for authorized keys

func NewSignKeysBatch added in v1.6.0

func NewSignKeysBatch(n int) []*SignKeys

NewSignKeysBatch creates a set of eth random signing keys

func (*SignKeys) AccountSIK added in v1.9.0

func (k *SignKeys) AccountSIK(secret []byte) ([]byte, error)

AccountSIK method generates the Secret Identity Key for the current SignKeys with the signature of the DefaultSIKPayload and the user secret (if it is provided) following the definition:

SIK = poseidon(address, signature, secret)

The secret could be nil.

func (*SignKeys) AccountSIKnullifier added in v1.9.0

func (k *SignKeys) AccountSIKnullifier(electionID, secret []byte) ([]byte, error)

AccountSIKnullifier method composes the nullifier of the current SignKeys for the desired election id and the secret provided.

func (*SignKeys) AddAuthKey

func (k *SignKeys) AddAuthKey(address ethcommon.Address)

AddAuthKey adds a new authorized address key

func (*SignKeys) AddHexKey

func (k *SignKeys) AddHexKey(privHex string) error

AddHexKey imports a private hex key

func (*SignKeys) Address

func (k *SignKeys) Address() ethcommon.Address

Address returns the SignKeys ethereum address

func (*SignKeys) AddressString

func (k *SignKeys) AddressString() string

AddressString returns the ethereum Address as string

func (*SignKeys) Generate

func (k *SignKeys) Generate() error

Generate generates new keys

func (*SignKeys) HexString

func (k *SignKeys) HexString() (string, string)

HexString returns the public compressed and private keys as hex strings

func (*SignKeys) PrivateKey added in v1.4.0

func (k *SignKeys) PrivateKey() types.HexBytes

PrivateKey returns the private key

func (*SignKeys) PublicKey

func (k *SignKeys) PublicKey() types.HexBytes

PublicKey returns the compressed public key

func (*SignKeys) SIKsignature added in v1.9.0

func (k *SignKeys) SIKsignature() ([]byte, error)

SIKsignature signs the default vocdoni sik payload. It envolves the SignEthereum method, using the DefaultSIKPayload and discarding the last byte of the signature (used for recovery), different that the same byte of a signature generated with javascript.

func (*SignKeys) Sign

func (k *SignKeys) Sign(txData []byte) ([]byte, error)

Sign signs a raw message. TxData is the full transaction payload (no HexString nor a Hash)

func (*SignKeys) SignEthereum added in v1.3.0

func (k *SignKeys) SignEthereum(message []byte) ([]byte, error)

SignEthereum signs a message. Message is a normal string (no HexString nor a Hash)

func (*SignKeys) SignVocdoniMsg added in v1.3.0

func (k *SignKeys) SignVocdoniMsg(message []byte) ([]byte, error)

SignVocdoniMsg signs a vocdoni message. Message is the full payload (no HexString nor a Hash)

func (*SignKeys) SignVocdoniTx added in v1.3.0

func (k *SignKeys) SignVocdoniTx(txData []byte, chainID string) ([]byte, error)

SignVocdoniTx signs a vocdoni transaction. TxData is the full transaction payload (no HexString nor a Hash)

func (*SignKeys) VerifySender

func (k *SignKeys) VerifySender(message, signature []byte) (bool, ethcommon.Address, error)

VerifySender verifies if a message is sent by some Authorized address key

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL