Skip to content

Commit

Permalink
support go-onchain-credential-adapter (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-korotya authored Feb 27, 2024
1 parent e420c85 commit bd36fc2
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-c.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
containers:
- 1.21.1-bullseye
- 1.21.4-bullseye
runs-on: ubuntu-22.04
container: golang:${{matrix.containers}}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
containers:
- 1.21.1-bullseye
- 1.21.4-bullseye
runs-on: ubuntu-20.04
container: golang:${{ matrix.containers }}
env:
Expand Down
96 changes: 72 additions & 24 deletions cmd/polygonid/polygonid.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,9 @@ func PLGNProfileID(jsonResponse **C.char, in *C.char,
// Additional configuration may be required for Reverse Hash Service
// revocation validation. In other case cfg may be nil.
//
// Sample configuration:
// The configuration example may be found in the [README.md] file.
//
// {
// "ethereumUrl": "http://localhost:8545",
// "stateContractAddr": "0xEA9aF2088B4a9770fC32A12fD42E61BDD317E655",
// "reverseHashServiceUrl": "http://localhost:8003"
// }
// [README.md]: https://github.com/0xPolygonID/c-polygonid/blob/main/README.md#configuration
//
//export PLGNAtomicQuerySigV2Inputs
func PLGNAtomicQuerySigV2Inputs(jsonResponse **C.char, in *C.char, cfg *C.char,
Expand Down Expand Up @@ -667,13 +663,9 @@ func marshalInputsResponse(
// Additional configuration may be required for Reverse Hash Service
// revocation validation. In other case cfg may be nil.
//
// Sample configuration:
// The configuration example may be found in the [README.md] file.
//
// {
// "ethereumUrl": "http://localhost:8545",
// "stateContractAddr": "0xEA9aF2088B4a9770fC32A12fD42E61BDD317E655",
// "reverseHashServiceUrl": "http://localhost:8003"
// }
// [README.md]: https://github.com/0xPolygonID/c-polygonid/blob/main/README.md#configuration
//
//export PLGNAtomicQueryMtpV2Inputs
func PLGNAtomicQueryMtpV2Inputs(jsonResponse **C.char, in *C.char, cfg *C.char,
Expand Down Expand Up @@ -734,13 +726,9 @@ func PLGNMtpV2Inputs(jsonResponse **C.char, in *C.char,
// Additional configuration may be required for Reverse Hash Service
// revocation validation. In other case cfg may be nil.
//
// Sample configuration:
// The configuration example may be found in the [README.md] file.
//
// {
// "ethereumUrl": "http://localhost:8545",
// "stateContractAddr": "0xEA9aF2088B4a9770fC32A12fD42E61BDD317E655",
// "reverseHashServiceUrl": "http://localhost:8003"
// }
// [README.md]: https://github.com/0xPolygonID/c-polygonid/blob/main/README.md#configuration
//
//export PLGNAtomicQuerySigV2OnChainInputs
func PLGNAtomicQuerySigV2OnChainInputs(jsonResponse **C.char, in *C.char,
Expand All @@ -759,13 +747,9 @@ func PLGNAtomicQuerySigV2OnChainInputs(jsonResponse **C.char, in *C.char,
// Additional configuration may be required for Reverse Hash Service
// revocation validation. In other case cfg may be nil.
//
// Sample configuration:
// The configuration example may be found in the [README.md] file.
//
// {
// "ethereumUrl": "http://localhost:8545",
// "stateContractAddr": "0xEA9aF2088B4a9770fC32A12fD42E61BDD317E655",
// "reverseHashServiceUrl": "http://localhost:8003"
// }
// [README.md]: https://github.com/0xPolygonID/c-polygonid/blob/main/README.md#configuration
//
//export PLGNAtomicQueryMtpV2OnChainInputs
func PLGNAtomicQueryMtpV2OnChainInputs(jsonResponse **C.char, in *C.char,
Expand Down Expand Up @@ -861,6 +845,70 @@ func PLGNCacheCredentials(in *C.char, cfg *C.char, status **C.PLGNStatus) bool {
return true
}

// PLGNW3CCredentialFromOnchainHex returns a verifiable credential from an onchain data hex string.
//
// Sample input:
//
// {
// "issuerDID": "did:polygonid:polygon:mumbai:2qCU58EJgrEMJvPfhUCnFCwuKQTkX8VmJX2sJCH6C8",
// "hexdata": "0x0...",
// "version": "0.0.1"
// }
//
// The configuration example may be found in the [README.md] file.
//
// [README.md]: https://github.com/0xPolygonID/c-polygonid/blob/main/README.md#configuration
//
//export PLGNW3CCredentialFromOnchainHex
func PLGNW3CCredentialFromOnchainHex(jsonResponse **C.char, in *C.char,
cfg *C.char, status **C.PLGNStatus) bool {

if jsonResponse == nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_NIL_POINTER,
"jsonResponse pointer is nil")
return false
}

if in == nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_NIL_POINTER,
"pointer to request is nil")
return false
}

ctx, cancel := logAPITime()
defer cancel()

ctx2, cancel2 := context.WithTimeout(ctx, defaultTimeout)
defer cancel2()

inData := C.GoBytes(unsafe.Pointer(in), C.int(C.strlen(in)))

envCfg, err := createEnvConfig(cfg)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

credential, err := c_polygonid.W3CCredentialFromOnchainHex(
ctx2,
envCfg,
inData,
)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

credentialJSON, err := json.Marshal(credential)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

*jsonResponse = C.CString(string(credentialJSON))
return true
}

// createEnvConfig returns empty config if input json is nil.
func createEnvConfig(cfgJson *C.char) (c_polygonid.EnvConfig, error) {
var cfgData []byte
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/0xPolygonID/c-polygonid

go 1.21
go 1.21.4

require (
github.com/dgraph-io/badger/v4 v4.2.0
github.com/ethereum/go-ethereum v1.13.11
github.com/iden3/contracts-abi/state/go/abi v0.0.0-20230405152923-4a25f6f1f0f4
github.com/ethereum/go-ethereum v1.13.12
github.com/iden3/go-circuits/v2 v2.0.1
github.com/iden3/go-iden3-core/v2 v2.0.4-0.20240129182142-32180363999b
github.com/iden3/go-iden3-core/v2 v2.0.4
github.com/iden3/go-iden3-crypto v0.0.15
github.com/iden3/go-merkletree-sql/v2 v2.0.6
github.com/iden3/go-onchain-credential-adapter v0.0.0-20240223120548-5d8d1d28c6d1
github.com/iden3/go-schema-processor/v2 v2.3.0
github.com/iden3/merkletree-proof v0.1.0
github.com/piprate/json-gold v0.5.1-0.20230111113000-6ddbe6e6f19f
Expand Down Expand Up @@ -44,6 +44,8 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/iden3/contracts-abi/onchain-credential-status-resolver/go/abi v0.0.0-20230911113809-c58b7e7a69b0 // indirect
github.com/iden3/contracts-abi/onchain-non-merklized-issuer-base/v0/go/abi v0.0.0-20240222110004-06aa097d1318 // indirect
github.com/iden3/contracts-abi/state/go/abi v0.0.0-20230405152923-4a25f6f1f0f4 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R3nlY=
github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-ethereum v1.13.11 h1:b51Dsm+rEg7anFRUMGB8hODXHvNfcRKzz9vcj8wSdUs=
github.com/ethereum/go-ethereum v1.13.11/go.mod h1:gFtlVORuUcT+UUIcJ/veCNjkuOSujCi338uSHJrYAew=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/ethereum/go-ethereum v1.13.12 h1:iDr9UM2JWkngBHGovRJEQn4Kor7mT4gt9rUZqB5M29Y=
github.com/ethereum/go-ethereum v1.13.12/go.mod h1:hKL2Qcj1OvStXNSEDbucexqnEt1Wh4Cz329XsjAalZY=
github.com/fjl/memsize v0.0.2 h1:27txuSD9or+NZlnOWdKUxeBzTAUkWCVh+4Gf2dWFOzA=
github.com/fjl/memsize v0.0.2/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
Expand Down Expand Up @@ -156,16 +156,20 @@ github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/iden3/contracts-abi/onchain-credential-status-resolver/go/abi v0.0.0-20230911113809-c58b7e7a69b0 h1:Fu1/tAINi9FzZ0nKoEVOGXWzL1l15tR1loJx5sQEQ8k=
github.com/iden3/contracts-abi/onchain-credential-status-resolver/go/abi v0.0.0-20230911113809-c58b7e7a69b0/go.mod h1:8fkd2xyUG/V7ovpvZRyD2LyK2zZ4ALbgf5vJGyhzKdg=
github.com/iden3/contracts-abi/onchain-non-merklized-issuer-base/v0/go/abi v0.0.0-20240222110004-06aa097d1318 h1:uCDKrWlaN4WT5Yrbjc3DxwMxQsWor26w9Q0polWU4PE=
github.com/iden3/contracts-abi/onchain-non-merklized-issuer-base/v0/go/abi v0.0.0-20240222110004-06aa097d1318/go.mod h1:vMgeBNntJeIDZrIDY0d6sZQU+JigUo4h+m95qeU03ew=
github.com/iden3/contracts-abi/state/go/abi v0.0.0-20230405152923-4a25f6f1f0f4 h1:iPvYa/AhhGo3juoUFDm/fBE2CZKy4WfQu7JY90tRf9Q=
github.com/iden3/contracts-abi/state/go/abi v0.0.0-20230405152923-4a25f6f1f0f4/go.mod h1:TxgIrXCvxms3sbOdsy8kTvffUCIpEEifNy0fSXdkU4w=
github.com/iden3/go-circuits/v2 v2.0.1 h1:tcJtBE8aLJsf9qpBoTUKE143Mne025cunQnSExMXaKo=
github.com/iden3/go-circuits/v2 v2.0.1/go.mod h1:VIFIp51+IH0hOzjnKhb84bCeyq7hq76zX/C14ua6zh4=
github.com/iden3/go-iden3-core/v2 v2.0.4-0.20240129182142-32180363999b h1:9tdFuHcaCiHy7xehrY6bIWlSPlPZH8H7jYU3pFvWIP0=
github.com/iden3/go-iden3-core/v2 v2.0.4-0.20240129182142-32180363999b/go.mod h1:L9PxhWPvoS9qTb3inEkZBm1RpjHBt+VTwvxssdzbAdw=
github.com/iden3/go-iden3-core/v2 v2.0.4 h1:ggzC2zgOWgJAAcuG9X8bQG1r4gAoHZWqY7aLV8b1qgc=
github.com/iden3/go-iden3-core/v2 v2.0.4/go.mod h1:L9PxhWPvoS9qTb3inEkZBm1RpjHBt+VTwvxssdzbAdw=
github.com/iden3/go-iden3-crypto v0.0.15 h1:4MJYlrot1l31Fzlo2sF56u7EVFeHHJkxGXXZCtESgK4=
github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/iden3/go-merkletree-sql/v2 v2.0.6 h1:vsVDImnvnHf7Ggr45ptFOXJyWNA/8IwVQO1jzRLUlY8=
github.com/iden3/go-merkletree-sql/v2 v2.0.6/go.mod h1:kRhHKYpui5DUsry5RpveP6IC4XMe6iApdV9VChRYuEk=
github.com/iden3/go-onchain-credential-adapter v0.0.0-20240223120548-5d8d1d28c6d1 h1:xpKhrLYi0s6ocUCrFwkjG0YQjndDGMW7/cgU9KjLyig=
github.com/iden3/go-onchain-credential-adapter v0.0.0-20240223120548-5d8d1d28c6d1/go.mod h1:1RVClY0JdWmzgx3ZBvy0Z2bNyPg2iz+0R9Ryl2Z3s64=
github.com/iden3/go-schema-processor/v2 v2.3.0 h1:86tnt1myHntcG+9pJ3I+0ycc6V59sITrsPSt0k7/DhU=
github.com/iden3/go-schema-processor/v2 v2.3.0/go.mod h1:BcHVDZyn8q8vUlL+XpOo7hpwXmEjxzO8ao1LkvFsM+k=
github.com/iden3/merkletree-proof v0.1.0 h1:AHmpkbCTLKv1MWWt6YwogB65E4y6UZthbZYPed81w4U=
Expand Down
32 changes: 18 additions & 14 deletions inputs_sig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ func preserveIPFSHttpCli() func() {
}
}

func removeIdFromEthBody(body []byte) []byte {
var ethBody map[string]any
err := json.Unmarshal(body, &ethBody)
if err != nil {
panic(err)
}
if stringFromJsonObj(ethBody, "jsonrpc") == "2.0" &&
stringFromJsonObj(ethBody, "method") == "eth_call" {

delete(ethBody, "id")
}
body, err = json.Marshal(ethBody)
if err != nil {
panic(err)
}
return body
}

func TestPrepareInputs(t *testing.T) {
defer mockBadgerLog(t)()

Expand Down Expand Up @@ -88,20 +106,6 @@ func TestPrepareInputs(t *testing.T) {
}
}

removeIdFromEthBody := func(body []byte) []byte {
var ethBody map[string]any
err := json.Unmarshal(body, &ethBody)
require.NoError(t, err)
if stringFromJsonObj(ethBody, "jsonrpc") == "2.0" &&
stringFromJsonObj(ethBody, "method") == "eth_call" {

delete(ethBody, "id")
}
body, err = json.Marshal(ethBody)
require.NoError(t, err)
return body
}

t.Run("AtomicQueryMtpV2Onchain", func(t *testing.T) {
defer httpmock.MockHTTPClient(t,
map[string]string{
Expand Down
67 changes: 67 additions & 0 deletions onchain_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Loading

0 comments on commit bd36fc2

Please sign in to comment.