From 51dfde22e1a0967e944122a92068acc93f8a5ee7 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 14:19:59 +0200 Subject: [PATCH 1/8] add trust anchor type to machine module Signed-off-by: Lorenz Herzberger --- app/app.go | 1 + proto/planetmintgo/machine/machine.proto | 4 + testutil/keeper/machine.go | 3 + x/machine/keeper/keeper.go | 3 + x/machine/keeper/trust_anchor.go | 31 ++++ x/machine/types/keys.go | 2 + x/machine/types/machine.pb.go | 224 ++++++++++++++++++++--- 7 files changed, 242 insertions(+), 26 deletions(-) create mode 100644 x/machine/keeper/trust_anchor.go diff --git a/app/app.go b/app/app.go index 27cfef0..e3a84f4 100644 --- a/app/app.go +++ b/app/app.go @@ -533,6 +533,7 @@ func New( keys[machinemoduletypes.TAIndexKey], keys[machinemoduletypes.IssuerPlanetmintIndexKey], keys[machinemoduletypes.IssuerLiquidIndexKey], + keys[machinemoduletypes.TrustAnchorKey], keys[machinemoduletypes.MemStoreKey], app.GetSubspace(machinemoduletypes.ModuleName), ) diff --git a/proto/planetmintgo/machine/machine.proto b/proto/planetmintgo/machine/machine.proto index 223a104..8b7ad1a 100644 --- a/proto/planetmintgo/machine/machine.proto +++ b/proto/planetmintgo/machine/machine.proto @@ -31,3 +31,7 @@ message MachineIndex { string issuerPlanetmint = 2; string issuerLiquid = 3; } + +message TrustAnchor { + string pubkey = 1; +} diff --git a/testutil/keeper/machine.go b/testutil/keeper/machine.go index 01d07f2..8458ae0 100644 --- a/testutil/keeper/machine.go +++ b/testutil/keeper/machine.go @@ -23,6 +23,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { taIndexStoreKey := sdk.NewKVStoreKey(types.TAIndexKey) issuerPlanetmintIndexStoreKey := sdk.NewKVStoreKey(types.IssuerPlanetmintIndexKey) issuerLiquidIndexStoreKey := sdk.NewKVStoreKey(types.IssuerLiquidIndexKey) + trustAnchorStoreKey := sdk.NewKVStoreKey(types.TrustAnchorKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) db := tmdb.NewMemDB() @@ -31,6 +32,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { stateStore.MountStoreWithDB(taIndexStoreKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(issuerPlanetmintIndexStoreKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(issuerLiquidIndexStoreKey, storetypes.StoreTypeIAVL, db) + stateStore.MountStoreWithDB(trustAnchorStoreKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) require.NoError(t, stateStore.LoadLatestVersion()) @@ -50,6 +52,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { taIndexStoreKey, issuerPlanetmintIndexStoreKey, issuerLiquidIndexStoreKey, + trustAnchorStoreKey, memStoreKey, paramsSubspace, ) diff --git a/x/machine/keeper/keeper.go b/x/machine/keeper/keeper.go index 741adc2..5d01ef6 100644 --- a/x/machine/keeper/keeper.go +++ b/x/machine/keeper/keeper.go @@ -19,6 +19,7 @@ type ( taIndexStoreKey storetypes.StoreKey issuerPlanetmintIndexStoreKey storetypes.StoreKey issuerLiquidIndexStoreKey storetypes.StoreKey + taStoreKey storetypes.StoreKey memKey storetypes.StoreKey paramstore paramtypes.Subspace } @@ -30,6 +31,7 @@ func NewKeeper( indexStoreKey, issuerPlanetmintIndexStoreKey, issuerLiquidIndexStoreKey, + taStoreKey, memKey storetypes.StoreKey, ps paramtypes.Subspace, ) *Keeper { @@ -44,6 +46,7 @@ func NewKeeper( taIndexStoreKey: indexStoreKey, issuerPlanetmintIndexStoreKey: issuerPlanetmintIndexStoreKey, issuerLiquidIndexStoreKey: issuerLiquidIndexStoreKey, + taStoreKey: taStoreKey, memKey: memKey, paramstore: ps, } diff --git a/x/machine/keeper/trust_anchor.go b/x/machine/keeper/trust_anchor.go new file mode 100644 index 0000000..2d7929e --- /dev/null +++ b/x/machine/keeper/trust_anchor.go @@ -0,0 +1,31 @@ +package keeper + +import ( + "planetmint-go/x/machine/types" + + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (k Keeper) StoreTrustAnchor(ctx sdk.Context, ta types.TrustAnchor) { + store := prefix.NewStore(ctx.KVStore(k.taStoreKey), types.KeyPrefix(types.TrustAnchorKey)) + appendValue := k.cdc.MustMarshal(&ta) + store.Set(GetTrustAnchorBytes(ta.Pubkey), appendValue) +} + +func (k Keeper) GetTrustAnchor(ctx sdk.Context, pubKey string) (val types.TrustAnchor, found bool) { + store := prefix.NewStore(ctx.KVStore(k.taStoreKey), types.KeyPrefix(types.TrustAnchorKey)) + trustAnchor := store.Get(GetTrustAnchorBytes(pubKey)) + + if trustAnchor == nil { + return val, false + } + if err := k.cdc.Unmarshal(trustAnchor, &val); err != nil { + return val, false + } + return val, true +} + +func GetTrustAnchorBytes(pubKey string) []byte { + return []byte(pubKey) +} diff --git a/x/machine/types/keys.go b/x/machine/types/keys.go index d1c7455..bcd18f0 100644 --- a/x/machine/types/keys.go +++ b/x/machine/types/keys.go @@ -20,6 +20,8 @@ const ( IssuerPlanetmintIndexKey = "Machine/IssuerPlanetmintIndex/" IssuerLiquidIndexKey = "Machine/IssuerLiquidIndex/" + + TrustAnchorKey = "Machine/trustAnchor/" ) func KeyPrefix(p string) []byte { diff --git a/x/machine/types/machine.pb.go b/x/machine/types/machine.pb.go index 5360e8b..a456012 100644 --- a/x/machine/types/machine.pb.go +++ b/x/machine/types/machine.pb.go @@ -274,10 +274,55 @@ func (m *MachineIndex) GetIssuerLiquid() string { return "" } +type TrustAnchor struct { + Pubkey string `protobuf:"bytes,1,opt,name=pubkey,proto3" json:"pubkey,omitempty"` +} + +func (m *TrustAnchor) Reset() { *m = TrustAnchor{} } +func (m *TrustAnchor) String() string { return proto.CompactTextString(m) } +func (*TrustAnchor) ProtoMessage() {} +func (*TrustAnchor) Descriptor() ([]byte, []int) { + return fileDescriptor_1bb279745bef7c4b, []int{3} +} +func (m *TrustAnchor) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *TrustAnchor) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_TrustAnchor.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 *TrustAnchor) XXX_Merge(src proto.Message) { + xxx_messageInfo_TrustAnchor.Merge(m, src) +} +func (m *TrustAnchor) XXX_Size() int { + return m.Size() +} +func (m *TrustAnchor) XXX_DiscardUnknown() { + xxx_messageInfo_TrustAnchor.DiscardUnknown(m) +} + +var xxx_messageInfo_TrustAnchor proto.InternalMessageInfo + +func (m *TrustAnchor) GetPubkey() string { + if m != nil { + return m.Pubkey + } + return "" +} + func init() { proto.RegisterType((*Machine)(nil), "planetmintgo.machine.Machine") proto.RegisterType((*Metadata)(nil), "planetmintgo.machine.Metadata") proto.RegisterType((*MachineIndex)(nil), "planetmintgo.machine.MachineIndex") + proto.RegisterType((*TrustAnchor)(nil), "planetmintgo.machine.TrustAnchor") } func init() { @@ -285,32 +330,34 @@ func init() { } var fileDescriptor_1bb279745bef7c4b = []byte{ - // 394 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcf, 0x4a, 0xf3, 0x40, - 0x14, 0xc5, 0x3b, 0x6d, 0xbf, 0x36, 0xb9, 0xed, 0x87, 0x75, 0x10, 0x99, 0x85, 0x86, 0x90, 0x55, - 0x10, 0x4d, 0x41, 0x17, 0x82, 0x4b, 0xed, 0xa6, 0x60, 0x41, 0xb2, 0x74, 0x37, 0x26, 0x63, 0x1d, - 0x6c, 0x26, 0x31, 0x99, 0x4a, 0x05, 0x1f, 0xc1, 0x85, 0x8f, 0xe5, 0xb2, 0x4b, 0xc1, 0x8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f2, 0x17, 0xfd, 0x9c, 0x9e, 0x7c, 0x49, 0x18, 0x69, 0xd9, 0xc8, - 0xfd, 0xef, 0x2b, 0xed, 0xbc, 0x21, 0x30, 0xca, 0x2a, 0xee, 0x40, 0x6d, 0x98, 0x64, 0x9a, 0x6e, - 0x2e, 0x15, 0x44, 0xf6, 0xcc, 0x03, 0x56, 0xc2, 0x2d, 0x1c, 0x76, 0x61, 0x87, 0x66, 0x19, 0x93, - 0x3d, 0x76, 0xcf, 0x05, 0x97, 0x39, 0x98, 0x82, 0xf2, 0x7a, 0x8c, 0x8f, 0x61, 0x97, 0x86, 0xa1, - 0xd2, 0x74, 0xd4, 0xa3, 0x92, 0x5e, 0xf5, 0x7b, 0x0a, 0xbc, 0xe9, 0x6f, 0x0e, 0x9c, 0x57, 0x68, - 0xeb, 0x7f, 0xdd, 0x17, 0x21, 0x9b, 0xac, 0xc2, 0x40, 0xeb, 0x30, 0xb6, 0xa1, 0xaf, 0xfe, 0x11, - 0x7d, 0x6d, 0x13, 0xfd, 0xe5, 0xf9, 0xc7, 0xcc, 0x42, 0xd3, 0x99, 0x85, 0xbe, 0x67, 0x16, 0x7a, - 0x9f, 0x5b, 0x95, 0xe9, 0xdc, 0xaa, 0x7c, 0xce, 0xad, 0xca, 0xed, 0xe1, 0x92, 0xf1, 0xc9, 0x30, - 0xee, 0x4e, 0x16, 0x1b, 0x9c, 0x43, 0xcc, 0xee, 0x1a, 0x6a, 0x81, 0xcf, 0x7e, 0x02, 0x00, 0x00, - 0xff, 0xff, 0xb9, 0x18, 0x24, 0xb2, 0xe6, 0x02, 0x00, 0x00, + // 417 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x4f, 0x0b, 0xd3, 0x30, + 0x18, 0xc6, 0x97, 0x6d, 0x6e, 0xed, 0xbb, 0x89, 0x33, 0x88, 0xe4, 0xa0, 0xa5, 0x14, 0x84, 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0xe6, 0xb8, 0xe8, 0x15, 0x6b, 0x35, 0xef, + 0x99, 0x3d, 0x7f, 0xf9, 0x37, 0x00, 0x00, 0xff, 0xff, 0x2f, 0x0c, 0xb1, 0x11, 0x0d, 0x03, 0x00, + 0x00, } func (m *Machine) Marshal() (dAtA []byte, err error) { @@ -510,6 +557,36 @@ func (m *MachineIndex) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *TrustAnchor) 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 *TrustAnchor) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *TrustAnchor) 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 = encodeVarintMachine(dAtA, i, uint64(len(m.Pubkey))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func encodeVarintMachine(dAtA []byte, offset int, v uint64) int { offset -= sovMachine(v) base := offset @@ -616,6 +693,19 @@ func (m *MachineIndex) Size() (n int) { return n } +func (m *TrustAnchor) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Pubkey) + if l > 0 { + n += 1 + l + sovMachine(uint64(l)) + } + return n +} + func sovMachine(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1301,6 +1391,88 @@ func (m *MachineIndex) Unmarshal(dAtA []byte) error { } return nil } +func (m *TrustAnchor) 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 ErrIntOverflowMachine + } + 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: TrustAnchor: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: TrustAnchor: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + 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 ErrIntOverflowMachine + } + 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 ErrInvalidLengthMachine + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthMachine + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pubkey = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipMachine(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthMachine + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipMachine(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From 9f1ef39fecdc70fe55803494df2c2415de14b0a2 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 15:36:26 +0200 Subject: [PATCH 2/8] ignite scaffold message register-trust-anchor trust-anchor:TrustAnchor --module machine Signed-off-by: Lorenz Herzberger --- docs/static/openapi.yml | 7 + proto/planetmintgo/machine/machine.proto | 4 - proto/planetmintgo/machine/trust_anchor.proto | 8 + proto/planetmintgo/machine/tx.proto | 11 +- x/machine/client/cli/tx.go | 1 + .../client/cli/tx_register_trust_anchor.go | 47 ++ .../msg_server_register_trust_anchor.go | 17 + x/machine/module_simulation.go | 23 + x/machine/simulation/register_trust_anchor.go | 29 ++ x/machine/types/codec.go | 4 + x/machine/types/machine.pb.go | 224 ++-------- .../types/message_register_trust_anchor.go | 46 ++ .../message_register_trust_anchor_test.go | 40 ++ x/machine/types/trust_anchor.pb.go | 316 +++++++++++++ x/machine/types/tx.pb.go | 415 +++++++++++++++++- 15 files changed, 977 insertions(+), 215 deletions(-) create mode 100644 proto/planetmintgo/machine/trust_anchor.proto create mode 100644 x/machine/client/cli/tx_register_trust_anchor.go create mode 100644 x/machine/keeper/msg_server_register_trust_anchor.go create mode 100644 x/machine/simulation/register_trust_anchor.go create mode 100644 x/machine/types/message_register_trust_anchor.go create mode 100644 x/machine/types/message_register_trust_anchor_test.go create mode 100644 x/machine/types/trust_anchor.pb.go diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 7f08301..de82b8f 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -75382,6 +75382,8 @@ definitions: type: string planetmintgo.machine.MsgAttestMachineResponse: type: object + planetmintgo.machine.MsgRegisterTrustAnchorResponse: + type: object planetmintgo.machine.Params: type: object description: Params defines the parameters for the module. @@ -75432,3 +75434,8 @@ definitions: description: params holds all the parameters of this module. type: object description: QueryParamsResponse is response type for the Query/Params RPC method. + planetmintgo.machine.TrustAnchor: + type: object + properties: + pubkey: + type: string diff --git a/proto/planetmintgo/machine/machine.proto b/proto/planetmintgo/machine/machine.proto index 8b7ad1a..223a104 100644 --- a/proto/planetmintgo/machine/machine.proto +++ b/proto/planetmintgo/machine/machine.proto @@ -31,7 +31,3 @@ message MachineIndex { string issuerPlanetmint = 2; string issuerLiquid = 3; } - -message TrustAnchor { - string pubkey = 1; -} diff --git a/proto/planetmintgo/machine/trust_anchor.proto b/proto/planetmintgo/machine/trust_anchor.proto new file mode 100644 index 0000000..2f5d84c --- /dev/null +++ b/proto/planetmintgo/machine/trust_anchor.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package planetmintgo.machine; + +option go_package = "planetmint-go/x/machine/types"; + +message TrustAnchor { + string pubkey = 1; +} \ No newline at end of file diff --git a/proto/planetmintgo/machine/tx.proto b/proto/planetmintgo/machine/tx.proto index 49ce6ad..595e7ee 100644 --- a/proto/planetmintgo/machine/tx.proto +++ b/proto/planetmintgo/machine/tx.proto @@ -3,12 +3,14 @@ syntax = "proto3"; package planetmintgo.machine; import "planetmintgo/machine/machine.proto"; +import "planetmintgo/machine/trust_anchor.proto"; option go_package = "planetmint-go/x/machine/types"; // Msg defines the Msg service. service Msg { - rpc AttestMachine (MsgAttestMachine) returns (MsgAttestMachineResponse); + rpc AttestMachine (MsgAttestMachine ) returns (MsgAttestMachineResponse ); + rpc RegisterTrustAnchor (MsgRegisterTrustAnchor) returns (MsgRegisterTrustAnchorResponse); } message MsgAttestMachine { string creator = 1; @@ -17,3 +19,10 @@ message MsgAttestMachine { message MsgAttestMachineResponse {} +message MsgRegisterTrustAnchor { + string creator = 1; + TrustAnchor trustAnchor = 2; +} + +message MsgRegisterTrustAnchorResponse {} + diff --git a/x/machine/client/cli/tx.go b/x/machine/client/cli/tx.go index c7a068c..6b36d67 100644 --- a/x/machine/client/cli/tx.go +++ b/x/machine/client/cli/tx.go @@ -26,6 +26,7 @@ func GetTxCmd() *cobra.Command { } cmd.AddCommand(CmdAttestMachine()) + cmd.AddCommand(CmdRegisterTrustAnchor()) // this line is used by starport scaffolding # 1 return cmd diff --git a/x/machine/client/cli/tx_register_trust_anchor.go b/x/machine/client/cli/tx_register_trust_anchor.go new file mode 100644 index 0000000..832484a --- /dev/null +++ b/x/machine/client/cli/tx_register_trust_anchor.go @@ -0,0 +1,47 @@ +package cli + +import ( + "strconv" + + "encoding/json" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/spf13/cobra" + "planetmint-go/x/machine/types" +) + +var _ = strconv.Itoa(0) + +func CmdRegisterTrustAnchor() *cobra.Command { + cmd := &cobra.Command{ + Use: "register-trust-anchor [trust-anchor]", + Short: "Broadcast message register-trust-anchor", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) (err error) { + argTrustAnchor := new(types.TrustAnchor) + err = json.Unmarshal([]byte(args[0]), argTrustAnchor) + if err != nil { + return err + } + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := types.NewMsgRegisterTrustAnchor( + clientCtx.GetFromAddress().String(), + argTrustAnchor, + ) + if err := msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/machine/keeper/msg_server_register_trust_anchor.go b/x/machine/keeper/msg_server_register_trust_anchor.go new file mode 100644 index 0000000..39c911c --- /dev/null +++ b/x/machine/keeper/msg_server_register_trust_anchor.go @@ -0,0 +1,17 @@ +package keeper + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + "planetmint-go/x/machine/types" +) + +func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegisterTrustAnchor) (*types.MsgRegisterTrustAnchorResponse, error) { + ctx := sdk.UnwrapSDKContext(goCtx) + + // TODO: Handling the message + _ = ctx + + return &types.MsgRegisterTrustAnchorResponse{}, nil +} diff --git a/x/machine/module_simulation.go b/x/machine/module_simulation.go index 21f839d..0c247fc 100644 --- a/x/machine/module_simulation.go +++ b/x/machine/module_simulation.go @@ -27,6 +27,10 @@ const ( // TODO: Determine the simulation weight value defaultWeightMsgAttestMachine int = 100 + opWeightMsgRegisterTrustAnchor = "op_weight_msg_register_trust_anchor" + // TODO: Determine the simulation weight value + defaultWeightMsgRegisterTrustAnchor int = 100 + // this line is used by starport scaffolding # simapp/module/const ) @@ -68,6 +72,17 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp machinesimulation.SimulateMsgAttestMachine(am.accountKeeper, am.bankKeeper, am.keeper), )) + var weightMsgRegisterTrustAnchor int + simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgRegisterTrustAnchor, &weightMsgRegisterTrustAnchor, nil, + func(_ *rand.Rand) { + weightMsgRegisterTrustAnchor = defaultWeightMsgRegisterTrustAnchor + }, + ) + operations = append(operations, simulation.NewWeightedOperation( + weightMsgRegisterTrustAnchor, + machinesimulation.SimulateMsgRegisterTrustAnchor(am.accountKeeper, am.bankKeeper, am.keeper), + )) + // this line is used by starport scaffolding # simapp/module/operation return operations @@ -84,6 +99,14 @@ func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Wei return nil }, ), + simulation.NewWeightedProposalMsg( + opWeightMsgRegisterTrustAnchor, + defaultWeightMsgRegisterTrustAnchor, + func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg { + machinesimulation.SimulateMsgRegisterTrustAnchor(am.accountKeeper, am.bankKeeper, am.keeper) + return nil + }, + ), // this line is used by starport scaffolding # simapp/module/OpMsg } } diff --git a/x/machine/simulation/register_trust_anchor.go b/x/machine/simulation/register_trust_anchor.go new file mode 100644 index 0000000..83a43c4 --- /dev/null +++ b/x/machine/simulation/register_trust_anchor.go @@ -0,0 +1,29 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "planetmint-go/x/machine/keeper" + "planetmint-go/x/machine/types" +) + +func SimulateMsgRegisterTrustAnchor( + ak types.AccountKeeper, + bk types.BankKeeper, + k keeper.Keeper, +) simtypes.Operation { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, + ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { + simAccount, _ := simtypes.RandomAcc(r, accs) + msg := &types.MsgRegisterTrustAnchor{ + Creator: simAccount.Address.String(), + } + + // TODO: Handling the RegisterTrustAnchor simulation + + return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "RegisterTrustAnchor simulation not implemented"), nil, nil + } +} diff --git a/x/machine/types/codec.go b/x/machine/types/codec.go index 05b3865..efff526 100644 --- a/x/machine/types/codec.go +++ b/x/machine/types/codec.go @@ -9,6 +9,7 @@ import ( func RegisterCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&MsgAttestMachine{}, "machine/AttestMachine", nil) + cdc.RegisterConcrete(&MsgRegisterTrustAnchor{}, "machine/RegisterTrustAnchor", nil) // this line is used by starport scaffolding # 2 } @@ -16,6 +17,9 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), &MsgAttestMachine{}, ) + registry.RegisterImplementations((*sdk.Msg)(nil), + &MsgRegisterTrustAnchor{}, + ) // this line is used by starport scaffolding # 3 msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) diff --git a/x/machine/types/machine.pb.go b/x/machine/types/machine.pb.go index a456012..5360e8b 100644 --- a/x/machine/types/machine.pb.go +++ b/x/machine/types/machine.pb.go @@ -274,55 +274,10 @@ func (m *MachineIndex) GetIssuerLiquid() string { return "" } -type TrustAnchor struct { - Pubkey string `protobuf:"bytes,1,opt,name=pubkey,proto3" json:"pubkey,omitempty"` -} - -func (m *TrustAnchor) Reset() { *m = TrustAnchor{} } -func (m *TrustAnchor) String() string { return proto.CompactTextString(m) } -func (*TrustAnchor) ProtoMessage() {} -func (*TrustAnchor) Descriptor() ([]byte, []int) { - return fileDescriptor_1bb279745bef7c4b, []int{3} -} -func (m *TrustAnchor) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *TrustAnchor) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_TrustAnchor.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 *TrustAnchor) XXX_Merge(src proto.Message) { - xxx_messageInfo_TrustAnchor.Merge(m, src) -} -func (m *TrustAnchor) XXX_Size() int { - return m.Size() -} -func (m *TrustAnchor) XXX_DiscardUnknown() { - xxx_messageInfo_TrustAnchor.DiscardUnknown(m) -} - -var xxx_messageInfo_TrustAnchor proto.InternalMessageInfo - -func (m *TrustAnchor) GetPubkey() string { - if m != nil { - return m.Pubkey - } - return "" -} - func init() { proto.RegisterType((*Machine)(nil), "planetmintgo.machine.Machine") proto.RegisterType((*Metadata)(nil), "planetmintgo.machine.Metadata") proto.RegisterType((*MachineIndex)(nil), "planetmintgo.machine.MachineIndex") - proto.RegisterType((*TrustAnchor)(nil), "planetmintgo.machine.TrustAnchor") } func init() { @@ -330,34 +285,32 @@ func init() { } var fileDescriptor_1bb279745bef7c4b = []byte{ - // 417 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x4f, 0x0b, 0xd3, 0x30, - 0x18, 0xc6, 0x97, 0x6d, 0x6e, 0xed, 0xbb, 0x89, 0x33, 0x88, 0xe4, 0xa0, 0xa5, 0x14, 0x84, 0x22, - 0xda, 0x81, 0x1e, 0x04, 0x6f, 0xea, 0x2e, 0x03, 0x07, 0x52, 0x3c, 0x79, 0xcb, 0xda, 0xb8, 0x85, - 0xad, 0x49, 0x6d, 0x52, 0xd9, 0xc0, 0x8f, 0xe0, 0xc1, 0x8f, 0xe5, 0x71, 0x47, 0xc1, 0x8b, 0x6c, - 0x5f, 0x44, 0x9a, 0xa6, 0x9b, 0xfb, 0x73, 0xf0, 0xd4, 0xf7, 0x79, 0xde, 0x27, 0xbc, 0xcd, 0x2f, - 0x2f, 0x04, 0xf9, 0x9a, 0x0a, 0xa6, 0x33, 0x2e, 0xf4, 0x42, 0x8e, 0x33, 0x9a, 0x2c, 0xb9, 0x60, - 0xcd, 0x37,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xdc, 0x5a, 0xe1, 0x10, 0xee, 0x51, 0xa5, 0x98, - 0x9e, 0xb0, 0xcf, 0x5c, 0x70, 0x5d, 0x81, 0xa9, 0x29, 0x5f, 0xda, 0xf8, 0x19, 0xdc, 0xa7, 0x69, - 0x6a, 0x6a, 0xba, 0x9e, 0x50, 0x4d, 0xdf, 0x4d, 0x27, 0x06, 0xbc, 0x1b, 0x5f, 0x37, 0x82, 0x6f, - 0x30, 0xb4, 0x6f, 0x3d, 0x15, 0x29, 0xdb, 0x9c, 0xc3, 0x40, 0x97, 0x30, 0x6e, 0xa1, 0x6f, 0xff, - 0x27, 0xfa, 0xce, 0x35, 0xfa, 0xe0, 0x09, 0x0c, 0x3e, 0x16, 0xa5, 0xd2, 0x6f, 0x44, 0xb2, 0x94, - 0x66, 0x83, 0xf2, 0x72, 0xbe, 0x62, 0x5b, 0x3b, 0xd9, 0xaa, 0xb7, 0xaf, 0x7e, 0xee, 0x3d, 0xb4, - 0xdb, 0x7b, 0xe8, 0xcf, 0xde, 0x43, 0x3f, 0x0e, 0x5e, 0x6b, 0x77, 0xf0, 0x5a, 0xbf, 0x0e, 0x5e, - 0xeb, 0xd3, 0xe3, 0xd3, 0x53, 0x3c, 0x5f, 0xc8, 0xf1, 0xe6, 0xb8, 0xe8, 0x15, 0x6b, 0x35, 0xef, - 0x99, 0x3d, 0x7f, 0xf9, 0x37, 0x00, 0x00, 0xff, 0xff, 0x2f, 0x0c, 0xb1, 0x11, 0x0d, 0x03, 0x00, - 0x00, + // 394 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcf, 0x4a, 0xf3, 0x40, + 0x14, 0xc5, 0x3b, 0x6d, 0xbf, 0x36, 0xb9, 0xed, 0x87, 0x75, 0x10, 0x99, 0x85, 0x86, 0x90, 0x55, + 0x10, 0x4d, 0x41, 0x17, 0x82, 0x4b, 0xed, 0xa6, 0x60, 0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61, + 0x19, 0xe0, 0x0b, 0x30, 0x22, 0x26, 0x69, 0x48, 0x25, 0x25, 0x60, 0x23, 0xb7, 0x75, 0x6a, 0x79, + 0xdb, 0xb0, 0x79, 0x03, 0xdd, 0xf2, 0x17, 0xfd, 0x9c, 0x9e, 0x7c, 0x49, 0x18, 0x69, 0xd9, 0xc8, + 0xfd, 0xef, 0x2b, 0xed, 0xbc, 0x21, 0x30, 0xca, 0x2a, 0xee, 0x40, 0x6d, 0x98, 0x64, 0x9a, 0x6e, + 0x2e, 0x15, 0x44, 0xf6, 0xcc, 0x03, 0x56, 0xc2, 0x2d, 0x1c, 0x76, 0x61, 0x87, 0x66, 0x19, 0x93, + 0x3d, 0x76, 0xcf, 0x05, 0x97, 0x39, 0x98, 0x82, 0xf2, 0x7a, 0x8c, 0x8f, 0x61, 0x97, 0x86, 0xa1, + 0xd2, 0x74, 0xd4, 0xa3, 0x92, 0x5e, 0xf5, 0x7b, 0x0a, 0xbc, 0xe9, 0x6f, 0x0e, 0x9c, 0x57, 0x68, + 0xeb, 0x7f, 0xdd, 0x17, 0x21, 0x9b, 0xac, 0xc2, 0x40, 0xeb, 0x30, 0xb6, 0xa1, 0xaf, 0xfe, 0x11, + 0x7d, 0x6d, 0x13, 0xfd, 0xe5, 0xf9, 0xc7, 0xcc, 0x42, 0xd3, 0x99, 0x85, 0xbe, 0x67, 0x16, 0x7a, + 0x9f, 0x5b, 0x95, 0xe9, 0xdc, 0xaa, 0x7c, 0xce, 0xad, 0xca, 0xed, 0xe1, 0x92, 0xf1, 0xc9, 0x30, + 0xee, 0x4e, 0x16, 0x1b, 0x9c, 0x43, 0xcc, 0xee, 0x1a, 0x6a, 0x81, 0xcf, 0x7e, 0x02, 0x00, 0x00, + 0xff, 0xff, 0xb9, 0x18, 0x24, 0xb2, 0xe6, 0x02, 0x00, 0x00, } func (m *Machine) Marshal() (dAtA []byte, err error) { @@ -557,36 +510,6 @@ func (m *MachineIndex) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *TrustAnchor) 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 *TrustAnchor) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *TrustAnchor) 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 = encodeVarintMachine(dAtA, i, uint64(len(m.Pubkey))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - func encodeVarintMachine(dAtA []byte, offset int, v uint64) int { offset -= sovMachine(v) base := offset @@ -693,19 +616,6 @@ func (m *MachineIndex) Size() (n int) { return n } -func (m *TrustAnchor) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Pubkey) - if l > 0 { - n += 1 + l + sovMachine(uint64(l)) - } - return n -} - func sovMachine(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1391,88 +1301,6 @@ func (m *MachineIndex) Unmarshal(dAtA []byte) error { } return nil } -func (m *TrustAnchor) 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 ErrIntOverflowMachine - } - 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: TrustAnchor: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: TrustAnchor: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - 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 ErrIntOverflowMachine - } - 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 ErrInvalidLengthMachine - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthMachine - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Pubkey = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipMachine(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthMachine - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipMachine(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/machine/types/message_register_trust_anchor.go b/x/machine/types/message_register_trust_anchor.go new file mode 100644 index 0000000..6cd6b25 --- /dev/null +++ b/x/machine/types/message_register_trust_anchor.go @@ -0,0 +1,46 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +const TypeMsgRegisterTrustAnchor = "register_trust_anchor" + +var _ sdk.Msg = &MsgRegisterTrustAnchor{} + +func NewMsgRegisterTrustAnchor(creator string, trustAnchor *TrustAnchor) *MsgRegisterTrustAnchor { + return &MsgRegisterTrustAnchor{ + Creator: creator, + TrustAnchor: trustAnchor, + } +} + +func (msg *MsgRegisterTrustAnchor) Route() string { + return RouterKey +} + +func (msg *MsgRegisterTrustAnchor) Type() string { + return TypeMsgRegisterTrustAnchor +} + +func (msg *MsgRegisterTrustAnchor) GetSigners() []sdk.AccAddress { + creator, err := sdk.AccAddressFromBech32(msg.Creator) + if err != nil { + panic(err) + } + return []sdk.AccAddress{creator} +} + +func (msg *MsgRegisterTrustAnchor) GetSignBytes() []byte { + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) +} + +func (msg *MsgRegisterTrustAnchor) ValidateBasic() error { + _, err := sdk.AccAddressFromBech32(msg.Creator) + if err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) + } + return nil +} diff --git a/x/machine/types/message_register_trust_anchor_test.go b/x/machine/types/message_register_trust_anchor_test.go new file mode 100644 index 0000000..cac2644 --- /dev/null +++ b/x/machine/types/message_register_trust_anchor_test.go @@ -0,0 +1,40 @@ +package types + +import ( + "testing" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/stretchr/testify/require" + "planetmint-go/testutil/sample" +) + +func TestMsgRegisterTrustAnchor_ValidateBasic(t *testing.T) { + tests := []struct { + name string + msg MsgRegisterTrustAnchor + err error + }{ + { + name: "invalid address", + msg: MsgRegisterTrustAnchor{ + Creator: "invalid_address", + }, + err: sdkerrors.ErrInvalidAddress, + }, { + name: "valid address", + msg: MsgRegisterTrustAnchor{ + Creator: sample.AccAddress(), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.msg.ValidateBasic() + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} diff --git a/x/machine/types/trust_anchor.pb.go b/x/machine/types/trust_anchor.pb.go new file mode 100644 index 0000000..57b7b73 --- /dev/null +++ b/x/machine/types/trust_anchor.pb.go @@ -0,0 +1,316 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: planetmintgo/machine/trust_anchor.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 TrustAnchor struct { + Pubkey string `protobuf:"bytes,1,opt,name=pubkey,proto3" json:"pubkey,omitempty"` +} + +func (m *TrustAnchor) Reset() { *m = TrustAnchor{} } +func (m *TrustAnchor) String() string { return proto.CompactTextString(m) } +func (*TrustAnchor) ProtoMessage() {} +func (*TrustAnchor) Descriptor() ([]byte, []int) { + return fileDescriptor_c743aa40c45e7aac, []int{0} +} +func (m *TrustAnchor) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *TrustAnchor) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_TrustAnchor.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 *TrustAnchor) XXX_Merge(src proto.Message) { + xxx_messageInfo_TrustAnchor.Merge(m, src) +} +func (m *TrustAnchor) XXX_Size() int { + return m.Size() +} +func (m *TrustAnchor) XXX_DiscardUnknown() { + xxx_messageInfo_TrustAnchor.DiscardUnknown(m) +} + +var xxx_messageInfo_TrustAnchor proto.InternalMessageInfo + +func (m *TrustAnchor) GetPubkey() string { + if m != nil { + return m.Pubkey + } + return "" +} + +func init() { + proto.RegisterType((*TrustAnchor)(nil), "planetmintgo.machine.TrustAnchor") +} + +func init() { + proto.RegisterFile("planetmintgo/machine/trust_anchor.proto", fileDescriptor_c743aa40c45e7aac) +} + +var fileDescriptor_c743aa40c45e7aac = []byte{ + // 149 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x2f, 0xc8, 0x49, 0xcc, + 0x4b, 0x2d, 0xc9, 0xcd, 0xcc, 0x2b, 0x49, 0xcf, 0xd7, 0xcf, 0x4d, 0x4c, 0xce, 0xc8, 0xcc, 0x4b, + 0xd5, 0x2f, 0x29, 0x2a, 0x2d, 0x2e, 0x89, 0x4f, 0xcc, 0x4b, 0xce, 0xc8, 0x2f, 0xd2, 0x2b, 0x28, + 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x41, 0x56, 0xa8, 0x07, 0x55, 0xa8, 0xa4, 0xca, 0xc5, 0x1d, 0x02, + 0x52, 0xeb, 0x08, 0x56, 0x2a, 0x24, 0xc6, 0xc5, 0x56, 0x50, 0x9a, 0x94, 0x9d, 0x5a, 0x29, 0xc1, + 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0xe5, 0x39, 0x99, 0x9f, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, + 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, + 0xb1, 0x1c, 0x43, 0x94, 0x2c, 0xc2, 0x58, 0xdd, 0xf4, 0x7c, 0xfd, 0x0a, 0x84, 0x13, 0x2a, 0x0b, + 0x52, 0x8b, 0x93, 0xd8, 0xc0, 0x96, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x05, 0x42, 0xb6, + 0x02, 0xa7, 0x00, 0x00, 0x00, +} + +func (m *TrustAnchor) 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 *TrustAnchor) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *TrustAnchor) 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 = encodeVarintTrustAnchor(dAtA, i, uint64(len(m.Pubkey))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintTrustAnchor(dAtA []byte, offset int, v uint64) int { + offset -= sovTrustAnchor(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *TrustAnchor) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Pubkey) + if l > 0 { + n += 1 + l + sovTrustAnchor(uint64(l)) + } + return n +} + +func sovTrustAnchor(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozTrustAnchor(x uint64) (n int) { + return sovTrustAnchor(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *TrustAnchor) 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 ErrIntOverflowTrustAnchor + } + 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: TrustAnchor: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: TrustAnchor: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + 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 ErrIntOverflowTrustAnchor + } + 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 ErrInvalidLengthTrustAnchor + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTrustAnchor + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pubkey = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTrustAnchor(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTrustAnchor + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipTrustAnchor(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, ErrIntOverflowTrustAnchor + } + 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, ErrIntOverflowTrustAnchor + } + 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, ErrIntOverflowTrustAnchor + } + 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, ErrInvalidLengthTrustAnchor + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupTrustAnchor + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthTrustAnchor + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthTrustAnchor = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowTrustAnchor = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupTrustAnchor = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/machine/types/tx.pb.go b/x/machine/types/tx.pb.go index c86400e..49773e2 100644 --- a/x/machine/types/tx.pb.go +++ b/x/machine/types/tx.pb.go @@ -115,29 +115,125 @@ func (m *MsgAttestMachineResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgAttestMachineResponse proto.InternalMessageInfo +type MsgRegisterTrustAnchor struct { + Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` + TrustAnchor *TrustAnchor `protobuf:"bytes,2,opt,name=trustAnchor,proto3" json:"trustAnchor,omitempty"` +} + +func (m *MsgRegisterTrustAnchor) Reset() { *m = MsgRegisterTrustAnchor{} } +func (m *MsgRegisterTrustAnchor) String() string { return proto.CompactTextString(m) } +func (*MsgRegisterTrustAnchor) ProtoMessage() {} +func (*MsgRegisterTrustAnchor) Descriptor() ([]byte, []int) { + return fileDescriptor_85ac37e5c8e5251d, []int{2} +} +func (m *MsgRegisterTrustAnchor) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgRegisterTrustAnchor) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgRegisterTrustAnchor.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 *MsgRegisterTrustAnchor) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgRegisterTrustAnchor.Merge(m, src) +} +func (m *MsgRegisterTrustAnchor) XXX_Size() int { + return m.Size() +} +func (m *MsgRegisterTrustAnchor) XXX_DiscardUnknown() { + xxx_messageInfo_MsgRegisterTrustAnchor.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgRegisterTrustAnchor proto.InternalMessageInfo + +func (m *MsgRegisterTrustAnchor) GetCreator() string { + if m != nil { + return m.Creator + } + return "" +} + +func (m *MsgRegisterTrustAnchor) GetTrustAnchor() *TrustAnchor { + if m != nil { + return m.TrustAnchor + } + return nil +} + +type MsgRegisterTrustAnchorResponse struct { +} + +func (m *MsgRegisterTrustAnchorResponse) Reset() { *m = MsgRegisterTrustAnchorResponse{} } +func (m *MsgRegisterTrustAnchorResponse) String() string { return proto.CompactTextString(m) } +func (*MsgRegisterTrustAnchorResponse) ProtoMessage() {} +func (*MsgRegisterTrustAnchorResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_85ac37e5c8e5251d, []int{3} +} +func (m *MsgRegisterTrustAnchorResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgRegisterTrustAnchorResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgRegisterTrustAnchorResponse.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 *MsgRegisterTrustAnchorResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgRegisterTrustAnchorResponse.Merge(m, src) +} +func (m *MsgRegisterTrustAnchorResponse) XXX_Size() int { + return m.Size() +} +func (m *MsgRegisterTrustAnchorResponse) XXX_DiscardUnknown() { + xxx_messageInfo_MsgRegisterTrustAnchorResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgRegisterTrustAnchorResponse proto.InternalMessageInfo + func init() { proto.RegisterType((*MsgAttestMachine)(nil), "planetmintgo.machine.MsgAttestMachine") proto.RegisterType((*MsgAttestMachineResponse)(nil), "planetmintgo.machine.MsgAttestMachineResponse") + proto.RegisterType((*MsgRegisterTrustAnchor)(nil), "planetmintgo.machine.MsgRegisterTrustAnchor") + proto.RegisterType((*MsgRegisterTrustAnchorResponse)(nil), "planetmintgo.machine.MsgRegisterTrustAnchorResponse") } func init() { proto.RegisterFile("planetmintgo/machine/tx.proto", fileDescriptor_85ac37e5c8e5251d) } var fileDescriptor_85ac37e5c8e5251d = []byte{ - // 216 bytes of a gzipped FileDescriptorProto + // 305 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x2d, 0xc8, 0x49, 0xcc, 0x4b, 0x2d, 0xc9, 0xcd, 0xcc, 0x2b, 0x49, 0xcf, 0xd7, 0xcf, 0x4d, 0x4c, 0xce, 0xc8, 0xcc, 0x4b, 0xd5, 0x2f, 0xa9, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x41, 0x96, 0xd6, 0x83, 0x4a, - 0x4b, 0x29, 0x61, 0xd5, 0x04, 0xa5, 0x21, 0x3a, 0x95, 0x52, 0xb9, 0x04, 0x7c, 0x8b, 0xd3, 0x1d, - 0x4b, 0x4a, 0x52, 0x8b, 0x4b, 0x7c, 0x21, 0x32, 0x42, 0x12, 0x5c, 0xec, 0xc9, 0x45, 0xa9, 0x89, - 0x25, 0xf9, 0x45, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x30, 0xae, 0x90, 0x39, 0x17, 0x3b, - 0x54, 0xbb, 0x04, 0x93, 0x02, 0xa3, 0x06, 0xb7, 0x91, 0xac, 0x1e, 0x36, 0x9b, 0xf5, 0xa0, 0x26, - 0x05, 0xc1, 0x54, 0x2b, 0x49, 0x71, 0x49, 0xa0, 0x5b, 0x13, 0x94, 0x5a, 0x5c, 0x90, 0x9f, 0x57, - 0x9c, 0x6a, 0x94, 0xc7, 0xc5, 0xec, 0x5b, 0x9c, 0x2e, 0x94, 0xce, 0xc5, 0x8b, 0xea, 0x0c, 0x35, - 0x1c, 0x66, 0xa3, 0x99, 0x23, 0xa5, 0x47, 0x9c, 0x3a, 0x98, 0x7d, 0x4e, 0xe6, 0x27, 0x1e, 0xc9, - 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, - 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0x85, 0x14, 0xca, 0xba, 0xe9, 0xf9, 0xfa, 0x15, 0x88, - 0x80, 0xae, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0x07, 0x99, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, - 0x78, 0x82, 0x2b, 0xac, 0x8d, 0x01, 0x00, 0x00, + 0x4b, 0x29, 0x61, 0xd5, 0x04, 0xa5, 0x21, 0x3a, 0xa5, 0xd4, 0xb1, 0x1b, 0x5c, 0x54, 0x5a, 0x5c, + 0x12, 0x9f, 0x98, 0x97, 0x9c, 0x91, 0x5f, 0x04, 0x51, 0xa8, 0x94, 0xca, 0x25, 0xe0, 0x5b, 0x9c, + 0xee, 0x58, 0x52, 0x92, 0x5a, 0x5c, 0xe2, 0x0b, 0x51, 0x26, 0x24, 0xc1, 0xc5, 0x9e, 0x5c, 0x94, + 0x9a, 0x58, 0x92, 0x5f, 0x24, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0xe3, 0x0a, 0x99, 0x73, + 0xb1, 0x43, 0xcd, 0x92, 0x60, 0x52, 0x60, 0xd4, 0xe0, 0x36, 0x92, 0xd5, 0xc3, 0xe6, 0x44, 0x3d, + 0xa8, 0x49, 0x41, 0x30, 0xd5, 0x4a, 0x52, 0x5c, 0x12, 0xe8, 0xd6, 0x04, 0xa5, 0x16, 0x17, 0xe4, + 0xe7, 0x15, 0xa7, 0x2a, 0x95, 0x73, 0x89, 0xf9, 0x16, 0xa7, 0x07, 0xa5, 0xa6, 0x67, 0x16, 0x97, + 0xa4, 0x16, 0x85, 0x80, 0xdc, 0xe8, 0x08, 0x76, 0x22, 0x1e, 0x87, 0x38, 0x73, 0x71, 0x97, 0x20, + 0x14, 0x42, 0x1d, 0xa3, 0x88, 0xdd, 0x31, 0x48, 0x26, 0x06, 0x21, 0xeb, 0x52, 0x52, 0xe0, 0x92, + 0xc3, 0x6e, 0x31, 0xcc, 0x69, 0x46, 0x2f, 0x19, 0xb9, 0x98, 0x7d, 0x8b, 0xd3, 0x85, 0xd2, 0xb9, + 0x78, 0x51, 0x83, 0x48, 0x0d, 0x87, 0xbf, 0xd1, 0xfc, 0x28, 0xa5, 0x47, 0x9c, 0x3a, 0x98, 0x85, + 0x42, 0x95, 0x5c, 0xc2, 0xd8, 0x02, 0x42, 0x07, 0xa7, 0x31, 0x58, 0x54, 0x4b, 0x99, 0x90, 0xa2, + 0x1a, 0x66, 0xb5, 0x93, 0xf9, 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, 0x21, + 0xa5, 0x52, 0xdd, 0xf4, 0x7c, 0xfd, 0x0a, 0x44, 0x7a, 0xaa, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, + 0xa7, 0x24, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x66, 0xa7, 0x9c, 0x05, 0xcd, 0x02, 0x00, + 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -153,6 +249,7 @@ const _ = grpc.SupportPackageIsVersion4 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type MsgClient interface { AttestMachine(ctx context.Context, in *MsgAttestMachine, opts ...grpc.CallOption) (*MsgAttestMachineResponse, error) + RegisterTrustAnchor(ctx context.Context, in *MsgRegisterTrustAnchor, opts ...grpc.CallOption) (*MsgRegisterTrustAnchorResponse, error) } type msgClient struct { @@ -172,9 +269,19 @@ func (c *msgClient) AttestMachine(ctx context.Context, in *MsgAttestMachine, opt return out, nil } +func (c *msgClient) RegisterTrustAnchor(ctx context.Context, in *MsgRegisterTrustAnchor, opts ...grpc.CallOption) (*MsgRegisterTrustAnchorResponse, error) { + out := new(MsgRegisterTrustAnchorResponse) + err := c.cc.Invoke(ctx, "/planetmintgo.machine.Msg/RegisterTrustAnchor", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // MsgServer is the server API for Msg service. type MsgServer interface { AttestMachine(context.Context, *MsgAttestMachine) (*MsgAttestMachineResponse, error) + RegisterTrustAnchor(context.Context, *MsgRegisterTrustAnchor) (*MsgRegisterTrustAnchorResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. @@ -184,6 +291,9 @@ type UnimplementedMsgServer struct { func (*UnimplementedMsgServer) AttestMachine(ctx context.Context, req *MsgAttestMachine) (*MsgAttestMachineResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method AttestMachine not implemented") } +func (*UnimplementedMsgServer) RegisterTrustAnchor(ctx context.Context, req *MsgRegisterTrustAnchor) (*MsgRegisterTrustAnchorResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method RegisterTrustAnchor not implemented") +} func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) @@ -207,6 +317,24 @@ func _Msg_AttestMachine_Handler(srv interface{}, ctx context.Context, dec func(i return interceptor(ctx, in, info, handler) } +func _Msg_RegisterTrustAnchor_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgRegisterTrustAnchor) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).RegisterTrustAnchor(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/planetmintgo.machine.Msg/RegisterTrustAnchor", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).RegisterTrustAnchor(ctx, req.(*MsgRegisterTrustAnchor)) + } + return interceptor(ctx, in, info, handler) +} + var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "planetmintgo.machine.Msg", HandlerType: (*MsgServer)(nil), @@ -215,6 +343,10 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "AttestMachine", Handler: _Msg_AttestMachine_Handler, }, + { + MethodName: "RegisterTrustAnchor", + Handler: _Msg_RegisterTrustAnchor_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "planetmintgo/machine/tx.proto", @@ -285,6 +417,71 @@ func (m *MsgAttestMachineResponse) MarshalToSizedBuffer(dAtA []byte) (int, error return len(dAtA) - i, nil } +func (m *MsgRegisterTrustAnchor) 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 *MsgRegisterTrustAnchor) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgRegisterTrustAnchor) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.TrustAnchor != nil { + { + size, err := m.TrustAnchor.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if len(m.Creator) > 0 { + i -= len(m.Creator) + copy(dAtA[i:], m.Creator) + i = encodeVarintTx(dAtA, i, uint64(len(m.Creator))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *MsgRegisterTrustAnchorResponse) 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 *MsgRegisterTrustAnchorResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgRegisterTrustAnchorResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + func encodeVarintTx(dAtA []byte, offset int, v uint64) int { offset -= sovTx(v) base := offset @@ -322,6 +519,32 @@ func (m *MsgAttestMachineResponse) Size() (n int) { return n } +func (m *MsgRegisterTrustAnchor) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Creator) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + if m.TrustAnchor != nil { + l = m.TrustAnchor.Size() + n += 1 + l + sovTx(uint64(l)) + } + return n +} + +func (m *MsgRegisterTrustAnchorResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + func sovTx(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -496,6 +719,174 @@ func (m *MsgAttestMachineResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *MsgRegisterTrustAnchor) 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 ErrIntOverflowTx + } + 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: MsgRegisterTrustAnchor: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgRegisterTrustAnchor: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Creator", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + 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 ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Creator = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TrustAnchor", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.TrustAnchor == nil { + m.TrustAnchor = &TrustAnchor{} + } + if err := m.TrustAnchor.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgRegisterTrustAnchorResponse) 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 ErrIntOverflowTx + } + 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: MsgRegisterTrustAnchorResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgRegisterTrustAnchorResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTx(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From 7ada150ae0201ae22853f34ced5a946e0fbf7b4b Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 16:03:46 +0200 Subject: [PATCH 3/8] remove circular dependency Signed-off-by: Lorenz Herzberger --- x/machine/keeper/trust_anchor.go | 2 +- x/machine/types/message_register_trust_anchor_test.go | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/x/machine/keeper/trust_anchor.go b/x/machine/keeper/trust_anchor.go index 2d7929e..a36d392 100644 --- a/x/machine/keeper/trust_anchor.go +++ b/x/machine/keeper/trust_anchor.go @@ -7,7 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func (k Keeper) StoreTrustAnchor(ctx sdk.Context, ta types.TrustAnchor) { +func (k Keeper) StoreTrustAnchor(ctx sdk.Context, ta types.TrustAnchor, activated bool) { store := prefix.NewStore(ctx.KVStore(k.taStoreKey), types.KeyPrefix(types.TrustAnchorKey)) appendValue := k.cdc.MustMarshal(&ta) store.Set(GetTrustAnchorBytes(ta.Pubkey), appendValue) diff --git a/x/machine/types/message_register_trust_anchor_test.go b/x/machine/types/message_register_trust_anchor_test.go index cac2644..dac5f61 100644 --- a/x/machine/types/message_register_trust_anchor_test.go +++ b/x/machine/types/message_register_trust_anchor_test.go @@ -5,7 +5,6 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" - "planetmint-go/testutil/sample" ) func TestMsgRegisterTrustAnchor_ValidateBasic(t *testing.T) { @@ -20,11 +19,6 @@ func TestMsgRegisterTrustAnchor_ValidateBasic(t *testing.T) { Creator: "invalid_address", }, err: sdkerrors.ErrInvalidAddress, - }, { - name: "valid address", - msg: MsgRegisterTrustAnchor{ - Creator: sample.AccAddress(), - }, }, } for _, tt := range tests { From 7d78e0f9b8083ce9359ce2fbb56b99817bf95630 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 17:28:39 +0200 Subject: [PATCH 4/8] add test for trust anchor store Signed-off-by: Lorenz Herzberger --- x/machine/keeper/trust_anchor.go | 26 +++++++++++----- x/machine/keeper/trust_anchor_test.go | 44 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 x/machine/keeper/trust_anchor_test.go diff --git a/x/machine/keeper/trust_anchor.go b/x/machine/keeper/trust_anchor.go index a36d392..782b89d 100644 --- a/x/machine/keeper/trust_anchor.go +++ b/x/machine/keeper/trust_anchor.go @@ -9,21 +9,31 @@ import ( func (k Keeper) StoreTrustAnchor(ctx sdk.Context, ta types.TrustAnchor, activated bool) { store := prefix.NewStore(ctx.KVStore(k.taStoreKey), types.KeyPrefix(types.TrustAnchorKey)) - appendValue := k.cdc.MustMarshal(&ta) + // if activated is set to true then store 1 else 0 + var appendValue []byte + if activated { + appendValue = []byte{1} + } else { + appendValue = []byte{0} + } store.Set(GetTrustAnchorBytes(ta.Pubkey), appendValue) } -func (k Keeper) GetTrustAnchor(ctx sdk.Context, pubKey string) (val types.TrustAnchor, found bool) { +func (k Keeper) GetTrustAnchor(ctx sdk.Context, pubKey string) (val types.TrustAnchor, activated bool, found bool) { store := prefix.NewStore(ctx.KVStore(k.taStoreKey), types.KeyPrefix(types.TrustAnchorKey)) - trustAnchor := store.Get(GetTrustAnchorBytes(pubKey)) + trustAnchorActivated := store.Get(GetTrustAnchorBytes(pubKey)) - if trustAnchor == nil { - return val, false + if trustAnchorActivated == nil { + return val, false, false } - if err := k.cdc.Unmarshal(trustAnchor, &val); err != nil { - return val, false + + // if stored byte is 1 then return activated equals true + val.Pubkey = pubKey + if trustAnchorActivated[0] == 1 { + return val, true, true + } else { + return val, false, true } - return val, true } func GetTrustAnchorBytes(pubKey string) []byte { diff --git a/x/machine/keeper/trust_anchor_test.go b/x/machine/keeper/trust_anchor_test.go new file mode 100644 index 0000000..fbb4eda --- /dev/null +++ b/x/machine/keeper/trust_anchor_test.go @@ -0,0 +1,44 @@ +package keeper_test + +import ( + "fmt" + "testing" + + keepertest "planetmint-go/testutil/keeper" + + "planetmint-go/x/machine/keeper" + "planetmint-go/x/machine/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" +) + +func createNTrustAnchor(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.TrustAnchor { + items := make([]types.TrustAnchor, n) + for i := range items { + items[i].Pubkey = fmt.Sprintf("pubkey%v", i) + var activated bool + if i%2 == 1 { + activated = true + } else { + activated = false + } + keeper.StoreTrustAnchor(ctx, items[i], activated) + } + return items +} + +func TestGetTrustAnchor(t *testing.T) { + keeper, ctx := keepertest.MachineKeeper(t) + items := createNTrustAnchor(keeper, ctx, 10) + for i, item := range items { + ta, activated, found := keeper.GetTrustAnchor(ctx, item.Pubkey) + assert.True(t, found) + assert.Equal(t, item, ta) + if i%2 == 1 { + assert.True(t, activated) + } else { + assert.False(t, activated) + } + } +} From 688f13e171d970a8170a0bfb7aba7dd420dd2aa7 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 17:36:30 +0200 Subject: [PATCH 5/8] add update trust anchor test case Signed-off-by: Lorenz Herzberger --- x/machine/keeper/trust_anchor_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/x/machine/keeper/trust_anchor_test.go b/x/machine/keeper/trust_anchor_test.go index fbb4eda..1e7b3ac 100644 --- a/x/machine/keeper/trust_anchor_test.go +++ b/x/machine/keeper/trust_anchor_test.go @@ -42,3 +42,19 @@ func TestGetTrustAnchor(t *testing.T) { } } } + +func TestUpdateTrustAnchor(t *testing.T) { + keeper, ctx := keepertest.MachineKeeper(t) + items := createNTrustAnchor(keeper, ctx, 10) + for _, item := range items { + ta, activated, _ := keeper.GetTrustAnchor(ctx, item.Pubkey) + if !activated { + keeper.StoreTrustAnchor(ctx, ta, true) + } + } + + for _, item := range items { + _, activated, _ := keeper.GetTrustAnchor(ctx, item.Pubkey) + assert.True(t, activated) + } +} From b0d4ffad7a019e9c516e74c5bac3f5a1971ecf65 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 19:51:06 +0200 Subject: [PATCH 6/8] add unit test for msg_server_register_trust_anchor Signed-off-by: Lorenz Herzberger --- testutil/sample/sample.go | 6 ++++ .../msg_server_register_trust_anchor.go | 18 ++++++++-- x/machine/keeper/msg_server_test.go | 33 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index f1d1e56..4adac68 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -114,3 +114,9 @@ func ExtendedKeyPair(cfg chaincfg.Params) (string, string) { } return xprivKey.String(), xpubKey.String() } + +func TrustAnchor() machinetypes.TrustAnchor { + return machinetypes.TrustAnchor{ + Pubkey: PubKey, + } +} diff --git a/x/machine/keeper/msg_server_register_trust_anchor.go b/x/machine/keeper/msg_server_register_trust_anchor.go index 39c911c..886c6b0 100644 --- a/x/machine/keeper/msg_server_register_trust_anchor.go +++ b/x/machine/keeper/msg_server_register_trust_anchor.go @@ -2,16 +2,28 @@ package keeper import ( "context" + "errors" + + config "planetmint-go/config" + "planetmint-go/x/machine/types" sdk "github.com/cosmos/cosmos-sdk/types" - "planetmint-go/x/machine/types" ) func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegisterTrustAnchor) (*types.MsgRegisterTrustAnchorResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Handling the message - _ = ctx + isValidTrustAnchorPubkey := validateExtendedPublicKey(msg.TrustAnchor.Pubkey, config.LiquidNetParams) + if !isValidTrustAnchorPubkey { + return nil, errors.New("invalid trust anchor pubkey") + } + + _, _, found := k.GetTrustAnchor(ctx, msg.TrustAnchor.Pubkey) + if found { + return nil, errors.New("trust anchor is already registered") + } + + k.StoreTrustAnchor(ctx, *msg.TrustAnchor, false) return &types.MsgRegisterTrustAnchorResponse{}, nil } diff --git a/x/machine/keeper/msg_server_test.go b/x/machine/keeper/msg_server_test.go index 39778cd..62a626f 100644 --- a/x/machine/keeper/msg_server_test.go +++ b/x/machine/keeper/msg_server_test.go @@ -46,3 +46,36 @@ func TestMsgServerAttestMachineInvalidLiquidKey(t *testing.T) { _, err := msgServer.AttestMachine(ctx, msg) assert.EqualError(t, err, "invalid liquid key") } + +func TestMsgServerRegisterTrustAnchor(t *testing.T) { + _, pk := sample.KeyPair() + ta := sample.TrustAnchor() + msg := types.NewMsgRegisterTrustAnchor(pk, &ta) + msgServer, ctx := setupMsgServer(t) + res, err := msgServer.RegisterTrustAnchor(ctx, msg) + if assert.NoError(t, err) { + assert.Equal(t, &types.MsgRegisterTrustAnchorResponse{}, res) + } +} + +func TestMsgServerRegisterTrustAnchorTwice(t *testing.T) { + _, pk := sample.KeyPair() + ta := sample.TrustAnchor() + msg := types.NewMsgRegisterTrustAnchor(pk, &ta) + msgServer, ctx := setupMsgServer(t) + res, err := msgServer.RegisterTrustAnchor(ctx, msg) + if assert.NoError(t, err) { + assert.Equal(t, &types.MsgRegisterTrustAnchorResponse{}, res) + } + _, err = msgServer.RegisterTrustAnchor(ctx, msg) + assert.EqualError(t, err, "trust anchor is already registered") +} + +func TestMsgServerRegisterTrustAnchorInvalidPubkey(t *testing.T) { + _, pk := sample.KeyPair() + ta := sample.TrustAnchor() + msg := types.NewMsgRegisterTrustAnchor(pk, &ta) + msgServer, ctx := setupMsgServer(t) + _, err := msgServer.RegisterTrustAnchor(ctx, msg) + assert.EqualError(t, err, "invalid trust anchor pubkey") +} From 4bb3e694a562dbddc09e4aa365e007566676df4c Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 21:41:18 +0200 Subject: [PATCH 7/8] add secp256k1 public key validation Signed-off-by: Lorenz Herzberger --- .../msg_server_register_trust_anchor.go | 24 +++++++++++++++++-- x/machine/keeper/msg_server_test.go | 4 +++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/x/machine/keeper/msg_server_register_trust_anchor.go b/x/machine/keeper/msg_server_register_trust_anchor.go index 886c6b0..99a329f 100644 --- a/x/machine/keeper/msg_server_register_trust_anchor.go +++ b/x/machine/keeper/msg_server_register_trust_anchor.go @@ -2,9 +2,9 @@ package keeper import ( "context" + "encoding/hex" "errors" - config "planetmint-go/config" "planetmint-go/x/machine/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,7 +13,7 @@ import ( func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegisterTrustAnchor) (*types.MsgRegisterTrustAnchorResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - isValidTrustAnchorPubkey := validateExtendedPublicKey(msg.TrustAnchor.Pubkey, config.LiquidNetParams) + isValidTrustAnchorPubkey := validatePublicKey(msg.TrustAnchor.Pubkey) if !isValidTrustAnchorPubkey { return nil, errors.New("invalid trust anchor pubkey") } @@ -27,3 +27,23 @@ func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegi return &types.MsgRegisterTrustAnchorResponse{}, nil } + +func validatePublicKey(pubkey string) bool { + pubkeyBytes, err := hex.DecodeString(pubkey) + if err != nil { + return false + } + + // Check if byte slice has correct length + if len(pubkeyBytes) != 33 { + return false + } + + // Check if byte slice starts with 0x02 or 0x03 + firstByte := pubkeyBytes[0] + if firstByte != 0x02 && firstByte != 0x03 { + return false + } + + return true +} diff --git a/x/machine/keeper/msg_server_test.go b/x/machine/keeper/msg_server_test.go index 62a626f..2905824 100644 --- a/x/machine/keeper/msg_server_test.go +++ b/x/machine/keeper/msg_server_test.go @@ -73,7 +73,9 @@ func TestMsgServerRegisterTrustAnchorTwice(t *testing.T) { func TestMsgServerRegisterTrustAnchorInvalidPubkey(t *testing.T) { _, pk := sample.KeyPair() - ta := sample.TrustAnchor() + ta := types.TrustAnchor{ + Pubkey: "invalidpublickey", + } msg := types.NewMsgRegisterTrustAnchor(pk, &ta) msgServer, ctx := setupMsgServer(t) _, err := msgServer.RegisterTrustAnchor(ctx, msg) From 307dfd1e120aa24d34dee1d4856b6986cca07508 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 5 Sep 2023 21:43:22 +0200 Subject: [PATCH 8/8] fix staticcheck error Signed-off-by: Lorenz Herzberger --- x/machine/types/message_register_trust_anchor.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/machine/types/message_register_trust_anchor.go b/x/machine/types/message_register_trust_anchor.go index 6cd6b25..281bfe5 100644 --- a/x/machine/types/message_register_trust_anchor.go +++ b/x/machine/types/message_register_trust_anchor.go @@ -1,6 +1,7 @@ package types import ( + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -40,7 +41,7 @@ func (msg *MsgRegisterTrustAnchor) GetSignBytes() []byte { func (msg *MsgRegisterTrustAnchor) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) + return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) } return nil }