feat: attribute typing #40

Merged
0xmuralik merged 24 commits from murali/attribute-typing into main 2022-11-15 06:21:14 +00:00
5 changed files with 53 additions and 4 deletions
Showing only changes of commit 647669cf17 - Show all commits

View File

@ -0,0 +1,11 @@
record:
type: ServiceProviderRegistration
bond_id: madeUpBondID
laconic_id: madeUpLaconicID
x500:
common_name: cerc-io
organization_unit: xyz
organization_name: abc
state_name: california
country: US

View File

@ -0,0 +1,6 @@
record:
type: WebsiteRegistrationRecord
url: https://cerc.io
repo_registration_record_cid: QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D
build_artifact_cid: QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9
tls_cerc_cid: QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR

View File

@ -61,7 +61,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
if test.createRecord {
dir, err := os.Getwd()
sr.NoError(err)
payloadType, err := cli.GetPayloadFromFile(dir + "/../helpers/examples/example1.yml")
payloadType, err := cli.GetPayloadFromFile(dir + "/../helpers/examples/service_provider_example.yml")
sr.NoError(err)
payload, err := payloadType.ToPayload()
sr.NoError(err)

View File

@ -300,7 +300,7 @@ func (k Keeper) ProcessAttributes(ctx sdk.Context, record types.RecordType) erro
for key := range record.Attributes {
if key == "x500" {
// #nosec G705
for x500Key, x500Val := range record.Attributes[key].(map[string]string) {
for x500Key, x500Val := range record.Attributes[key].(map[string]interface{}) {
indexKey := GetAttributesIndexKey(fmt.Sprintf("x500%s", x500Key), x500Val)
if err := k.SetAttributeMapping(ctx, indexKey, record.ID); err != nil {
return err

View File

@ -4,10 +4,12 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
"github.com/cerc-io/laconicd/x/nameservice/helpers"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
canonicalJson "github.com/gibson042/canonicaljson-go"
"github.com/gogo/protobuf/proto"
)
const (
@ -41,7 +43,7 @@ func (payloadObj *PayloadType) ToPayload() (Payload, error) {
}
func payLoadAttributes(recordPayLoad map[string]interface{}) (*codectypes.Any, error) {
recordType, ok := recordPayLoad["Type"]
recordType, ok := recordPayLoad["type"]
if !ok {
return &codectypes.Any{}, fmt.Errorf("cannot get type from payload")
}
@ -75,8 +77,38 @@ func payLoadAttributes(recordPayLoad map[string]interface{}) (*codectypes.Any, e
// It will unmarshal with record attributes
func (payload Payload) ToReadablePayload() PayloadType {
var payloadType PayloadType
payloadType.Record = helpers.UnMarshalMapFromJSONBytes(payload.Record.Attributes.Value)
var bz []byte
s := strings.Split(payload.Record.Attributes.TypeUrl, ".")
switch s[len(s)-1] {
case "ServiceProviderRegistration":
{
var attributes ServiceProviderRegistration
err := proto.Unmarshal(payload.Record.Attributes.Value, &attributes)
if err != nil {
panic("Proto unmarshal error")
}
bz, err = json.Marshal(attributes)
if err != nil {
panic("JSON marshal error")
}
}
case "WebsiteRegistrationRecord":
{
var attributes WebsiteRegistrationRecord
err := proto.Unmarshal(payload.Record.Attributes.Value, &attributes)
if err != nil {
panic("Proto unmarshal error")
}
bz, err = json.Marshal(attributes)
if err != nil {
panic("JSON marshal error")
}
}
}
payloadType.Record = helpers.UnMarshalMapFromJSONBytes(bz)
payloadType.Signatures = payload.Signatures
return payloadType