Skip to content

Commit

Permalink
Fix check for prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 8, 2024
1 parent e3ab881 commit 3c58ca9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
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
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), 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];
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;
}
return ticker + "-" + randomSequence;
}

private validateExtendedIdentifier(prefix: string, randomSequence: string, ticker: string, parts: string[]): void {
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

0 comments on commit 3c58ca9

Please sign in to comment.