All files / tools validate-remark.ts

96% Statements 72/75
58.33% Branches 14/24
100% Functions 10/10
96% Lines 72/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                              5x           5x                   5x                   5x 71x 71x       71x 71x           5x         5x       5x       5x         5x         5x         5x 694x 694x 1x   693x 2x   691x 309x           5x   213x   213x 213x 114x 114x   99x           5x   280x   280x 280x 140x 140x   145x           5x   81x   81x 81x 71x   10x           5x   20x   20x 20x 5x   15x           5x   51x   51x 51x 22x   29x           5x   3x   3x 3x 3x               5x   17x   17x 17x 15x   2x           5x   23x   23x 23x 9x   14x 14x          
import { OP_TYPES, PREFIX, VERSION } from "./constants";
import {
  assert,
  object,
  define,
  string,
  pattern,
  number,
  any,
  optional,
  type,
  is,
} from "superstruct";
import { getRemarkData } from "./utils";
 
const DataStruct = object({
  protocol: string(),
  data: any(),
  type: string(),
});
 
const CollectionStruct = type({
  name: string(),
  max: number(),
  issuer: string(),
  symbol: string(),
  id: string(),
  metadata: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))),
  data: optional(DataStruct),
});
 
const NFTStruct = type({
  name: string(),
  collection: string(),
  instance: string(),
  transferable: number(),
  sn: string(),
  data: optional(DataStruct),
  metadata: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))),
});
 
const IsBigInt = define("BigInt", (value: any) => {
  try {
    Iif (!is(value, string())) {
      return false;
    }
 
    const priceBigInt = BigInt(parseInt(value));
    return typeof priceBigInt === "bigint";
  } catch (error) {
    return false;
  }
});
 
const LISTStruct = type({
  id: string(),
  price: IsBigInt,
});
 
const BUYStruct = type({
  id: string(),
});
 
const CONSUMEStruct = type({
  id: string(),
});
 
const SENDStruct = type({
  id: string(),
  recipient: string(),
});
 
const EMOTEStruct = type({
  id: string(),
  unicode: string(),
});
 
const CHANGEISSUERStruct = type({
  id: string(),
  issuer: string(),
});
 
export const validateBase = (remark: string, opType: OP_TYPES) => {
  const [prefix, op_type, version] = remark.split("::");
  if (prefix.toUpperCase() !== PREFIX) {
    throw new Error("Invalid remark - does not start with RMRK");
  }
  if (op_type !== opType) {
    throw new Error(`The op code needs to be ${opType}, but it is ${op_type}`);
  }
  if (version !== VERSION) {
    throw new Error(
      `This remark was issued under version ${version} instead of ${VERSION}`
    );
  }
};
 
export const validateCollection = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, dataString] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.MINT);
    const obj = getRemarkData(dataString);
    return assert(obj, CollectionStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateNFT = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, dataString] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.MINTNFT);
    const obj = getRemarkData(dataString);
    return assert(obj, NFTStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateList = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id, price] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.LIST);
    return assert({ id, price }, LISTStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateSend = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id, recipient] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.SEND);
    return assert({ id, recipient }, SENDStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateEmote = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id, unicode] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.EMOTE);
    return assert({ id, unicode }, EMOTEStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateChangeIssuer = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id, issuer] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.CHANGEISSUER);
    return assert({ id, issuer }, CHANGEISSUERStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateBuy = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.BUY);
    return assert({ id }, BUYStruct);
  } catch (error) {
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};
 
export const validateConsume = (remark: string): any => {
  // With array destructuring it's important to not remove unused destructured variables, as order is important
  const [_prefix, _op_type, _version, id] = remark.split("::");
 
  try {
    validateBase(remark, OP_TYPES.CONSUME);
    return assert({ id }, CONSUMEStruct);
  } catch (error) {
    console.log("StructError is:", error);
    throw new Error(
      error?.message || "Something went wrong during remark validation"
    );
  }
};