|
| 1 | +import { computeHashOnElements } from '../hash'; |
| 2 | +import { BigNumberish } from '../number'; |
| 3 | +import { encodeShortString } from '../shortString'; |
| 4 | +import { getSelectorFromName } from '../stark'; |
| 5 | +import { TypedData } from './types'; |
| 6 | +import { validateTypedData } from './utils'; |
| 7 | + |
| 8 | +export * from './types'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once |
| 12 | + * in the resulting array. |
| 13 | + * |
| 14 | + * @param {TypedData} typedData |
| 15 | + * @param {string} type |
| 16 | + * @param {string[]} [dependencies] |
| 17 | + * @return {string[]} |
| 18 | + */ |
| 19 | +export const getDependencies = ( |
| 20 | + typedData: TypedData, |
| 21 | + type: string, |
| 22 | + dependencies: string[] = [] |
| 23 | +): string[] => { |
| 24 | + // `getDependencies` is called by most other functions, so we validate the JSON schema here |
| 25 | + if (!validateTypedData(typedData)) { |
| 26 | + throw new Error('Typed data does not match JSON schema'); |
| 27 | + } |
| 28 | + |
| 29 | + if (dependencies.includes(type)) { |
| 30 | + return dependencies; |
| 31 | + } |
| 32 | + |
| 33 | + if (!typedData.types[type]) { |
| 34 | + return dependencies; |
| 35 | + } |
| 36 | + |
| 37 | + return [ |
| 38 | + type, |
| 39 | + ...typedData.types[type].reduce<string[]>( |
| 40 | + (previous, t) => [ |
| 41 | + ...previous, |
| 42 | + ...getDependencies(typedData, t.type, previous).filter( |
| 43 | + (dependency) => !previous.includes(dependency) |
| 44 | + ), |
| 45 | + ], |
| 46 | + [] |
| 47 | + ), |
| 48 | + ]; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Encode a type to a string. All dependant types are alphabetically sorted. |
| 53 | + * |
| 54 | + * @param {TypedData} typedData |
| 55 | + * @param {string} type |
| 56 | + * @return {string} |
| 57 | + */ |
| 58 | +export const encodeType = (typedData: TypedData, type: string): string => { |
| 59 | + const [primary, ...dependencies] = getDependencies(typedData, type); |
| 60 | + const types = [primary, ...dependencies.sort()]; |
| 61 | + |
| 62 | + return types |
| 63 | + .map((dependency) => { |
| 64 | + return `${dependency}(${typedData.types[dependency].map((t) => `${t.type} ${t.name}`)})`; |
| 65 | + }) |
| 66 | + .join(''); |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Get a type string as hash. |
| 71 | + * |
| 72 | + * @param {TypedData} typedData |
| 73 | + * @param {string} type |
| 74 | + * @return {string} |
| 75 | + */ |
| 76 | +export const getTypeHash = (typedData: TypedData, type: string): string => { |
| 77 | + return getSelectorFromName(encodeType(typedData, type)); |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as tuple, which consists of |
| 82 | + * an array of ABI compatible types, and an array of corresponding values. |
| 83 | + * |
| 84 | + * @param {TypedData} typedData |
| 85 | + * @param {string} type |
| 86 | + * @param {any} data |
| 87 | + * @returns {[string, string]} |
| 88 | + */ |
| 89 | +const encodeValue = (typedData: TypedData, type: string, data: unknown): [string, string] => { |
| 90 | + if (typedData.types[type]) { |
| 91 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 92 | + return ['felt', getStructHash(typedData, type, data as Record<string, unknown>)]; |
| 93 | + } |
| 94 | + |
| 95 | + if (type === 'shortString') { |
| 96 | + return ['felt', encodeShortString(data as string)]; |
| 97 | + } |
| 98 | + |
| 99 | + if (type === 'felt*') { |
| 100 | + return ['felt', computeHashOnElements(data as string[])]; |
| 101 | + } |
| 102 | + |
| 103 | + return [type, data as string]; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All |
| 108 | + * dependant types are automatically encoded. |
| 109 | + * |
| 110 | + * @param {TypedData} typedData |
| 111 | + * @param {string} type |
| 112 | + * @param {Record<string, any>} data |
| 113 | + */ |
| 114 | +export const encodeData = <T extends TypedData>(typedData: T, type: string, data: T['message']) => { |
| 115 | + const [types, values] = typedData.types[type].reduce<[string[], string[]]>( |
| 116 | + ([ts, vs], field) => { |
| 117 | + if (data[field.name] === undefined || data[field.name] === null) { |
| 118 | + throw new Error(`Cannot encode data: missing data for '${field.name}'`); |
| 119 | + } |
| 120 | + |
| 121 | + const value = data[field.name]; |
| 122 | + const [t, encodedValue] = encodeValue(typedData, field.type, value); |
| 123 | + |
| 124 | + return [ |
| 125 | + [...ts, t], |
| 126 | + [...vs, encodedValue], |
| 127 | + ]; |
| 128 | + }, |
| 129 | + [['felt'], [getTypeHash(typedData, type)]] |
| 130 | + ); |
| 131 | + |
| 132 | + return [types, values]; |
| 133 | +}; |
| 134 | + |
| 135 | +/** |
| 136 | + * Get encoded data as a hash. The data should be a key -> value object with all the required values. All dependant |
| 137 | + * types are automatically encoded. |
| 138 | + * |
| 139 | + * @param {TypedData} typedData |
| 140 | + * @param {string} type |
| 141 | + * @param {Record<string, any>} data |
| 142 | + * @return {Buffer} |
| 143 | + */ |
| 144 | +export const getStructHash = <T extends TypedData>( |
| 145 | + typedData: T, |
| 146 | + type: string, |
| 147 | + data: T['message'] |
| 148 | +) => { |
| 149 | + return computeHashOnElements(encodeData(typedData, type, data)[1]); |
| 150 | +}; |
| 151 | + |
| 152 | +/** |
| 153 | + * Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed |
| 154 | + * with Keccak256. |
| 155 | + * |
| 156 | + * @param {TypedData} typedData |
| 157 | + * @param {boolean} hash |
| 158 | + * @return {string} |
| 159 | + */ |
| 160 | +export const getMessageHash = (typedData: TypedData, account: BigNumberish): string => { |
| 161 | + const message = [ |
| 162 | + encodeShortString('StarkNet Message'), |
| 163 | + getStructHash(typedData, 'EIP712Domain', typedData.domain), |
| 164 | + account, |
| 165 | + getStructHash(typedData, typedData.primaryType, typedData.message), |
| 166 | + ]; |
| 167 | + |
| 168 | + return computeHashOnElements(message); |
| 169 | +}; |
0 commit comments