aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/test-utils/src/signing_utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'contracts/test-utils/src/signing_utils.ts')
-rw-r--r--contracts/test-utils/src/signing_utils.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/contracts/test-utils/src/signing_utils.ts b/contracts/test-utils/src/signing_utils.ts
new file mode 100644
index 000000000..21f864bfa
--- /dev/null
+++ b/contracts/test-utils/src/signing_utils.ts
@@ -0,0 +1,29 @@
+import { SignatureType } from '@0x/types';
+import * as ethUtil from 'ethereumjs-util';
+
+export const signingUtils = {
+ signMessage(message: Buffer, privateKey: Buffer, signatureType: SignatureType): Buffer {
+ if (signatureType === SignatureType.EthSign) {
+ const prefixedMessage = ethUtil.hashPersonalMessage(message);
+ const ecSignature = ethUtil.ecsign(prefixedMessage, privateKey);
+ const signature = Buffer.concat([
+ ethUtil.toBuffer(ecSignature.v),
+ ecSignature.r,
+ ecSignature.s,
+ ethUtil.toBuffer(signatureType),
+ ]);
+ return signature;
+ } else if (signatureType === SignatureType.EIP712) {
+ const ecSignature = ethUtil.ecsign(message, privateKey);
+ const signature = Buffer.concat([
+ ethUtil.toBuffer(ecSignature.v),
+ ecSignature.r,
+ ecSignature.s,
+ ethUtil.toBuffer(signatureType),
+ ]);
+ return signature;
+ } else {
+ throw new Error(`${signatureType} is not a valid signature type`);
+ }
+ },
+};