aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util/signed_order.ts
blob: a84e0686cec9dd3bb9434e6ffa1874431f474e00 (plain) (blame)
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
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';

import { crypto } from './crypto';
import { SignedOrderParams } from './types';

export class SignedOrder {
    public params: SignedOrderParams;
    private _web3Wrapper: Web3Wrapper;
    constructor(web3Wrapper: Web3Wrapper, params: SignedOrderParams) {
        this.params = params;
        this._web3Wrapper = web3Wrapper;
    }
    public isValidSignature() {
        const { v, r, s } = this.params;
        const orderHash = this.getOrderHashHex();
        const msgHash = ethUtil.hashPersonalMessage(ethUtil.toBuffer(orderHash));
        try {
            const pubKey = ethUtil.ecrecover(msgHash, v, ethUtil.toBuffer(r), ethUtil.toBuffer(s));
            const recoveredAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
            return recoveredAddress === this.params.maker;
        } catch (err) {
            return false;
        }
    }
    public createFill(shouldThrowOnInsufficientBalanceOrAllowance?: boolean, fillTakerTokenAmount?: BigNumber) {
        const fill = {
            orderAddresses: [
                this.params.maker,
                this.params.taker,
                this.params.makerToken,
                this.params.takerToken,
                this.params.feeRecipient,
            ],
            orderValues: [
                this.params.makerTokenAmount,
                this.params.takerTokenAmount,
                this.params.makerFee,
                this.params.takerFee,
                this.params.expirationTimestampInSec,
                this.params.salt,
            ],
            fillTakerTokenAmount: fillTakerTokenAmount || this.params.takerTokenAmount,
            shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance,
            v: this.params.v,
            r: this.params.r,
            s: this.params.s,
        };
        return fill;
    }
    public createCancel(cancelTakerTokenAmount?: BigNumber) {
        const cancel = {
            orderAddresses: [
                this.params.maker,
                this.params.taker,
                this.params.makerToken,
                this.params.takerToken,
                this.params.feeRecipient,
            ],
            orderValues: [
                this.params.makerTokenAmount,
                this.params.takerTokenAmount,
                this.params.makerFee,
                this.params.takerFee,
                this.params.expirationTimestampInSec,
                this.params.salt,
            ],
            cancelTakerTokenAmount: cancelTakerTokenAmount || this.params.takerTokenAmount,
        };
        return cancel;
    }
    public getOrderHashHex(): string {
        const orderHash = crypto.solSHA3([
            this.params.exchangeContractAddress,
            this.params.maker,
            this.params.taker,
            this.params.makerToken,
            this.params.takerToken,
            this.params.feeRecipient,
            this.params.makerTokenAmount,
            this.params.takerTokenAmount,
            this.params.makerFee,
            this.params.takerFee,
            this.params.expirationTimestampInSec,
            this.params.salt,
        ]);
        const orderHashHex = ethUtil.bufferToHex(orderHash);
        return orderHashHex;
    }
}