Skip to content

Commit

Permalink
fix: remove superstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed May 30, 2022
1 parent e42f75b commit 6f13cf0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 84 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"json-bigint": "^1.0.0",
"minimalistic-assert": "^1.0.1",
"pako": "^2.0.4",
"superstruct": "^0.15.3",
"url-join": "^4.0.1"
},
"lint-staged": {
Expand Down
83 changes: 15 additions & 68 deletions src/utils/typedData/types.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,29 @@
import {
Infer,
array,
intersection,
number,
object,
optional,
record,
refine,
string,
type as t,
union,
} from 'superstruct';

export const ATOMIC_TYPES = ['felt', 'felt*'];

// Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
// and modified to support starknet types

/**
* Checks if a type is valid with the given `typedData`. The following types are valid:
* - Atomic types: felt, felt*
* - Reference types: struct type (e.g. SomeStruct)
*
* @param {Record<string, unknown>} types
* @param {string} type
* @return {boolean}
*/
export const isValidType = (types: Record<string, unknown>, type: string): boolean => {
if (ATOMIC_TYPES.includes(type as string)) {
return true;
}

if (types[type]) {
return true;
}

return false;
};

const TYPE = refine(string(), 'Type', (type, context) => {
return isValidType(context.branch[0].types, type);
});

export const STARKNET_TYPE = object({
name: string(),
type: TYPE,
});

/**
* A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
*
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
* standard.
*/
export type StarkNetType = Infer<typeof STARKNET_TYPE>;

export const STARKNET_DOMAIN_TYPE = object({
name: optional(string()),
version: optional(string()),
chainId: optional(union([string(), number()])),
});
export interface StarkNetType {
name: string;
type: 'felt' | 'felt*' | string;
}

/**
* The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
*/
export type StarkNetDomain = Infer<typeof STARKNET_DOMAIN_TYPE>;

export const STARKNET_TYPED_DATA_TYPE = object({
types: intersection([
t({ StarkNetDomain: array(STARKNET_TYPE) }),
record(string(), array(STARKNET_TYPE)),
]),
primaryType: string(),
domain: STARKNET_DOMAIN_TYPE,
message: object(),
});
export interface StarkNetDomain extends Record<string, unknown> {
name?: string;
version?: string;
chainId?: string | number;
}

/**
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
*/
export type TypedData = Infer<typeof STARKNET_TYPED_DATA_TYPE>;
export interface TypedData {
types: Record<string, StarkNetType[]>;
primaryType: string;
domain: StarkNetDomain;
message: Record<string, unknown>;
}
11 changes: 7 additions & 4 deletions src/utils/typedData/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { is } from 'superstruct';

import { STARKNET_TYPED_DATA_TYPE, TypedData } from './types';
import { TypedData } from './types';

/**
* Validates that `data` matches the EIP-712 JSON schema.
Expand All @@ -9,5 +7,10 @@ import { STARKNET_TYPED_DATA_TYPE, TypedData } from './types';
* @return {boolean}
*/
export const validateTypedData = (data: unknown): data is TypedData => {
return is(data, STARKNET_TYPED_DATA_TYPE);
const typedData = data as TypedData;

// Validate that the data matches the EIP-712 JSON schema
const valid = Boolean(typedData.types && typedData.primaryType && typedData.message);

return valid;
};

0 comments on commit 6f13cf0

Please sign in to comment.