Skip to content

Commit

Permalink
fix: remove eip712 reference
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 9, 2021
1 parent e528f7d commit 039a360
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export * as stark from './utils/stark';
export * as ec from './utils/ellipticCurve';
export * as uint256 from './utils/uint256';
export * as shortString from './utils/shortString';
export * as eip712 from './utils/eip712';
export * as typedData from './utils/typedData';
8 changes: 4 additions & 4 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import assert from 'minimalistic-assert';

import { Provider } from '../provider';
import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
import { TypedData, getMessageHash } from '../utils/eip712';
import { sign } from '../utils/ellipticCurve';
import { addHexPrefix } from '../utils/encode';
import { hashMessage } from '../utils/hash';
import { toBN } from '../utils/number';
import { getSelectorFromName } from '../utils/stark';
import { TypedData, getMessageHash } from '../utils/typedData';
import { SignerInterface } from './interface';

export class Signer extends Provider implements SignerInterface {
Expand Down Expand Up @@ -78,8 +78,8 @@ export class Signer extends Provider implements SignerInterface {
* @returns the signature of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
public async sign(typedData: TypedData): Promise<Signature> {
return sign(this.keyPair, await this.hash(typedData));
public async signMessage(typedData: TypedData): Promise<Signature> {
return sign(this.keyPair, await this.hashMessage(typedData));
}

/**
Expand All @@ -89,7 +89,7 @@ export class Signer extends Provider implements SignerInterface {
* @returns the hash of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
public async hash(typedData: TypedData): Promise<string> {
public async hashMessage(typedData: TypedData): Promise<string> {
return getMessageHash(typedData, this.address);
}
}
6 changes: 3 additions & 3 deletions src/signer/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Provider } from '../provider';
import { AddTransactionResponse, Signature, Transaction } from '../types';
import { TypedData } from '../utils/eip712/types';
import { TypedData } from '../utils/typedData/types';

export abstract class SignerInterface extends Provider {
public abstract address: string;
Expand All @@ -23,7 +23,7 @@ export abstract class SignerInterface extends Provider {
* @returns the signature of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
public abstract sign(typedData: TypedData): Promise<Signature>;
public abstract signMessage(typedData: TypedData): Promise<Signature>;

/**
* Hash a JSON object with pederson hash and return the hash
Expand All @@ -32,5 +32,5 @@ export abstract class SignerInterface extends Provider {
* @returns the hash of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
public abstract hash(typedData: TypedData): Promise<string>;
public abstract hashMessage(typedData: TypedData): Promise<string>;
}
2 changes: 1 addition & 1 deletion src/utils/eip712/index.ts → src/utils/typedData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const getStructHash = <T extends TypedData>(
export const getMessageHash = (typedData: TypedData, account: BigNumberish): string => {
const message = [
encodeShortString('StarkNet Message'),
getStructHash(typedData, 'EIP712Domain', typedData.domain),
getStructHash(typedData, 'StarkNetDomain', typedData.domain),
account,
getStructHash(typedData, typedData.primaryType, typedData.message),
];
Expand Down
21 changes: 9 additions & 12 deletions src/utils/eip712/types.ts → src/utils/typedData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
number,
object,
optional,
pattern,
record,
refine,
string,
Expand Down Expand Up @@ -44,7 +43,7 @@ const TYPE = refine(string(), 'Type', (type, context) => {
return isValidType(context.branch[0].types, type);
});

export const EIP_712_TYPE = object({
export const STARKNET_TYPE = object({
name: string(),
type: TYPE,
});
Expand All @@ -55,32 +54,30 @@ export const EIP_712_TYPE = object({
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
* standard.
*/
export type EIP712Type = Infer<typeof EIP_712_TYPE>;
export type StarkNetType = Infer<typeof STARKNET_TYPE>;

export const EIP_712_DOMAIN_TYPE = object({
export const STARKNET_DOMAIN_TYPE = object({
name: optional(string()),
version: optional(string()),
chainId: optional(union([string(), number()])),
verifyingContract: optional(pattern(string(), /^0x[0-9a-z]{40}$/i)),
salt: optional(union([array(number()), pattern(string(), /^0x[0-9a-z]{64}$/i)])),
});

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

export const EIP_712_TYPED_DATA_TYPE = object({
export const STARKNET_TYPED_DATA_TYPE = object({
types: intersection([
t({ EIP712Domain: array(EIP_712_TYPE) }),
record(string(), array(EIP_712_TYPE)),
t({ StarkNetDomain: array(STARKNET_TYPE) }),
record(string(), array(STARKNET_TYPE)),
]),
primaryType: string(),
domain: EIP_712_DOMAIN_TYPE,
domain: STARKNET_DOMAIN_TYPE,
message: object(),
});

/**
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
*/
export type TypedData = Infer<typeof EIP_712_TYPED_DATA_TYPE>;
export type TypedData = Infer<typeof STARKNET_TYPED_DATA_TYPE>;
4 changes: 2 additions & 2 deletions src/utils/eip712/utils.ts → src/utils/typedData/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { is } from 'superstruct';

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

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

0 comments on commit 039a360

Please sign in to comment.