diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-08 08:05:43 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:16 +0800 |
commit | 3ff8a319c5460caaf9edc1bea68e807fe0611aaa (patch) | |
tree | 77c255e42d1dfcedad7f1692c9467e28cd9a3802 /packages/contracts/src/utils/signing_utils.ts | |
parent | cd8f8e1e4acc00e9e31265050456a4c98d79a7d4 (diff) | |
download | dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.tar.gz dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.tar.zst dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.zip |
Add utils for hashing and signing orders, update wrappers
Diffstat (limited to 'packages/contracts/src/utils/signing_utils.ts')
-rw-r--r-- | packages/contracts/src/utils/signing_utils.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/packages/contracts/src/utils/signing_utils.ts b/packages/contracts/src/utils/signing_utils.ts new file mode 100644 index 000000000..21b69619c --- /dev/null +++ b/packages/contracts/src/utils/signing_utils.ts @@ -0,0 +1,30 @@ +import * as ethUtil from 'ethereumjs-util'; + +import { SignatureType } from './types'; + +export const signingUtils = { + signMessage(message: Buffer, secretKey: Buffer, signatureType: SignatureType): Buffer { + if (signatureType === SignatureType.Ecrecover) { + const prefixedMessage = ethUtil.hashPersonalMessage(message); + const ecSignature = ethUtil.ecsign(prefixedMessage, secretKey); + const signature = Buffer.concat([ + ethUtil.toBuffer(signatureType), + ethUtil.toBuffer(ecSignature.v), + ecSignature.r, + ecSignature.s, + ]); + return signature; + } else if (signatureType === SignatureType.EIP712) { + const ecSignature = ethUtil.ecsign(message, secretKey); + const signature = Buffer.concat([ + ethUtil.toBuffer(signatureType), + ethUtil.toBuffer(ecSignature.v), + ecSignature.r, + ecSignature.s, + ]); + return signature; + } else { + throw new Error(`${signatureType} is not a valid signature type`); + } + }, +}; |