Skip to content

Commit

Permalink
Refactor user profile and roles handling
Browse files Browse the repository at this point in the history
- Updated `BurnAndMintDto` to extend `SubmitCallDTO` instead of `ChainCallDTO`.
- Made `roles` in `UserProfile` optional and updated related types to ensure consistency.
- Changed `identityKey` in `ChainUserAPI` to use `UserAlias`.
- Adjusted user role handling in various tests and services to accommodate the new optional roles structure.
- Updated return types in `getUserProfile` functions to use `UserProfileWithRoles` for better type safety.

These changes enhance type safety and align the code with the latest SDK conventions.

Signed-off-by: Jakub Dzikowski <[email protected]>
  • Loading branch information
dzikowski committed Jan 23, 2025
1 parent f621116 commit 512a715
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 24 deletions.
13 changes: 2 additions & 11 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
{
"workbench.colorCustomizations": {
"titleBar.activeBackground": "#4f7bc6",
"titleBar.activeForeground": "#ffffff",
"activityBar.background": "#4f7bc6",
"statusBar.background": "#4f7bc6",
"statusBar.foreground": "#ffffff",
"statusBar.border": "#4f7bc6",
"statusBarItem.remoteBackground": "#4f7bc6",
"statusBarItem.remoteForeground": "#ffffff"
}
}
"files.eol": "\n"
}
7 changes: 5 additions & 2 deletions chain-api/src/types/UserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { IsNotEmpty, IsString, ValidateIf } from "class-validator";
import { IsNotEmpty, IsOptional, IsString, ValidateIf } from "class-validator";
import { JSONSchema } from "class-validator-jsonschema";

import { IsUserAlias } from "../validators";
Expand Down Expand Up @@ -56,8 +56,11 @@ export class UserProfile extends ChainObject {
.sort()
.join(", ")}, but you can use arbitrary strings to define your own roles.`
})
@IsOptional()
@IsString({ each: true })
roles: string[];
roles?: string[];
}

export const UP_INDEX_KEY = "GCUP";

export type UserProfileWithRoles = UserProfile & { roles: string[] };
2 changes: 1 addition & 1 deletion chain-api/src/types/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class BurnTokensDto extends SubmitCallDTO {
"Mints are executed under the identity of the calling user of this function. " +
"All operations occur in the same transaction, meaning either all succeed or none are written to chain."
})
export class BurnAndMintDto extends ChainCallDTO {
export class BurnAndMintDto extends SubmitCallDTO {
static MAX_ARR_SIZE = 1000;

@JSONSchema({
Expand Down
8 changes: 8 additions & 0 deletions chain-api/src/types/dtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ export class ChainCallDTO {
}

public sign(privateKey: string, useDer = false): void {
if (useDer) {
if (this.signing === SigningScheme.TON) {
throw new ValidationFailedError("TON signing scheme does not support DER signatures");
} else {
this.signerPublicKey = signatures.getPublicKey(privateKey);
}
}

if (this.signing === SigningScheme.TON) {
const keyBuffer = Buffer.from(privateKey, "base64");
this.signature = signatures.ton.getSignature(this, keyBuffer, this.prefix).toString("base64");
Expand Down
4 changes: 3 additions & 1 deletion chain-client/src/api/ChainUserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { UserAlias } from "@gala-chain/api";

import { ChainClient, ChainUser } from "../generic";

export interface ChainUserAPI {
privateKey: string;
identityKey: string;
identityKey: UserAlias;
publicKey: string;
ethAddress: string;
}
Expand Down
2 changes: 1 addition & 1 deletion chain-test/src/data/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ChainUserWithRoles {
ethAddress: string;
publicKey: string;
privateKey: string;
roles: string[] | undefined;
roles: string[];
}

export function randomUser(
Expand Down
2 changes: 1 addition & 1 deletion chain-test/src/unit/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Fixture<Ctx extends TestGalaChainContext, T extends GalaContract<Ctx>> {
this.ctx.callingUserData = {
alias: user.identityKey,
ethAddress: user.ethAddress,
roles: user.roles ?? []
roles: user.roles
};
return this;
}
Expand Down
3 changes: 2 additions & 1 deletion chaincode/src/contracts/PublicKeyContract.migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SubmitCallDTO,
UpdateUserRolesDto,
UserProfile,
UserProfileWithRoles,
UserRole,
createValidSubmitDTO
} from "@gala-chain/api";
Expand Down Expand Up @@ -60,7 +61,7 @@ describe("Migration from allowedOrgs to allowedRoles", () => {

async function getUserProfile() {
const dto = new ChainCallDTO().signed(user.privateKey);
const resp = await chaincode.invoke<GalaChainSuccessResponse<UserProfile>>(
const resp = await chaincode.invoke<GalaChainSuccessResponse<UserProfileWithRoles>>(
"PublicKeyContract:GetMyProfile",
dto
);
Expand Down
5 changes: 3 additions & 2 deletions chaincode/src/contracts/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PublicKey,
SigningScheme,
UserProfile,
UserProfileWithRoles,
ValidationFailedError,
signatures
} from "@gala-chain/api";
Expand Down Expand Up @@ -133,7 +134,7 @@ async function getUserProfile(
ctx: GalaChainContext,
publicKey: string,
signing: SigningScheme
): Promise<UserProfile> {
): Promise<UserProfileWithRoles> {
const address = PublicKeyService.getUserAddress(publicKey, signing);
const profile = await PublicKeyService.getUserProfile(ctx, address);

Expand All @@ -147,7 +148,7 @@ async function getUserProfile(
async function getUserProfileAndPublicKey(
ctx: GalaChainContext,
address
): Promise<{ profile: UserProfile; publicKey: PublicKey }> {
): Promise<{ profile: UserProfileWithRoles; publicKey: PublicKey }> {
const profile = await PublicKeyService.getUserProfile(ctx, address);

if (profile === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion chaincode/src/contracts/authorize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe("authorization", () => {

const user = {
...ChainUser.withRandomKeys(customUser.alias),
roles: customUser.roles
roles: customUser.roles ?? [...UserProfile.DEFAULT_ROLES]
};

const ContractClass = TestContractClass(type, verifySignature, allowedOrgs, allowedRoles);
Expand Down
10 changes: 7 additions & 3 deletions chaincode/src/services/PublicKeyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
UnauthorizedError,
UserAlias,
UserProfile,
UserProfileWithRoles,
normalizePublicKey,
signatures
} from "@gala-chain/api";
Expand Down Expand Up @@ -97,7 +98,10 @@ export class PublicKeyService {
: signatures.getEthAddress(signatures.getNonCompactHexPublicKey(publicKey));
}

public static async getUserProfile(ctx: Context, address: string): Promise<UserProfile | undefined> {
public static async getUserProfile(
ctx: Context,
address: string
): Promise<UserProfileWithRoles | undefined> {
const key = PublicKeyService.getUserProfileKey(ctx, address);
const data = await ctx.stub.getState(key);

Expand All @@ -108,7 +112,7 @@ export class PublicKeyService {
userProfile.roles = Array.from(UserProfile.DEFAULT_ROLES);
}

return userProfile;
return userProfile as UserProfileWithRoles;
}

// check if we want the profile of the admin
Expand All @@ -135,7 +139,7 @@ export class PublicKeyService {
adminProfile.alias = alias;
adminProfile.roles = Array.from(UserProfile.ADMIN_ROLES);

return adminProfile;
return adminProfile as UserProfileWithRoles;
}
}

Expand Down

0 comments on commit 512a715

Please sign in to comment.