Skip to content

Commit

Permalink
update eip712 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Aug 20, 2024
1 parent eb6b0fb commit 510a477
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 127 deletions.
109 changes: 39 additions & 70 deletions pkg/services/blockchain/eth/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ var (

var IdentityStateAPITypes = apitypes.Types{
"IdentityState": []apitypes.Type{
{Name: "from", Type: "address"},
{Name: "timestamp", Type: "uint256"},
{Name: "identity", Type: "uint256"},
{Name: "userID", Type: "uint256"},
{Name: "state", Type: "uint256"},
{Name: "replacedByState", Type: "uint256"},
{Name: "createdAtTimestamp", Type: "uint256"},
{Name: "replacedAtTimestamp", Type: "uint256"},
},
"EIP712Domain": []apitypes.Type{
Expand All @@ -74,11 +71,9 @@ var IdentityStateAPITypes = apitypes.Types{

var GlobalStateAPITypes = apitypes.Types{
"GlobalState": []apitypes.Type{
{Name: "from", Type: "address"},
{Name: "timestamp", Type: "uint256"},
{Name: "userID", Type: "uint256"},
{Name: "root", Type: "uint256"},
{Name: "replacedByRoot", Type: "uint256"},
{Name: "createdAtTimestamp", Type: "uint256"},
{Name: "replacedAtTimestamp", Type: "uint256"},
},
"EIP712Domain": []apitypes.Type{
Expand Down Expand Up @@ -185,52 +180,41 @@ func (r *Resolver) Resolve(
err error
)

if did.IDStrings[2] == "000000000000000000000000000000000000000000" {
// If gist root is not provided, it will try to resolve with gist root 0
if opts.GistRoot == nil {
opts.GistRoot = big.NewInt(0)
}
gistInfo, err = r.resolveGistRootOnly(ctx, opts.GistRoot)
userID, err := core.IDFromDID(did)
if err != nil {
return services.IdentityState{},
fmt.Errorf("invalid did format for did '%s': %v", did, err)
}

switch {
case opts.GistRoot != nil:
stateInfo, gistInfo, err = r.resolveStateByGistRoot(ctx, userID, opts.GistRoot)
case opts.State != nil:
stateInfo, err = r.resolveState(ctx, userID, opts.State)
default:
stateInfo, gistInfo, err = r.resolveLatest(ctx, userID)
}
// err == http.505
if err != nil && (!errors.Is(err, services.ErrNotFound) || opts.GistRoot != nil) {
return services.IdentityState{}, err
}

if opts.State != nil && errors.Is(err, services.ErrNotFound) {
idGen, err := core.CheckGenesisStateID(userID.BigInt(), opts.State)
if err != nil {
return services.IdentityState{}, err
}
} else {
userID, err := core.IDFromDID(did)
if err != nil {
if !idGen {
return services.IdentityState{},
fmt.Errorf("invalid did format for did '%s': %v", did, err)
fmt.Errorf("identity '%s' state '%s' is not found and is not genesis", userID.String(), opts.State)
}

switch {
case opts.GistRoot != nil:
stateInfo, gistInfo, err = r.resolveStateByGistRoot(ctx, userID, opts.GistRoot)
case opts.State != nil:
stateInfo, err = r.resolveState(ctx, userID, opts.State)
default:
stateInfo, gistInfo, err = r.resolveLatest(ctx, userID)
}
// err == http.505
if err != nil && !errors.Is(err, services.ErrNotFound) {
return services.IdentityState{}, err
}

if opts.State != nil && errors.Is(err, services.ErrNotFound) {
idGen, err := core.CheckGenesisStateID(userID.BigInt(), opts.State)
if err != nil {
return services.IdentityState{}, err
}
if !idGen {
return services.IdentityState{},
fmt.Errorf("identity '%s' state '%s' is not found and is not genesis", userID.String(), opts.State)
}
stateInfo = &contract.IStateStateInfo{
State: opts.State,
ReplacedByState: big.NewInt(0),
CreatedAtTimestamp: big.NewInt(0),
ReplacedAtTimestamp: big.NewInt(0),
CreatedAtBlock: big.NewInt(0),
ReplacedAtBlock: big.NewInt(0),
}
stateInfo = &contract.IStateStateInfo{
State: opts.State,
ReplacedByState: big.NewInt(0),
CreatedAtTimestamp: big.NewInt(0),
ReplacedAtTimestamp: big.NewInt(0),
CreatedAtBlock: big.NewInt(0),
ReplacedAtBlock: big.NewInt(0),
}
}

Expand Down Expand Up @@ -299,33 +283,23 @@ func TimeStampFn() string {
}

func (r *Resolver) TypedData(primaryType services.PrimaryType, did w3c.DID, identityState services.IdentityState, walletAddress string) (apitypes.TypedData, error) {
identity := "0"
if did.IDStrings[2] != "000000000000000000000000000000000000000000" {
userID, err := core.IDFromDID(did)
if err != nil {
return apitypes.TypedData{},
fmt.Errorf("invalid did format for did '%s': %v", did, err)
}
identity = userID.BigInt().String()
id, err := core.IDFromDID(did)
if err != nil {
return apitypes.TypedData{},
fmt.Errorf("invalid did format for did '%s': %v", did, err)
}
userID := id.BigInt().String()

root := "0"
state := "0"
createdAtTimestamp := "0"
replacedByRoot := "0"
replacedByState := "0"
replacedAtTimestamp := "0"

if identityState.StateInfo != nil {
state = identityState.StateInfo.State.String()
replacedByState = identityState.StateInfo.ReplacedByState.String()
createdAtTimestamp = identityState.StateInfo.CreatedAtTimestamp.String()
replacedAtTimestamp = identityState.StateInfo.ReplacedAtTimestamp.String()
}
if identityState.GistInfo != nil {
root = identityState.GistInfo.Root.String()
replacedByRoot = identityState.GistInfo.ReplacedByRoot.String()
createdAtTimestamp = identityState.GistInfo.CreatedAtTimestamp.String()
replacedAtTimestamp = identityState.GistInfo.ReplacedAtTimestamp.String()
}

Expand All @@ -339,23 +313,18 @@ func (r *Resolver) TypedData(primaryType services.PrimaryType, did w3c.DID, iden
primaryTypeString = "IdentityState"
apiTypes = IdentityStateAPITypes
message = apitypes.TypedDataMessage{
"from": walletAddress,
"timestamp": timestamp,
"identity": identity,
"userID": userID,
"state": state,
"replacedByState": replacedByState,
"createdAtTimestamp": createdAtTimestamp,
"replacedAtTimestamp": replacedAtTimestamp,
}
case services.GlobalStateType:
primaryTypeString = "GlobalState"
apiTypes = GlobalStateAPITypes
message = apitypes.TypedDataMessage{
"from": walletAddress,
"timestamp": timestamp,
"userID": userID,
"root": root,
"replacedByRoot": replacedByRoot,
"createdAtTimestamp": createdAtTimestamp,
"replacedAtTimestamp": replacedAtTimestamp,
}
}
Expand Down
34 changes: 3 additions & 31 deletions pkg/services/blockchain/eth/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ func TestNotFoundErr(t *testing.T) {
}

func TestResolveSignature_Success(t *testing.T) {
userEmptyDID, _ := w3c.ParseDID("did:polygonid:polygon:amoy:000000000000000000000000000000000000000000")

tests := []struct {
name string
opts *services.ResolverOpts
Expand Down Expand Up @@ -254,7 +252,7 @@ func TestResolveSignature_Success(t *testing.T) {
ReplacedByRoot: big.NewInt(0),
ReplacedAtTimestamp: big.NewInt(0),
},
Signature: "0x6276946bac246584ed6eaa2d5e43be5147e67cc7aa3b969c82bb9b1670e8de8b7f7410286f25d6bee4330b4bc260286cf8505358ffa29c8e677e4f05d78acf131c",
Signature: "0x4640cdda90e9340e4283e5e5d622137ee624142a53a66b98552eae931dab8eb41f25b4c9eae4771be41e7e1dcac364fc2b98cc6adf02586b158c24c3ca36f8af1c",
},
},
{
Expand All @@ -281,7 +279,7 @@ func TestResolveSignature_Success(t *testing.T) {
ReplacedAtTimestamp: big.NewInt(0),
},
GistInfo: nil,
Signature: "0xdd07cd99ee8aa853c3e942aa5d57bfb844cae3db35fe29e8fc635ff4b2f5377d4b3c65f270474e6c5931b3d77f536233bc56d63172da8dba188f1f6fa51a10cb1c",
Signature: "0xc373a5a9df5c9227af61724bccaacffb117bf96437d9d7c41aff9be9f7662890716f2254dfc750b3768f2afa45843b53a8139264aa79626f4ad351f9390321841c",
},
},
{
Expand Down Expand Up @@ -316,33 +314,7 @@ func TestResolveSignature_Success(t *testing.T) {
ReplacedByRoot: big.NewInt(0),
ReplacedAtTimestamp: big.NewInt(0),
},
Signature: "0xdd07cd99ee8aa853c3e942aa5d57bfb844cae3db35fe29e8fc635ff4b2f5377d4b3c65f270474e6c5931b3d77f536233bc56d63172da8dba188f1f6fa51a10cb1c",
},
},
{
name: "resolve only gist",
opts: &services.ResolverOpts{
GistRoot: big.NewInt(400),
Signature: string(document.EthereumEip712SignatureProof2021Type),
},
userDID: userEmptyDID,
contractMock: func(c *cm.MockStateContract) {
latestGist := big.NewInt(400)
latestGistInfo := contract.IStateGistRootInfo{Root: big.NewInt(400), CreatedAtTimestamp: big.NewInt(0), ReplacedByRoot: big.NewInt(0), ReplacedAtTimestamp: big.NewInt(0)}
c.EXPECT().GetGISTRootInfo(gomock.Any(), latestGist).Return(latestGistInfo, nil)
},
timeStamp: func() string {
return "0"
},
expectedIdentityState: services.IdentityState{
StateInfo: nil,
GistInfo: &services.GistInfo{
Root: big.NewInt(400),
CreatedAtTimestamp: big.NewInt(0),
ReplacedByRoot: big.NewInt(0),
ReplacedAtTimestamp: big.NewInt(0),
},
Signature: "0xe64e080d08b948e5303b49288f1ff599df5b21fd20d7a944026a17e69f860e21662538ec1f8cba2f4a76e7c25d0f5cf506dc16bbc3148158ed81dd899528c69f1c",
Signature: "0xc373a5a9df5c9227af61724bccaacffb117bf96437d9d7c41aff9be9f7662890716f2254dfc750b3768f2afa45843b53a8139264aa79626f4ad351f9390321841c",
},
},
}
Expand Down
42 changes: 16 additions & 26 deletions pkg/services/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,25 @@ func (d *DidDocumentServices) GetDidDocument(ctx context.Context, did string, op
return errResolution, err
}

blockchain := ""
network := ""
userID := core.ID{}
if userDID.IDStrings[2] == "000000000000000000000000000000000000000000" {
blockchain = userDID.IDStrings[0]
network = userDID.IDStrings[1]
} else {
userID, err = core.IDFromDID(*userDID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}
userID, err := core.IDFromDID(*userDID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

b, err := core.BlockchainFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}
b, err := core.BlockchainFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

n, err := core.NetworkIDFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}
blockchain = string(b)
network = string(n)
n, err := core.NetworkIDFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

resolver, err := d.resolvers.GetResolverByNetwork(blockchain, network)
resolver, err := d.resolvers.GetResolverByNetwork(string(b), string(n))
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
Expand Down Expand Up @@ -113,7 +103,7 @@ func (d *DidDocumentServices) GetDidDocument(ctx context.Context, did string, op

chainIDStateAddress := resolver.BlockchainID()

if err == nil && userDID.IDStrings[2] != "000000000000000000000000000000000000000000" {
if err == nil {
didResolution.DidDocument.Context = append(didResolution.DidDocument.Context.([]string), document.EcdsaSecp256k1RecoveryContext)
addressString := fmt.Sprintf("%x", addr)
blockchainAccountID := fmt.Sprintf("eip155:%s:0x%s", strings.Split(chainIDStateAddress, ":")[0], addressString)
Expand Down

0 comments on commit 510a477

Please sign in to comment.