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 prefix handling for esdt prefix #526

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ describe("test tokens and token computer", async () => {
let nonce = tokenComputer.extractNonceFromExtendedIdentifier(extendedIdentifier);
assert.equal(nonce, 10);

const extendedIdentifierWithPrefix = "test-TEST-123456-0a";
nonce = tokenComputer.extractNonceFromExtendedIdentifier(extendedIdentifierWithPrefix);
assert.equal(nonce, 10);

const fungibleTokenIdentifier = "FNG-123456";
nonce = tokenComputer.extractNonceFromExtendedIdentifier(fungibleTokenIdentifier);
assert.equal(nonce, 0);

const fungibleTokenIdentifierWithPrefix = "fun-FNG-123456";
nonce = tokenComputer.extractNonceFromExtendedIdentifier(fungibleTokenIdentifierWithPrefix);
assert.equal(nonce, 0);
});

it("should extract identifier from extended identifier", async () => {
Expand Down
36 changes: 22 additions & 14 deletions src/tokens.ts
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -236,46 +236,50 @@ export class TokenComputer {
extractNonceFromExtendedIdentifier(identifier: string): number {
const parts = identifier.split("-");

this.checkIfExtendedIdentifierWasProvided(parts);
this.checkLengthOfRandomSequence(parts[1]);
const { prefix, ticker, randomSequence } = this.splitIdentifierIntoComponents(identifier);
this.validateExtendedIdentifier(prefix, randomSequence, ticker, parts);

// in case the identifier of a fungible token is provided
if (parts.length == 2) {
// If identifier is for a fungible token (2 parts or 3 with prefix), return 0
if (parts.length === 2 || (prefix && parts.length === 3)) {
return 0;
}

const hexNonce = Buffer.from(parts[2], "hex");
return decodeUnsignedNumber(hexNonce);
// Otherwise, decode the last part as an unsigned number
const hecNonce = parts[parts.length - 1];
popenta marked this conversation as resolved.
Show resolved Hide resolved
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
return decodeUnsignedNumber(Buffer.from(hecNonce, "hex"));
}

extractIdentifierFromExtendedIdentifier(identifier: string): string {
const parts = identifier.split("-");
const { prefix, ticker, randomSequence } = this.splitIdentifierIntoComponents(parts);
const { prefix, ticker, randomSequence } = this.splitIdentifierIntoComponents(identifier);

this.checkIfExtendedIdentifierWasProvided(parts);
this.ensureTokenTickerValidity(ticker);
this.checkLengthOfRandomSequence(randomSequence);
this.validateExtendedIdentifier(prefix, randomSequence, ticker, parts);
if (prefix) {
this.checkLengthOfPrefix(prefix);
return prefix + "-" + ticker + "-" + randomSequence;
Copy link
Contributor

Choose a reason for hiding this comment

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

We could have had string interpolation / optional 🙃

}
return ticker + "-" + randomSequence;
}

private validateExtendedIdentifier(prefix: string, randomSequence: string, ticker: string, parts: string[]): void {
popenta marked this conversation as resolved.
Show resolved Hide resolved
this.checkIfExtendedIdentifierWasProvided(prefix, parts);
this.ensureTokenTickerValidity(ticker);
this.checkLengthOfRandomSequence(randomSequence);
}

private splitIdentifierIntoComponents(identifier: string): { prefix: any; ticker: any; randomSequence: any } {
const parts = identifier.split("-");
if (parts.length >= 3 && parts[2].length === this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
if (this.isLowercaseAlphanumeric(parts[0]))
return { prefix: parts[0], ticker: parts[1], randomSequence: parts[2] };
}

return { prefix: null, ticker: parts[0], randomSequence: parts[1] };
}

private checkIfExtendedIdentifierWasProvided(tokenParts: string[]): void {
private checkIfExtendedIdentifierWasProvided(prefix: string, tokenParts: string[]): void {
// this is for the identifiers of fungible tokens
const MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 2;
// this is for the identifiers of nft, sft and meta-esdt
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 4;
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = prefix ? 4 : 3;

if (
tokenParts.length < MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED ||
Expand All @@ -285,6 +289,10 @@ export class TokenComputer {
}
}

private isLowercaseAlphanumeric(str: string): boolean {
return /^[a-z0-9]+$/.test(str);
}

private checkLengthOfRandomSequence(randomSequence: string): void {
if (randomSequence.length !== this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
throw new ErrInvalidTokenIdentifier(
Expand Down
Loading