feat: add controller creation step to allocate

This commit is contained in:
Prad Nukala 2024-10-21 13:07:38 -04:00
parent 937b65ae95
commit 603da5560f
2 changed files with 10 additions and 1 deletions

View File

@ -63,28 +63,36 @@ func (k Querier) Allocate(goCtx context.Context, req *types.QueryAllocateRequest
ctx.Logger().Error(fmt.Sprintf("Error getting current schema: %s", err.Error())) ctx.Logger().Error(fmt.Sprintf("Error getting current schema: %s", err.Error()))
return nil, types.ErrInvalidSchema.Wrap(err.Error()) return nil, types.ErrInvalidSchema.Wrap(err.Error())
} }
// 2. Generate MPC Keyshares for new Account
shares, err := mpc.GenerateKeyshares() shares, err := mpc.GenerateKeyshares()
if err != nil { if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error generating keyshares: %s", err.Error())) ctx.Logger().Error(fmt.Sprintf("Error generating keyshares: %s", err.Error()))
return nil, types.ErrInvalidSchema.Wrap(err.Error()) return nil, types.ErrInvalidSchema.Wrap(err.Error())
} }
// 3. Create Controller from Keyshares
con, err := didtypes.NewController(shares) con, err := didtypes.NewController(shares)
if err != nil { if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error creating controller: %s", err.Error())) ctx.Logger().Error(fmt.Sprintf("Error creating controller: %s", err.Error()))
return nil, err return nil, types.ErrControllerCreation.Wrap(err.Error())
} }
// 4. Create a new vault PWA for service-worker
v, err := types.NewVault("", con.SonrAddress(), con.ChainID(), sch) v, err := types.NewVault("", con.SonrAddress(), con.ChainID(), sch)
if err != nil { if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error creating vault: %s", err.Error())) ctx.Logger().Error(fmt.Sprintf("Error creating vault: %s", err.Error()))
return nil, types.ErrInvalidSchema.Wrap(err.Error()) return nil, types.ErrInvalidSchema.Wrap(err.Error())
} }
// 5. Add to IPFS and Return CID for User Claims in Gateway
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS) cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS)
if err != nil { if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error adding to IPFS: %s", err.Error())) ctx.Logger().Error(fmt.Sprintf("Error adding to IPFS: %s", err.Error()))
return nil, types.ErrVaultAssembly.Wrap(err.Error()) return nil, types.ErrVaultAssembly.Wrap(err.Error())
} }
// 6. Return final response
return &types.QueryAllocateResponse{ return &types.QueryAllocateResponse{
Success: true, Success: true,
Cid: cid.String(), Cid: cid.String(),

View File

@ -6,6 +6,7 @@ var (
ErrInvalidGenesisState = sdkerrors.Register(ModuleName, 100, "invalid genesis state") ErrInvalidGenesisState = sdkerrors.Register(ModuleName, 100, "invalid genesis state")
ErrInvalidSchema = sdkerrors.Register(ModuleName, 200, "invalid schema") ErrInvalidSchema = sdkerrors.Register(ModuleName, 200, "invalid schema")
ErrVaultAssembly = sdkerrors.Register(ModuleName, 201, "vault assembly") ErrVaultAssembly = sdkerrors.Register(ModuleName, 201, "vault assembly")
ErrControllerCreation = sdkerrors.Register(ModuleName, 202, "failed to create controller")
ErrUnsupportedKeyEncoding = sdkerrors.Register(ModuleName, 300, "unsupported key encoding") ErrUnsupportedKeyEncoding = sdkerrors.Register(ModuleName, 300, "unsupported key encoding")
ErrUnsopportedChainCode = sdkerrors.Register(ModuleName, 301, "unsupported chain code") ErrUnsopportedChainCode = sdkerrors.Register(ModuleName, 301, "unsupported chain code")
ErrUnsupportedKeyCurve = sdkerrors.Register(ModuleName, 302, "unsupported key curve") ErrUnsupportedKeyCurve = sdkerrors.Register(ModuleName, 302, "unsupported key curve")