Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
mpcs
dfss
Commits
7fda94e9
Commit
7fda94e9
authored
Mar 21, 2016
by
Richer Maximilien
Committed by
Loïck Bonniot
Apr 15, 2016
Browse files
[c] Implement evidences reception and emission
parent
525d85ce
Changes
11
Hide whitespace changes
Inline
Side-by-side
dfssc/api/client.pb.go
View file @
7fda94e9
...
...
@@ -36,11 +36,16 @@ const _ = proto.ProtoPackageIsVersion1
// Promise message contains all the required information to verify
// the identity of the sender and reciever, and the actual promise
//
// * sequence is transmitted by platform and identical across clients
// * TODO implement an global signature for content
type
Promise
struct
{
RecipientKeyHash
[]
byte
`protobuf:"bytes,1,opt,name=recipientKeyHash,proto3" json:"recipientKeyHash,omitempty"`
SenderKeyHash
[]
byte
`protobuf:"bytes,2,opt,name=senderKeyHash,proto3" json:"senderKeyHash,omitempty"`
SignatureUuid
string
`protobuf:"bytes,3,opt,name=signatureUuid" json:"signatureUuid,omitempty"`
ContractUuid
string
`protobuf:"bytes,4,opt,name=contractUuid" json:"contractUuid,omitempty"`
RecipientKeyHash
[]
byte
`protobuf:"bytes,1,opt,name=recipientKeyHash,proto3" json:"recipientKeyHash,omitempty"`
SenderKeyHash
[]
byte
`protobuf:"bytes,2,opt,name=senderKeyHash,proto3" json:"senderKeyHash,omitempty"`
Index
uint32
`protobuf:"varint,3,opt,name=index" json:"index,omitempty"`
ContractDocumentHash
string
`protobuf:"bytes,4,opt,name=contractDocumentHash" json:"contractDocumentHash,omitempty"`
SignatureUuid
string
`protobuf:"bytes,5,opt,name=signatureUuid" json:"signatureUuid,omitempty"`
ContractUuid
string
`protobuf:"bytes,6,opt,name=contractUuid" json:"contractUuid,omitempty"`
}
func
(
m
*
Promise
)
Reset
()
{
*
m
=
Promise
{}
}
...
...
@@ -63,7 +68,8 @@ func (m *Signature) String() string { return proto.CompactTextString(
func
(
*
Signature
)
ProtoMessage
()
{}
func
(
*
Signature
)
Descriptor
()
([]
byte
,
[]
int
)
{
return
fileDescriptor0
,
[]
int
{
1
}
}
// Hello message is used when discovering peers. It contains the current version of the software.
// Hello message is used when discovering peers.
// It contains the current version of the software.
type
Hello
struct
{
Version
string
`protobuf:"bytes,1,opt,name=version" json:"version,omitempty"`
}
...
...
@@ -199,24 +205,24 @@ var _Client_serviceDesc = grpc.ServiceDesc{
}
var
fileDescriptor0
=
[]
byte
{
// 30
2
bytes of a gzipped FileDescriptorProto
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x09
,
0x6e
,
0x88
,
0x02
,
0xff
,
0x
b
4
,
0x
92
,
0x
4f
,
0x4
e
,
0xf
3
,
0x30
,
0x10
,
0x
c5
,
0x
9b
,
0x
a
f
,
0xf
f
,
0x
be
,
0x8
c
,
0x
42
,
0x
85
,
0x
bc
,
0xaa
,
0x0
2
,
0x48
,
0x
10
,
0x
75
,
0x
81
,
0x5
8
,
0x
24
,
0x
52
,
0x
39
,
0x
42
,
0x
41
,
0x
aa
,
0x
c
4
,
0x0
6
,
0x
05
,
0x
38
,
0x
80
,
0x
71
,
0x
a6
,
0x
60
,
0x
29
,
0x
8d
,
0xa
3
,
0x
b1
,
0x
8b
,
0xc4
,
0x
3
5
,
0x
b
8
,
0x0
3
,
0x
47
,
0x
e1
,
0x
5e
,
0xa
4
,
0xd
3
,
0x
26
,
0x2
5
,
0x
6a
,
0x
17
,
0x
6c
,
0x
d8
,
0x
44
,
0x
79
,
0x
cf
,
0x
bf
,
0x
b1
,
0x
df
,
0x
7
8
,
0x
0
c
,
0x
27
,
0x
d9
,
0xc
2
,
0x
da
,
0x
64
,
0x
fd
,
0x
51
,
0x
8
9
,
0x
2
c
,
0x
75
,
0x
a2
,
0x
72
,
0x
8d
,
0x
85
,
0x8
b
,
0x
4b
,
0x
32
,
0x
ce
,
0x
88
,
0x
6e
,
0x
e5
,
0x
84
,
0x
6
7
,
0x
0
d
,
0x
51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
0x1
d
,
0xf
c
,
0x
bb
,
0x
4e
,
0x
fc
,
0x
f7
,
0x
5
c
,
0x
47
,
0xe
0
,
0x
f
7
,
0x
56
,
0x9
4
,
0x
6d
,
0x
c0
,
0x
7d
,
0x
32
,
0x
e8
,
0x
4
e
,
0x
99
,
0x
92
,
0x
08
,
0x
a6
,
0x
5b
,
0x
59
,
0x
14
,
0x9
a
,
0x
1d
,
0x
c0
,
0x
fc
,
0x45
,
0x
a
2
,
0x
51
,
0x
b
a
,
0x
22
,
0x4
3
,
0x
7f
,
0x
f5
,
0x
e
1
,
0xc
1
,
0x
6c
,
0x
4d
,
0x
3b
,
0x
66
,
0x
29
,
0x0
4
,
0x
f7
,
0x
28
,
0x
b9
,
0x
ed
,
0x
b6
,
0x
17
,
0x
a4
,
0x
6
e
,
0x
b
d
,
0x
e9
,
0x
3
7
,
0x
8
a
,
0x
17
,
0x8
4
,
0x
ae
,
0x
11
,
0x
35
,
0x
ae
,
0x
7
5
,
0x
2e
,
0x
93
,
0x
1
1
,
0x
5b
,
0xc
1
,
0x
82
,
0x
f4
,
0x3
f
,
0x
ef
,
0x
d
a
,
0x
6b
,
0x
7a
,
0x
3
c
,
0x
d
0
,
0x
73
,
0x
01
,
0x
ff
,
0x
37
,
0x
ca
,
0x
08
,
0x
ed
,
0x
42
,
0x
30
,
0x
a
0
,
0x
2a
,
0xe
5
,
0x8
a
,
0x
7
f
,
0x
dd
,
0x
93
,
0x
d1
,
0xe
3
,
0x
8c
,
0x
be
,
0xf
2
,
0x
ea
,
0x2b
,
0x00
,
0x00
,
0xff
,
0xff
,
0x
2d
,
0x
9f
,
0x
c1
,
0x
c3
,
0x
0d
,
0x02
,
0x00
,
0x00
,
}
dfssc/api/client.proto
View file @
7fda94e9
...
...
@@ -12,11 +12,17 @@ service Client {
// Promise message contains all the required information to verify
// the identity of the sender and reciever, and the actual promise
//
// * sequence is transmitted by platform and identical across clients
// * TODO implement an global signature for content
message
Promise
{
bytes
recipientKeyHash
=
1
;
// SHA-512
bytes
senderKeyHash
=
2
;
// SHA-512
string
signatureUuid
=
3
;
string
contractUuid
=
4
;
bytes
recipientKeyHash
=
1
;
// SHA-512
bytes
senderKeyHash
=
2
;
// SHA-512
uint32
index
=
3
;
// The index of the sequence for this message
string
contractDocumentHash
=
4
;
// Hash of the contract to be signed
string
signatureUuid
=
5
;
// Avoid re-use of the promise in another ctx
string
contractUuid
=
6
;
// TODO the crypto payload
}
// Signature message contains all the required information to verify
...
...
@@ -29,7 +35,8 @@ message Signature {
string
contractUuid
=
5
;
}
// Hello message is used when discovering peers. It contains the current version of the software.
// Hello message is used when discovering peers.
// It contains the current version of the software.
message
Hello
{
string
version
=
1
;
}
dfssc/sign/promises.go
0 → 100644
View file @
7fda94e9
package
sign
import
(
"errors"
"fmt"
"time"
cAPI
"dfss/dfssc/api"
dAPI
"dfss/dfssd/api"
pAPI
"dfss/dfssp/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// CreatePromise creates a promise from 'from' to 'to', in the context of the SignatureManager
// provided the specified sequence indexes are valid
func
(
m
*
SignatureManager
)
CreatePromise
(
from
,
to
uint32
)
(
*
cAPI
.
Promise
,
error
)
{
if
int
(
from
)
>=
len
(
m
.
keyHash
)
||
int
(
to
)
>=
len
(
m
.
keyHash
)
{
return
nil
,
errors
.
New
(
"Invalid id for promise creation"
)
}
if
m
.
currentIndex
<
0
{
return
nil
,
errors
.
New
(
"Invalid currentIndex for promise creation"
)
}
promise
:=
&
cAPI
.
Promise
{
RecipientKeyHash
:
m
.
keyHash
[
to
],
SenderKeyHash
:
m
.
keyHash
[
from
],
Index
:
uint32
(
m
.
currentIndex
),
ContractDocumentHash
:
m
.
contract
.
File
.
Hash
,
SignatureUuid
:
m
.
uuid
,
ContractUuid
:
m
.
contract
.
UUID
,
}
return
promise
,
nil
}
// SendPromise sends the specified promise to the specified peer
func
(
m
*
SignatureManager
)
SendPromise
(
promise
*
cAPI
.
Promise
,
to
uint32
)
(
*
pAPI
.
ErrorCode
,
error
)
{
connection
,
err
:=
m
.
GetClient
(
to
)
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{},
err
}
// Handle the timeout
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Minute
)
defer
cancel
()
errCode
,
err
:=
(
*
connection
)
.
TreatPromise
(
ctx
,
promise
)
if
err
==
grpc
.
ErrClientConnTimeout
{
dAPI
.
DLog
(
"Promise timeout for ["
+
fmt
.
Sprintf
(
"%d"
,
to
)
+
"]"
)
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_TIMEOUT
,
Message
:
"promise timeout"
},
err
}
else
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_INTERR
,
Message
:
"internal server error"
},
err
}
m
.
archives
.
sentPromises
=
append
(
m
.
archives
.
sentPromises
,
promise
)
return
errCode
,
nil
}
dfssc/sign/protocol.go
View file @
7fda94e9
package
sign
import
(
"errors"
"fmt"
"time"
cAPI
"dfss/dfssc/api"
"dfss/dfssc/common"
dAPI
"dfss/dfssd/api"
pAPI
"dfss/dfssp/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Sign makes the SignatureManager perform its specified signature
var
(
hashToID
map
[
string
]
uint32
incomingPromises
chan
*
cAPI
.
Promise
incomingSignatures
chan
*
cAPI
.
Signature
)
// Sign performe all the message exchange for the contract to be signed
//
// * Initialize the SignatureManager from starter.go
// * Compute the reversed map [mail -> ID] of signers
// * Make channels for handlers
// * Promises rounds
// * Signature round
func
(
m
*
SignatureManager
)
Sign
()
error
{
myID
,
currentIndex
,
nextIndex
,
err
:=
m
.
Initialize
()
myID
,
nextIndex
,
err
:=
m
.
Initialize
()
if
err
!=
nil
{
return
err
}
hashToID
=
m
.
makeSignersHashToIDMap
()
incomingPromises
=
make
(
chan
*
cAPI
.
Promise
)
incomingSignatures
=
make
(
chan
*
cAPI
.
Signature
)
// Promess rounds
for
nextIndex
!=
-
1
{
pendingSet
,
err1
:=
common
.
GetPendingSet
(
m
.
sequence
,
myID
,
currentIndex
)
// Follow the sequence until there is no next occurence of me
for
nextIndex
>
0
{
// Set of the promise we are waiting for
pendingSet
,
err1
:=
common
.
GetPendingSet
(
m
.
sequence
,
myID
,
m
.
currentIndex
)
if
err1
!=
nil
{
return
err1
// err is renamed to avoid shadowing err on linter check
}
sendSet
,
err1
:=
common
.
GetSendSet
(
m
.
sequence
,
myID
,
currentIndex
)
// Set of the promises we must send
sendSet
,
err1
:=
common
.
GetSendSet
(
m
.
sequence
,
myID
,
m
.
currentIndex
)
if
err1
!=
nil
{
return
err1
}
// Reception of the due promesses
for
len
(
pendingSet
)
!=
0
{
i
:=
0
// TODO
// Improve, because potential memory leak
// see https://github.com/golang/go/wiki/SliceTricks
pendingSet
=
append
(
pendingSet
[
:
i
],
pendingSet
[
i
+
1
:
]
...
)
}
c
:=
make
(
chan
int
)
// Sending of the due promesses
/*
for _, id := range sendSet {
go func(id) {
promise, err := m.CreatePromise(id)
recpt := m.SendPromise(promise, id)
c <- id
}(id)
}
*/
// Verifying we sent all the due promesses
for
_
=
range
sendSet
{
<-
c
}
// Exchange messages
m
.
promiseRound
(
pendingSet
,
sendSet
,
myID
)
currentIndex
=
nextIndex
nextIndex
,
err1
=
common
.
FindNextIndex
(
m
.
sequence
,
myID
,
currentIndex
)
m
.
currentIndex
=
nextIndex
nextIndex
,
err1
=
common
.
FindNextIndex
(
m
.
sequence
,
myID
,
m
.
currentIndex
)
if
err1
!=
nil
{
return
err1
}
...
...
@@ -78,46 +71,6 @@ func (m *SignatureManager) Sign() error {
return
nil
}
// CreatePromise creates a promise from 'from' to 'to', in the context of the SignatureManager
// provided the specified sequence indexes are valid
func
(
m
*
SignatureManager
)
CreatePromise
(
from
,
to
uint32
)
(
*
cAPI
.
Promise
,
error
)
{
if
int
(
from
)
>=
len
(
m
.
keyHash
)
||
int
(
to
)
>=
len
(
m
.
keyHash
)
{
return
&
cAPI
.
Promise
{},
errors
.
New
(
"Invalid id for promise creation"
)
}
promise
:=
&
cAPI
.
Promise
{
RecipientKeyHash
:
m
.
keyHash
[
to
],
SenderKeyHash
:
m
.
keyHash
[
from
],
SignatureUuid
:
m
.
uuid
,
ContractUuid
:
m
.
contract
.
UUID
,
}
return
promise
,
nil
}
// SendPromise sends the specified promise to the specified peer
// TODO
func
(
m
*
SignatureManager
)
SendPromise
(
promise
*
cAPI
.
Promise
,
to
uint32
)
(
*
pAPI
.
ErrorCode
,
error
)
{
connection
,
err
:=
m
.
GetClient
(
to
)
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{},
err
}
// Handle the timeout
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Minute
)
defer
cancel
()
errCode
,
err
:=
(
*
connection
)
.
TreatPromise
(
ctx
,
promise
)
if
err
==
grpc
.
ErrClientConnTimeout
{
dAPI
.
DLog
(
"Promise timeout for ["
+
fmt
.
Sprintf
(
"%d"
,
to
)
+
"]"
)
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_TIMEOUT
,
Message
:
"promise timeout"
},
err
}
else
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_INTERR
,
Message
:
"internal server error"
},
err
}
m
.
archives
.
sentPromises
=
append
(
m
.
archives
.
sentPromises
,
promise
)
return
errCode
,
nil
}
// GetClient retrieves the Client to the specified sequence id provided it exists
func
(
m
*
SignatureManager
)
GetClient
(
to
uint32
)
(
*
cAPI
.
ClientClient
,
error
)
{
mailto
:=
m
.
contract
.
Signers
[
to
]
.
Email
...
...
@@ -129,75 +82,61 @@ func (m *SignatureManager) GetClient(to uint32) (*cAPI.ClientClient, error) {
return
m
.
peers
[
mailto
],
nil
}
// SendAllSigns creates and sends signatures to all the signers of the contract
// TODO
// Use goroutines to send in parallel
func
(
m
*
SignatureManager
)
SendAllSigns
()
error
{
myID
,
err
:=
m
.
FindID
()
if
err
!=
nil
{
return
err
}
// makeEMailMap build an association to reverse a hash to the sequence ID
func
(
m
*
SignatureManager
)
makeSignersHashToIDMap
()
map
[
string
]
uint32
{
sendSet
:=
common
.
GetAllButOne
(
m
.
sequence
,
myID
)
for
_
,
id
:=
range
sendSet
{
signature
,
err
:=
m
.
CreateSignature
(
myID
,
id
)
if
err
!=
nil
{
return
err
}
hashToID
:=
make
(
map
[
string
]
uint32
)
_
,
err
=
m
.
SendSignature
(
signature
,
id
)
if
err
!=
nil
{
return
err
}
signers
:=
m
.
contract
.
Signers
for
id
,
signer
:=
range
signers
{
hashToID
[
signer
.
Hash
]
=
uint32
(
id
)
}
return
nil
return
hashToID
}
// CreateSignature creates a signature from from to to, in the context of the SignatureManager
// provided the specified sequence indexes are valid
// TODO
// Implement a true cryptographic signature
func
(
m
*
SignatureManager
)
CreateSignature
(
from
,
to
uint32
)
(
*
cAPI
.
Signature
,
error
)
{
if
int
(
from
)
>=
len
(
m
.
keyHash
)
||
int
(
to
)
>=
len
(
m
.
keyHash
)
{
return
&
cAPI
.
Signature
{},
errors
.
New
(
"Invalid id for signature creation"
)
}
signature
:=
&
cAPI
.
Signature
{
RecipientKeyHash
:
m
.
keyHash
[
to
],
SenderKeyHash
:
m
.
keyHash
[
from
],
Signature
:
"Signature"
,
SignatureUuid
:
m
.
uuid
,
ContractUuid
:
m
.
contract
.
UUID
,
func
(
m
*
SignatureManager
)
promiseRound
(
pendingSet
,
sendSet
[]
uint32
,
myID
uint32
)
{
// Reception of the due promises
// TODO this ctx needs a timeout !
for
len
(
pendingSet
)
>
0
{
promise
:=
<-
incomingPromises
senderID
,
exist
:=
hashToID
[
string
(
promise
.
SenderKeyHash
)]
if
exist
{
var
err
error
pendingSet
,
err
=
common
.
Remove
(
pendingSet
,
senderID
)
if
err
!=
nil
{
// Recieve unexpected promise, ignore ?
}
m
.
archives
.
recievedPromises
=
append
(
m
.
archives
.
recievedPromises
,
promise
)
}
else
{
// Wrong sender keyHash
}
}
return
signature
,
nil
}
// SendSignature sends the specified signature to the specified peer
// TODO
func
(
m
*
SignatureManager
)
SendSignature
(
signature
*
cAPI
.
Signature
,
to
uint32
)
(
*
pAPI
.
ErrorCode
,
error
)
{
connection
,
err
:=
m
.
GetClient
(
to
)
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{},
err
c
:=
make
(
chan
*
cAPI
.
Promise
)
// Sending of due promises
for
_
,
id
:=
range
sendSet
{
// The signature manager is read only - safe !
go
func
(
id
uint32
,
m
*
SignatureManager
)
{
promise
,
err
:=
m
.
CreatePromise
(
myID
,
id
)
if
err
!=
nil
{
// Failed to create promise
}
_
,
err
=
m
.
SendPromise
(
promise
,
id
)
if
err
!=
nil
{
// We don't check if the promise has been recieved
}
c
<-
promise
}(
id
,
m
)
}
//
TODO
// Handle the timeout
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Minute
)
defer
cancel
()
errCode
,
err
:=
(
*
connection
)
.
TreatSignature
(
ctx
,
signature
)
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{},
err
//
Verifying we sent all the due promesses
for
_
=
range
sendSet
{
promise
:=
<-
c
if
promise
!=
nil
{
m
.
archives
.
sentPromises
=
append
(
m
.
archives
.
sentPromises
,
promise
)
}
else
{
// something appened during the goroutine
}
}
m
.
archives
.
sentSignatures
=
append
(
m
.
archives
.
sentSignatures
,
signature
)
return
errCode
,
nil
}
// RecieveAllSigns is not done yet
// TODO
func
(
m
*
SignatureManager
)
RecieveAllSigns
()
error
{
return
nil
}
dfssc/sign/server.go
View file @
7fda94e9
...
...
@@ -15,7 +15,11 @@ type clientServer struct{}
//
// Handle incoming TreatPromise messages
func
(
s
*
clientServer
)
TreatPromise
(
ctx
context
.
Context
,
in
*
cAPI
.
Promise
)
(
*
pAPI
.
ErrorCode
,
error
)
{
// TODO
// Pass the message to Sign()
if
incomingPromises
!=
nil
{
incomingPromises
<-
in
}
return
nil
,
nil
}
...
...
dfssc/sign/signatures.go
0 → 100644
View file @
7fda94e9
package
sign
import
(
"dfss/dfssc/common"
"errors"
"fmt"
"time"
cAPI
"dfss/dfssc/api"
dAPI
"dfss/dfssd/api"
pAPI
"dfss/dfssp/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// SendAllSigns creates and sends signatures to all the signers of the contract
// TODO Use goroutines to send in parallel
func
(
m
*
SignatureManager
)
SendAllSigns
()
error
{
myID
,
err
:=
m
.
FindID
()
if
err
!=
nil
{
return
err
}
// compute a set of all signers exept me
sendSet
:=
common
.
GetAllButOne
(
m
.
sequence
,
myID
)
for
_
,
id
:=
range
sendSet
{
signature
,
err
:=
m
.
CreateSignature
(
myID
,
id
)
if
err
!=
nil
{
return
err
}
_
,
err
=
m
.
SendSignature
(
signature
,
id
)
if
err
!=
nil
{
return
err
}
}
return
nil
}
// CreateSignature creates a signature from a sequence ID to another
// provided the specified sequence indexes are valid
// TODO Implement a true cryptographic signature
func
(
m
*
SignatureManager
)
CreateSignature
(
from
,
to
uint32
)
(
*
cAPI
.
Signature
,
error
)
{
if
int
(
from
)
>=
len
(
m
.
keyHash
)
||
int
(
to
)
>=
len
(
m
.
keyHash
)
{
return
&
cAPI
.
Signature
{},
errors
.
New
(
"Invalid id for signature creation"
)
}
signature
:=
&
cAPI
.
Signature
{
RecipientKeyHash
:
m
.
keyHash
[
to
],
SenderKeyHash
:
m
.
keyHash
[
from
],
Signature
:
"Signature"
,
SignatureUuid
:
m
.
uuid
,
ContractUuid
:
m
.
contract
.
UUID
,
}
return
signature
,
nil
}
// SendSignature sends the specified signature to the specified peer
func
(
m
*
SignatureManager
)
SendSignature
(
signature
*
cAPI
.
Signature
,
to
uint32
)
(
*
pAPI
.
ErrorCode
,
error
)
{
connection
,
err
:=
m
.
GetClient
(
to
)
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{},
err
}
// Handle the timeout
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Minute
)
defer
cancel
()
errCode
,
err
:=
(
*
connection
)
.
TreatSignature
(
ctx
,
signature
)
if
err
==
grpc
.
ErrClientConnTimeout
{
dAPI
.
DLog
(
"Signature timeout for ["
+
fmt
.
Sprintf
(
"%d"
,
to
)
+
"]"
)
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_TIMEOUT
,
Message
:
"signature timeout"
},
err
}
else
if
err
!=
nil
{
return
&
pAPI
.
ErrorCode
{
Code
:
pAPI
.
ErrorCode_INTERR
,
Message
:
"internal server error"
},
err
}
m
.
archives
.
sentSignatures
=
append
(
m
.
archives
.
sentSignatures
,
signature
)
return
errCode
,
nil
}
// RecieveAllSigns is not done yet
func
(
m
*
SignatureManager
)
RecieveAllSigns
()
error
{
myID
,
err
:=
m
.
FindID
()
if
err
!=
nil
{
return
err
}
// compute a set of all signers exept me
pendingSet
:=
common
.
GetAllButOne
(
m
.
sequence
,
myID
)
// TODO this ctx needs a timeout !
for
len
(
pendingSet
)
>
0
{
signature
:=
<-
incomingSignatures
senderID
,
exist
:=
hashToID
[
string
(
signature
.
SenderKeyHash
)]
if
exist
{
var
err
error
pendingSet
,
err
=
common
.
Remove
(
pendingSet
,
senderID
)
if
err
!=
nil
{
// Recieve unexpected signature, ignore ?
}
m
.
archives
.
recievedSignatures
=
append
(
m
.
archives
.
recievedSignatures
,
signature
)
}
else
{
// Wrong sender keyHash
}
}
return
nil
}
dfssc/sign/starter.go
View file @
7fda94e9
...
...
@@ -20,18 +20,19 @@ import (
// SignatureManager handles the signature of a contract.
type
SignatureManager
struct
{
auth
*
security
.
AuthContainer
localPort
int
contract
*
contract
.
JSON
platform
pAPI
.
PlatformClient
peers
map
[
string
]
*
cAPI
.
ClientClient
nbReady
int
cServer
*
grpc
.
Server
sequence
[]
uint32
uuid
string
keyHash
[][]
byte
mail
string
archives
*
Archives
auth
*
security
.
AuthContainer
localPort
int
contract
*
contract
.
JSON
// contains the contractUUID, the list of the signers' hashes, the hash of the contract
platform
pAPI
.
PlatformClient
peers
map
[
string
]
*
cAPI
.
ClientClient
nbReady
int
cServer
*
grpc
.
Server
sequence
[]
uint32