aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/test/utils/signing_utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/contracts/test/utils/signing_utils.ts')
-rw-r--r--packages/contracts/test/utils/signing_utils.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/contracts/test/utils/signing_utils.ts b/packages/contracts/test/utils/signing_utils.ts
new file mode 100644
index 000000000..9c711c72c
--- /dev/null
+++ b/packages/contracts/test/utils/signing_utils.ts
@@ -0,0 +1,29 @@
+import { SignatureType } from '@0xproject/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`);
+ }
+ },
+};