-
Notifications
You must be signed in to change notification settings - Fork 2
/
onchain_adapter.go
67 lines (59 loc) · 1.66 KB
/
onchain_adapter.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
package c_polygonid
import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
convertor "github.com/iden3/go-onchain-credential-adapter"
"github.com/iden3/go-schema-processor/v2/merklize"
"github.com/iden3/go-schema-processor/v2/verifiable"
)
type w3CCredentialFromOnchainHexRequest struct {
IssuerDID coreDID `json:"issuerDID"`
Hexdata string `json:"hexdata"`
Version string `json:"version"`
}
func W3CCredentialFromOnchainHex(
ctx context.Context,
envCfg EnvConfig,
in []byte,
) (*verifiable.W3CCredential, error) {
var inParams w3CCredentialFromOnchainHexRequest
if err := json.Unmarshal(in, &inParams); err != nil {
return nil, fmt.Errorf("failed to unmarshal input params: %w", err)
}
issuerDID := w3c.DID(inParams.IssuerDID)
chainID, err := core.ChainIDfromDID(issuerDID)
if err != nil {
return nil, fmt.Errorf("failed to get chain id from issuer: %w", err)
}
chainConfig, ok := envCfg.ChainConfigs[chainID]
if !ok {
return nil, fmt.Errorf("chain id '%d' not found in config", chainID)
}
ethcli, err := ethclient.DialContext(ctx, chainConfig.RPCUrl)
if err != nil {
return nil,
fmt.Errorf("failed to connect to ethereum: %w", err)
}
defer ethcli.Close()
credential, err := convertor.W3CCredentialFromOnchainHex(
ctx,
ethcli,
&issuerDID,
inParams.Hexdata,
inParams.Version,
convertor.WithMerklizeOptions(
merklize.Options{
DocumentLoader: envCfg.documentLoader(),
},
),
)
if err != nil {
return nil,
fmt.Errorf("failed to convert onchain hex to W3C credential: %w", err)
}
return credential, nil
}