signed

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: BSD-3-Clause Imports: 11 Imported by: 6

Documentation

Overview

Package signed is an implementation of the transaction abstraction.

It uses a signature to make sure the identity owns the transaction. The nonce is a monotonically increasing number that is used to prevent a replay attack of an existing transaction.

Documentation Last Review: 08.10.2020

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterTransactionFormat

func RegisterTransactionFormat(f serde.Format, e serde.FormatEngine)

RegisterTransactionFormat registers the engine for the provided format.

Types

type Client

type Client interface {
	GetNonce(access.Identity) (uint64, error)
}

Client is the interface the manager is using to get the nonce of an identity. It allows a local implementation, or through a network client.

type PublicKeyFac

type PublicKeyFac struct{}

PublicKeyFac is the key of the public key factory.

type SignatureFac

type SignatureFac struct{}

SignatureFac is the key of the signature factory.

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction is a signed transaction using a nonce to protect itself against replay attack.

- implements txn.Transaction

func NewTransaction

func NewTransaction(nonce uint64, pk crypto.PublicKey, opts ...TransactionOption) (
	*Transaction,
	error,
)

NewTransaction creates a new transaction with the provided nonce.

func (*Transaction) Fingerprint

func (t *Transaction) Fingerprint(w io.Writer) error

Fingerprint implements serde.Fingerprinter. It writes a deterministic binary representation of the transaction.

func (*Transaction) GetArg

func (t *Transaction) GetArg(key string) []byte

GetArg implements txn.Transaction. It returns the value of the argument if it is set, otherwise nil.

func (*Transaction) GetArgs

func (t *Transaction) GetArgs() []string

GetArgs returns the list of arguments available.

func (*Transaction) GetID

func (t *Transaction) GetID() []byte

GetID implements txn.Transaction. It returns the ID of the transaction.

func (*Transaction) GetIdentity

func (t *Transaction) GetIdentity() access.Identity

GetIdentity implements txn.Transaction. It returns nil.

func (*Transaction) GetNonce

func (t *Transaction) GetNonce() uint64

GetNonce implements txn.Transaction. It returns the nonce of the transaction.

func (*Transaction) GetSignature

func (t *Transaction) GetSignature() crypto.Signature

GetSignature returns the signature of the transaction.

func (*Transaction) Serialize

func (t *Transaction) Serialize(ctx serde.Context) ([]byte, error)

Serialize implements serde.Message. It returns the serialized data of the transaction.

func (*Transaction) Sign

func (t *Transaction) Sign(signer crypto.Signer) error

Sign signs the transaction and stores the signature.

type TransactionFactory

type TransactionFactory struct {
	// contains filtered or unexported fields
}

TransactionFactory is a factory to deserialize transactions.

- implements serde.Factory

func NewTransactionFactory

func NewTransactionFactory() TransactionFactory

NewTransactionFactory returns a new factory.

func (TransactionFactory) Deserialize

func (f TransactionFactory) Deserialize(ctx serde.Context, data []byte) (serde.Message, error)

Deserialize implements serde.Factory. It populates the transaction from the data if appropriate, otherwise it returns an error.

func (TransactionFactory) TransactionOf

func (f TransactionFactory) TransactionOf(ctx serde.Context, data []byte) (txn.Transaction, error)

TransactionOf implements txn.TransactionFactory. It populates the transaction from the data if appropriate, otherwise it returns an error.

type TransactionManager

type TransactionManager struct {
	// contains filtered or unexported fields
}

TransactionManager is a manager to create signed transactions. It manages the nonce by itself, except if the transaction is refused by the ledger. In that case the manager should be synchronized before creating a new one.

- implements txn.Manager

func NewManager

func NewManager(signer crypto.Signer, client Client) *TransactionManager

NewManager creates a new transaction manager.

- implements txn.Manager

func (*TransactionManager) Make

func (mgr *TransactionManager) Make(args ...txn.Arg) (txn.Transaction, error)

Make implements txn.Manager. It creates a transaction populated with the arguments.

Example
package main

import (
	"fmt"

	"go.dedis.ch/dela/core/access"
	"go.dedis.ch/dela/crypto/bls"
)

func main() {
	signer := bls.NewSigner()

	manager := NewManager(signer, exampleClient{nonce: 5})

	tx, err := manager.Make()
	if err != nil {
		panic("failed to create first transaction: " + err.Error())
	}

	fmt.Println(tx.GetNonce())

	err = manager.Sync()
	if err != nil {
		panic("failed to synchronize: " + err.Error())
	}

	tx, err = manager.Make()
	if err != nil {
		panic("failed to create second transaction: " + err.Error())
	}

	fmt.Println(tx.GetNonce())

}

// exampleClient is an example of a manager client. It always synchronize the
// manager to the nonce value.
//
// - implements signed.Client
type exampleClient struct {
	nonce uint64
}

// GetNonce implements signed.Client. It always return the same nonce for
// simplicity.
func (cl exampleClient) GetNonce(identity access.Identity) (uint64, error) {
	return cl.nonce, nil
}
Output:

0
5

func (*TransactionManager) Sync

func (mgr *TransactionManager) Sync() error

Sync implements txn.Manager. It fetches the latest nonce of the signer to create valid transactions.

type TransactionOption

type TransactionOption func(*template)

TransactionOption is the type of options to create a transaction.

func WithArg

func WithArg(key string, value []byte) TransactionOption

WithArg is an option to set an argument with the key and the value.

func WithHashFactory

func WithHashFactory(f crypto.HashFactory) TransactionOption

WithHashFactory is an option to set a different hash factory when creating a transaction.

func WithSignature

func WithSignature(sig crypto.Signature) TransactionOption

WithSignature is an option to set a valid signature. The signature will be verified against the identity.

Directories

Path Synopsis
Package controller implements a CLI controller to inject a transaction manager.
Package controller implements a CLI controller to inject a transaction manager.

Jump to

Keyboard shortcuts

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