laconicd/utils/json.go
Bipul Prasad 505ad2ab70
[vulcanize/dxns#8] Migrating auctions module from DXNS (#3)
* Migrating auctions module from DXNS

* Adding appropriate comments on proto definitions

* Addressing review comments and fixing build issues

* Addressing review comments

* Adding README for auction module and fixing build/run issues

* Removing DXNS references and migrating DXNS utils to Ethermint

* Setting default genesis param values

* Defining constants in params and assigning ParamSetPairs

* Fixing issues with auction bid reveals

Co-authored-by: bipulprasad <Bipul@qubecinema.com>
2021-10-01 15:19:01 +05:30

53 lines
1.2 KiB
Go

//
// Copyright 2020 Wireline, Inc.
//
package utils
import (
"bytes"
"errors"
canonicalJson "github.com/gibson042/canonicaljson-go"
cbor "github.com/ipfs/go-ipld-cbor"
mh "github.com/multiformats/go-multihash"
)
// GenerateHash returns the hash of the canonicalized JSON input.
func GenerateHash(json map[string]interface{}) (string, []byte, error) {
content, err := canonicalJson.Marshal(json)
if err != nil {
return "", nil, err
}
cid, err := CIDFromJSONBytes(content)
if err != nil {
return "", nil, err
}
return cid, content, nil
}
// CIDFromJSONBytes returns CID (cbor) for json (as bytes).
func CIDFromJSONBytes(content []byte) (string, error) {
cid, err := cbor.FromJSON(bytes.NewReader(content), mh.SHA2_256, -1)
if err != nil {
return "", err
}
return cid.String(), nil
}
// GetAttributeAsString returns a map attribute as string, if possible.
func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) {
if value, ok := obj[attr]; ok {
if valueStr, ok := value.(string); ok {
return valueStr, nil
}
return "", errors.New("attribute not of string type")
}
return "", errors.New("attribute not found")
}