Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support go-onchain-credential-adapter #57

Merged
merged 24 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c268244
Add didMethods config option to register custom DID methods
olomix Jan 29, 2024
23c29eb
Get rid of custom ChainID type and knownChainIDs map. Use the ones fr…
olomix Feb 2, 2024
b848380
Add tests for custom DID methods registration and do refactorings bas…
olomix Feb 3, 2024
8dd29f6
Resolve recocation status using go-schema-processor CredentialStatusR…
olomix Feb 15, 2024
430371d
Supress badger logging in tests
olomix Feb 16, 2024
118fc6b
Upgrade GitHub action version to suppress warnings
olomix Feb 16, 2024
c49a53b
Migrate to local ipfs node instead of from secrets (#56)
olomix Feb 16, 2024
1e0d12c
Add a new function PLGNNewGenesisID similar to PLGNCalculateGenesisID…
olomix Feb 21, 2024
585ee1d
fix test
olomix Feb 21, 2024
b04721c
Add deprecation comment to PLGNCalculateGenesisID
olomix Feb 21, 2024
358e688
Merge branch 'register_chain' into migrate_core_proof
olomix Feb 21, 2024
1ab6004
fix merge issuers
olomix Feb 21, 2024
4c36446
check if somebody uses old fields in configuration and return an error
olomix Feb 21, 2024
26d2f55
fix typing
olomix Feb 21, 2024
d033030
fix TestNewEnvConfigFromJSON test
olomix Feb 21, 2024
c1912aa
fix TestEnvConfig_UnmarshalJSON test
olomix Feb 21, 2024
a27536e
Fix handling error in PLGNNewGenesisID function
olomix Feb 22, 2024
c71a923
support go-onchain-credential-adapter
ilya-korotya Feb 23, 2024
88a00a3
dump golang version
ilya-korotya Feb 23, 2024
406e224
fix comments
ilya-korotya Feb 26, 2024
84a2e56
add input and config samples for PLGNW3CCredentialFromOnchainHex method
ilya-korotya Feb 26, 2024
e013f28
Remove legacy configuration options from doc comments
olomix Feb 26, 2024
1aa987a
fix docstring links
olomix Feb 26, 2024
3866a9c
merged main
olomix Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.21.1
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests-c.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
strategy:
matrix:
containers:
- 1.21.1-bullseye
- 1.21.4-bullseye
runs-on: ubuntu-22.04
container: golang:${{matrix.containers}}
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/go-build
Expand All @@ -45,11 +45,11 @@ jobs:
runs-on: macos-12
if: github.event_name == 'push' || github.event_name == 'release'
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: 1.21.1
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
Expand Down
15 changes: 10 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ jobs:
strategy:
matrix:
containers:
- 1.21.1-bullseye
- 1.21.4-bullseye
runs-on: ubuntu-20.04
container: golang:${{ matrix.containers }}
env:
IPFS_URL: ${{secrets.IPFS_URL }}
IPFS_URL: http://ipfs:5001
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/go-build
/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: go test -v -race -timeout=60s -find-unused-httpresp ./...
- run: go test -race -timeout=60s -find-unused-httpresp ./...
services:
ipfs:
image: ipfs/kubo:v0.26.0
ports:
- 5001:5001
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ The configuration object is a JSON document with the following structure:
```json5
{
"ipfsNodeUrl": "http://localhost:5001", // IPFS Node URL
"didMethods": [
{
"name": "ethr", // DID method name
"blockchain": "ethereum", // Blockchain name
"network": "mainnet", // Network name
"networkFlag": 6, // Network flag
"methodByte": "0b010011", // Method byte
"chainID": "10293"
}
],
"chainConfigs": {
"1": { // Chain ID as decimal
"rpcUrl": "http://localhost:8545", // RPC URL
Expand Down
1 change: 1 addition & 0 deletions badger_cache_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestGetPubRemoteDocument(t *testing.T) {
defer mockBadgerLog(t)()
flushCacheDB()

cacheEng, err := newBadgerCacheEngine()
Expand Down
5 changes: 5 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/dgraph-io/badger/v4"
)

var badgerLogger badger.Logger = nil

var globalDB *badger.DB
var dbCnt int
var dbCond = sync.NewCond(&sync.Mutex{})
Expand Down Expand Up @@ -99,6 +101,9 @@ func openDB() (*badger.DB, error) {
}

opts := badger.DefaultOptions(badgerPath)
if badgerLogger != nil {
opts.Logger = badgerLogger
}

return badger.Open(opts)
}
Expand Down
46 changes: 46 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ func doWithBadger(t testing.TB, db *badger.DB) {
require.NoError(t, err)
}

type testBadgerLogger struct {
t testing.TB
silent bool
}

func (t *testBadgerLogger) Errorf(s string, i ...interface{}) {
if t.silent {
return
}
t.t.Errorf("[BADGER ERROR]"+s, i...)
}

func (t *testBadgerLogger) Warningf(s string, i ...interface{}) {
if t.silent {
return
}
t.t.Logf("[BADGER WARNING]"+s, i...)
}

func (t *testBadgerLogger) Infof(s string, i ...interface{}) {
if t.silent {
return
}
t.t.Logf("[BADGER INFO]"+s, i...)
}

func (t *testBadgerLogger) Debugf(s string, i ...interface{}) {
if t.silent {
return
}
t.t.Logf("[BADGER DEBUG]"+s, i...)
}

func mockBadgerLog(t testing.TB) func() {
orig := badgerLogger
badgerLogger = &testBadgerLogger{t, true}
return func() {
badgerLogger = orig
}
}

func BenchmarkBadgerWithOpening(b *testing.B) {
for i := 0; i < b.N; i++ {
func() {
Expand All @@ -58,6 +99,8 @@ func BenchmarkBadgerWithoutOpening(b *testing.B) {
}

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

dbPath, err := getBadgerPath()
require.NoError(t, err)

Expand All @@ -71,6 +114,7 @@ func TestBadger(t *testing.T) {
}

func TestGetCacheDB(t *testing.T) {
defer mockBadgerLog(t)()
db1, close1, err := getCacheDB()
require.NoError(t, err)
db2, close2, err := getCacheDB()
Expand Down Expand Up @@ -135,6 +179,7 @@ func set(db *badger.DB, key string, value string) {
}

func TestCleanCache(t *testing.T) {
defer mockBadgerLog(t)()
db1, close1, err := getCacheDB()
require.NoError(t, err)

Expand Down Expand Up @@ -169,6 +214,7 @@ func TestCleanCache(t *testing.T) {
}

func TestMultipleCleanup(t *testing.T) {
defer mockBadgerLog(t)()
_, close1, err := getCacheDB()
require.NoError(t, err)

Expand Down
118 changes: 88 additions & 30 deletions cmd/polygonid/polygonid.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,66 +216,66 @@ func PLGNAuthV2InputsMarshal(jsonResponse **C.char, in *C.char,
return true
}

// Deprecated: Use PLGNNewGenesisID instead. It supports environment
// configuration, giving the ability to register custom DID methods.
//
//export PLGNCalculateGenesisID
func PLGNCalculateGenesisID(jsonResponse **C.char, in *C.char,
status **C.PLGNStatus) bool {

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

var req struct {
ClaimsTreeRoot *jsonIntStr `json:"claimsTreeRoot"`
Blockchain core.Blockchain `json:"blockchain"`
Network core.NetworkID `json:"network"`
}

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

err := json.Unmarshal([]byte(C.GoString(in)), &req)
inStr := C.GoString(in)
resp, err := c_polygonid.NewGenesysID(ctx, c_polygonid.EnvConfig{},
[]byte(inStr))
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

state, err := merkletree.HashElems(req.ClaimsTreeRoot.Int(),
merkletree.HashZero.BigInt(), merkletree.HashZero.BigInt())
respB, err := json.Marshal(resp)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

typ, err := core.BuildDIDType(core.DIDMethodPolygonID, req.Blockchain,
req.Network)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
*jsonResponse = C.CString(string(respB))
return true
}

//export PLGNNewGenesisID
func PLGNNewGenesisID(jsonResponse **C.char, in *C.char, cfg *C.char,
status **C.PLGNStatus) bool {

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

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

coreID, err := core.NewIDFromIdenState(typ, state.BigInt())
envCfg, err := createEnvConfig(cfg)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

did, err := core.ParseDIDFromID(*coreID)
inStr := C.GoString(in)
resp, err := c_polygonid.NewGenesysID(ctx, envCfg, []byte(inStr))
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

resp := struct {
DID string `json:"did"`
ID string `json:"id"`
IDAsInt string `json:"idAsInt"`
}{
DID: did.String(),
ID: coreID.String(),
IDAsInt: coreID.BigInt().String(),
}
respB, err := json.Marshal(resp)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
Expand Down Expand Up @@ -861,15 +861,73 @@ 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 configuration:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's an input, not a configuration

//
// {
// "issuerDID": "did:polygonid:polygon:mumbai:2qCU58EJgrEMJvPfhUCnFCwuKQTkX8VmJX2sJCH6C8",
// "hexdata": "0x0...",
// "version": "0.0.1"
// }
//
//export PLGNW3CCredentialFromOnchainHex
func PLGNW3CCredentialFromOnchainHex(jsonResponse **C.char, in *C.char,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a documentation to this function?

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 cfg c_polygonid.EnvConfig
var err error
var cfgData []byte
if cfgJson != nil {
cfgData := C.GoBytes(unsafe.Pointer(cfgJson), C.int(C.strlen(cfgJson)))
err = json.Unmarshal(cfgData, &cfg)
cfgData = C.GoBytes(unsafe.Pointer(cfgJson), C.int(C.strlen(cfgJson)))
}
return cfg, err
return c_polygonid.NewEnvConfigFromJSON(cfgData)
}

type atomicQueryInputsFn func(ctx context.Context, cfg c_polygonid.EnvConfig,
Expand Down
Loading
Loading