Skip to content

Commit

Permalink
Genesis from ethereum address (#81)
Browse files Browse the repository at this point in the history
* Add new function PLGNNewGenesisIDFromEth

* bump go versions

* add missing files
  • Loading branch information
olomix authored Jul 18, 2024
1 parent c307e48 commit e1f8ae0
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 40 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: 1.22.3
- uses: golangci/golangci-lint-action@v3
go-version: 1.22.5
- uses: golangci/golangci-lint-action@v6
with:
version: v1.58.1
version: v1.59.1
6 changes: 3 additions & 3 deletions .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.22.3-bookworm
- 1.22.5-bookworm
runs-on: ubuntu-22.04
container: golang:${{matrix.containers}}
steps:
Expand Down Expand Up @@ -46,9 +46,9 @@ jobs:
if: github.event_name == 'push' || github.event_name == 'release'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: 1.22.3
go-version: 1.22.5
- uses: actions/cache@v4
with:
path: |
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.22.3-bookworm
- 1.22.5-bookworm
runs-on: ubuntu-20.04
container: golang:${{ matrix.containers }}
env:
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ After each build target there will be an `ios/libpolygonid.h` header file.

## Run tests

The library build be built with previous command.
The library build be built with previous command. And copy it to `ios/libpolygonid.a` file.
For example.
```shell
make darwin-arm64
cp ios/libpolygonid-darwin-arm64.a ios/libpolygonid.a
```

Then you can build and run tests.

```shell
mkdir examples/build
Expand Down
96 changes: 65 additions & 31 deletions cmd/polygonid/polygonid.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,36 +254,16 @@ func PLGNCalculateGenesisID(jsonResponse **C.char, in *C.char,
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
}

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

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
}
return callGenericFn(c_polygonid.NewGenesysID, jsonResponse, in, cfg,
status)
}

respB, err := json.Marshal(resp)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}
//export PLGNNewGenesisIDFromEth
func PLGNNewGenesisIDFromEth(jsonResponse **C.char, in *C.char, cfg *C.char,
status **C.PLGNStatus) bool {

*jsonResponse = C.CString(string(respB))
return true
return callGenericFn(c_polygonid.NewGenesysIDFromEth, jsonResponse, in, cfg,
status)
}

//export PLGNCreateClaim
Expand Down Expand Up @@ -1027,6 +1007,43 @@ func prepareInputs(ctx context.Context, fn atomicQueryInputsFn,
return true
}

func callGenericFn[R any](
fn func(context.Context, c_polygonid.EnvConfig, []byte) (R, error),
jsonResponse **C.char, in *C.char, cfg *C.char,
status **C.PLGNStatus) bool {

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

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

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

inStr := C.GoString(in)
resp, err := fn(ctx, envCfg, []byte(inStr))
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

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

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

const debug = false
const tracing = false

Expand All @@ -1042,15 +1059,32 @@ func getFuncName(skip int) string {
return parts[len(parts)-1]
}

func logAPITime() (context.Context, func()) {
type logAPITimeOptions struct {
skipMoreFrames int
}

func withSkipFrames(skip int) logAPITimeOption {
return func(o *logAPITimeOptions) {
o.skipMoreFrames = skip
}
}

type logAPITimeOption func(*logAPITimeOptions)

func logAPITime(optFns ...logAPITimeOption) (context.Context, func()) {
ctx := context.Background()

if !debug && !tracing {
return ctx, func() {}
}

var opts logAPITimeOptions
for _, optFn := range optFns {
optFn(&opts)
}

var taskCanceler interface{ End() }
funcName := getFuncName(2)
funcName := getFuncName(2 + opts.skipMoreFrames)

if tracing {
ctx, taskCanceler = trace.NewTask(ctx, funcName)
Expand All @@ -1066,7 +1100,7 @@ func logAPITime() (context.Context, func()) {
taskCanceler.End()
}

if start.IsZero() {
if !start.IsZero() {
fmt.Printf("[%v] API call took %v\n", funcName,
time.Since(start))
}
Expand Down
6 changes: 6 additions & 0 deletions examples/json_functions_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ TEST testCases[] = {
.cfg = "testdata/new_genesis_id_cfg.json",
.out = "testdata/new_genesis_id_out.json",
.fn2 = &PLGNNewGenesisID
},
{
.in = "testdata/new_genesis_id_from_eth_in.json",
.cfg = "testdata/new_genesis_id_cfg.json",
.out = "testdata/new_genesis_id_from_eth_out.json",
.fn2 = &PLGNNewGenesisIDFromEth
}
// timestamp is different on each call, so we can't just compare output for equality
// this test is failed because ec2-34-243-185-133.eu-west-1.compute.amazonaws.com:8888 is down
Expand Down
6 changes: 6 additions & 0 deletions examples/testdata/new_genesis_id_from_eth_in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ethAddress":"0x850174569F4FeDEdFFF12F112602d3FDAcb9e21B",
"blockchain":"polygon",
"network":"mumbai",
"method":"polygonid"
}
5 changes: 5 additions & 0 deletions examples/testdata/new_genesis_id_from_eth_out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"did": "did:polygonid:polygon:mumbai:2qCU58EJgrELzewx3jhSWLDDdunCaWCAuKVgz7GmyK",
"id": "2qCU58EJgrELzewx3jhSWLDDdunCaWCAuKVgz7GmyK",
"idAsInt": "18925340278420228466712879433563154448903652530982176890458034425491886594"
}
65 changes: 65 additions & 0 deletions inputs_sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,71 @@ func NewGenesysID(ctx context.Context, cfg EnvConfig,
nil
}

func NewGenesysIDFromEth(ctx context.Context, cfg EnvConfig,
in []byte) (GenesysIDResponse, error) {

var req struct {
EthAddr *common.Address `json:"ethAddress"`
Blockchain *core.Blockchain `json:"blockchain"`
Network *core.NetworkID `json:"network"`
Method *core.DIDMethod `json:"method"`
}

if in == nil {
return GenesysIDResponse{}, errors.New("request is empty")
}

err := json.Unmarshal(in, &req)
if err != nil {
return GenesysIDResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}

if req.EthAddr == nil {
return GenesysIDResponse{},
errors.New("ethereum address is not set in the request")
}

if req.Blockchain == nil {
return GenesysIDResponse{},
errors.New("blockchain is not set in the request")
}

if req.Network == nil {
return GenesysIDResponse{},
errors.New("network is not set in the request")
}

if req.Method == nil {
// for backward compatibility, if method is not set, use polygon
var m = core.DIDMethodPolygonID
req.Method = &m
}

typ, err := core.BuildDIDType(*req.Method, *req.Blockchain,
*req.Network)
if err != nil {
return GenesysIDResponse{},
fmt.Errorf("failed to build DID type: %w", err)
}

genesis := core.GenesisFromEthAddress(*req.EthAddr)
coreID := core.NewID(typ, genesis)

did, err := core.ParseDIDFromID(coreID)
if err != nil {
return GenesysIDResponse{},
fmt.Errorf("failed to make DID from ID: %w", err)
}

return GenesysIDResponse{
DID: did.String(),
ID: coreID.String(),
IDAsInt: coreID.BigInt().String(),
},
nil
}

type DescribeIDResponse struct {
DID string `json:"did"`
ID string `json:"id"`
Expand Down
20 changes: 20 additions & 0 deletions inputs_sig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,23 @@ func TestMkVPObj(t *testing.T) {
})
}
}

func TestNewGenesysIDFromEth(t *testing.T) {
in := `{
"ethAddress":"0x850174569F4FeDEdFFF12F112602d3FDAcb9e21B",
"blockchain":"polygon",
"network":"mumbai",
"method":"polygonid"
}`

ctx := context.Background()

resp, err := NewGenesysIDFromEth(ctx, EnvConfig{}, []byte(in))
require.NoError(t, err)
wantResp := GenesysIDResponse{
DID: "did:polygonid:polygon:mumbai:2qCU58EJgrELzewx3jhSWLDDdunCaWCAuKVgz7GmyK",
ID: "2qCU58EJgrELzewx3jhSWLDDdunCaWCAuKVgz7GmyK",
IDAsInt: "18925340278420228466712879433563154448903652530982176890458034425491886594",
}
require.Equal(t, wantResp, resp)
}

0 comments on commit e1f8ae0

Please sign in to comment.