-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93b1094
commit 073256e
Showing
12 changed files
with
356 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package iden3comm | ||
|
||
// Iden3DirectiveType represents the type of directive | ||
type Iden3DirectiveType string | ||
|
||
// Constants for Iden3DirectiveType | ||
const ( | ||
TransparentPaymentDirectiveType Iden3DirectiveType = "TransparentPaymentDirective" | ||
) | ||
|
||
// TransparentPaymentCredential represents credential information | ||
type TransparentPaymentCredential struct { | ||
Type string `json:"type"` | ||
Context string `json:"context"` | ||
} | ||
|
||
// TransparentPaymentRequestData represents payment request information | ||
type TransparentPaymentRequestData struct { | ||
Recipient string `json:"recipient"` | ||
Amount string `json:"amount"` | ||
Token string `json:"token,omitempty"` | ||
Expiration string `json:"expiration"` | ||
Nonce string `json:"nonce"` | ||
Metadata string `json:"metadata"` | ||
} | ||
|
||
// TransparentPaymentDirectivePayload represents the payload for a transparent payment directive | ||
type TransparentPaymentDirectivePayload struct { | ||
Credential TransparentPaymentCredential `json:"credential"` | ||
PaymentData TransparentPaymentRequestData `json:"paymentData"` | ||
PermitSignature string `json:"permitSignature"` | ||
Description string `json:"description,omitempty"` | ||
} | ||
|
||
// TransparentPaymentDirective represents a complete transparent payment directive | ||
type TransparentPaymentDirective struct { | ||
Type Iden3DirectiveType `json:"type"` | ||
Purpose ProtocolMessage `json:"purpose,omitempty"` | ||
Context string `json:"context,omitempty"` | ||
Data []TransparentPaymentDirectivePayload `json:"data"` | ||
} | ||
|
||
// Iden3Directive is currently an alias for TransparentPaymentDirective | ||
// Can be expanded to a union type using interfaces if more directive types are added | ||
type Iden3Directive = TransparentPaymentDirective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package iden3comm_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/iden3/iden3comm/v2" | ||
) | ||
|
||
func TestExtractDirectiveFromMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
message iden3comm.BasicMessage | ||
expected []iden3comm.Iden3Directive | ||
}{ | ||
{ | ||
name: "No directive attachments", | ||
message: iden3comm.BasicMessage{ | ||
Attachments: iden3comm.Attachments{ | ||
{Type: "otherType"}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "With directive attachments", | ||
message: iden3comm.BasicMessage{ | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: iden3comm.Iden3DirectiveAttachmentType, | ||
Data: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := tt.message.Attachments.ExtractDirectives() | ||
if !reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("expected %v, got %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPropagateDirectiveIntoMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
message iden3comm.BasicMessage | ||
incomingDirective []iden3comm.Iden3Directive | ||
expected iden3comm.BasicMessage | ||
}{ | ||
{ | ||
name: "No incoming directives", | ||
message: iden3comm.BasicMessage{ | ||
ID: "test", | ||
}, | ||
incomingDirective: []iden3comm.Iden3Directive{}, | ||
expected: iden3comm.BasicMessage{ | ||
ID: "test", | ||
}, | ||
}, | ||
{ | ||
name: "No existing attachments", | ||
message: iden3comm.BasicMessage{ | ||
ID: "test", | ||
}, | ||
incomingDirective: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
expected: iden3comm.BasicMessage{ | ||
ID: "test", | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: iden3comm.Iden3DirectiveAttachmentType, | ||
Data: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "With existing directive attachments", | ||
message: iden3comm.BasicMessage{ | ||
ID: "test", | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: iden3comm.Iden3DirectiveAttachmentType, | ||
Data: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
incomingDirective: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
expected: iden3comm.BasicMessage{ | ||
ID: "test", | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: iden3comm.Iden3DirectiveAttachmentType, | ||
Data: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "With existing directive attachments and other attachments", | ||
message: iden3comm.BasicMessage{ | ||
ID: "test", | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: "otherType", | ||
}, | ||
}, | ||
}, | ||
incomingDirective: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
expected: iden3comm.BasicMessage{ | ||
ID: "test", | ||
Attachments: iden3comm.Attachments{ | ||
{ | ||
Type: "otherType", | ||
}, | ||
{ | ||
Type: iden3comm.Iden3DirectiveAttachmentType, | ||
Data: []iden3comm.Iden3Directive{ | ||
{ | ||
Type: iden3comm.TransparentPaymentDirectiveType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.message.Attachments.AddDirectives(tt.incomingDirective) | ||
if !reflect.DeepEqual(tt.message, tt.expected) { | ||
t.Errorf("expected %v, got %v", tt.expected, tt.message) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.