Skip to content

Commit

Permalink
Fixed built in private key interface error messages. (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hathoriel authored Sep 7, 2021
1 parent b29929a commit 752f100
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.24.8",
"version": "1.24.9",
"description": "Tatum API client allows browsers and Node.js clients to interact with Tatum API.",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
25 changes: 19 additions & 6 deletions src/model/validation/Mint721BuiltInPrivateKeyValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import {SignatureIdValidator} from './SignatureIdValidator';

@ValidatorConstraint({name: 'builtInPrivateKey', async: false})
export class Mint721BuiltInPrivateKeyValidator implements ValidatorConstraintInterface {

private message: string | null = null;

public defaultMessage(validationArguments?: ValidationArguments) {
if (this.message) {
return this.message;
}
return 'If you fill signatureId or privateKey/secret/fromPrivateKey, then tokenId, contractAddress must be present.';
}

Expand All @@ -16,12 +22,12 @@ export class Mint721BuiltInPrivateKeyValidator implements ValidatorConstraintInt
if (isAllowedChain) {
return true
} else {
if(!this.validateNonBuiltInPrivateKey(data, validationArguments)) {
if (!this.validateNonBuiltInPrivateKey(data, validationArguments)) {
return false
}
}
} else {
if(!this.validateNonBuiltInPrivateKey(data, validationArguments)) {
if (!this.validateNonBuiltInPrivateKey(data, validationArguments)) {
return false
}
}
Expand All @@ -30,30 +36,37 @@ export class Mint721BuiltInPrivateKeyValidator implements ValidatorConstraintInt
}

private validateNonBuiltInPrivateKey(data: any, validationArguments?: ValidationArguments) {
if (data.chain === Currency.CELO && (!data.feeCurrency || ![Currency.CELO, Currency.CUSD, Currency.CEUR].includes(data.feeCurrency) )) {
if (data.chain === Currency.CELO && (!data.feeCurrency || ![Currency.CELO, Currency.CUSD, Currency.CEUR].includes(data.feeCurrency))) {
this.message = 'CELO chain must have assigned feeCurrency field.'
return false
}

if (!data.tokenId || !maxLength(data.tokenId, 256)) {
this.message = 'Field tokenId must have 256 digits maximum.';
return false
}

if (!data.contractAddress || !maxLength(data.contractAddress, 43) || !minLength(data.contractAddress, 42)) {
this.message = 'Field contractAddress must have between 42 and 43 characters.';
return false
}

if(!data.fromPrivateKey && !data.signatureId) {

if (!data.fromPrivateKey && !data.signatureId) {
this.message = 'Field fromPrivateKey or signatureId must be filled.';
return false
}

if (data.fromPrivateKey) {
if(!maxLength(data.fromPrivateKey, 66) || !minLength(data.fromPrivateKey, 64)) {
if (!maxLength(data.fromPrivateKey, 66) || !minLength(data.fromPrivateKey, 64)) {
this.message = 'Field fromPrivateKey must have between 64 and 66 characters.';
return false
}
}

if(data.signatureId) {
if (data.signatureId) {
if (!maxLength(data.signatureId, 36) || !minLength(data.signatureId, 36) || !isUUID(data.signatureId, 4)) {
this.message = 'Field fromPrivateKey must have 36 characters and must be in form of UUID.';
return false;
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/model/validation/mint721BuiltInPrivateKeyValidator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Currency } from '../request'
import { mintNFTWithUri } from '../../nft'

describe('Mint721BuiltInPrivateKeyValidator tests', () => {
it('should test mint CELO with signature id', async () => {
const txId = await mintNFTWithUri(true, {
"chain": Currency.CELO,
"to": "0xBC2eBA680EE50d685cc4Fe65f102AA70AfB27D3F",
"url": "https://www.seznam.cz",
"feeCurrency": Currency.CELO,
"tokenId": "1",
"signatureId": "e23c9cb0-0650-4d41-b8c1-dfa3f9b76fad",
"contractAddress": "0x45871ED5F15203C0ce791eFE5f4B5044833aE10e",
})
expect(txId).not.toBeNull()
})

it('should test mint CELO with private key', async () => {
const txId = await mintNFTWithUri(true, {
"chain": Currency.CELO,
"to": "0xBC2eBA680EE50d685cc4Fe65f102AA70AfB27D3F",
"url": "https://www.seznam.cz",
"feeCurrency": Currency.CELO,
"tokenId": "1",
"fromPrivateKey": "89f09a62c9601d660dcdbeab15fbecfc07933971465fab3ba9fe1354035d805d",
"contractAddress": "0x45871ED5F15203C0ce791eFE5f4B5044833aE10e",
})
expect(txId).not.toBeNull()
})

it('should test mint CELO without private key or signature id', async () => {
const txId = await mintNFTWithUri(true, {
"chain": Currency.CELO,
"to": "0xBC2eBA680EE50d685cc4Fe65f102AA70AfB27D3F",
"url": "https://www.seznam.cz",
"feeCurrency": Currency.CELO,
})
expect(txId).not.toBeNull()
})

it('should test mint TRON without private key or signature id', async () => {
const t = async () => await mintNFTWithUri(true, {
"chain": Currency.TRON,
"to": "0xBC2eBA680EE50d685cc4Fe65f102AA70AfB27D3F",
"url": "https://www.seznam.cz",
})
await expect(t()).rejects.toThrow()
})

})

0 comments on commit 752f100

Please sign in to comment.