feat: attribute typing #40
11
x/nameservice/helpers/examples/service_provider_example.yml
Normal file
11
x/nameservice/helpers/examples/service_provider_example.yml
Normal 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
|
@ -0,0 +1,6 @@
|
|||||||
|
record:
|
||||||
|
type: WebsiteRegistrationRecord
|
||||||
|
url: https://cerc.io
|
||||||
|
repo_registration_record_cid: QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D
|
||||||
|
build_artifact_cid: QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9
|
||||||
|
tls_cerc_cid: QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR
|
@ -61,7 +61,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
|
|||||||
if test.createRecord {
|
if test.createRecord {
|
||||||
dir, err := os.Getwd()
|
dir, err := os.Getwd()
|
||||||
sr.NoError(err)
|
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)
|
sr.NoError(err)
|
||||||
payload, err := payloadType.ToPayload()
|
payload, err := payloadType.ToPayload()
|
||||||
sr.NoError(err)
|
sr.NoError(err)
|
||||||
|
@ -300,7 +300,7 @@ func (k Keeper) ProcessAttributes(ctx sdk.Context, record types.RecordType) erro
|
|||||||
for key := range record.Attributes {
|
for key := range record.Attributes {
|
||||||
if key == "x500" {
|
if key == "x500" {
|
||||||
// #nosec G705
|
// #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)
|
indexKey := GetAttributesIndexKey(fmt.Sprintf("x500%s", x500Key), x500Val)
|
||||||
if err := k.SetAttributeMapping(ctx, indexKey, record.ID); err != nil {
|
if err := k.SetAttributeMapping(ctx, indexKey, record.ID); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -4,10 +4,12 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/cerc-io/laconicd/x/nameservice/helpers"
|
"github.com/cerc-io/laconicd/x/nameservice/helpers"
|
||||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||||
canonicalJson "github.com/gibson042/canonicaljson-go"
|
canonicalJson "github.com/gibson042/canonicaljson-go"
|
||||||
|
"github.com/gogo/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -41,7 +43,7 @@ func (payloadObj *PayloadType) ToPayload() (Payload, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func payLoadAttributes(recordPayLoad map[string]interface{}) (*codectypes.Any, error) {
|
func payLoadAttributes(recordPayLoad map[string]interface{}) (*codectypes.Any, error) {
|
||||||
recordType, ok := recordPayLoad["Type"]
|
recordType, ok := recordPayLoad["type"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return &codectypes.Any{}, fmt.Errorf("cannot get type from payload")
|
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
|
// It will unmarshal with record attributes
|
||||||
func (payload Payload) ToReadablePayload() PayloadType {
|
func (payload Payload) ToReadablePayload() PayloadType {
|
||||||
var payloadType 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
|
payloadType.Signatures = payload.Signatures
|
||||||
|
|
||||||
return payloadType
|
return payloadType
|
||||||
|
Loading…
Reference in New Issue
Block a user