-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
index.ts
979 lines (894 loc) · 27.7 KB
/
index.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
import * as ethUtil from 'ethereumjs-util';
import * as ethAbi from 'ethereumjs-abi';
import * as nacl from 'tweetnacl';
import * as naclUtil from 'tweetnacl-util';
import { intToHex, isHexString, stripHexPrefix } from 'ethjs-util';
import { padWithZeroes } from './utils';
/**
* This is the message format used for `V1` of `signTypedData`.
*/
export type TypedDataV1 = EIP712TypedData[];
/**
* This represents a single field in a `V1` `signTypedData` message.
*
* @property name - The name of the field.
* @property type - The type of a field (must be a supported Solidity type).
* @property value - The value of the field.
*/
export interface EIP712TypedData {
name: string;
type: string;
value: any;
}
export enum Version {
V1 = 'V1',
V3 = 'V3',
V4 = 'V4',
}
export interface EthEncryptedData {
version: string;
nonce: string;
ephemPublicKey: string;
ciphertext: string;
}
export interface MessageTypeProperty {
name: string;
type: string;
}
export interface MessageTypes {
EIP712Domain: MessageTypeProperty[];
[additionalProperties: string]: MessageTypeProperty[];
}
/**
* This is the message format used for `signTypeData`, for all versions
* except `V1`.
*
* @template T - The custom types used by this message.
* @property types - The custom types used by this message.
* @property primaryType - The type of the message.
* @property domain - Signing domain metadata. The signing domain is the intended context for the
* signature (e.g. the dapp, protocol, etc. that it's intended for). This data is used to
* construct the domain seperator of the message.
* @property domain.name - The name of the signing domain.
* @property domain.version - The current major version of the signing domain.
* @property domain.chainId - The chain ID of the signing domain.
* @property domain.verifyingContract - The address of the contract that can verify the signature.
* @property domain.salt - A disambiguating salt for the protocol.
* @property message - The message to be signed.
*/
export interface TypedMessage<T extends MessageTypes> {
types: T;
primaryType: keyof T;
domain: {
name?: string;
version?: string;
chainId?: number;
verifyingContract?: string;
salt?: ArrayBuffer;
};
message: Record<string, unknown>;
}
export const TYPED_MESSAGE_SCHEMA = {
type: 'object',
properties: {
types: {
type: 'object',
additionalProperties: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
type: { type: 'string', enum: getSolidityTypes() },
},
required: ['name', 'type'],
},
},
},
primaryType: { type: 'string' },
domain: { type: 'object' },
message: { type: 'object' },
},
required: ['types', 'primaryType', 'domain', 'message'],
};
/**
* Get a list of all Solidity types.
*
* @returns A list of all Solidity types.
*/
function getSolidityTypes() {
const types = ['bool', 'address', 'string', 'bytes'];
const ints = Array.from(new Array(32)).map(
(_, index) => `int${(index + 1) * 8}`,
);
const uints = Array.from(new Array(32)).map(
(_, index) => `uint${(index + 1) * 8}`,
);
const bytes = Array.from(new Array(32)).map(
(_, index) => `bytes${index + 1}`,
);
return [...types, ...ints, ...uints, ...bytes];
}
/**
* Validate that the given value is a valid version string.
*
* @param version - The version value to validate.
* @param allowedVersions - A list of allowed versions. If omitted, all versions are assumed to be
* allowed.
*/
function validateVersion(version: Version, allowedVersions?: Version[]) {
if (!Object.keys(Version).includes(version)) {
throw new Error(`Invalid version: '${version}'`);
} else if (allowedVersions && !allowedVersions.includes(version)) {
throw new Error(
`Version not allowed: '${version}'. Allowed versions are: ${allowedVersions.join(
', ',
)}`,
);
}
}
/**
* Returns `true` if the given value is nullish.
*
* @param value - The value being checked.
* @returns Whether the value is nullish.
*/
function isNullish(value) {
return value === null || value === undefined;
}
/**
* Encode a single field.
*
* @param types - All type definitions.
* @param name - The name of the field to encode.
* @param type - The type of the field being encoded.
* @param value - The value to encode.
* @param version - The EIP-712 version the encoding should comply with.
* @returns Encoded representation of the field.
*/
function encodeField(
types: Record<string, MessageTypeProperty[]>,
name: string,
type: string,
value: any,
version: Version.V3 | Version.V4,
): [type: string, value: any] {
validateVersion(version, [Version.V3, Version.V4]);
if (types[type] !== undefined) {
return [
'bytes32',
version === Version.V4 && value == null // eslint-disable-line no-eq-null
? '0x0000000000000000000000000000000000000000000000000000000000000000'
: ethUtil.keccak(encodeData(type, value, types, version)),
];
}
if (value === undefined) {
throw new Error(`missing value for field ${name} of type ${type}`);
}
if (type === 'bytes') {
return ['bytes32', ethUtil.keccak(value)];
}
if (type === 'string') {
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
if (typeof value === 'string') {
value = Buffer.from(value, 'utf8');
}
return ['bytes32', ethUtil.keccak(value)];
}
if (type.lastIndexOf(']') === type.length - 1) {
if (version === Version.V3) {
throw new Error(
'Arrays are unimplemented in encodeData; use V4 extension',
);
}
const parsedType = type.slice(0, type.lastIndexOf('['));
const typeValuePairs = value.map((item) =>
encodeField(types, name, parsedType, item, version),
);
return [
'bytes32',
ethUtil.keccak(
ethAbi.rawEncode(
typeValuePairs.map(([t]) => t),
typeValuePairs.map(([, v]) => v),
),
),
];
}
return [type, value];
}
/**
* Encodes an object by encoding and concatenating each of its members.
*
* @param primaryType - The root type.
* @param data - The object to encode.
* @param types - Type definitions for all types included in the message.
* @param version - The EIP-712 version the encoding should comply with.
* @returns An encoded representation of an object.
*/
function encodeData(
primaryType: string,
data: Record<string, unknown>,
types: Record<string, MessageTypeProperty[]>,
version: Version.V3 | Version.V4,
): Buffer {
validateVersion(version, [Version.V3, Version.V4]);
const encodedTypes = ['bytes32'];
const encodedValues: unknown[] = [hashType(primaryType, types)];
for (const field of types[primaryType]) {
if (version === Version.V3 && data[field.name] === undefined) {
continue;
}
const [type, value] = encodeField(
types,
field.name,
field.type,
data[field.name],
version,
);
encodedTypes.push(type);
encodedValues.push(value);
}
return ethAbi.rawEncode(encodedTypes, encodedValues);
}
/**
* Encodes the type of an object by encoding a comma delimited list of its members.
*
* @param primaryType - The root type to encode.
* @param types - Type definitions for all types included in the message.
* @returns An encoded representation of the primary type.
*/
function encodeType(
primaryType: string,
types: Record<string, MessageTypeProperty[]>,
): string {
let result = '';
const unsortedDeps = findTypeDependencies(primaryType, types);
unsortedDeps.delete(primaryType);
const deps = [primaryType, ...Array.from(unsortedDeps).sort()];
for (const type of deps) {
const children = types[type];
if (!children) {
throw new Error(`No type definition specified: ${type}`);
}
result += `${type}(${types[type]
.map(({ name, type: t }) => `${t} ${name}`)
.join(',')})`;
}
return result;
}
/**
* Finds all types within a type definition object.
*
* @param primaryType - The root type.
* @param types - Type definitions for all types included in the message.
* @param results - The current set of accumulated types.
* @returns The set of all types found in the type definition.
*/
function findTypeDependencies(
primaryType: string,
types: Record<string, MessageTypeProperty[]>,
results: Set<string> = new Set(),
): Set<string> {
[primaryType] = primaryType.match(/^\w*/u);
if (results.has(primaryType) || types[primaryType] === undefined) {
return results;
}
results.add(primaryType);
for (const field of types[primaryType]) {
findTypeDependencies(field.type, types, results);
}
return results;
}
/**
* Hashes an object.
*
* @param primaryType - The root type.
* @param data - The object to hash.
* @param types - Type definitions for all types included in the message.
* @param version - The EIP-712 version the encoding should comply with.
* @returns The hash of the object.
*/
function hashStruct(
primaryType: string,
data: Record<string, unknown>,
types: Record<string, MessageTypeProperty[]>,
version: Version.V3 | Version.V4,
): Buffer {
validateVersion(version, [Version.V3, Version.V4]);
return ethUtil.keccak(encodeData(primaryType, data, types, version));
}
/**
* Hashes the type of an object.
*
* @param primaryType - The root type to hash.
* @param types - Type definitions for all types included in the message.
* @returns The hash of the object type.
*/
function hashType(
primaryType: string,
types: Record<string, MessageTypeProperty[]>,
): Buffer {
return ethUtil.keccak(encodeType(primaryType, types));
}
/**
* Removes properties from a message object that are not defined per EIP-712.
*
* @param data - The typed message object.
* @returns The typed message object with only allowed fields.
*/
function sanitizeData<T extends MessageTypes>(
data: TypedMessage<T>,
): TypedMessage<T> {
const sanitizedData: Partial<TypedMessage<T>> = {};
for (const key in TYPED_MESSAGE_SCHEMA.properties) {
if (data[key]) {
sanitizedData[key] = data[key];
}
}
if ('types' in sanitizedData) {
sanitizedData.types = { EIP712Domain: [], ...sanitizedData.types };
}
return sanitizedData as Required<TypedMessage<T>>;
}
/**
* Hash a typed message according to EIP-712. The returned message starts with the EIP-712 prefix,
* which is "1901", followed by the hash of the domain separator, then the data (if any).
* The result is hashed again and returned.
*
* This function does not sign the message. The resulting hash must still be signed to create an
* EIP-712 signature.
*
* @param typedData - The typed message to hash.
* @param version - The EIP-712 version the encoding should comply with.
* @returns The hash of the typed message.
*/
function eip712Hash<T extends MessageTypes>(
typedData: TypedMessage<T>,
version: Version.V3 | Version.V4,
): Buffer {
validateVersion(version, [Version.V3, Version.V4]);
const sanitizedData = sanitizeData(typedData);
const parts = [Buffer.from('1901', 'hex')];
parts.push(
hashStruct(
'EIP712Domain',
sanitizedData.domain,
sanitizedData.types,
version,
),
);
if (sanitizedData.primaryType !== 'EIP712Domain') {
parts.push(
hashStruct(
// TODO: Validate that this is a string, so this type cast can be removed.
sanitizedData.primaryType as string,
sanitizedData.message,
sanitizedData.types,
version,
),
);
}
return ethUtil.keccak(Buffer.concat(parts));
}
/**
* A collection of utility functions used for signing typed data
*/
export const TypedDataUtils = {
encodeData,
encodeType,
findTypeDependencies,
hashStruct,
hashType,
sanitizeData,
eip712Hash,
};
/**
* Concatenate an extended ECDSA signature into a hex string.
*
* @param v - The 'v' portion of the signature.
* @param r - The 'r' portion of the signature.
* @param s - The 's' portion of the signature.
* @returns The concatenated ECDSA signature.
*/
export function concatSig(v: Buffer, r: Buffer, s: Buffer): string {
const rSig = ethUtil.fromSigned(r);
const sSig = ethUtil.fromSigned(s);
const vSig = ethUtil.bufferToInt(v);
const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64);
const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64);
const vStr = stripHexPrefix(intToHex(vSig));
return ethUtil.addHexPrefix(rStr.concat(sStr, vStr));
}
/**
* Normalize the input to a 0x-prefixed hex string.
*
* @param input - The value to normalize.
* @returns The normalized 0x-prefixed hex string.
*/
export function normalize(input: number | string): string {
if (!input) {
return undefined;
}
if (typeof input === 'number') {
const buffer = ethUtil.toBuffer(input);
input = ethUtil.bufferToHex(buffer);
}
if (typeof input !== 'string') {
let msg = 'eth-sig-util.normalize() requires hex string or integer input.';
msg += ` received ${typeof input}: ${input}`;
throw new Error(msg);
}
return ethUtil.addHexPrefix(input.toLowerCase());
}
/**
* Create an Ethereum-specific signature for a message.
*
* This function is equivalent to the `eth_sign` Ethereum JSON-RPC method as specified in EIP-1417,
* as well as the MetaMask's `personal_sign` method.
*
* @param options - The personal sign options.
* @param options.privateKey - The key to sign with.
* @param options.data - The data to sign.
* @returns The signature.
*/
export function personalSign({
privateKey,
data,
}: {
privateKey: Buffer;
data: unknown;
}): string {
if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(privateKey)) {
throw new Error('Missing privateKey parameter');
}
const message = legacyToBuffer(data);
const msgHash = ethUtil.hashPersonalMessage(message);
const sig = ethUtil.ecsign(msgHash, privateKey);
const serialized = concatSig(ethUtil.toBuffer(sig.v), sig.r, sig.s);
return serialized;
}
/**
* Recover the address of the account used to create the given Ethereum signature. The message
* must have been signed using the `personalSign` function, or an equivalent function.
*
* @param options - The signature recovery options.
* @param options.data - The message that was signed.
* @param options.signature - The signature for the message.
* @returns The address of the message signer.
*/
export function recoverPersonalSignature({
data,
signature,
}: {
data: unknown;
signature: string;
}): string {
if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(signature)) {
throw new Error('Missing signature parameter');
}
const publicKey = getPublicKeyFor(data, signature);
const sender = ethUtil.publicToAddress(publicKey);
const senderHex = ethUtil.bufferToHex(sender);
return senderHex;
}
/**
* Recover the public key of the account used to create the given Ethereum signature. The message
* must have been signed using the `personalSign` function, or an equivalent function.
*
* @param options - The public key recovery options.
* @param options.data - The message that was signed.
* @param options.signature - The signature for the message.
* @returns The public key of the message signer.
*/
export function extractPublicKey({
data,
signature,
}: {
data: unknown;
signature: string;
}): string {
if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(signature)) {
throw new Error('Missing signature parameter');
}
const publicKey = getPublicKeyFor(data, signature);
return `0x${publicKey.toString('hex')}`;
}
/**
* Generate the "V1" hash for the provided typed message.
*
* The hash will be generated in accordance with an earlier version of the EIP-712
* specification. This hash is used in `signTypedData_v1`.
*
* @param typedData - The typed message.
* @returns The type hash for the provided message.
*/
export function typedSignatureHash(typedData: EIP712TypedData[]): string {
const hashBuffer = _typedSignatureHash(typedData);
return ethUtil.bufferToHex(hashBuffer);
}
/**
* Encrypt a message.
*
* @param options - The encryption options.
* @param options.publicKey - The public key of the message recipient.
* @param options.data - The message data.
* @param options.version - The type of encryption to use.
* @returns The encrypted data.
*/
export function encrypt({
publicKey,
data,
version,
}: {
publicKey: string;
data: unknown;
version: string;
}): EthEncryptedData {
if (isNullish(publicKey)) {
throw new Error('Missing publicKey parameter');
} else if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(version)) {
throw new Error('Missing version parameter');
}
switch (version) {
case 'x25519-xsalsa20-poly1305': {
if (typeof data !== 'string') {
throw new Error('Message data must be given as a string');
}
// generate ephemeral keypair
const ephemeralKeyPair = nacl.box.keyPair();
// assemble encryption parameters - from string to UInt8
let pubKeyUInt8Array;
try {
pubKeyUInt8Array = naclUtil.decodeBase64(publicKey);
} catch (err) {
throw new Error('Bad public key');
}
const msgParamsUInt8Array = naclUtil.decodeUTF8(data);
const nonce = nacl.randomBytes(nacl.box.nonceLength);
// encrypt
const encryptedMessage = nacl.box(
msgParamsUInt8Array,
nonce,
pubKeyUInt8Array,
ephemeralKeyPair.secretKey,
);
// handle encrypted data
const output = {
version: 'x25519-xsalsa20-poly1305',
nonce: naclUtil.encodeBase64(nonce),
ephemPublicKey: naclUtil.encodeBase64(ephemeralKeyPair.publicKey),
ciphertext: naclUtil.encodeBase64(encryptedMessage),
};
// return encrypted msg data
return output;
}
default:
throw new Error('Encryption type/version not supported');
}
}
/**
* Encrypt a message in a way that obscures the message length.
*
* The message is padded to a multiple of 2048 before being encrypted so that the length of the
* resulting encrypted message can't be used to guess the exact length of the original message.
*
* @param options - The encryption options.
* @param options.publicKey - The public key of the message recipient.
* @param options.data - The message data.
* @param options.version - The type of encryption to use.
* @returns The encrypted data.
*/
export function encryptSafely({
publicKey,
data,
version,
}: {
publicKey: string;
data: unknown;
version: string;
}): EthEncryptedData {
if (isNullish(publicKey)) {
throw new Error('Missing publicKey parameter');
} else if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(version)) {
throw new Error('Missing version parameter');
}
const DEFAULT_PADDING_LENGTH = 2 ** 11;
const NACL_EXTRA_BYTES = 16;
if (typeof data === 'object' && 'toJSON' in data) {
// remove toJSON attack vector
// TODO, check all possible children
throw new Error(
'Cannot encrypt with toJSON property. Please remove toJSON property',
);
}
// add padding
const dataWithPadding = {
data,
padding: '',
};
// calculate padding
const dataLength = Buffer.byteLength(
JSON.stringify(dataWithPadding),
'utf-8',
);
const modVal = dataLength % DEFAULT_PADDING_LENGTH;
let padLength = 0;
// Only pad if necessary
if (modVal > 0) {
padLength = DEFAULT_PADDING_LENGTH - modVal - NACL_EXTRA_BYTES; // nacl extra bytes
}
dataWithPadding.padding = '0'.repeat(padLength);
const paddedMessage = JSON.stringify(dataWithPadding);
return encrypt({ publicKey, data: paddedMessage, version });
}
/**
* Decrypt a message.
*
* @param options - The decryption options.
* @param options.encryptedData - The encrypted data.
* @param options.privateKey - The private key to decrypt with.
* @returns The decrypted message.
*/
export function decrypt({
encryptedData,
privateKey,
}: {
encryptedData: EthEncryptedData;
privateKey: string;
}): string {
if (isNullish(encryptedData)) {
throw new Error('Missing encryptedData parameter');
} else if (isNullish(privateKey)) {
throw new Error('Missing privateKey parameter');
}
switch (encryptedData.version) {
case 'x25519-xsalsa20-poly1305': {
// string to buffer to UInt8Array
const recieverPrivateKeyUint8Array = nacl_decodeHex(privateKey);
const recieverEncryptionPrivateKey = nacl.box.keyPair.fromSecretKey(
recieverPrivateKeyUint8Array,
).secretKey;
// assemble decryption parameters
const nonce = naclUtil.decodeBase64(encryptedData.nonce);
const ciphertext = naclUtil.decodeBase64(encryptedData.ciphertext);
const ephemPublicKey = naclUtil.decodeBase64(
encryptedData.ephemPublicKey,
);
// decrypt
const decryptedMessage = nacl.box.open(
ciphertext,
nonce,
ephemPublicKey,
recieverEncryptionPrivateKey,
);
// return decrypted msg data
let output;
try {
output = naclUtil.encodeUTF8(decryptedMessage);
} catch (err) {
throw new Error('Decryption failed.');
}
if (output) {
return output;
}
throw new Error('Decryption failed.');
}
default:
throw new Error('Encryption type/version not supported.');
}
}
/**
* Decrypt a message that has been encrypted using `encryptSafely`.
*
* @param options - The decryption options.
* @param options.encryptedData - The encrypted data.
* @param options.privateKey - The private key to decrypt with.
* @returns The decrypted message.
*/
export function decryptSafely({
encryptedData,
privateKey,
}: {
encryptedData: EthEncryptedData;
privateKey: string;
}): string {
if (isNullish(encryptedData)) {
throw new Error('Missing encryptedData parameter');
} else if (isNullish(privateKey)) {
throw new Error('Missing privateKey parameter');
}
const dataWithPadding = JSON.parse(decrypt({ encryptedData, privateKey }));
return dataWithPadding.data;
}
/**
* Get the encryption public key for the given key.
*
* @param privateKey - The private key to generate the encryption public key with.
* @returns The encryption public key.
*/
export function getEncryptionPublicKey(privateKey: string): string {
const privateKeyUint8Array = nacl_decodeHex(privateKey);
const encryptionPublicKey =
nacl.box.keyPair.fromSecretKey(privateKeyUint8Array).publicKey;
return naclUtil.encodeBase64(encryptionPublicKey);
}
/**
* Sign typed data according to EIP-712. The signing differs based upon the `version`.
*
* V1 is based upon [an early version of EIP-712](https://github.com/ethereum/EIPs/pull/712/commits/21abe254fe0452d8583d5b132b1d7be87c0439ca)
* that lacked some later security improvements, and should generally be
* neglected in favor of later versions.
*
* V3 is based on EIP-712, except that arrays and recursive data structures
* are not supported.
*
* V4 is based on EIP-712, and includes full support of arrays and recursive
* data structures.
*
* @param options - The signing options.
* @param options.privateKey - The private key to sign with.
* @param options.data - The typed data to sign.
* @param options.version - The signing version to use.
* @returns The signature.
*/
export function signTypedData<V extends Version, T extends MessageTypes>({
privateKey,
data,
version,
}: {
privateKey: Buffer;
data: V extends 'V1' ? TypedDataV1 : TypedMessage<T>;
version: V;
}): string {
validateVersion(version);
if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(privateKey)) {
throw new Error('Missing private key parameter');
}
const messageHash =
version === Version.V1
? _typedSignatureHash(data as TypedDataV1)
: TypedDataUtils.eip712Hash(
data as TypedMessage<T>,
version as Version.V3 | Version.V4,
);
const sig = ethUtil.ecsign(messageHash, privateKey);
return concatSig(ethUtil.toBuffer(sig.v), sig.r, sig.s);
}
/**
* Recover the address of the account that created the given EIP-712
* signature. The version provided must match the version used to
* create the signature.
*
* @param options - The signature recovery options.
* @param options.data - The data that was signed.
* @param options.signature - The message signature.
* @param options.version - The signing version to use.
* @returns The address of the signer.
*/
export function recoverTypedSignature<
V extends Version,
T extends MessageTypes,
>({
data,
signature,
version,
}: {
data: V extends 'V1' ? TypedDataV1 : TypedMessage<T>;
signature: string;
version: V;
}): string {
validateVersion(version);
if (isNullish(data)) {
throw new Error('Missing data parameter');
} else if (isNullish(signature)) {
throw new Error('Missing signature parameter');
}
const messageHash =
version === Version.V1
? _typedSignatureHash(data as TypedDataV1)
: TypedDataUtils.eip712Hash(
data as TypedMessage<T>,
version as Version.V3 | Version.V4,
);
const publicKey = recoverPublicKey(messageHash, signature);
const sender = ethUtil.publicToAddress(publicKey);
return ethUtil.bufferToHex(sender);
}
/**
* Generate the "V1" hash for the provided typed message.
*
* The hash will be generated in accordance with an earlier version of the EIP-712
* specification. This hash is used in `signTypedData_v1`.
*
* @param typedData - The typed message.
* @returns The type hash for the provided message.
*/
function _typedSignatureHash(typedData: TypedDataV1): Buffer {
const error = new Error('Expect argument to be non-empty array');
if (
typeof typedData !== 'object' ||
!('length' in typedData) ||
!typedData.length
) {
throw error;
}
const data = typedData.map(function (e) {
if (e.type !== 'bytes') {
return e.value;
}
return legacyToBuffer(e.value);
});
const types = typedData.map(function (e) {
return e.type;
});
const schema = typedData.map(function (e) {
if (!e.name) {
throw error;
}
return `${e.type} ${e.name}`;
});
return ethAbi.soliditySHA3(
['bytes32', 'bytes32'],
[
ethAbi.soliditySHA3(new Array(typedData.length).fill('string'), schema),
ethAbi.soliditySHA3(types, data),
],
);
}
/**
* Recover the public key from the given signature and message hash.
*
* @param messageHash - The hash of the signed message.
* @param signature - The signature.
* @returns The public key of the signer.
*/
function recoverPublicKey(messageHash: Buffer, signature: string): Buffer {
const sigParams = ethUtil.fromRpcSig(signature);
return ethUtil.ecrecover(messageHash, sigParams.v, sigParams.r, sigParams.s);
}
/**
* Get the public key for the given signature and message.
*
* @param message - The message that was signed.
* @param signature - The signature.
* @returns The public key of the signer.
*/
function getPublicKeyFor(message: unknown, signature: string): Buffer {
const messageHash = ethUtil.hashPersonalMessage(legacyToBuffer(message));
return recoverPublicKey(messageHash, signature);
}
/**
* Convert a hex string to the UInt8Array format used by nacl.
*
* @param msgHex - The string to convert.
* @returns The converted string.
*/
function nacl_decodeHex(msgHex: string): Uint8Array {
const msgBase64 = Buffer.from(msgHex, 'hex').toString('base64');
return naclUtil.decodeBase64(msgBase64);
}
/**
* Convert a value to a Buffer. This function should be equivalent to the `toBuffer` function in
* `ethereumjs-util@5.2.1`.
*
* @param value - The value to convert to a Buffer.
* @returns The given value as a Buffer.
*/
function legacyToBuffer(value: unknown) {
return typeof value === 'string' && !isHexString(value)
? Buffer.from(value)
: ethUtil.toBuffer(value);
}