Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name property to Accounts #2081

Merged
merged 16 commits into from
Nov 6, 2024

Conversation

hectorgomezv
Copy link
Member

Summary

This PR adds the name property to the Account entity. As this property is intended to be encrypted at the database level, and a unique constraint should also be applied to that column, an additional name_hash is added. The unique constraint is applied to the name_hash, so we can ensure name cannot contain duplicates once they are decrypted (the name values cannot be compared directly as they are encrypted).

Changes

  • Add a migration to add name and name_hash columns to the accounts table.
  • Adds the name property to the Account entity.
  • Modifies the business logic to create an account, by including the name field.

@hectorgomezv hectorgomezv self-assigned this Oct 31, 2024
@hectorgomezv hectorgomezv requested a review from a team as a code owner October 31, 2024 14:59
const hash = crypto.createHash('sha256');
hash.update(name);
const nameHash = hash.digest('hex');
// TODO: encrypt the name
Copy link
Member Author

@hectorgomezv hectorgomezv Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual name encryption will be added in a forthcoming PR, along with AWS KMS support, which is needed to encrypt the name.

@hectorgomezv hectorgomezv force-pushed the 1898-user-accounts-add-username-to-account branch from 34ab268 to cfc7732 Compare October 31, 2024 15:52
@hectorgomezv hectorgomezv marked this pull request as draft October 31, 2024 16:09
@hectorgomezv hectorgomezv marked this pull request as ready for review October 31, 2024 22:28
}),
]),
);
});

it.todo('should fail if the name already exists');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 Added in 2069930

Comment on lines 11 to 13
// Note: regular expression is simplified because faker has limited support for regex.
// https://fakerjs.dev/api/helpers#fromregexp
.with('name', faker.helpers.fromRegExp(/[a-zA-Z0-9]{12,20}/i))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: what do you think about extracting this considering it's also used in the accountBuilder? Otherwise we could use one of the word faker methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extracted it in 2069930

Comment on lines 6 to 14
name: z
.string()
.min(3, { message: 'Account names must be at least 3 characters long' })
.max(20, { message: 'Account names must be at most 20 characters long' })
.regex(/^[a-zA-Z0-9]+(?:[._-][a-zA-Z0-9]+)*$/, {
message:
'Account names must start with a letter or number and can contain alphanumeric characters, periods, underscores, and hyphens',
}),
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before regarding extracting. We could have a AccountNameSchema, etc. that is tested and extended for this and the AccountSchema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2069930

@@ -68,4 +75,8 @@ describe('CreateAccountDtoSchema', () => {
);
});
});

describe('name', () => {
// TODO: Add tests for name validation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2069930

@hectorgomezv hectorgomezv requested a review from iamacook November 4, 2024 15:51
}),
]),
);
});

it('should fail if the name already exists', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check that the name hashes are unique?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree, but I think we cannot test that at the datasource level as the hash is not included in the function input parameters, it is calculated internally. I've added a test to verify the hashes are unique (the uniqueness constraint is applied) to the migration tests (00009_account-names.spec.ts) in 30b3e87

@hectorgomezv hectorgomezv requested a review from iamacook November 5, 2024 15:31
export function accountBuilder(): IBuilder<Account> {
return new Builder<Account>()
.with('id', faker.number.int({ max: DB_MAX_SAFE_INTEGER }))
.with('group_id', faker.number.int({ max: DB_MAX_SAFE_INTEGER }))
.with('address', getAddress(faker.finance.ethereumAddress()))
.with('name', faker.helpers.fromRegExp(accountNameSimpleRegex))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: what do you think about creating a specific builder for this and using that everywhere to ensure the fromRegexp is used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fc749ad

@hectorgomezv hectorgomezv requested a review from iamacook November 6, 2024 16:11
@hectorgomezv hectorgomezv merged commit af13f38 into main Nov 6, 2024
20 checks passed
@hectorgomezv hectorgomezv deleted the 1898-user-accounts-add-username-to-account branch November 6, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants