UNPKG

7.35 kBTypeScriptView Raw
1/// <reference types="node" />
2export declare type TypedData = string | EIP712TypedData | EIP712TypedData[];
3interface EIP712TypedData {
4 name: string;
5 type: string;
6 value: any;
7}
8export declare type Version = 'V1' | 'V2' | 'V3' | 'V4';
9export interface EthEncryptedData {
10 version: string;
11 nonce: string;
12 ephemPublicKey: string;
13 ciphertext: string;
14}
15export declare type SignedMsgParams<D> = Required<MsgParams<D>>;
16export interface MsgParams<D> {
17 data: D;
18 sig?: string;
19}
20interface MessageTypeProperty {
21 name: string;
22 type: string;
23}
24interface MessageTypes {
25 EIP712Domain: MessageTypeProperty[];
26 [additionalProperties: string]: MessageTypeProperty[];
27}
28export interface TypedMessage<T extends MessageTypes> {
29 types: T;
30 primaryType: keyof T;
31 domain: {
32 name?: string;
33 version?: string;
34 chainId?: number;
35 verifyingContract?: string;
36 };
37 message: Record<string, unknown>;
38}
39declare const TYPED_MESSAGE_SCHEMA: {
40 type: string;
41 properties: {
42 types: {
43 type: string;
44 additionalProperties: {
45 type: string;
46 items: {
47 type: string;
48 properties: {
49 name: {
50 type: string;
51 };
52 type: {
53 type: string;
54 };
55 };
56 required: string[];
57 };
58 };
59 };
60 primaryType: {
61 type: string;
62 };
63 domain: {
64 type: string;
65 };
66 message: {
67 type: string;
68 };
69 };
70 required: string[];
71};
72/**
73 * A collection of utility functions used for signing typed data
74 */
75declare const TypedDataUtils: {
76 /**
77 * Encodes an object by encoding and concatenating each of its members
78 *
79 * @param {string} primaryType - Root type
80 * @param {Object} data - Object to encode
81 * @param {Object} types - Type definitions
82 * @returns {Buffer} - Encoded representation of an object
83 */
84 encodeData(primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, useV4?: boolean): Buffer;
85 /**
86 * Encodes the type of an object by encoding a comma delimited list of its members
87 *
88 * @param {string} primaryType - Root type to encode
89 * @param {Object} types - Type definitions
90 * @returns {string} - Encoded representation of the type of an object
91 */
92 encodeType(primaryType: string, types: Record<string, MessageTypeProperty[]>): string;
93 /**
94 * Finds all types within a type definition object
95 *
96 * @param {string} primaryType - Root type
97 * @param {Object} types - Type definitions
98 * @param {Array} results - current set of accumulated types
99 * @returns {Array} - Set of all types found in the type definition
100 */
101 findTypeDependencies(primaryType: string, types: Record<string, MessageTypeProperty[]>, results?: string[]): string[];
102 /**
103 * Hashes an object
104 *
105 * @param {string} primaryType - Root type
106 * @param {Object} data - Object to hash
107 * @param {Object} types - Type definitions
108 * @returns {Buffer} - Hash of an object
109 */
110 hashStruct(primaryType: string, data: Record<string, unknown>, types: Record<string, unknown>, useV4?: boolean): Buffer;
111 /**
112 * Hashes the type of an object
113 *
114 * @param {string} primaryType - Root type to hash
115 * @param {Object} types - Type definitions
116 * @returns {Buffer} - Hash of an object
117 */
118 hashType(primaryType: string, types: Record<string, unknown>): Buffer;
119 /**
120 * Removes properties from a message object that are not defined per EIP-712
121 *
122 * @param {Object} data - typed message object
123 * @returns {Object} - typed message object with only allowed fields
124 */
125 sanitizeData<T extends MessageTypes>(data: string | EIP712TypedData | EIP712TypedData[] | TypedMessage<T>): TypedMessage<T>;
126 /**
127 * Signs a typed message as per EIP-712 and returns its keccak hash
128 *
129 * @param {Object} typedData - Types message data to sign
130 * @returns {Buffer} - keccak hash of the resulting signed message
131 */
132 sign<T_1 extends MessageTypes>(typedData: string | EIP712TypedData[] | Partial<EIP712TypedData> | Partial<TypedMessage<T_1>>, useV4?: boolean): Buffer;
133};
134declare function concatSig(v: Buffer, r: Buffer, s: Buffer): string;
135declare function normalize(input: number | string): string;
136declare function personalSign<T extends MessageTypes>(privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string;
137declare function recoverPersonalSignature<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string;
138declare function extractPublicKey<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string;
139declare function externalTypedSignatureHash(typedData: EIP712TypedData[]): string;
140declare function signTypedDataLegacy<T extends MessageTypes>(privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string;
141declare function recoverTypedSignatureLegacy<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string;
142declare function encrypt<T extends MessageTypes>(receiverPublicKey: string, msgParams: MsgParams<TypedData | TypedMessage<T>>, version: string): EthEncryptedData;
143declare function encryptSafely<T extends MessageTypes>(receiverPublicKey: string, msgParams: MsgParams<TypedData | TypedMessage<T>>, version: string): EthEncryptedData;
144declare function decrypt(encryptedData: EthEncryptedData, receiverPrivateKey: string): string;
145declare function decryptSafely(encryptedData: EthEncryptedData, receiverPrivateKey: string): string;
146declare function getEncryptionPublicKey(privateKey: string): string;
147/**
148 * A generic entry point for all typed data methods to be passed, includes a version parameter.
149 */
150declare function signTypedMessage<T extends MessageTypes>(privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>, version?: Version): string;
151declare function recoverTypedMessage<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>, version?: Version): string;
152declare function signTypedData<T extends MessageTypes>(privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string;
153declare function signTypedData_v4<T extends MessageTypes>(privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string;
154declare function recoverTypedSignature<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string;
155declare function recoverTypedSignature_v4<T extends MessageTypes>(msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string;
156export { TYPED_MESSAGE_SCHEMA, TypedDataUtils, concatSig, normalize, personalSign, recoverPersonalSignature, extractPublicKey, externalTypedSignatureHash as typedSignatureHash, signTypedDataLegacy, recoverTypedSignatureLegacy, encrypt, encryptSafely, decrypt, decryptSafely, getEncryptionPublicKey, signTypedMessage, recoverTypedMessage, signTypedData, signTypedData_v4, recoverTypedSignature, recoverTypedSignature_v4, };