aboutsummaryrefslogblamecommitdiffstats
path: root/packages/contracts/test/utils/signing_utils.ts
blob: 9c711c72c8f9b3ba67877b1d173da27f0f571637 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                 

                                           
                             
                                                                                            
                                                      
                                                                         
                                                                            
                                             


                                                
                                                


                                                            
                                                                    
                                             


                                                
                                                






                                                                              
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`);
        }
    },
};