WIP: airgapped

This commit is contained in:
programmer10110 2020-08-11 12:46:13 +03:00
parent 27b6d32b11
commit a06907dae5
2 changed files with 52 additions and 21 deletions

View File

@ -16,25 +16,30 @@ import (
) )
type AirgappedMachine struct { type AirgappedMachine struct {
dkgInstance *dkg.DKG // should be a map or something to distinguish different rounds dkgInstances map[string]*dkg.DKG // should be a map or something to distinguish different rounds
qrProcessor qr.Processor qrProcessor qr.Processor
} }
func NewAirgappedMachine() *AirgappedMachine { func NewAirgappedMachine() *AirgappedMachine {
machine := &AirgappedMachine{ machine := &AirgappedMachine{
dkgInstance: dkg.Init(), dkgInstances: make(map[string]*dkg.DKG),
qrProcessor: qr.NewCameraProcessor(), qrProcessor: qr.NewCameraProcessor(),
} }
return machine return machine
} }
func (am *AirgappedMachine) handleStateDkgPubKeysAwaitConfirmations(o *client.Operation) error { func (am *AirgappedMachine) handleStateDkgPubKeysAwaitConfirmations(o *client.Operation) error {
pubKeyBz, err := am.dkgInstance.GetPubKey().MarshalBinary() dkgInstance, ok := am.dkgInstances[o.DKGIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", o.DKGIdentifier)
}
pubKeyBz, err := dkgInstance.GetPubKey().MarshalBinary()
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal pubkey: %w", err) return fmt.Errorf("failed to marshal pubkey: %w", err)
} }
req := requests.DKGProposalPubKeyConfirmationRequest{ req := requests.DKGProposalPubKeyConfirmationRequest{
ParticipantId: am.dkgInstance.ParticipantID, ParticipantId: dkgInstance.ParticipantID,
PubKey: pubKeyBz, PubKey: pubKeyBz,
CreatedAt: nil, CreatedAt: nil,
} }
@ -51,6 +56,12 @@ func (am *AirgappedMachine) handleStateDkgCommitsAwaitConfirmations(o *client.Op
payload responses.DKGProposalPubKeyParticipantResponse payload responses.DKGProposalPubKeyParticipantResponse
err error err error
) )
dkgInstance, ok := am.dkgInstances[o.DKGIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", o.DKGIdentifier)
}
if err = json.Unmarshal(o.Payload, &payload); err != nil { if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err) return fmt.Errorf("failed to unmarshal payload: %w", err)
} }
@ -60,20 +71,22 @@ func (am *AirgappedMachine) handleStateDkgCommitsAwaitConfirmations(o *client.Op
if err = pubKey.UnmarshalBinary(entry.PubKey); err != nil { if err = pubKey.UnmarshalBinary(entry.PubKey); err != nil {
return fmt.Errorf("failed to unmarshal pubkey: %w", err) return fmt.Errorf("failed to unmarshal pubkey: %w", err)
} }
am.dkgInstance.StorePubKey(entry.Title, pubKey) dkgInstance.StorePubKey(entry.Title, pubKey)
} }
if err = am.dkgInstance.InitDKGInstance(3); err != nil { // TODO: threshold if err = dkgInstance.InitDKGInstance(3); err != nil { // TODO: threshold
return fmt.Errorf("failed to init dkg instance: %w", err) return fmt.Errorf("failed to init dkg instance: %w", err)
} }
commits, err := json.Marshal(am.dkgInstance.GetCommits()) commits, err := json.Marshal(dkgInstance.GetCommits())
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal commits: %w", err) return fmt.Errorf("failed to marshal commits: %w", err)
} }
am.dkgInstances[o.DKGIdentifier] = dkgInstance
req := requests.DKGProposalCommitConfirmationRequest{ req := requests.DKGProposalCommitConfirmationRequest{
ParticipantId: am.dkgInstance.ParticipantID, ParticipantId: dkgInstance.ParticipantID,
Commit: commits, Commit: commits,
} }
reqBz, err := json.Marshal(req) reqBz, err := json.Marshal(req)
@ -89,6 +102,12 @@ func (am *AirgappedMachine) handleStateDkgDealsAwaitConfirmations(o *client.Oper
payload responses.DKGProposalCommitParticipantResponse payload responses.DKGProposalCommitParticipantResponse
err error err error
) )
dkgInstance, ok := am.dkgInstances[o.DKGIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", o.DKGIdentifier)
}
if err = json.Unmarshal(o.Payload, &payload); err != nil { if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err) return fmt.Errorf("failed to unmarshal payload: %w", err)
} }
@ -98,14 +117,16 @@ func (am *AirgappedMachine) handleStateDkgDealsAwaitConfirmations(o *client.Oper
if err = json.Unmarshal(entry.Commit, &commits); err != nil { if err = json.Unmarshal(entry.Commit, &commits); err != nil {
return fmt.Errorf("failed to unmarshal commits: %w", err) return fmt.Errorf("failed to unmarshal commits: %w", err)
} }
am.dkgInstance.StoreCommits(entry.Title, commits) dkgInstance.StoreCommits(entry.Title, commits)
} }
deals, err := am.dkgInstance.GetDeals() deals, err := dkgInstance.GetDeals()
if err != nil { if err != nil {
return fmt.Errorf("failed to get deals: %w", err) return fmt.Errorf("failed to get deals: %w", err)
} }
am.dkgInstances[o.DKGIdentifier] = dkgInstance
// Here we should create N=len(deals) private (encrypted) messages to participants but i don't know how to it yet // Here we should create N=len(deals) private (encrypted) messages to participants but i don't know how to it yet
//------------------------------------------------------- //-------------------------------------------------------
dealsBz, err := json.Marshal(deals) dealsBz, err := json.Marshal(deals)
@ -114,7 +135,7 @@ func (am *AirgappedMachine) handleStateDkgDealsAwaitConfirmations(o *client.Oper
} }
req := requests.DKGProposalDealConfirmationRequest{ req := requests.DKGProposalDealConfirmationRequest{
ParticipantId: am.dkgInstance.ParticipantID, ParticipantId: dkgInstance.ParticipantID,
Deal: dealsBz, Deal: dealsBz,
} }
//------------------------------------------------------- //-------------------------------------------------------
@ -132,6 +153,12 @@ func (am *AirgappedMachine) handleStateDkgResponsesAwaitConfirmations(o *client.
payload responses.DKGProposalDealParticipantResponse payload responses.DKGProposalDealParticipantResponse
err error err error
) )
dkgInstance, ok := am.dkgInstances[o.DKGIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", o.DKGIdentifier)
}
if err = json.Unmarshal(o.Payload, &payload); err != nil { if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err) return fmt.Errorf("failed to unmarshal payload: %w", err)
} }
@ -142,22 +169,24 @@ func (am *AirgappedMachine) handleStateDkgResponsesAwaitConfirmations(o *client.
return fmt.Errorf("failed to unmarshal commits: %w", err) return fmt.Errorf("failed to unmarshal commits: %w", err)
} }
for _, deal := range deals { for _, deal := range deals {
am.dkgInstance.StoreDeal(entry.Title, &deal) dkgInstance.StoreDeal(entry.Title, &deal)
} }
} }
processedResponses, err := am.dkgInstance.ProcessDeals() processedResponses, err := dkgInstance.ProcessDeals()
if err != nil { if err != nil {
return fmt.Errorf("failed to process deals: %w", err) return fmt.Errorf("failed to process deals: %w", err)
} }
am.dkgInstances[o.DKGIdentifier] = dkgInstance
responsesBz, err := json.Marshal(processedResponses) responsesBz, err := json.Marshal(processedResponses)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal deals") return fmt.Errorf("failed to marshal deals")
} }
req := requests.DKGProposalResponseConfirmationRequest{ req := requests.DKGProposalResponseConfirmationRequest{
ParticipantId: am.dkgInstance.ParticipantID, ParticipantId: dkgInstance.ParticipantID,
Response: responsesBz, Response: responsesBz,
} }

View File

@ -13,11 +13,13 @@ const (
) )
type Operation struct { type Operation struct {
ID string // UUID4 ID string // UUID4
Type OperationType Type OperationType
Payload []byte Payload []byte
Result []byte Result []byte
CreatedAt time.Time CreatedAt time.Time
DKGIdentifier string
To string
} }
func (o *Operation) Check(o2 *Operation) error { func (o *Operation) Check(o2 *Operation) error {