-
Notifications
You must be signed in to change notification settings - Fork 22
/
factory.go
137 lines (110 loc) · 4.52 KB
/
factory.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package factory
import (
"fmt"
"strings"
"github.com/hyperledger/aries-framework-go/spi/storage"
"github.com/trustbloc/edge-core/pkg/log"
"github.com/trustbloc/sidetree-core-go/pkg/api/cas"
"github.com/trustbloc/sidetree-core-go/pkg/api/protocol"
"github.com/trustbloc/sidetree-core-go/pkg/compression"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/doccomposer"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/doctransformer/didtransformer"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/docvalidator/didvalidator"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/operationapplier"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/operationparser"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/txnprovider"
"github.com/trustbloc/orb/pkg/config"
ctxcommon "github.com/trustbloc/orb/pkg/context/common"
"github.com/trustbloc/orb/pkg/hashlink"
"github.com/trustbloc/orb/pkg/metrics"
vcommon "github.com/trustbloc/orb/pkg/protocolversion/versions/common"
protocolcfg "github.com/trustbloc/orb/pkg/protocolversion/versions/v1_0/config"
orboperationparser "github.com/trustbloc/orb/pkg/versions/1_0/operationparser"
"github.com/trustbloc/orb/pkg/versions/1_0/operationparser/validators/anchortime"
"github.com/trustbloc/orb/pkg/versions/1_0/txnprocessor"
)
var logger = log.New("protocol-v1_0")
// Factory implements version 0.1 of the Sidetree protocol.
type Factory struct{}
// New returns a version 1.0 implementation of the Sidetree protocol.
func New() *Factory {
return &Factory{}
}
// Create creates a new protocol version.
func (v *Factory) Create(version string, casClient cas.Client, casResolver ctxcommon.CASResolver,
opStore ctxcommon.OperationStore, provider storage.Provider,
sidetreeCfg *config.Sidetree) (protocol.Version, error) {
p := protocolcfg.GetProtocolConfig()
opParser := operationparser.New(p,
operationparser.WithAnchorTimeValidator(anchortime.New(p.MaxOperationTimeDelta)),
operationparser.WithAnchorOriginValidator(sidetreeCfg.AllowedOriginsValidator),
)
orbParser := orboperationparser.New(opParser)
cp := compression.New(compression.WithDefaultAlgorithms())
op := txnprovider.NewOperationProvider(p, opParser, &casReader{casResolver}, cp,
txnprovider.WithSourceCASURIFormatter(formatWebCASURI))
oh := txnprovider.NewOperationHandler(p, casClient, cp, opParser, metrics.Get())
dc := doccomposer.New()
oa := operationapplier.New(p, opParser, dc)
dv := didvalidator.New()
dt := didtransformer.New(
didtransformer.WithMethodContext(sidetreeCfg.MethodContext),
didtransformer.WithBase(sidetreeCfg.EnableBase),
didtransformer.WithIncludePublishedOperations(sidetreeCfg.IncludePublishedOperations),
didtransformer.WithIncludeUnpublishedOperations(sidetreeCfg.IncludeUnpublishedOperations))
var orbTxnProcessorOpts []txnprocessor.Option
if sidetreeCfg.UnpublishedOpStore != nil {
orbTxnProcessorOpts = append(orbTxnProcessorOpts,
txnprocessor.WithUnpublishedOperationStore(sidetreeCfg.UnpublishedOpStore,
sidetreeCfg.UnpublishedOperationStoreOperationTypes))
}
orbTxnProcessor := txnprocessor.New(
&txnprocessor.Providers{
OpStore: opStore,
OperationProtocolProvider: op,
},
orbTxnProcessorOpts...,
)
return &vcommon.ProtocolVersion{
VersionStr: version,
P: p,
TxnProcessor: orbTxnProcessor,
OpParser: orbParser,
OpApplier: oa,
DocComposer: dc,
OpHandler: oh,
OpProvider: op,
DocValidator: dv,
DocTransformer: dt,
}, nil
}
type casReader struct {
resolver ctxcommon.CASResolver
}
func (c *casReader) Read(cid string) ([]byte, error) {
data, _, err := c.resolver.Resolve(nil, cid, nil)
if err != nil {
return nil, fmt.Errorf("failed to resolve CID: %w", err)
}
return data, nil
}
func formatWebCASURI(uri, serviceURI string) (string, error) {
// A CAS URI can either be a CID or a hashlink.
hash, err := hashlink.GetResourceHashFromHashLink(uri)
if err != nil {
logger.Debugf("CAS URI [%s] is not a hashlink: %s. Assuming that it's a plain hash.", uri, err)
hash = uri
}
// A serviceURI URI looks like this: https://orb.domain1.com/services/orb.
parts := strings.Split(serviceURI, ":")
scheme := parts[0]
host := strings.Split(parts[1][2:], "/")[0]
// The WebCAS URI will look like this: https:orb.domain1.com:<hash>.
casURI := fmt.Sprintf("%s:%s:%s", scheme, host, hash)
logger.Debugf("Adding alternate CAS URI: %s", casURI)
return casURI, nil
}