-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
45 lines (36 loc) · 1.01 KB
/
payload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package doc
import (
"github.com/bahner/go-ma/multi"
cbor "github.com/fxamacker/cbor/v2"
"github.com/multiformats/go-multicodec"
"lukechampine.com/blake3"
)
// Payload generates the unsigned DID,
// This is everything non-metadata in the DID document.
// We don't use a pointer here, so that we don't have to reiterate the
// struct in the function. We just need to change the signature.
func Payload(d Document) (Document, error) {
d.Proof = Proof{}
return d, nil
}
// Marshals the payload to CBOR for publication
func (d *Document) MarshalPayloadToCBOR() ([]byte, error) {
p, err := Payload(*d)
if err != nil {
return nil, err
}
return cbor.Marshal(p)
}
func (d *Document) PayloadHash() ([]byte, error) {
p, err := d.MarshalPayloadToCBOR()
if err != nil {
return nil, ErrPayloadMarshal
}
// Hash the payload
hashed := blake3.Sum256(p)
multicodecHashed, err := multi.MulticodecEncode(multicodec.Blake3, hashed[:])
if err != nil {
return nil, ErrPayloadMultiencode
}
return multicodecHashed, nil
}