signatureArchives.go 4.59 KB
Newer Older
Caro Axel's avatar
Caro Axel committed
1 2 3
package entities

import (
Caro Axel's avatar
Caro Axel committed
4 5
	"bytes"

Caro Axel's avatar
Caro Axel committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	"gopkg.in/mgo.v2/bson"
)

// Signer : represents a signer for the TTP
type Signer struct {
	ID   bson.ObjectId `key:"_id" bson:"_id"`   // Internal id of a Signer
	Hash []byte        `key:"hash" bson:"hash"` // The SHA-512 hash of the signer's certificate
}

// NewSigner : creates a new Signer with the specified hash
//
// The specified hash validity is not checked in this function (see function IsValidSignerHash)
func NewSigner(hash []byte) *Signer {
	return &Signer{
		ID:   bson.NewObjectId(),
		Hash: hash,
	}
}

// SignatureArchives : represents the valid archives related to a signature process
type SignatureArchives struct {
	ID bson.ObjectId `key:"_id" bson:"_id"` // Internal id of a SignatureArchives - The unique signature identifier

29 30 31 32
	Sequence []uint32 `key:"sequence" bson:"sequence"` // Signing sequence
	Signers  []Signer `key:"signers" bson:"signers"`   // List of signers
	TextHash []byte   `key:"textHash" bson:"textHash"` // Small hash of the contract
	Seal     []byte   `key:"seal" bson:"seal"`         // Seal provided by the platform to authentify the context
Caro Axel's avatar
Caro Axel committed
33

Caro Axel's avatar
Caro Axel committed
34
	ReceivedPromises []Promise       `key:"receivedPromises" bson:"receivedPromises"` // Set of valid received promises (1 by sender)
Caro Axel's avatar
Caro Axel committed
35 36 37 38 39 40 41
	AbortedSigners   []AbortedSigner `key:"abortedSigners" bson:"abortedSigners"`     // Signers that were sent an abort token
	DishonestSigners []uint32        `key:"dishonestSigners" bson:"dishonestSigners"` // Indexes of the signers that were evaluated as dishonest

	SignedContract []byte `key:"signedContract" bson:"signedContract"` // Signed contract resulting of the signing process
}

// NewSignatureArchives : creates a new SignatureArchives with the specified parameters
42
func NewSignatureArchives(signatureUUID bson.ObjectId, sequence []uint32, signers []Signer, textHash, seal []byte) *SignatureArchives {
Caro Axel's avatar
Caro Axel committed
43 44 45
	return &SignatureArchives{
		ID: signatureUUID,

46 47 48 49
		Sequence: sequence,
		Signers:  signers,
		TextHash: textHash,
		Seal:     seal,
Caro Axel's avatar
Caro Axel committed
50

51 52 53
		ReceivedPromises: make([]Promise, 0),
		AbortedSigners:   make([]AbortedSigner, 0),
		DishonestSigners: make([]uint32, 0),
Caro Axel's avatar
Caro Axel committed
54

55
		SignedContract: make([]byte, 0),
Caro Axel's avatar
Caro Axel committed
56 57 58 59 60 61 62 63
	}
}

// Promise : represents a valid promise
type Promise struct {
	ID bson.ObjectId `key:"_id" bson:"_id"` // Internal id of a Promise

	RecipientKeyIndex uint32 `key:"recipientKeyIndex" bson:"recipientKeyIndex"` // Index of the hash of the recipient's certificate in
Caro Axel's avatar
Caro Axel committed
64
	// the `Signers` field of the enclosing SignatureArchives (identical to the one in the signers hashes array of the incoming promises)
65
	SenderKeyIndex uint32 `key:"senderKeyIndex" bson:"senderKeyIndex"` // Index of the hash of the sender's certificate in
Caro Axel's avatar
Caro Axel committed
66
	// the `Signers` field of the enclosing SignatureArchives (identical to the one in the signers hashes array of the incoming promises)
Caro Axel's avatar
Caro Axel committed
67 68 69 70 71 72 73 74 75 76 77 78 79
	SequenceIndex uint32 `key:"sequenceIndex" bson:"sequenceIndex"` // Sequence index of the promise
}

// NewPromise : creates a new Promise with the specified fields
func NewPromise(recipientIndex, senderIndex, sequenceIndex uint32) *Promise {
	return &Promise{
		ID:                bson.NewObjectId(),
		RecipientKeyIndex: recipientIndex,
		SenderKeyIndex:    senderIndex,
		SequenceIndex:     sequenceIndex,
	}
}

ElyKar's avatar
ElyKar committed
80
// Equal : determines if the two specified promises share the same information, without considering the
Caro Axel's avatar
Caro Axel committed
81
// bson object id.
ElyKar's avatar
ElyKar committed
82
func (p1 *Promise) Equal(p2 *Promise) bool {
Caro Axel's avatar
Caro Axel committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	if p1.RecipientKeyIndex != p2.RecipientKeyIndex {
		return false
	}
	if p1.SenderKeyIndex != p2.SenderKeyIndex {
		return false
	}
	if p1.SequenceIndex != p2.SequenceIndex {
		return false
	}
	return true
}

// AbortedSigner : represents a signer who was sent an abort token
type AbortedSigner struct {
	ID          bson.ObjectId `key:"_id" bson:"_id"`                 // Internal id of an AbortedSigner
	SignerIndex uint32        `key:"signerIndex" bson:"signerIndex"` // Index of the signer in the signers set
	AbortIndex  uint32        `key:"abortIndex" bson:"abortIndex"`   // Index in the sequence where the signer contacted the ttp and recieveed an abort token
}

// NewAbortedSigner : creates a new AbortedSigner with the specified fields
func NewAbortedSigner(signerIndex, abortIndex uint32) *AbortedSigner {
	return &AbortedSigner{
		ID:          bson.NewObjectId(),
		SignerIndex: signerIndex,
		AbortIndex:  abortIndex,
	}
}
Caro Axel's avatar
Caro Axel committed
110 111 112 113 114 115 116 117 118 119 120 121

// ContainsSigner : determines whether or not the specified signer is one of the signers,
// and also returns the sequence id of said signer.
func (archives *SignatureArchives) ContainsSigner(hash []byte) (bool, uint32) {
	for i, s := range archives.Signers {
		if bytes.Equal(hash, s.Hash) {
			return true, uint32(i)
		}
	}

	return false, uint32(0)
}