11package claim
22
33import (
4- "encoding/hex"
54 "encoding/json"
65 "strings"
6+ "time"
77
88 "github.com/ockam-network/ockam"
99 "github.com/ockam-network/ockam/random"
@@ -14,10 +14,13 @@ type Data map[string]interface{}
1414
1515// Claim is
1616type Claim struct {
17- id string
18- data Data
19- issuer ockam.Entity
20- subject ockam.Entity
17+ nonce string
18+ id string
19+ data Data
20+ _type string
21+ issuer ockam.Entity
22+ subject ockam.Entity
23+ signatures []ockam.Signature
2124}
2225
2326// Option is
@@ -31,15 +34,16 @@ func New(data Data, options ...Option) (*Claim, error) {
3134 option (c )
3235 }
3336
34- s , err := random .GenerateAlphaNumericString (31 )
37+ nonce , err := random .GenerateAlphaNumericString (10 )
3538 if err != nil {
3639 return nil , err
3740 }
41+ c .nonce = nonce
3842
3943 if c .issuer != nil {
40- c .id = c .issuer .ID ().String () + "/claim/" + s
44+ c .id = c .issuer .ID ().String () + "/claim/" + nonce
4145 } else {
42- c .id = s
46+ c .id = nonce
4347 }
4448
4549 return c , nil
@@ -52,6 +56,13 @@ func ID(id string) Option {
5256 }
5357}
5458
59+ // Type is
60+ func Type (t string ) Option {
61+ return func (c * Claim ) {
62+ c ._type = t
63+ }
64+ }
65+
5566// Issuer is
5667func Issuer (e ockam.Entity ) Option {
5768 return func (c * Claim ) {
@@ -71,9 +82,19 @@ func (c *Claim) ID() string {
7182 return c .id
7283}
7384
74- // SetID sets the claim's ID
75- func (c * Claim ) SetID (id string ) {
76- c .id = id
85+ // Nonce returns the claim's Nonce
86+ func (c * Claim ) Nonce () string {
87+ return c .nonce
88+ }
89+
90+ // Type returns the claim's Type
91+ func (c * Claim ) Type () string {
92+ return c ._type
93+ }
94+
95+ // SetType sets the claim's Type
96+ func (c * Claim ) SetType (t string ) {
97+ c ._type = t
7798}
7899
79100// Issuer returns the claim's Issuer entity
@@ -119,15 +140,44 @@ func (c *Claim) Signatures() []ockam.Signature {
119140}
120141
121142// AddSignature is
122- func (c * Claim ) AddSignature (ockam.Signature ) {
123- }
143+ func (c * Claim ) AddSignature (s ockam.Signature ) {
144+ c .signatures = append (c .signatures , s )
145+ }
146+
147+ // MarshalJSON is
148+ func (c * Claim ) MarshalJSON () ([]byte , error ) {
149+ type j struct {
150+ Context []string `json:"@context"`
151+ ID string `json:"id"`
152+ Type []string `json:"type"`
153+ Issuer string `json:"issuer"`
154+ Issued string `json:"issued"`
155+ Claim map [string ]interface {} `json:"claim"`
156+ Signatures []map [string ]interface {} `json:"signatures,omitempty"`
157+ }
124158
125- // MarshalBinary is
126- func (c Claim ) MarshalBinary () ([]byte , error ) {
127- b , err := json .Marshal (c .data )
128- if err != nil {
129- return nil , err
159+ vc := & j {
160+ Context : []string {"https://w3id.org/identity/v1" , "https://w3id.org/security/v1" },
161+ ID : c .ID (),
162+ Type : []string {c .Type ()},
163+ Issuer : c .Issuer ().ID ().String (),
164+ Issued : time .Now ().UTC ().Format ("2006-01-02" ),
130165 }
131- s := hex .EncodeToString (b )
132- return []byte (c .id + "=" + s ), nil
166+
167+ vc .Claim = c .Data ()
168+ vc .Claim ["id" ] = c .Subject ().ID ().String ()
169+
170+ for _ , s := range c .signatures {
171+ sj := map [string ]interface {}{
172+ "type" : s .Type (),
173+ "created" : s .Created (),
174+ "creator" : s .Creator (),
175+ "domain" : s .Domain (),
176+ "nonce" : s .Nonce (),
177+ "signatureValue" : s .SignatureValue (),
178+ }
179+ vc .Signatures = append (vc .Signatures , sj )
180+ }
181+
182+ return json .Marshal (vc )
133183}
0 commit comments