ignite scaffold type asset hash signature pubkey --module asset

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-07-03 15:16:49 +02:00
parent a8d91a4ced
commit 490f35ee5d
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
6 changed files with 542 additions and 0 deletions

View File

@ -46437,6 +46437,42 @@ paths:
}
tags:
- Query
/planetmint-go/asset/params:
get:
summary: Parameters queries the parameters of the module.
operationId: PlanetmintgoAssetParams
responses:
'200':
description: A successful response.
schema:
type: object
properties:
params:
description: params holds all the parameters of this module.
type: object
description: >-
QueryParamsResponse is response type for the Query/Params RPC
method.
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
tags:
- Query
/planetmint-go/machine/get_machine_by_public_key/{publicKey}:
get:
summary: Queries a list of GetMachineByPublicKey items.
@ -75280,6 +75316,16 @@ definitions:
description: |-
Version defines the versioning scheme used to negotiate the IBC verison in
the connection handshake.
planetmintgo.asset.Params:
type: object
description: Params defines the parameters for the module.
planetmintgo.asset.QueryParamsResponse:
type: object
properties:
params:
description: params holds all the parameters of this module.
type: object
description: QueryParamsResponse is response type for the Query/Params RPC method.
planetmintgo.machine.Machine:
type: object
properties:

View File

@ -0,0 +1,11 @@
syntax = "proto3";
package planetmintgo.asset;
option go_package = "planetmint-go/x/asset/types";
message Asset {
string hash = 1;
string signature = 2;
string pubkey = 3;
}

29
x/asset/keeper/asset.go Normal file
View File

@ -0,0 +1,29 @@
package keeper
import (
"planetmint-go/x/asset/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) StoreAsset(ctx sdk.Context, asset types.Asset) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
appendValue := k.cdc.MustMarshal(&asset)
store.Set(GetAssetHashBytes(asset.Hash), appendValue)
}
func (k Keeper) GetAsset(ctx sdk.Context, hash string) (val types.Asset, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
asset := store.Get(GetAssetHashBytes(hash))
if asset == nil {
return val, false
}
k.cdc.MustUnmarshal(asset, &val)
return val, true
}
func GetAssetHashBytes(hash string) []byte {
bz := []byte(hash)
return bz
}

View File

@ -0,0 +1,37 @@
package keeper_test
import (
"crypto/sha256"
"strconv"
"testing"
keepertest "planetmint-go/testutil/keeper"
"planetmint-go/x/asset/keeper"
"planetmint-go/x/asset/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)
func createNAsset(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Asset {
items := make([]types.Asset, n)
for i := range items {
hash := sha256.Sum256([]byte(strconv.FormatInt(int64(i), 2)))
hashStr := string(hash[:])
items[i].Hash = hashStr
items[i].Pubkey = "pubkey"
items[i].Signature = "sign"
keeper.StoreAsset(ctx, items[i])
}
return items
}
func TestGetAsset(t *testing.T) {
keeper, ctx := keepertest.AssetKeeper(t)
items := createNAsset(keeper, ctx, 10)
for _, item := range items {
asset, found := keeper.GetAsset(ctx, item.Hash)
assert.True(t, found)
assert.Equal(t, item, asset)
}
}

417
x/asset/types/asset.pb.go Normal file
View File

@ -0,0 +1,417 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: planetmintgo/asset/asset.proto
package types
import (
fmt "fmt"
proto "github.com/cosmos/gogoproto/proto"
io "io"
math "math"
math_bits "math/bits"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Asset struct {
Hash string `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
Signature string `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty"`
Pubkey string `protobuf:"bytes,3,opt,name=pubkey,proto3" json:"pubkey,omitempty"`
}
func (m *Asset) Reset() { *m = Asset{} }
func (m *Asset) String() string { return proto.CompactTextString(m) }
func (*Asset) ProtoMessage() {}
func (*Asset) Descriptor() ([]byte, []int) {
return fileDescriptor_03dd37a25f684e6e, []int{0}
}
func (m *Asset) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *Asset) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_Asset.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *Asset) XXX_Merge(src proto.Message) {
xxx_messageInfo_Asset.Merge(m, src)
}
func (m *Asset) XXX_Size() int {
return m.Size()
}
func (m *Asset) XXX_DiscardUnknown() {
xxx_messageInfo_Asset.DiscardUnknown(m)
}
var xxx_messageInfo_Asset proto.InternalMessageInfo
func (m *Asset) GetHash() string {
if m != nil {
return m.Hash
}
return ""
}
func (m *Asset) GetSignature() string {
if m != nil {
return m.Signature
}
return ""
}
func (m *Asset) GetPubkey() string {
if m != nil {
return m.Pubkey
}
return ""
}
func init() {
proto.RegisterType((*Asset)(nil), "planetmintgo.asset.Asset")
}
func init() { proto.RegisterFile("planetmintgo/asset/asset.proto", fileDescriptor_03dd37a25f684e6e) }
var fileDescriptor_03dd37a25f684e6e = []byte{
// 166 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x2b, 0xc8, 0x49, 0xcc,
0x4b, 0x2d, 0xc9, 0xcd, 0xcc, 0x2b, 0x49, 0xcf, 0xd7, 0x4f, 0x2c, 0x2e, 0x4e, 0x2d, 0x81, 0x90,
0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x42, 0xc8, 0xf2, 0x7a, 0x60, 0x19, 0xa5, 0x40, 0x2e,
0x56, 0x47, 0x10, 0x43, 0x48, 0x88, 0x8b, 0x25, 0x23, 0xb1, 0x38, 0x43, 0x82, 0x51, 0x81, 0x51,
0x83, 0x33, 0x08, 0xcc, 0x16, 0x92, 0xe1, 0xe2, 0x2c, 0xce, 0x4c, 0xcf, 0x4b, 0x2c, 0x29, 0x2d,
0x4a, 0x95, 0x60, 0x02, 0x4b, 0x20, 0x04, 0x84, 0xc4, 0xb8, 0xd8, 0x0a, 0x4a, 0x93, 0xb2, 0x53,
0x2b, 0x25, 0x98, 0xc1, 0x52, 0x50, 0x9e, 0x93, 0xe9, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9,
0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e,
0xcb, 0x31, 0x44, 0x49, 0x23, 0x1c, 0xa0, 0x9b, 0x9e, 0xaf, 0x5f, 0x01, 0x75, 0x63, 0x49, 0x65,
0x41, 0x6a, 0x71, 0x12, 0x1b, 0xd8, 0x91, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x4a, 0x35,
0x90, 0xe0, 0xc6, 0x00, 0x00, 0x00,
}
func (m *Asset) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *Asset) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *Asset) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if len(m.Pubkey) > 0 {
i -= len(m.Pubkey)
copy(dAtA[i:], m.Pubkey)
i = encodeVarintAsset(dAtA, i, uint64(len(m.Pubkey)))
i--
dAtA[i] = 0x1a
}
if len(m.Signature) > 0 {
i -= len(m.Signature)
copy(dAtA[i:], m.Signature)
i = encodeVarintAsset(dAtA, i, uint64(len(m.Signature)))
i--
dAtA[i] = 0x12
}
if len(m.Hash) > 0 {
i -= len(m.Hash)
copy(dAtA[i:], m.Hash)
i = encodeVarintAsset(dAtA, i, uint64(len(m.Hash)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func encodeVarintAsset(dAtA []byte, offset int, v uint64) int {
offset -= sovAsset(v)
base := offset
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return base
}
func (m *Asset) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Hash)
if l > 0 {
n += 1 + l + sovAsset(uint64(l))
}
l = len(m.Signature)
if l > 0 {
n += 1 + l + sovAsset(uint64(l))
}
l = len(m.Pubkey)
if l > 0 {
n += 1 + l + sovAsset(uint64(l))
}
return n
}
func sovAsset(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
func sozAsset(x uint64) (n int) {
return sovAsset(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *Asset) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowAsset
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: Asset: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: Asset: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowAsset
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthAsset
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthAsset
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Hash = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Signature", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowAsset
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthAsset
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthAsset
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Signature = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 3:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Pubkey", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowAsset
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthAsset
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthAsset
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Pubkey = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipAsset(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthAsset
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipAsset(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
depth := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowAsset
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowAsset
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
iNdEx++
if dAtA[iNdEx-1] < 0x80 {
break
}
}
case 1:
iNdEx += 8
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowAsset
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
if length < 0 {
return 0, ErrInvalidLengthAsset
}
iNdEx += length
case 3:
depth++
case 4:
if depth == 0 {
return 0, ErrUnexpectedEndOfGroupAsset
}
depth--
case 5:
iNdEx += 4
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthAsset
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthAsset = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowAsset = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupAsset = fmt.Errorf("proto: unexpected end of group")
)

View File

@ -12,6 +12,8 @@ const (
// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_asset"
AssetKey = "Asset/value/"
)
func KeyPrefix(p string) []byte {